Package cz.matfyz.aai.fantom.message

Examples of cz.matfyz.aai.fantom.message.MessageUpdate


    }
   
    // If the message is MessageUpdate, also update the numbers of tickets
    // and captured phantoms
    if(message instanceof MessageUpdate) {
      MessageUpdate msgUpdate = (MessageUpdate)message;
      for(Actor phantom : msgUpdate.getCapturedPhantoms())
        phantom.capture();
     
      for(ActorTickets tickets : msgUpdate.getActorTickets()) {
        Actor actor = tickets.getActor();
        for(Entry<TransportType, Integer> ttype : tickets.getTickets().entrySet()) {
          actor.setNumberOfTickets(ttype.getKey(), ttype.getValue());
        }
      }
View Full Code Here


      throw e;
    }

    // Inform the phantom about the initial placement of the detectives.
    logger.debug("Sending information about detective placement to the phantom");
    msg = new MessageUpdate(graph, detectivePlacements.getMoves(), null);
    phantomClient.sendMessage(msg);
    logger.trace("Message was sent");

    // Place the phantom.
    logger.debug("Receiving actor placement from the phantom");
    msg = phantomClient.receiveMessage(graph);
    if(!(msg instanceof MessageMove))
      throw new ProtocolException("'move' message was expected from the detective client", phantomClient);
    logger.trace("Message received");
   
    MessageMove phantomPlacement = (MessageMove) msg;
    try {
      logger.debug("Placing phantoms on the graph");
      graph.verifyActorPlacement(phantomPlacement, phantomClient);
      graph.placeActors(phantomPlacement);
      logActorPlacement(phantomClient, phantomPlacement);
      logger.trace("Phantoms were placed");
    }
    catch(ClientException e) {
      e.setClient(phantomClient);
      throw e;
    }

    processCapturedPhantoms(phantoms);
    if(phantoms.isEmpty()) {
      logger.info("All phantoms were captured, detectives won the game");
      return new MessageUpdate(graph, phantomPlacement.getMoves(), ClientType.DETECTIVE);
    }

    logger.debug("Letting the phantom start.");
    msg = new MessageUpdate(graph, new ActorMove[0], null);
    phantomClient.sendMessage(msg);
    logger.trace("Phantom starts playing.");

    return null;
  }
View Full Code Here

        }
        pams = moves;
      }
    }
    if(phantoms.isEmpty()) {
      return new MessageUpdate(graph, pams, ClientType.DETECTIVE);
    }
    msg = new MessageUpdate(graph, pams, null);
    detectiveClient.sendMessage(msg);
    logger.trace("Message was sent");

    // Get the moves of detectives.
    logger.debug("Reading movement information from detectives");
    msg = detectiveClient.receiveMessage(graph);
    if(!(msg instanceof MessageMove))
      throw new ProtocolException("'move' message was expected from the detective client", detectiveClient);
    logger.trace("Movement information from the phantom was received");
    MessageMove detectiveMoves = (MessageMove) msg;
    try {
      logActorMovement(detectiveClient, detectiveMoves);
      logger.debug("Updating actor positions in the graph");
      graph.moveActors(detectives, detectiveMoves);
      logger.trace("Actor positions were updated");
    }
    catch(ProtocolException e) {
      e.setClient(detectiveClient);
      throw e;
    }
   
    // First process phantoms captured in the last move...
    logger.debug("Processing captured phantoms");
    processCapturedPhantoms(phantoms);
    if(phantoms.isEmpty()) {
      //return ClientType.DETECTIVE;
      return new MessageUpdate(graph, detectiveMoves.getMoves(), ClientType.DETECTIVE);
    }

    logger.debug("Distributing tickets used by detectives to phantoms");
    for(MessageMove.ActorMove m : detectiveMoves.getMoves()) {
      TransportType transport = m.getTransportType();

      if(transport.isUsedByPhantom()) {
        int lucky = rnd.nextInt(phantoms.size()); //TODO: With multiple phantoms, how should the tickets be distributed?
        Actor luckyActor = phantoms.get(lucky);
        logger.info(String.format("Phantom %s gets one ticket for transport %s", luckyActor.getId(), transport.getName()));
        phantoms.get(lucky).addTicket(transport);
      }
    }

    // Inform the phantom.
    logger.debug("Sending phantom information about movement of the detectives");
    ClientType winner = null;
    // If this is the last round, the phantom escaped and won the game
    if(round == graph.getGameLength()) {
      logger.info("Detectives did not catch the phantoms, phantoms won the game");
      winner = ClientType.PHANTOM;
      return new MessageUpdate(graph, detectiveMoves.getMoves(), winner);
    }
   
    MessageUpdate detectiveUpdate = new MessageUpdate(graph, detectiveMoves.getMoves(), winner);
    phantomClient.sendMessage(detectiveUpdate);
    logger.trace("Message was sent");

    logger.debug("Nobody won the current round");
    return null;
View Full Code Here

      } else {
        phantoms.add(a);
      }
    }

    MessageUpdate result = startGame(phantoms, detectives);
    if(result != null) {
      return result;
    }

    for(int round = 1; round <= graph.getGameLength(); round++) {
View Full Code Here

    }
    else {
      ClientType recipient = winner.getMoves()[0].getActor().getClientType();
      if(recipient != ClientType.DETECTIVE) {
        detectiveMsg = winner;
        phantomMsg = new MessageUpdate(graph, new ActorMove[0], winner.getWinner());
      } else {
        detectiveMsg = new MessageUpdate(graph, new ActorMove[0], winner.getWinner());
        phantomMsg = winner;
      }
    }
    detectiveClient.sendMessage(detectiveMsg);
    phantomClient.sendMessage(phantomMsg);
View Full Code Here

  public void run() throws ServerException {
    initClients();
   
    for(int game = 0; game < gameCount; game++) {
      logger.info(String.format("Starting game %d", game));
      MessageUpdate winner = runGame();
      gameOver(winner);
      logger.info(String.format("Game %d ended", game));
    }
   
    logger.info(String.format("Games won by the detectives: %d", detectiveWins));
View Full Code Here

          while(true) {
            msg = receiveMessage(graph);
            if(!(msg instanceof MessageUpdate))
              throw new ProtocolException(String.format("Message 'update' was expected, got '%s'", msg.getMessageType()), null);

            MessageUpdate msgUpdate = (MessageUpdate)msg;
           
            // Update the agent, if nobody did win the game in the last round, or if there
            // is information about movements of the other agent (e.g. about the movement
            // of the detectives, that captured the phantom, or the movement of the phantom
            // that commited suicide).
            if(msgUpdate.getWinner() == null || msgUpdate.getMoves().length > 0)
              receiveUpdate(msgUpdate);
            // If there is a winner, end the round
            if(msgUpdate.getWinner() != null) {
              agent.end(msgUpdate.getWinner());
              continue tournament_loop;
            }
           
            sendMoves();
          }
View Full Code Here

TOP

Related Classes of cz.matfyz.aai.fantom.message.MessageUpdate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.