Newer
Older
CactusServer / src / main / java / cactusServer / utils / App.java
package cactusServer.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;
import org.ntlab.radishforandroidstudio.framework.model3D.Model3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
import org.ntlab.radishforandroidstudio.framework.physics.AngularVelocity3D;
import org.ntlab.radishforandroidstudio.framework.physics.PhysicsUtility;
import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D;

import cactusServer.entities.*;
import cactusServer.entities.Character;
import cactusServer.entities.EmoteState.EmoteType;
import cactusServer.entities.MovableObject.*;
import cactusServer.models.Accounts;
import cactusServer.models.BulletModelManager;
import cactusServer.models.CharacterModelManager;
import cactusServer.models.CollisionManager;
import cactusServer.models.Instances;
import cactusServer.models.ObjectModelManager;
import cactusServer.models.StageModelManager;
import cactusServer.resources.InstancesRest;
import net.arnx.jsonic.JSON;

@SuppressWarnings("unused")
@ApplicationPath("/rest")
public class App extends ResourceConfig implements Runnable {
	private ScheduledThreadPoolExecutor task = new ScheduledThreadPoolExecutor(1000);
	private long lasttime = System.nanoTime();
	private boolean debugflag = false;

	public App() {
		int interval = (debugflag) ? 1000 : 1;
		ObjectModelManager.getInstance();
		StageModelManager.getInstance();
		CharacterModelManager.getInstance();
		BulletModelManager.getInstance();
		Instances.getInstance();
		Accounts.getInstance();
		CollisionManager.getInstance();
		initDummy();
		start(interval);
		System.out.println("pass");
	}

	@Override
	public void run() {
		// System.out.println("タスクを実行");
		CollisionManager.getInstance().collisionRun(System.nanoTime() - lasttime);
		removeInactivePlayers();
		lasttime = System.nanoTime();
	}

	public void start(int interval) {
		task.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
	}

	private void initDummy() {
		// ダミーアカウント2つ
		Accounts.getInstance().createAcount("test", "test", "test");
		Accounts.getInstance().createAcount("test2", "test2", "test2");
		// ダミーインスタンス2つ
		Instances.getInstance().getInstances().put("test1", new Instance("test1", 0));
		Instances.getInstance().getInstances().put("test2", new Instance("test2", 1));
		// ダミーキャラクター4つ
		String accountUri = "/CactusServer/rest/accounts/"
				+ Accounts.getInstance().getAccountByID("test").getUniqueID();
		Character chara1 = new Character("test1", accountUri, "chara1", new Position3D(), new Quaternion3D(), 0);
		Character chara2 = new Character("test2", accountUri, "chara2", new Position3D(), new Quaternion3D(), 0);
		Instances.getInstance().getInstance("test1").getCharacters().put("chara1", chara1);
		Instances.getInstance().getInstance("test1").getCharacters().put("chara2", chara2);
		Instances.getInstance().getInstance("test2").getCharacters().put("chara1", chara1);
		Instances.getInstance().getInstance("test2").getCharacters().put("chara2", chara2);
		accountUri = "/CactusServer/rest/accounts/" + Accounts.getInstance().getAccountByID("test2").getUniqueID();
		chara1 = new Character("test2", accountUri, "chara3", new Position3D(), new Quaternion3D(), 0);
		Instances.getInstance().getInstance("test2").getCharacters().put("chara3", chara1);
		// ダミープレイヤー2つ
		chara1.setPosition(new Position3D(34.5, 23.4, 19));
		CameraState cameraState = new CameraState(1.24, new Quaternion3D(), 2.38, 3);
		Player player1 = new Player("test1", "chara1", null, null);
		Instances.getInstance().getPlayers().put("player1", player1);
		Player player2 = new Player("test2", "chara2", cameraState, EmoteType.DUMMY);
		Instances.getInstance().getPlayers().put("player2", player2);
		// ダミーオブジェクト
		Instances.getInstance().getInstance("test1").createObject(new Position3D(0, 0, 0), new Velocity3D(),
				new AngularVelocity3D(), new Quaternion3D(), new Attribute(true, 1), 0);
		// ダミーバレット
		Instances.getInstance().getInstance("test1").createBullet("player1", "testBulletId1", new Position3D(),
				new Quaternion3D());
		Instances.getInstance().getInstance("test1").createBullet("player1", "testBulletId2", new Position3D(),
				new Quaternion3D());

		// // 確認用
		// String player1Encode = JSON.encode(player1, true);
		// String player2Encode = JSON.encode(player2, true);
		// Player player1Decode = JSON.decode(player1Encode, Player.class);
		// Player player2Decode = JSON.decode(player2Encode, Player.class);
		// System.out.println(JSON.encode(player1Decode, true));
		// System.out.println(JSON.encode(player2Decode, true));

		// JSON json = new JSON();
		// String characterId = player1.getCharacterID();
		// String encodeCharacterId = json.encode(characterId);
		// System.out.println(characterId);
		// System.out.println(encodeCharacterId);
		// String position = json.encode(player1.getPosition());
		// System.out.println(position);
	}

	private void removeInactivePlayers() {
		long currentTime = System.nanoTime();
		Iterator<Map.Entry<String, Player>> it = Instances.getInstance().getPlayers().entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Player> entry = it.next();
			String playerId = entry.getKey();
			Player player = entry.getValue();
			long lastUpdateTime = player.getLastUpdateTime();
			System.out.println(
					currentTime + ", " + (lastUpdateTime + Player.STOP_TIME_LIMIT) + " // playerId: " + playerId);
			if (currentTime > (lastUpdateTime + Player.STOP_TIME_LIMIT)) {
				System.out.println("delete started (playerId: " + playerId + ")");
				player.destroy();
				it.remove();
				System.out.println("delete finished (playerId: " + playerId + ")");
			}
		}
		System.out.println();
	}
}