PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/server/firebase-service-modules/hand-history-provider-service/src/main/java/com/cubeia/poker/handhistory/provider/impl/HandHistoryProviderServiceImpl.java

https://bitbucket.org/biddyweb/cubeia-poker
Java | 212 lines | 170 code | 25 blank | 17 comment | 17 complexity | 93600a4fba9f4b670a7d2642030862b1 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /**
  2. * Copyright (C) 2010 Cubeia Ltd <info@cubeia.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as
  6. * published by the Free Software Foundation, either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package com.cubeia.poker.handhistory.provider.impl;
  18. import com.cubeia.firebase.api.action.service.ClientServiceAction;
  19. import com.cubeia.firebase.api.action.service.ServiceAction;
  20. import com.cubeia.firebase.api.server.SystemException;
  21. import com.cubeia.firebase.api.service.RoutableService;
  22. import com.cubeia.firebase.api.service.Service;
  23. import com.cubeia.firebase.api.service.ServiceContext;
  24. import com.cubeia.firebase.api.service.ServiceRouter;
  25. import com.cubeia.firebase.io.ProtocolObject;
  26. import com.cubeia.games.poker.handhistoryservice.io.protocol.*;
  27. import com.cubeia.firebase.io.StyxSerializer;
  28. import com.cubeia.games.poker.common.mongo.DatabaseStorageConfiguration;
  29. import com.cubeia.games.poker.common.mongo.MongoStorage;
  30. import com.cubeia.poker.handhistory.api.HandHistoryEvent;
  31. import com.cubeia.poker.handhistory.api.HistoricHand;
  32. import com.cubeia.poker.handhistory.provider.api.HandHistoryProviderService;
  33. import com.google.code.morphia.query.Query;
  34. import com.google.gson.*;
  35. import com.mongodb.BasicDBObject;
  36. import org.apache.log4j.Logger;
  37. import java.lang.reflect.Type;
  38. import java.nio.ByteBuffer;
  39. import java.util.List;
  40. public class HandHistoryProviderServiceImpl implements HandHistoryProviderService, Service, RoutableService {
  41. private static final Logger log = Logger.getLogger(HandHistoryProviderServiceImpl.class);
  42. public static final int MAX_HANDS = 500;
  43. public static final int MAX_HAND_IDS = 500;
  44. private ServiceRouter router;
  45. private MongoStorage mongoStorage;
  46. private DatabaseStorageConfiguration configuration;
  47. private enum PacketType {
  48. hand_ids,
  49. hand,
  50. hands,
  51. undefined
  52. }
  53. @Override
  54. public String getHandIds(int tableId, int playerId, int count, long time) {
  55. log.debug("GetHandIds request data - TableId: " + tableId + " PlayerId: " + playerId + " Count: " + count + " Time: " + time);
  56. String result = "[]";
  57. if (count > 0) {
  58. if (count > MAX_HAND_IDS) {
  59. count = MAX_HAND_IDS;
  60. }
  61. Query query = mongoStorage.createQuery(HistoricHand.class);
  62. query.field("table.tableId").equal(tableId);
  63. query.filter("seats elem", new BasicDBObject("playerId", playerId));
  64. query.retrievedFields(true, "id");
  65. result = convertToJson(query.order("-startTime").limit(count).asKeyList());
  66. }
  67. else {
  68. Query query = mongoStorage.createQuery(HistoricHand.class);
  69. query.field("table.tableId").equal(tableId);
  70. query.field("startTime").greaterThanOrEq(time);
  71. query.filter("seats elem", new BasicDBObject("playerId", playerId));
  72. query.retrievedFields(true, "id").retrievedFields(false, "_id");
  73. result = convertToJson(query.order("-startTime").limit(MAX_HAND_IDS).asKeyList());
  74. }
  75. return result;
  76. }
  77. @Override
  78. public String getHand(String handId, int playerId) {
  79. log.debug("GetHand request data - HandId: " + handId + " PlayerId: " + playerId);
  80. Query query = mongoStorage.createQuery(HistoricHand.class);
  81. query.field("id").equal(handId);
  82. query.filter("seats elem", new BasicDBObject("playerId", playerId));
  83. return convertToJson(query.asList());
  84. }
  85. @Override
  86. public String getHands(int tableId, int playerId, int count, long time) {
  87. log.debug("GetHands request data - TableId: " + tableId + " PlayerId: " + playerId + " Count: " + count + " Time: " + time);
  88. String result = "[]";
  89. if (count > 0) {
  90. if (count > MAX_HANDS) {
  91. count = MAX_HANDS;
  92. }
  93. Query query = mongoStorage.createQuery(HistoricHand.class);
  94. query.field("table.tableId").equal(tableId);
  95. query.filter("seats elem", new BasicDBObject("playerId", playerId));
  96. result = convertToJson(query.order("-startTime").limit(count).asList());
  97. }
  98. else {
  99. Query query = mongoStorage.createQuery(HistoricHand.class);
  100. query.field("table.tableId").equal(tableId);
  101. query.field("startTime").greaterThanOrEq(time);
  102. query.filter("seats elem", new BasicDBObject("playerId", playerId));
  103. result = convertToJson(query.order("-startTime").limit(MAX_HANDS).asList());
  104. }
  105. return result;
  106. }
  107. @Override
  108. public void setRouter(ServiceRouter router) {
  109. this.router = router;
  110. }
  111. @Override
  112. public void onAction(ServiceAction e) {
  113. log.debug("Hand history requested.");
  114. StyxSerializer serializer = new StyxSerializer(new ProtocolObjectFactory());
  115. ProtocolObject protocolObject = serializer.unpack(ByteBuffer.wrap(e.getData()));
  116. PacketType responseType = PacketType.undefined;
  117. String value = "";
  118. int tableId = -1;
  119. if (protocolObject.getClass() == HandHistoryProviderRequestHand.class) {
  120. HandHistoryProviderRequestHand request = (HandHistoryProviderRequestHand)protocolObject;
  121. value = getHand(request.handId, e.getPlayerId());
  122. responseType = PacketType.hand;
  123. } else if (protocolObject.getClass() == HandHistoryProviderRequestHands.class) {
  124. HandHistoryProviderRequestHands request = (HandHistoryProviderRequestHands)protocolObject;
  125. tableId = request.tableId;
  126. value = getHands(request.tableId, e.getPlayerId(), request.count, getTime(request.time));
  127. responseType = PacketType.hands;
  128. } else if (protocolObject.getClass() == HandHistoryProviderRequestHandIds.class) {
  129. HandHistoryProviderRequestHandIds request = (HandHistoryProviderRequestHandIds)protocolObject;
  130. tableId = request.tableId;
  131. value = getHandIds(request.tableId, e.getPlayerId(), request.count, getTime(request.time));
  132. responseType = PacketType.hand_ids;
  133. }
  134. String protocolValue = "{ \"packetType\" : \"" + responseType + "\" ,\"tableId\" : " + tableId + ", \"value\" : " + value + " }";
  135. ServiceAction action = new ClientServiceAction(e.getPlayerId(), -1, protocolValue.getBytes());
  136. router.dispatchToPlayer(e.getPlayerId(), action);
  137. }
  138. private long getTime(String value) {
  139. long time = 0L;
  140. if (!(value == null || value.isEmpty())) {
  141. try {
  142. time = Long.parseLong(value);
  143. }
  144. catch (Throwable t) { }
  145. }
  146. return time;
  147. }
  148. @Override
  149. public void init(ServiceContext context) throws SystemException {
  150. log.debug("HandHistoryProviderService STARTED! ");
  151. configuration = getConfiguration(context);
  152. mongoStorage = getMongoStorage();
  153. }
  154. protected DatabaseStorageConfiguration getConfiguration(ServiceContext context) {
  155. return new DatabaseStorageConfiguration().load(context.getServerConfigDirectory().getAbsolutePath());
  156. }
  157. protected MongoStorage getMongoStorage() {
  158. return new MongoStorage(configuration);
  159. }
  160. private String convertToJson(List hands) {
  161. Gson gson = createGson();
  162. return gson.toJson(hands);
  163. }
  164. private Gson createGson() {
  165. GsonBuilder b = new GsonBuilder();
  166. b.registerTypeAdapter(HandHistoryEvent.class, new HandHistorySerializer());
  167. //b.setPrettyPrinting();
  168. return b.create();
  169. }
  170. @Override
  171. public void destroy() { }
  172. @Override
  173. public void start() {
  174. mongoStorage.connect();
  175. }
  176. @Override
  177. public void stop() {
  178. mongoStorage.disconnect();
  179. }
  180. private static class HandHistorySerializer implements JsonSerializer<HandHistoryEvent> {
  181. @Override
  182. public JsonElement serialize(HandHistoryEvent src, Type typeOfSrc, JsonSerializationContext context) {
  183. Class<? extends HandHistoryEvent> cl = src.getClass();
  184. return context.serialize(src, cl);
  185. }
  186. }
  187. }