Esempio n. 1
0
  /**
   * Verify that when adding a second player to a game, the correct information for that player is
   * set and it does not conflict with the first player
   */
  @Test
  public void addSecondPlayerTest() {
    // setup dummy entityfactory
    EntityFactory dummyEntityFactory = EasyMock.createMock(EntityFactory.class);
    SnowmanFlag dummyFlag = EasyMock.createNiceMock(SnowmanFlag.class);
    EasyMock.expect(dummyFlag.getID()).andStubReturn(new Integer(0));
    EasyMock.replay(dummyFlag);
    EasyMock.expect(
            dummyEntityFactory.createSnowmanFlag(
                EasyMock.isA(SnowmanGame.class),
                EasyMock.isA(ETeamColor.class),
                EasyMock.isA(Coordinate.class),
                EasyMock.isA(Coordinate.class)))
        .andStubReturn(dummyFlag);
    EasyMock.replay(dummyEntityFactory);

    // create the player
    ClientSession session = EasyMock.createNiceMock(ClientSession.class);
    SnowmanPlayer dummyPlayer = EasyMock.createMock(SnowmanPlayer.class);
    ETeamColor color = ETeamColor.Red;

    // create the second player
    ClientSession session2 = EasyMock.createNiceMock(ClientSession.class);
    SnowmanPlayer dummyPlayer2 = EasyMock.createMock(SnowmanPlayer.class);
    ETeamColor color2 = ETeamColor.Blue;

    // create the game
    SnowmanGame game = new SnowmanGameImpl(gameName, 4, dummyEntityFactory);

    // record information that should be set on the player1
    dummyPlayer.setID(1);
    dummyPlayer.setLocation(EasyMock.anyFloat(), EasyMock.anyFloat());
    dummyPlayer.setTeamColor(color);
    dummyPlayer.setGame(game);
    EasyMock.expect(dummyPlayer.getSession()).andStubReturn(session);
    EasyMock.replay(dummyPlayer);

    // record information that should be set on the player2
    dummyPlayer2.setID(2);
    dummyPlayer2.setLocation(EasyMock.anyFloat(), EasyMock.anyFloat());
    dummyPlayer2.setTeamColor(color2);
    dummyPlayer2.setGame(game);
    EasyMock.expect(dummyPlayer2.getSession()).andStubReturn(session2);
    EasyMock.replay(dummyPlayer2);

    // record that the players should be added to the channel
    EasyMock.resetToDefault(gameChannel);
    EasyMock.expect(gameChannel.join(session)).andReturn(gameChannel);
    EasyMock.expect(gameChannel.join(session2)).andReturn(gameChannel);
    EasyMock.replay(gameChannel);

    // add the player
    game.addPlayer(dummyPlayer, color);
    game.addPlayer(dummyPlayer2, color2);

    // verify the calls
    EasyMock.verify(dummyPlayer);
    EasyMock.verify(dummyPlayer2);
    EasyMock.verify(gameChannel);
  }
Esempio n. 2
0
  public boolean removePlayer(RocketPlayer player) {
    logger.log(Level.INFO, "{0} leaves {1}", new Object[] {player.getName(), this.getName()});

    AppContext.getDataManager().markForUpdate(this);
    Channel tableChannel = tableChannelRef.getForUpdate();
    tableChannel.leave(player.getClientSessionRef().get());

    // The reference created below should be identical to one of the players
    // on our table. I think createReference has some clever logic to
    // not create another reference if one already exists.
    ManagedReference<RocketPlayer> playerRef = AppContext.getDataManager().createReference(player);

    if (playerRef.equals(player1Ref)) {
      player1Ref = null;
    } else if (playerRef.equals(player2Ref)) {
      player2Ref = null;
    } else if (playerRef.equals(player3Ref)) {
      player3Ref = null;
    } else if (playerRef.equals(player4Ref)) {
      player4Ref = null;
    } else {
      logger.log(
          Level.SEVERE, "player " + player.getName() + " doesn't appear to be one of this table!");
      return false;
    }

    setTableAvailable(true);
    maybeChangeStatusOnRemovePlayer();
    return true;
  }
 public MythologicChannelSessionListener(
     ClientSession clientSession, ManagedReference<Channel> channel1) {
   super(clientSession);
   // Join the session to all channels. We obtain the channel
   // in two different ways, by reference and by name.
   ChannelManager channelMgr = AppContext.getChannelManager();
   // We were passed a reference to the first channel.
   channel1.get().join(clientSession);
   // We look up the second channel by name.
   Channel channel2 = channelMgr.getChannel("a");
   channel2.join(clientSession);
 }
Esempio n. 4
0
  private void sendChannelMessage(VoResponseData rd) {

    Channel channel = AppContext.getChannelManager().getChannel(GameServer.CHANNEL_PRIMARY);
    ByteBuffer buf = null;

    try {
      buf = ByteBuffer.wrap(Global.SerializeAmf3(rd));
    } catch (IOException ex) {
      LOG.log(Level.SEVERE, "关联题任务发送消息时出错:{0}", ex);
    }

    channel.send(buf);
  }
Esempio n. 5
0
  public boolean addPlayer(RocketPlayer player) {
    ManagedReference<RocketPlayer> playerRef = AppContext.getDataManager().createReference(player);
    AppContext.getDataManager().markForUpdate(this);
    Channel tableChannel =
        tableChannelRef
            .getForUpdate(); // ?? Do I really need to get for update when adding new sessions?

    int playerId = 0;

    if (player1Ref == null) {
      player1Ref = playerRef;
      playerId = 1;
    } else if (player2Ref == null) {
      player2Ref = playerRef;
      playerId = 2;
    } else if (player3Ref == null) {
      player3Ref = playerRef;
      playerId = 3;
    } else if (player4Ref == null) {
      player4Ref = playerRef;
      playerId = 4;
    } else {
      // Theoretically, this should never happen as the tableAvailability
      // won't let players in if the table is full.
      logger.log(
          Level.SEVERE,
          "Not sure how, but a player has tried to join a table with no free spaces!");
      return false;
    }

    tableChannel.join(player.getClientSessionRef().get());
    player.setMyCurrentTable(AppContext.getDataManager().createReference(this));
    logger.log(
        Level.INFO,
        "{0} enters {1} as player " + playerId,
        new Object[] {player.getName(), this.getName()});
    tableChannel.send(
        Utils.encodeString(
            ClientServerMessageInteractor.createTableJoinSuccessMessage(
                player.getName(), playerId)));
    updateTableAvailabilty();
    maybeChangeStatusOnAddPlayer();
    return true;
  }