Exemplo n.º 1
0
  @Override
  public List<MessageToClient> request(MicroByteBuffer from, MicroByteBuffer to, Client client)
      throws RegolithException {
    Battle battle;
    List<MessageToClient> messages = new LinkedList<MessageToClient>();
    List<SocketChannel> recipients;
    if (battleManagerContext.getCreatedBattles().get(client.getAccount()) != null) {
      try {
        battle = battleCreationManager.startBattle(client.getAccount());
        BattleServiceDescriptor battleServiceDescriptor =
            ServerContextHelper.getWastingBattleServiceAddress(serverContext);
        if (battleServiceDescriptor != null) {
          BattleServiceFunctions battleService =
              (BattleServiceFunctions)
                  Naming.lookup(
                      "rmi://"
                          + battleServiceDescriptor.getHost()
                          + "/"
                          + battleServiceDescriptor.getName());
          if (battleService != null) {
            battleService.register(battle);
            Set<Client> clients = MainServerRequestUtils.getBattleClients(battle, serverContext);
            for (Client groupClient : clients) {
              Account account = groupClient.getAccount();
              recipients = new ArrayList<SocketChannel>(1);
              SocketChannel channel = serverContext.getChannel(account);
              recipients.add(channel);
              serverContext.removeChannel(account);
              serverContext.removeClient(channel);
              messages.add(
                  new MainMessageToClient(
                      recipients,
                      ServerStartBattleAnswer.answerSuccess(
                              to, battle, account, battleServiceDescriptor)
                          .serialize()));
            }

            List<SocketChannel> listeners =
                MainServerRequestUtils.getPassiveListenerChannels(clients, serverContext, battle);
            if (listeners.size() > 0) {
              messages.add(
                  new MainMessageToClient(
                      listeners, new ServerStopListenAnswer(to, battle).serialize()));
            }

            battleManagerContext.getBattlesById().remove(battle.getId());
            battleManagerContext.getCreatedBattles().remove(client.getAccount());
            battleManagerContext.getBattlesByAccount().remove(client.getAccount());
            battleManagerContext.getBattleListeners().remove(battle);
            battleManagerContext.getCompleteGroups().remove(battle);

            schedulerService.deleteBattle(battle);
          } else {
            recipients = new ArrayList<SocketChannel>(1);
            recipients.add(serverContext.getChannel(client.getAccount()));
            messages.add(
                new MainMessageToClient(
                    recipients, ServerStartBattleAnswer.answerFailure(to).serialize()));
          }
        } else {
          recipients = new ArrayList<SocketChannel>(1);
          recipients.add(serverContext.getChannel(client.getAccount()));
          messages.add(
              new MainMessageToClient(
                  recipients, ServerStartBattleAnswer.answerFailure(to).serialize()));
        }
      } catch (RegolithException e) {
        logger.error("START BATTLE EXCEPTION (" + client.getAccount().getName() + ")", e);
        recipients = new ArrayList<SocketChannel>(1);
        recipients.add(serverContext.getChannel(client.getAccount()));
        messages.add(
            new MainMessageToClient(
                recipients, ServerStartBattleAnswer.answerFailure(to).serialize()));
      } catch (NotBoundException e) {
        throw new RegolithException(e);
      } catch (MalformedURLException e) {
        throw new RegolithException(e);
      } catch (RemoteException e) {
        throw new RegolithException(e);
      }
    } else {
      recipients = new ArrayList<SocketChannel>(1);
      recipients.add(serverContext.getChannel(client.getAccount()));
      messages.add(
          new MainMessageToClient(
              recipients, ServerStartBattleAnswer.answerFailure(to).serialize()));
    }

    return messages;
  }