/** @author tuanda */
public class GetChooseAvatarBusiness extends AbstractBusiness {

  private static final Logger mLog =
      LoggerContext.getLoggerFactory().getLogger(GetChooseAvatarBusiness.class);

  @Override
  public int handleMessage(ISession aSession, IRequestMessage aReqMsg, IResponsePackage aResPkg) {
    mLog.debug("[GET_AVATARS]: Catch");
    MessageFactory msgFactory = aSession.getMessageFactory();
    GetChooseAvatarResponse resBuyAvatar =
        (GetChooseAvatarResponse) msgFactory.getResponseMessage(aReqMsg.getID());
    resBuyAvatar.session = aSession;
    try {
      GetChooseAvatarRequest rqBuyAvatar = (GetChooseAvatarRequest) aReqMsg;
      long uid = rqBuyAvatar.uid;
      String res = AvatarManager.getChooseAvatarForUser(uid, aSession.isMobileDevice());
      resBuyAvatar.setSuccess(res);
    } catch (Throwable t) {
      t.printStackTrace();
      resBuyAvatar.setFailure("Có lỗi xảy ra với avatar bạn muốn mua.");
    } finally {
      if ((resBuyAvatar != null)) {
        aResPkg.addMessage(resBuyAvatar);
      }
    }
    return 1;
  }
}
/** @author tuanda */
public class GetTopGameBusiness extends AbstractBusiness {

  private static final org.slf4j.Logger mLog =
      LoggerContext.getLoggerFactory().getLogger(GetTopGameBusiness.class);

  public int handleMessage(ISession aSession, IRequestMessage aReqMsg, IResponsePackage aResPkg) {
    mLog.debug("get TopGame");
    MessageFactory msgFactory = aSession.getMessageFactory();
    GetTopGameResponse resalbum =
        (GetTopGameResponse) msgFactory.getResponseMessage(aReqMsg.getID());

    try {

      GetTopGameRequest rqAlb = (GetTopGameRequest) aReqMsg;
      UserEntity entity = aSession.getUserEntity();
      CacheGameInfo cacheGame = new CacheGameInfo();
      List<UserEntity> lstUsers = cacheGame.getTopGame(entity.partnerId, rqAlb.gameId);
      StringBuilder sb = new StringBuilder();
      int userSize = lstUsers.size();

      for (int i = 0; i < userSize; i++) {
        UserEntity usrEntity = lstUsers.get(i);
        sb.append(usrEntity.mUid).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(usrEntity.mUsername).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(usrEntity.point).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(usrEntity.playsNumber).append(AIOConstants.SEPERATOR_BYTE_2);
      }

      if (userSize > 0) {
        sb.deleteCharAt(sb.length() - 1);
      }

      resalbum.setSuccess(sb.toString());
      aResPkg.addMessage(resalbum);

    } catch (Exception ex) {
      mLog.error(ex.getMessage(), ex);
      resalbum.setFailure("co loi xay ra");
      aResPkg.addMessage(resalbum);
    } //        catch(BusinessException ex)
    //        {
    //            mLog.warn(ex.getMessage());
    //            resalbum.setFailure(ex.getMessage());
    //
    //        }
    finally {
      //            if (resalbum!= null)
      //            {
      //                try {
      //                    aSession.write(resalbum);
      //                } catch (ServerException ex) {
      //                    mLog.error(ex.getMessage(), ex);
      //                }
      //
      //            }
    }
    return 1;
  }
}
public class XESendPrivateMessageJSON implements IMessageProtocol {

  private final Logger mLog =
      LoggerContext.getLoggerFactory().getLogger(XESendPrivateMessageJSON.class);

  @Override
  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    try {
      JSONObject jsonData = (JSONObject) aEncodedObj;
      XESendPrivateMessageRequest rq = (XESendPrivateMessageRequest) aDecodingObj;

      String[] arr = jsonData.getString("v").split(AIOConstants.SEPERATOR_BYTE_1);

      if (arr.length > 0) {
        rq.setToUserId(Long.valueOf(arr[0]));
      }

      if (arr.length > 1) {
        rq.setTitle(arr[1]);
      }

      if (arr.length > 2) {
        rq.setContent(arr[2]);
      }

      return true;
    } catch (Throwable t) {
      mLog.error("[DECODER] " + aDecodingObj.getID(), t);
      return false;
    }
  }

  @Override
  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();

      XESendPrivateMessageResponse res = (XESendPrivateMessageResponse) aResponseMessage;
      StringBuilder sb = new StringBuilder();
      sb.append(Integer.toString(aResponseMessage.getID())).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(Integer.toString(res.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);
      sb.append(res.getMessage());

      encodingObj.put("v", sb.toString());
      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + aResponseMessage.getID(), t);
      return null;
    }
  }
}
/** @author tuanda */
public class GetNewestBlogJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(GetNewestBlogJSON.class);

  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    try {
      JSONObject jsonData = (JSONObject) aEncodedObj;
      GetNewestBlogRequest blogRequest = (GetNewestBlogRequest) aDecodingObj;

      String v = jsonData.getString("v");
      String[] args = v.split(AIOConstants.SEPERATOR_BYTE_1);

      blogRequest.size = Integer.parseInt(args[1]);
      blogRequest.page = Integer.parseInt(args[0]);

      return true;
    } catch (JSONException ex) {
      mLog.error(ex.getMessage(), ex);
    }
    return true;
  }

  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();

      GetNewestBlogResponse res = (GetNewestBlogResponse) aResponseMessage;

      StringBuilder sb = new StringBuilder();
      sb.append(Integer.toString(aResponseMessage.getID())).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(Integer.toString(res.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);

      if (res.mCode == ResponseCode.FAILURE) {
        sb.append(res.errMsg);
      } else {
        if (res.value != null && !res.value.equals("")) {
          sb.append(res.value);
        }
      }

      encodingObj.put("v", sb.toString());

      return encodingObj;
    } catch (JSONException ex) {
      mLog.error(ex.getMessage(), ex);
    }

    return null;
  }
}
/** @author ThangTD */
public class XEGetInventoryJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(XEGetInventoryJSON.class);

  @Override
  public boolean decode(Object paramObject, IRequestMessage paramIRequestMessage)
      throws ServerException {
    return true;
  }

  @Override
  public Object encode(IResponseMessage paramIResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();
      XEGetInventoryResponse response = (XEGetInventoryResponse) paramIResponseMessage;

      StringBuilder sb = new StringBuilder();
      sb.append(Integer.toString(paramIResponseMessage.getID()))
          .append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(Integer.toString(response.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);

      if (response.mCode == ResponseCode.SUCCESS) {
        int size = response.itemsList.size();
        if (size != 0) {
          StringBuilder sb1 = new StringBuilder();
          for (int i = 0; i < size; i++) {
            InventoryItemEntity item = response.itemsList.get(i);
            sb1.append(item.getItemCode()).append(AIOConstants.SEPERATOR_BYTE_1);
            sb1.append(item.getName()).append(AIOConstants.SEPERATOR_BYTE_1);
            sb1.append(item.getDescription()).append(AIOConstants.SEPERATOR_BYTE_1);
            sb1.append(item.getQuantity()).append(AIOConstants.SEPERATOR_BYTE_1);
            sb1.append(item.getIsUsable() ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_2);
          }
          sb1.deleteCharAt(sb1.length() - 1);
          sb.append(sb1);
        }
      }

      encodingObj.put("v", sb);

      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + paramIResponseMessage.getID(), t);
      return null;
    }
  }
}
/** @author tuanda */
public class DeleteItemJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(DeleteItemJSON.class);

  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    try {
      JSONObject jsonData = (JSONObject) aEncodedObj;
      DeleteItemRequest newRequest = (DeleteItemRequest) aDecodingObj;
      String v = jsonData.getString("v");
      String[] arr = v.split(AIOConstants.SEPERATOR_BYTE_1);

      newRequest.systemObjectId = Integer.parseInt(arr[0]);
      newRequest.systemObjectRecordId = Long.parseLong(arr[1]);

      return true;
    } catch (JSONException ex) {
      mLog.error(ex.getMessage(), ex);
      return false;
    }
  }

  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();
      // put response data into json object

      // cast response obj
      DeleteItemResponse res = (DeleteItemResponse) aResponseMessage;

      StringBuilder sb = new StringBuilder();
      sb.append(Integer.toString(aResponseMessage.getID())).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(Integer.toString(res.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);

      if (res.mCode == ResponseCode.FAILURE) {
        sb.append(res.mErrorMsg);
      }

      encodingObj.put("v", sb.toString());

      // response encoded obj
      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + aResponseMessage.getID(), t);
      return null;
    }
  }
}
/** @author tuanda */
public class DeleteMessageBusiness extends AbstractBusiness {

  private static final Logger mLog =
      LoggerContext.getLoggerFactory().getLogger(DeleteMessageBusiness.class);

  public int handleMessage(ISession aSession, IRequestMessage aReqMsg, IResponsePackage aResPkg) {
    //        mLog.debug("[BET] : Catch  ; " + aSession.getUserName());
    MessageFactory msgFactory = aSession.getMessageFactory();
    DeleteMessageResponse resBet =
        (DeleteMessageResponse) msgFactory.getResponseMessage(aReqMsg.getID());

    try {
      DeleteMessageRequest rqAddComment = (DeleteMessageRequest) aReqMsg;
      MessageDB db = new MessageDB();
      db.deleteMessage(rqAddComment.type, rqAddComment.messageId, aSession.getUID());

      resBet.mCode = ResponseCode.SUCCESS;
      aSession.write(resBet);
      CacheUserInfo cache = new CacheUserInfo();
      cache.deleteCacheMessage(aSession.getUID());

    } catch (Throwable t) {
      // resBet.setFailure(ResponseCode.FAILURE, t.getMessage());
      mLog.error("Process message " + aReqMsg.getID() + " error.", t);
      try {
        resBet.setFailure(ResponseCode.FAILURE, "Co loi xay ra");
        aSession.write(resBet);
      } catch (ServerException ex) {
        // java.util.logging.Logger.getLogger(TurnBusiness.class.getName()).log(Level.SEVERE,
        // null, ex);
      }
    }

    return 1;
  }
}
Example #8
0
public class FastPlayJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(FastPlayJSON.class);

  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    try {
      // request data
      JSONObject jsonData = (JSONObject) aEncodedObj;
      FastPlayRequest fast = (FastPlayRequest) aDecodingObj;

      if (jsonData.has("v")) {
        String params = jsonData.getString("v");
        String[] arrValues = params.split(AIOConstants.SEPERATOR_BYTE_1);

        if (arrValues.length > 0) {
          fast.zoneId = Integer.valueOf(arrValues[0]);
        }
        if (arrValues.length > 1) {
          fast.setLevelId(Integer.valueOf(arrValues[1]));
        } else {
          fast.setLevelId(1);
        }

        return true;
      }

      // plain obj
      // decoding
      fast.zoneId = jsonData.getInt("room_id");
      return true;
    } catch (Throwable t) {
      mLog.error("[DECODER] " + aDecodingObj.getID(), t);
      return false;
    }
  }

  private void getMidEncode(FastPlayResponse fast, JSONObject encodingObj) throws JSONException {
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toString(MessagesID.FastPlay)).append(AIOConstants.SEPERATOR_BYTE_1);
    sb.append(Integer.toString(fast.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);
    if (fast.mCode == ResponseCode.SUCCESS) {
      sb.append(Long.toString(fast.matchID)).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(Integer.toString(fast.tableID));
    } else {
      sb.append(fast.message);
    }
    encodingObj.put("v", sb.toString());
  }

  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();
      FastPlayResponse fast = (FastPlayResponse) aResponseMessage;
      if (fast.session != null
          && fast.session.getByteProtocol() > AIOConstants.PROTOCOL_ADVERTISING) {
        getMidEncode(fast, encodingObj);

        return encodingObj;
      }

      // put response data into json object
      encodingObj.put("mid", aResponseMessage.getID());

      encodingObj.put("code", fast.mCode);
      // System.out.println(" chat.mUsername : "******"error_msg", fast.message);
      } else if (fast.mCode == ResponseCode.SUCCESS) {
        encodingObj.put("match_id", fast.matchID);
        encodingObj.put("nTable", fast.tableID);
      }
      // response encoded obj
      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + aResponseMessage.getID(), t);
      return null;
    }
  }
}
public class GetRichestsJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(GetRichestsJSON.class);

  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    return true;
  }

  private String finalProtocol(GetRichestsResponse getFrientList) {
    StringBuilder sb = new StringBuilder();
    for (UserEntity userEntity : getFrientList.mRichestsList) {
      sb.append(userEntity.mUid).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.mUsername).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.avatarID).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.level).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.money).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.playsNumber).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.mIsMale).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(userEntity.isLogin).append(AIOConstants.SEPERATOR_BYTE_2);
    }
    if (sb.length() > 0) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
  }

  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();

      GetRichestsResponse getRichestsList = (GetRichestsResponse) aResponseMessage;
      if (getRichestsList.session != null
          && getRichestsList.session.getByteProtocol() > AIOConstants.PROTOCOL_ADVERTISING) {
        StringBuilder sb = new StringBuilder();
        sb.append(Integer.toString(aResponseMessage.getID())).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(Integer.toString(getRichestsList.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);
        if (getRichestsList.mCode == ResponseCode.FAILURE) {
          sb.append(getRichestsList.mErrorMsg);
        } else {
          sb.append(finalProtocol(getRichestsList));
        }
        encodingObj.put("v", sb.toString());
        return encodingObj;
      }
      encodingObj.put("mid", aResponseMessage.getID());
      encodingObj.put("code", getRichestsList.mCode);
      if (getRichestsList.mCode == ResponseCode.FAILURE) {
      } else if (getRichestsList.mCode == ResponseCode.SUCCESS) {
        JSONArray arrRooms = new JSONArray();
        if (getRichestsList.mRichestsList != null) {
          for (UserEntity userEntity : getRichestsList.mRichestsList) {
            // with each playing room
            JSONObject jRoom = new JSONObject();
            // attached object
            jRoom.put("username", userEntity.mUsername);
            jRoom.put("uid", userEntity.mUid);
            jRoom.put("avatar", userEntity.avatarID);
            jRoom.put("level", userEntity.level);
            jRoom.put("money", userEntity.money);
            jRoom.put("PlaysNumber", userEntity.playsNumber);
            jRoom.put("sex", userEntity.mIsMale);
            arrRooms.put(jRoom);
          }
        }
        encodingObj.put("frient_list", arrRooms);
      }
      // response encoded obj
      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + aResponseMessage.getID(), t);
      return null;
    }
  }
}
Example #10
0
/** @author tuanda */
public class JoinJSON implements IMessageProtocol {

  private final Logger mLog = LoggerContext.getLoggerFactory().getLogger(JoinJSON.class);

  @Override
  public boolean decode(Object aEncodedObj, IRequestMessage aDecodingObj) throws ServerException {
    try {
      // request data
      JSONObject jsonData = (JSONObject) aEncodedObj;
      // request messsage
      JoinRequest matchJoin = (JoinRequest) aDecodingObj;

      String[] arrV = jsonData.getString("v").split(AIOConstants.SEPERATOR_BYTE_1);
      matchJoin.mMatchId = Long.parseLong(arrV[0]);
      // jsonData.getLong("v");
      if (arrV.length > 1) {
        matchJoin.matrixSize = Integer.parseInt(arrV[1]);
      }

      return true;

    } catch (Throwable t) {
      mLog.error("[DECODER] " + aDecodingObj.getID(), t);
      return false;
    }
  }

  private void newProtocol(JoinResponse matchJoin, JSONObject encodingObj) throws JSONException {
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toString(MessagesID.MATCH_JOIN)).append(AIOConstants.SEPERATOR_BYTE_1);
    sb.append(Integer.toString(matchJoin.mCode)).append(AIOConstants.SEPERATOR_NEW_MID);
    if (matchJoin.mCode == ResponseCode.FAILURE) {
      sb.append(matchJoin.mErrorMsg);
    } else {
      sb.append(matchJoin.mMatchId).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(matchJoin.minBet).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(matchJoin.isPlaying ? "1" : "0"); // .append(AIOConstants.SEPERATOR_BYTE_1);
      /*
       * sb.append(matchJoin.roomOwner.id).append(
       * AIOConstants.SEPERATOR_BYTE_1);
       */
      switch (matchJoin.zoneID) {
        case ZoneID.PHOM:
          {
            sb.append(AIOConstants.SEPERATOR_BYTE_1)
                .append(matchJoin.isAn ? "1" : "0")
                .append(AIOConstants.SEPERATOR_BYTE_1);
            sb.append(matchJoin.isTaiGui ? "1" : "0"); // .append(AIOConstants.SEPERATOR_BYTE_1);
            if (matchJoin.isObserve) {
              sb.append(AIOConstants.SEPERATOR_BYTE_1)
                  .append(matchJoin.duty)
                  .append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.turn).append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.deck).append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.currCard);
            }
            sb.append(AIOConstants.SEPERATOR_BYTE_3);
            sb.append(
                phomData(
                    matchJoin,
                    matchJoin.mPlayerPhom,
                    false)); // .append(AIOConstants.SEPERATOR_BYTE_2);
            if (!matchJoin.mWaitingPlayerPhom.isEmpty()) {
              sb.append(AIOConstants.SEPERATOR_BYTE_2)
                  .append(
                      phomData(
                          matchJoin,
                          matchJoin.mWaitingPlayerPhom,
                          true)); // .append(AIOConstants.SEPERATOR_BYTE_1);
            }

            break;
          }
        case ZoneID.TIENLEN:
          {
            sb.append(AIOConstants.SEPERATOR_BYTE_1)
                .append(
                    matchJoin.isHidePoker ? "1" : "0"); // .append(AIOConstants.SEPERATOR_BYTE_1);

            if (matchJoin.isObserve) {
              sb.append(AIOConstants.SEPERATOR_BYTE_1)
                  .append(matchJoin.duty)
                  .append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.turn).append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.cards); // .append(AIOConstants.SEPERATOR_BYTE_1);
            }
            sb.append(AIOConstants.SEPERATOR_BYTE_3);
            sb.append(
                tienLenData(
                    matchJoin,
                    matchJoin.mTienLenPlayer,
                    false)); // .append(AIOConstants.SEPERATOR_BYTE_1);
            if (!matchJoin.mWaitingPlayerTienlen.isEmpty()) {
              sb.append(AIOConstants.SEPERATOR_BYTE_2)
                  .append(
                      tienLenData(
                          matchJoin,
                          matchJoin.mWaitingPlayerTienlen,
                          true)); // .append(AIOConstants.SEPERATOR_BYTE_1);
            }
            break;
          }
        case ZoneID.SAM:
          {
            sb.append(AIOConstants.SEPERATOR_BYTE_1).append("0");
            if (matchJoin.isObserve) {
              sb.append(AIOConstants.SEPERATOR_BYTE_1)
                  .append("0")
                  .append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.turn).append(AIOConstants.SEPERATOR_BYTE_1);
              sb.append(matchJoin.cards); // .append(AIOConstants.SEPERATOR_BYTE_1);
            }
            sb.append(AIOConstants.SEPERATOR_BYTE_3);
            sb.append(samData(matchJoin, matchJoin.mPlayerSam, false));
            if (!matchJoin.mWaitingPlayerSam.isEmpty()) {
              sb.append(AIOConstants.SEPERATOR_BYTE_2)
                  .append(samData(matchJoin, matchJoin.mWaitingPlayerSam, true));
            }
            break;
          }

        case ZoneID.PIKACHU:
        case ZoneID.NEW_BA_CAY:
        case ZoneID.BAU_CUA_TOM_CA:
        case ZoneID.AILATRIEUPHU:

        default:
          {
            sb.append(AIOConstants.SEPERATOR_BYTE_3);
            sb.append(gameData(matchJoin, matchJoin.playing, false));
            if (!matchJoin.waiting.isEmpty()) {
              if (!matchJoin.playing.isEmpty()) {
                sb.append(AIOConstants.SEPERATOR_BYTE_2);
              }
              sb.append(gameData(matchJoin, matchJoin.waiting, true));
            }

            break;
          }
      }

      sb.append(AIOConstants.SEPERATOR_BYTE_3).append(matchJoin.phongID);
      if (matchJoin.zoneID == ZoneID.NEW_BA_CAY) {
        sb.append(AIOConstants.SEPERATOR_BYTE_3);
        sb.append(Long.toString(matchJoin.roomOwner.id));
      }
    }

    encodingObj.put("v", sb.toString());
    // return encodingObj;
  }

  private StringBuilder samData(
      JoinResponse matchJoin, List<? extends SimplePlayer> players, boolean isObserver) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < players.size(); i++) {
      SamPlayer player = (SamPlayer) players.get(i);
      sb.append(player.id).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(player.username).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(player.avatarID).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(player.cash).append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(player.isReady ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
      sb.append(isObserver ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1); //
      sb.append(player.isCai ? "1" : "0");
      if (!isObserver) {
        sb.append(AIOConstants.SEPERATOR_BYTE_1).append(player.playingCardSize());
      }
      if (i < players.size() - 1) {
        sb.append(AIOConstants.SEPERATOR_BYTE_2);
      }
    }

    return sb;
  }

  private StringBuilder tienLenData(
      JoinResponse matchJoin, ArrayList<TienLenPlayer> players, boolean isObserver) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < players.size(); i++) {
      TienLenPlayer player = players.get(i);
      if (player != null) {
        sb.append(player.id).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.username).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.avatarID).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.cash).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.isReady ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(isObserver ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1); //
        sb.append(Integer.toString(player.level));

        if (!isObserver) {
          sb.append(AIOConstants.SEPERATOR_BYTE_1).append(player.numHand);
        }
        if (i < players.size() - 1) {
          sb.append(AIOConstants.SEPERATOR_BYTE_2);
        }
      }
    }
    return sb;
  }

  private StringBuilder phomData(
      JoinResponse matchJoin, ArrayList<PhomPlayer> players, boolean isObserver) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < players.size(); i++) {
      PhomPlayer player = players.get(i);
      if (player != null) {
        sb.append(player.id).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.username).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.avatarID).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.cash).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.isReady ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(isObserver ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(Integer.toString(player.level));

        if (!isObserver) {
          sb.append(AIOConstants.SEPERATOR_BYTE_1);
          sb.append(player.cardToString(player.playingCards)).append(AIOConstants.SEPERATOR_BYTE_1);
          sb.append(
              player.cardToString(player.eatingCards)); // .append(AIOConstants.SEPERATOR_BYTE_1);
          if (player.haPhom) {
            sb.append(AIOConstants.SEPERATOR_BYTE_1).append(player.cardToString(player.phoms));
          } else {
            sb.append(AIOConstants.SEPERATOR_BYTE_1).append(0);
          }
        }

        if (i < players.size() - 1) {
          sb.append(AIOConstants.SEPERATOR_BYTE_2);
        }
      }
    }
    return sb;
  }

  private StringBuilder gameData(
      JoinResponse matchJoin, List<? extends SimplePlayer> players, boolean isObserver) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < players.size(); i++) {
      SimplePlayer player = players.get(i);
      //			if (isObserver) {
      //
      //				System.out.println("waiting: " + player.username);
      //			} else {
      //				System.out.println("playing: " + player.username);
      //			}
      if (player != null) {
        sb.append(player.id).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.username).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.avatarID).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.cash).append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(player.isReady ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(isObserver ? "1" : "0").append(AIOConstants.SEPERATOR_BYTE_1);
        sb.append(Integer.toString(player.level));

        if (matchJoin.zoneID == ZoneID.PIKACHU) {
          sb.append(AIOConstants.SEPERATOR_BYTE_1)
              .append(((PikachuPlayer) player).point); // .append(AIOConstants.SEPERATOR_BYTE_1);
        } else if (matchJoin.zoneID == ZoneID.LIENG && matchJoin.isPlaying) {
          sb.append(AIOConstants.SEPERATOR_BYTE_1)
              .append(Integer.toString(((LiengPlayer) player).getTotalRound()));
        }

        // if (i < players.size() - 1)
        sb.append(AIOConstants.SEPERATOR_BYTE_2);
      }
    }

    if (players.size() > 0) {
      sb.deleteCharAt(sb.length() - 1);
    }

    return sb;
  }

  @Override
  public Object encode(IResponseMessage aResponseMessage) throws ServerException {
    try {
      JSONObject encodingObj = new JSONObject();
      JoinResponse matchJoin = (JoinResponse) aResponseMessage;
      newProtocol(matchJoin, encodingObj);
      return encodingObj;
    } catch (Throwable t) {
      mLog.error("[ENCODER] " + aResponseMessage.getID(), t);
      return null;
    }
  }
}
Example #11
0
/** @author tuanda */
public class CacheGameInfo extends CacheUserInfo {
  //    private static final String FILE_NAME_SPACE = "file";
  private static final String GAME_NAME_SPACE = "game";
  private static final String TOP_GAME_CACHE_NAME_SPACE = "topG";
  private static final int GAME_TIME_CACHE = 60;
  private static final int TOP_GAME_TIME_CACHE = 300;
  private static final Logger mLog =
      LoggerContext.getLoggerFactory().getLogger(CacheGameInfo.class);

  private static List<UserEntity> getTopGameFromDB(int partnerId, int gameId)
      throws SQLException, Exception {
    UserDB db = new UserDB();
    return db.getTopGame(partnerId, gameId);
  }

  private String getPhongInfoFromGame(int zoneId, boolean isMobile) {
    RoomDB db = new RoomDB();

    List<NRoomEntity> rooms = db.getRooms(zoneId);
    StringBuilder sb = new StringBuilder();
    if (isMobile) {
      if (rooms != null) {
        int roomSize = rooms.size();
        for (int i = 0; i < roomSize; i++) {
          NRoomEntity entity = rooms.get(i);
          int playing;
          if (entity.getPhong() == null) {
            playing = 0;
          } else {
            playing = entity.getPhong().getPlaying();
          }

          sb.append(entity.getId()).append(AIOConstants.SEPERATOR_ELEMENT);
          sb.append(playing).append(AIOConstants.SEPERATOR_ELEMENT);
          sb.append(entity.getLv()).append(AIOConstants.SEPERATOR_ARRAY);
        }
      }

      if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
      }
    } else {
      if (rooms != null) {

        int roomSize = rooms.size();
        for (int i = 0; i < roomSize; i++) {
          NRoomEntity entity = rooms.get(i);
          int playing;
          if (entity.getPhong() == null) {
            playing = 0;
          } else {
            playing = entity.getPhong().getPlaying();
          }

          sb.append(entity.getId()).append(AIOConstants.SEPERATOR_BYTE_1);
          sb.append(playing).append(AIOConstants.SEPERATOR_BYTE_1);
          sb.append(entity.getLv()).append(AIOConstants.SEPERATOR_BYTE_2);
        }
      }

      if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
      }
    }
    return sb.toString();
  }

  public String getPhongInfo(int zoneId, boolean isMobile) {
    try {
      if (isUseCache) {
        IMemcacheClient client = cachedPool.borrowClient();
        String enity = null;
        try {
          String key = GAME_NAME_SPACE + Integer.toString(zoneId) + (isMobile ? "1" : "0");
          enity = (String) client.get(key);
          if (enity == null) {
            //                    loadFromDatabase++;
            enity = getPhongInfoFromGame(zoneId, isMobile);
            client.set(key, GAME_TIME_CACHE, enity);
          }

        } catch (Exception ex) {
          mLog.error(ex.getMessage(), ex);
        }
        cachedPool.returnClient(client);
        return enity;
      }
    } catch (Exception ex) {
      mLog.error(ex.getMessage(), ex);
    }

    return getPhongInfoFromGame(zoneId, isMobile);
  }

  public List<UserEntity> getTopGame(int partnerId, int gameId) {
    try {
      if (isUseCache) {
        IMemcacheClient client = cachedPool.borrowClient();
        List<UserEntity> enity = null;
        try {
          String key =
              TOP_GAME_CACHE_NAME_SPACE + Integer.toString(partnerId) + Integer.toString(gameId);
          enity = (List<UserEntity>) client.get(key);
          if (enity == null) {
            //                    loadFromDatabase++;
            enity = getTopGameFromDB(partnerId, gameId);
            client.set(key, TOP_GAME_TIME_CACHE, enity);
          }
        } catch (Exception ex) {
          mLog.error(ex.getMessage(), ex);
        }

        cachedPool.returnClient(client);
        return enity;
      }
    } catch (Exception ex) {
      mLog.error(ex.getMessage(), ex);
    }

    try {
      return getTopGameFromDB(partnerId, gameId);
    } catch (Exception ex) {
      mLog.error(ex.getMessage(), ex);
    }

    return null;
  }
}