/server/firebase-service-modules/tournament-history-storage-service/src/main/java/com/cubeia/poker/tournament/history/storage/impl/DatabaseStorageService.java
Java | 193 lines | 139 code | 37 blank | 17 comment | 0 complexity | e4c552b0fa000d9802ea8aaf44d930a2 MD5 | raw file
Possible License(s): AGPL-3.0
- /**
- * Copyright (C) 2010 Cubeia Ltd <info@cubeia.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.cubeia.poker.tournament.history.storage.impl;
- import com.cubeia.firebase.api.server.SystemException;
- import com.cubeia.firebase.api.service.Service;
- import com.cubeia.firebase.api.service.ServiceContext;
- import com.cubeia.games.poker.common.mongo.DatabaseStorageConfiguration;
- import com.cubeia.games.poker.common.mongo.MongoStorage;
- import com.cubeia.poker.tournament.history.api.HistoricTournament;
- import com.cubeia.poker.tournament.history.api.PlayerPosition;
- import com.cubeia.poker.tournament.history.api.TournamentEvent;
- import com.cubeia.poker.tournament.history.storage.api.TournamentHistoryPersistenceService;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DBObject;
- import com.mongodb.util.JSON;
- import org.apache.log4j.Logger;
- import org.bson.types.ObjectId;
- // Use Morphia instead.
- public class DatabaseStorageService implements TournamentHistoryPersistenceService, Service {
- private static final Logger log = Logger.getLogger(DatabaseStorageService.class);
- private MongoStorage mongoStorage;
- private DatabaseStorageConfiguration configuration;
- private static final String TOURNAMENT_COLLECTION = "tournaments";
- private Gson gson = createGson();
- @Override
- public String createHistoricTournament() {
- HistoricTournament tournament = new HistoricTournament();
- DBObject dbObject = persist(tournament);
- Object id = dbObject.get("_id");
- return id.toString();
- }
- @Override
- public HistoricTournament getHistoricTournament(String id) {
- DBObject dbObject = mongoStorage.getById(new ObjectId(id), TOURNAMENT_COLLECTION);
- return createGson().fromJson(dbObject.toString(), HistoricTournament.class);
- }
- @Override
- public void playerOut(int playerId, int position, int payoutInCents, String historicId, long now) {
- addEvent(historicId, new TournamentEvent(now, "player " + playerId + " out", "" + position));
- addPlayerPosition(historicId, playerId, position, payoutInCents);
- }
- @Override
- public void playerMoved(int playerId, int tableId, String historicId, long now) {
- addEvent(historicId, new TournamentEvent(now, "player " + playerId + " moved", "" + tableId));
- }
- @Override
- public void statusChanged(String status, String historicId, long now) {
- addEvent(historicId, new TournamentEvent(now, "status changed", status));
- }
- @Override
- public void blindsUpdated(String historicId, Integer ante, Integer smallBlind, Integer bigBlind, long now) {
- addEvent(historicId, new TournamentEvent(now, "blinds updated", ante + "/" + smallBlind + "/" + bigBlind));
- }
- @Override
- public void setStartTime(String historicId, long date) {
- setProperty(historicId, "startTime", date);
- }
- @Override
- public void setEndTime(String historicId, long date) {
- setProperty(historicId, "endTime", date);
- }
- @Override
- public void setName(String historicId, String name) {
- setProperty(historicId, "name", name);
- }
- @Override
- public void addTable(String historicId, String externalTableId) {
- pushObject(historicId, externalTableId, "tables");
- }
- @Override
- public void playerRegistered(String historicId, int playerId, long now) {
- addEvent(historicId, new TournamentEvent(now, "player registered", String.valueOf(playerId)));
- }
- @Override
- public void playerUnregistered(String historicId, int playerId, long now) {
- addEvent(historicId, new TournamentEvent(now, "player un-registered", String.valueOf(playerId)));
- }
- @Override
- public void playerFailedUnregistering(String historicId, int playerId, String message, long now) {
- addEvent(historicId, new TournamentEvent(now, "player failed un-registering: " + message, String.valueOf(playerId)));
- }
- @Override
- public void playerOpenedSession(String historicId, int playerId, String sessionId, long now) {
- addEvent(historicId, new TournamentEvent(now, "player opened session: " + sessionId, String.valueOf(playerId)));
- }
- @Override
- public void playerFailedOpeningSession(String historicId, int playerId, String message, long now) {
- addEvent(historicId, new TournamentEvent(now, "player failed opening session: " + message, String.valueOf(playerId)));
- }
- private void setProperty(String historicId, String propertyName, Object value) {
- BasicDBObject tournamentId = new BasicDBObject().append("_id", new ObjectId(historicId));
- DBObject update = new BasicDBObject().append("$set", new BasicDBObject(propertyName, value));
- log.debug("Storing property " + propertyName + " to " + value + " for " + historicId);
- mongoStorage.update(tournamentId, update, TOURNAMENT_COLLECTION);
- }
- private void addEvent(String historicId, TournamentEvent event) {
- pushObject(historicId, event, "events");
- }
- private void addPlayerPosition(String historicId, int playerId, int position, int payoutInCents) {
- pushObject(historicId, new PlayerPosition(playerId, position, payoutInCents), "positions");
- }
- private void pushObject(String historicId, Object object, String array) {
- log.debug("Pushing " + object + " to " + array + " for tournament " + historicId);
- BasicDBObject tournamentId = new BasicDBObject().append("_id", new ObjectId(historicId));
- BasicDBObject updateCommand = new BasicDBObject().append("$push", new BasicDBObject(array, JSON.parse(gson.toJson(object))));
- mongoStorage.update(tournamentId, updateCommand, TOURNAMENT_COLLECTION);
- }
- private DBObject persist(HistoricTournament tournament) {
- DBObject dbObject = (DBObject) JSON.parse(gson.toJson(tournament));
- mongoStorage.persist(dbObject, TOURNAMENT_COLLECTION);
- return dbObject;
- }
- private Gson createGson() {
- GsonBuilder b = new GsonBuilder();
- b.setPrettyPrinting();
- return b.create();
- }
- protected DatabaseStorageConfiguration getConfiguration(ServiceContext context) {
- return new DatabaseStorageConfiguration().load(context.getServerConfigDirectory().getAbsolutePath());
- }
- protected MongoStorage getMongoStorage() {
- return new MongoStorage(configuration);
- }
- @Override
- public void init(ServiceContext context) throws SystemException {
- configuration = getConfiguration(context);
- mongoStorage = getMongoStorage();
- }
- @Override
- public void start() {
- mongoStorage.connect();
- }
- @Override
- public void stop() {
- mongoStorage.disconnect();
- }
- @Override
- public void destroy() {
- }
- }