@Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    String[] parts = req.getRequestURI().split("/");
    if (parts.length != 4) {
      resp.sendError(404);
      return;
    }

    ChannelService channelService = ChannelServiceFactory.getChannelService();
    ChannelPresence presence = channelService.parsePresence(req);
    GameChannelId channelId = GameChannelId.parse(presence.clientId());

    // get match
    Match match =
        WordgameStore.getMatch(
            channelId.getGameId(), channelId.getMatchId(), channelId.getPlayerId());
    Player opponent = match.getOpponent();

    if (parts[3].equals("connected")) {
      // set the state of this player as connected
      match.getPlayer().setConnected();
      WordgameStore.storePlayer(channelId.getGameId(), channelId.getMatchId(), match.getPlayer());

      if (opponent != null) {

        if (opponent.getStatus() != Status.NOT_CONNECTED) {
          // notify the opponent about this player
          String opponentChannel =
              GameChannelId.getChannelId(channelId.getGameId(), channelId.getMatchId(), opponent);

          String msg = WordgameProtocol.notifyOpponentConnected(match.getPlayer());
          channelService.sendMessage(new ChannelMessage(opponentChannel, msg));
        }

        // notify this player about the opponent
        String msg = WordgameProtocol.notifyOpponentConnected(opponent);
        channelService.sendMessage(new ChannelMessage(presence.clientId(), msg));
      }
      return;
    }

    if (parts[3].equals("disconnected-DISABLED")) {

      // remove player from game
      WordgameStore.removePlayer(channelId.getGameId(), channelId.getMatchId(), match.getPlayer());

      // notify opponent
      if (opponent != null && opponent.getStatus() != Status.NOT_CONNECTED) {
        String opponentChannel =
            GameChannelId.getChannelId(channelId.getGameId(), channelId.getMatchId(), opponent);
        String partedMsg = WordgameProtocol.notifyParted();
        channelService.sendMessage(new ChannelMessage(opponentChannel, partedMsg));
      }
      return;
    }
  }
Esempio n. 2
0
 @Override
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
   ChannelService channelService = ChannelServiceFactory.getChannelService();
   ChannelPresence presence = channelService.parsePresence(request);
   Objectify ofy = ObjectifyService.begin();
   User user;
   try {
     // System.out.println(presence.clientId() + " connected");
     user =
         UserHelper.getUserWithEmail(ChannelApiHelper.getUserForAppkey(presence.clientId()), ofy);
   } catch (Exception e) {
     return;
   }
   user.isConnected = true;
   ofy.put(user);
   sendConnectionToContacts(ofy.get(user.getContacts()).values().iterator(), user.getEmail());
 }
Esempio n. 3
0
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    ChannelPresence presence = channelService.parsePresence(request);

    String[] parts = presence.clientId().split(">");
    String email = parts[0];

    Player player = ofy().load().type(Player.class).id(email).get();

    if (player == null) {
      throw new IllegalArgumentException("Player not found");
    }

    if (presence.isConnected()) {
      player.getChannels().add(presence.clientId());
      System.out.println(presence.clientId() + " connected");
    } else {
      player.getChannels().remove(presence.clientId());
      System.out.println(presence.clientId() + " disconncted");
    }

    ofy().save().entity(player).now();
  }