Exemplo n.º 1
0
  private void reportLocation(String newClient, LocalClient me) {
    // Suspend user input and random generator
    me.pause();

    // Wait 100ms for all packets to be queued
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    // Clear queue
    Mazewar.actionQueue.clear();
    Mazewar.ackTracker.clear();

    // Get my location
    DirectedPoint myDp = new DirectedPoint(me.getPoint(), me.getOrientation());

    // Prepare packet to report my location
    MazewarPacket outgoing = new MazewarPacket();
    outgoing.type = MazewarPacket.REPORT_LOCATION;
    outgoing.owner = Mazewar.myName;
    outgoing.directedPoint = myDp;
    outgoing.score = me.getScore();

    synchronized (Mazewar.connectedOuts) {
      logger.info("Reporting my location to: " + newClient);
      // Report my location to the new guy
      try {
        Mazewar.connectedOuts.get(newClient).writeObject(outgoing);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  public boolean sendEvent(LocalClient client, ClientEvent clientevent) {
    MazewarPacket payload = new MazewarPacket();
    payload.clientName = client.getName();

    if (clientevent.equals(ClientEvent.moveForward)) {
      payload.eventType = MazewarPacket.ACTION_MOVE_UP;
    } else if (clientevent.equals(ClientEvent.moveBackward)) {
      payload.eventType = MazewarPacket.ACTION_MOVE_DOWN;
    } else if (clientevent.equals(ClientEvent.turnLeft)) {
      payload.eventType = MazewarPacket.ACTION_TURN_LEFT;
    } else if (clientevent.equals(ClientEvent.turnRight)) {
      payload.eventType = MazewarPacket.ACTION_TURN_RIGHT;
    } else if (clientevent.equals(ClientEvent.fire)) {
      payload.eventType = MazewarPacket.ACTION_FIRE_PROJECTILE;
    } else if (clientevent.equals(ClientEvent.register)) {
      payload.eventType = MazewarPacket.REGISTER;
    } else if (clientevent.equals(ClientEvent.quit)) {
      payload.eventType = MazewarPacket.QUIT;
    } else {
      return false;
    }

    try {
      outStream.writeObject(payload);
    } catch (IOException e) {
      /* TODO how do we handle this? */
      e.printStackTrace();
      return false;
    }

    return true;
  }