PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/server/firebase-service-modules/tournament-history-storage-service/src/main/java/com/cubeia/poker/tournament/history/storage/impl/DatabaseStorageService.java

https://bitbucket.org/mstram/cubeia-poker
Java | 193 lines | 139 code | 37 blank | 17 comment | 0 complexity | e4c552b0fa000d9802ea8aaf44d930a2 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.tournament.history.storage.impl;
  18. import com.cubeia.firebase.api.server.SystemException;
  19. import com.cubeia.firebase.api.service.Service;
  20. import com.cubeia.firebase.api.service.ServiceContext;
  21. import com.cubeia.games.poker.common.mongo.DatabaseStorageConfiguration;
  22. import com.cubeia.games.poker.common.mongo.MongoStorage;
  23. import com.cubeia.poker.tournament.history.api.HistoricTournament;
  24. import com.cubeia.poker.tournament.history.api.PlayerPosition;
  25. import com.cubeia.poker.tournament.history.api.TournamentEvent;
  26. import com.cubeia.poker.tournament.history.storage.api.TournamentHistoryPersistenceService;
  27. import com.google.gson.Gson;
  28. import com.google.gson.GsonBuilder;
  29. import com.mongodb.BasicDBObject;
  30. import com.mongodb.DBObject;
  31. import com.mongodb.util.JSON;
  32. import org.apache.log4j.Logger;
  33. import org.bson.types.ObjectId;
  34. // Use Morphia instead.
  35. public class DatabaseStorageService implements TournamentHistoryPersistenceService, Service {
  36. private static final Logger log = Logger.getLogger(DatabaseStorageService.class);
  37. private MongoStorage mongoStorage;
  38. private DatabaseStorageConfiguration configuration;
  39. private static final String TOURNAMENT_COLLECTION = "tournaments";
  40. private Gson gson = createGson();
  41. @Override
  42. public String createHistoricTournament() {
  43. HistoricTournament tournament = new HistoricTournament();
  44. DBObject dbObject = persist(tournament);
  45. Object id = dbObject.get("_id");
  46. return id.toString();
  47. }
  48. @Override
  49. public HistoricTournament getHistoricTournament(String id) {
  50. DBObject dbObject = mongoStorage.getById(new ObjectId(id), TOURNAMENT_COLLECTION);
  51. return createGson().fromJson(dbObject.toString(), HistoricTournament.class);
  52. }
  53. @Override
  54. public void playerOut(int playerId, int position, int payoutInCents, String historicId, long now) {
  55. addEvent(historicId, new TournamentEvent(now, "player " + playerId + " out", "" + position));
  56. addPlayerPosition(historicId, playerId, position, payoutInCents);
  57. }
  58. @Override
  59. public void playerMoved(int playerId, int tableId, String historicId, long now) {
  60. addEvent(historicId, new TournamentEvent(now, "player " + playerId + " moved", "" + tableId));
  61. }
  62. @Override
  63. public void statusChanged(String status, String historicId, long now) {
  64. addEvent(historicId, new TournamentEvent(now, "status changed", status));
  65. }
  66. @Override
  67. public void blindsUpdated(String historicId, Integer ante, Integer smallBlind, Integer bigBlind, long now) {
  68. addEvent(historicId, new TournamentEvent(now, "blinds updated", ante + "/" + smallBlind + "/" + bigBlind));
  69. }
  70. @Override
  71. public void setStartTime(String historicId, long date) {
  72. setProperty(historicId, "startTime", date);
  73. }
  74. @Override
  75. public void setEndTime(String historicId, long date) {
  76. setProperty(historicId, "endTime", date);
  77. }
  78. @Override
  79. public void setName(String historicId, String name) {
  80. setProperty(historicId, "name", name);
  81. }
  82. @Override
  83. public void addTable(String historicId, String externalTableId) {
  84. pushObject(historicId, externalTableId, "tables");
  85. }
  86. @Override
  87. public void playerRegistered(String historicId, int playerId, long now) {
  88. addEvent(historicId, new TournamentEvent(now, "player registered", String.valueOf(playerId)));
  89. }
  90. @Override
  91. public void playerUnregistered(String historicId, int playerId, long now) {
  92. addEvent(historicId, new TournamentEvent(now, "player un-registered", String.valueOf(playerId)));
  93. }
  94. @Override
  95. public void playerFailedUnregistering(String historicId, int playerId, String message, long now) {
  96. addEvent(historicId, new TournamentEvent(now, "player failed un-registering: " + message, String.valueOf(playerId)));
  97. }
  98. @Override
  99. public void playerOpenedSession(String historicId, int playerId, String sessionId, long now) {
  100. addEvent(historicId, new TournamentEvent(now, "player opened session: " + sessionId, String.valueOf(playerId)));
  101. }
  102. @Override
  103. public void playerFailedOpeningSession(String historicId, int playerId, String message, long now) {
  104. addEvent(historicId, new TournamentEvent(now, "player failed opening session: " + message, String.valueOf(playerId)));
  105. }
  106. private void setProperty(String historicId, String propertyName, Object value) {
  107. BasicDBObject tournamentId = new BasicDBObject().append("_id", new ObjectId(historicId));
  108. DBObject update = new BasicDBObject().append("$set", new BasicDBObject(propertyName, value));
  109. log.debug("Storing property " + propertyName + " to " + value + " for " + historicId);
  110. mongoStorage.update(tournamentId, update, TOURNAMENT_COLLECTION);
  111. }
  112. private void addEvent(String historicId, TournamentEvent event) {
  113. pushObject(historicId, event, "events");
  114. }
  115. private void addPlayerPosition(String historicId, int playerId, int position, int payoutInCents) {
  116. pushObject(historicId, new PlayerPosition(playerId, position, payoutInCents), "positions");
  117. }
  118. private void pushObject(String historicId, Object object, String array) {
  119. log.debug("Pushing " + object + " to " + array + " for tournament " + historicId);
  120. BasicDBObject tournamentId = new BasicDBObject().append("_id", new ObjectId(historicId));
  121. BasicDBObject updateCommand = new BasicDBObject().append("$push", new BasicDBObject(array, JSON.parse(gson.toJson(object))));
  122. mongoStorage.update(tournamentId, updateCommand, TOURNAMENT_COLLECTION);
  123. }
  124. private DBObject persist(HistoricTournament tournament) {
  125. DBObject dbObject = (DBObject) JSON.parse(gson.toJson(tournament));
  126. mongoStorage.persist(dbObject, TOURNAMENT_COLLECTION);
  127. return dbObject;
  128. }
  129. private Gson createGson() {
  130. GsonBuilder b = new GsonBuilder();
  131. b.setPrettyPrinting();
  132. return b.create();
  133. }
  134. protected DatabaseStorageConfiguration getConfiguration(ServiceContext context) {
  135. return new DatabaseStorageConfiguration().load(context.getServerConfigDirectory().getAbsolutePath());
  136. }
  137. protected MongoStorage getMongoStorage() {
  138. return new MongoStorage(configuration);
  139. }
  140. @Override
  141. public void init(ServiceContext context) throws SystemException {
  142. configuration = getConfiguration(context);
  143. mongoStorage = getMongoStorage();
  144. }
  145. @Override
  146. public void start() {
  147. mongoStorage.connect();
  148. }
  149. @Override
  150. public void stop() {
  151. mongoStorage.disconnect();
  152. }
  153. @Override
  154. public void destroy() {
  155. }
  156. }