我正在使用服务器,每个请求都创建自己的堆栈
现在我有主对象,我将它传递给每个请求创建方法。
这个主对象lts称之为:GameManager拥有名为GameRooms的嵌套Map对象
并且每个房间都有嵌套对象(Map),称为玩家
像这样的东西:
public class GameManager {
private final Map<Integer, GameRoom> gameRooms;
public GameManager (Map<Integer, GameRoom> _gameRooms)
{
gameRooms = _gameRooms;
create only 1 room
gameRooms.put(0,new GameRoom());
}
}
public class GameRoom {
private final Map<Integer, Player> players;
public GameRoom()
{
players = new HashMap<Integer, Player>();
}
public Map<Integer, Player> getPlayers()
{
return players;
}
}
public int setPlayer(Player _player)
{
int playerId = _player.getID();
this.players.put(playerId,_player); // THIS doesn't keep the player reference
// //this.players.put(playerId,new Player(_player.getID())); // THIS also doesn't keep the player reference when i do New
}
public class Player {
private int id;
public Player (int _id)
{
id = _id
}
public int getID()
{
return id
}
}
服务器类
public class GameServerInitializer extends ChannelInitializer<Channel> {
private final GameManager gameManager;
private int i ;
public GameServerInitializer(GameManager _gameManager) {
this.gameManager = _gameManager;
i = 0;
}
//this called on each request
public void Request()
{
Player p = new Player(i++);
//if it the first request it will be 0
int s = this.gameManager.getGameRooms().get(0).getPlayers().size();
this.gameManager.getGameRooms().get(0).setPlayer(p);
//now it is 1 on the first request
int s1 = this.gameManager.getGameRooms().get(0).getPlayers().size();
}
}
现在从主服务器启动时
private final Map<Integer, GameRoom> gameRooms = new HashMap<Integer, GameRoom>();
private final GameManager gameManager = new GameManager(gameRooms);
new GameServerInitializer(gameManager);
现在我期待着 。this.gameManager.getGameRooms()得到(0).getPlayers()大小()。 将累积每个请求将添加1个玩家到GameRoom 但是每个请求的大小都不是0 这只适用于玩家,游戏室地图确实在请求中保持其大小