Caching game history.

This commit is contained in:
marcins78@gmail.com
2012-04-18 22:59:49 +00:00
parent 4dd971be52
commit d5937f8da4
6 changed files with 110 additions and 20 deletions

View File

@@ -13,6 +13,11 @@ public class DateUtils {
return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH);
}
public static int getCurrentMinute() {
Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
return date.get(Calendar.YEAR) * 100000000 + (date.get(Calendar.MONTH) + 1) * 1000000 + date.get(Calendar.DAY_OF_MONTH) * 10000 + date.get(Calendar.HOUR_OF_DAY) * 100 + date.get(Calendar.MINUTE);
}
public static int offsetDate(int start, int dayOffset) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

View File

@@ -0,0 +1,72 @@
package com.gempukku.lotro.game;
import com.gempukku.lotro.DateUtils;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GameHistoryService {
private GameHistoryDAO _gameHistoryDAO;
private Map<String, Integer> _playerGameCount = new ConcurrentHashMap<String, Integer>();
private Map<Long, Integer> _activeCountCachedTimes = new ConcurrentHashMap<Long, Integer>();
private Map<Long, Integer> _activeCountCachedValues = new ConcurrentHashMap<Long, Integer>();
private Map<Long, Integer> _gamesPlayedCountCachedTimes = new ConcurrentHashMap<Long, Integer>();
private Map<Long, Integer> _gamesPlayedCountCachedValues = new ConcurrentHashMap<Long, Integer>();
public GameHistoryService(GameHistoryDAO gameHistoryDAO) {
_gameHistoryDAO = gameHistoryDAO;
}
public void addGameHistory(String winner, String loser, String winReason, String loseReason, String winRecordingId, String loseRecordingId, String formatName, String winnerDeckName, String loserDeckName, Date startDate, Date endDate) {
_gameHistoryDAO.addGameHistory(winner, loser, winReason, loseReason, winRecordingId, loseRecordingId, formatName, winnerDeckName, loserDeckName, startDate, endDate);
Integer winnerCount = _playerGameCount.get(winner);
Integer loserCount = _playerGameCount.get(loser);
if (winnerCount != null)
_playerGameCount.put(winner, winnerCount + 1);
if (loserCount != null)
_playerGameCount.put(loser, loserCount + 1);
}
public int getGameHistoryForPlayerCount(Player player) {
Integer result = _playerGameCount.get(player.getName());
if (result != null)
return result;
int count = _gameHistoryDAO.getGameHistoryForPlayerCount(player);
_playerGameCount.put(player.getName(), count);
return count;
}
public List<GameHistoryEntry> getGameHistoryForPlayer(Player player, int start, int count) {
return _gameHistoryDAO.getGameHistoryForPlayer(player, start, count);
}
public int getActivePlayersInLastMs(long ms) {
Integer cachedOn = _activeCountCachedTimes.get(ms);
int minute = DateUtils.getCurrentMinute();
if (cachedOn != null && minute == cachedOn)
return _activeCountCachedValues.get(ms);
int result = _gameHistoryDAO.getActivePlayersInLastMs(ms);
_activeCountCachedValues.put(ms, result);
_activeCountCachedTimes.put(ms, minute);
return result;
}
public int getGamesPlayedCountInLastMs(long ms) {
Integer cachedOn = _gamesPlayedCountCachedTimes.get(ms);
int minute = DateUtils.getCurrentMinute();
if (cachedOn != null && minute == cachedOn)
return _gamesPlayedCountCachedValues.get(ms);
int result = _gameHistoryDAO.getGamesPlayedCountInLastMs(ms);
_gamesPlayedCountCachedValues.put(ms, result);
_gamesPlayedCountCachedTimes.put(ms, minute);
return result;
}
}

View File

@@ -1,7 +1,6 @@
package com.gempukku.lotro.game;
import com.gempukku.lotro.common.ApplicationRoot;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.game.state.EventSerializer;
import com.gempukku.lotro.game.state.GameEvent;
import com.gempukku.lotro.game.state.GatheringParticipantCommunicationChannel;
@@ -26,10 +25,10 @@ public class GameRecorder {
private static String _possibleChars = "abcdefghijklmnopqrstuvwxyz0123456789";
private static int _charsCount = _possibleChars.length();
private GameHistoryDAO _gameHistoryDao;
private GameHistoryService _gameHistoryService;
public GameRecorder(GameHistoryDAO gameHistoryDao) {
_gameHistoryDao = gameHistoryDao;
public GameRecorder(GameHistoryService gameHistoryService) {
_gameHistoryService = gameHistoryService;
}
public void migrateReplays() {
@@ -102,7 +101,7 @@ public class GameRecorder {
@Override
public void finishRecording(String winner, String winReason, String loser, String loseReason) {
Map<String, String> playerRecordingId = saveRecordedChannels(recordingChannels);
_gameHistoryDao.addGameHistory(winner, loser, winReason, loseReason, playerRecordingId.get(winner), playerRecordingId.get(loser), formatName, deckNames.get(winner), deckNames.get(loser), startData, new Date());
_gameHistoryService.addGameHistory(winner, loser, winReason, loseReason, playerRecordingId.get(winner), playerRecordingId.get(loser), formatName, deckNames.get(winner), deckNames.get(loser), startData, new Date());
}
};
}

View File

@@ -4,7 +4,6 @@ import com.gempukku.lotro.AbstractServer;
import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.common.CardType;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.logic.timing.GameResultListener;
import com.gempukku.lotro.logic.vo.LotroDeck;
import org.apache.log4j.Logger;
@@ -30,7 +29,7 @@ public class LotroServer extends AbstractServer {
private int _nextGameId = 1;
private DeckDAO _deckDao;
private GameHistoryDAO _gameHistoryDao;
private GameHistoryService _gameHistoryService;
private DefaultCardCollection _defaultCollection;
private CountDownLatch _collectionReadyLatch = new CountDownLatch(1);
@@ -39,9 +38,9 @@ public class LotroServer extends AbstractServer {
private boolean _test;
private GameRecorder _gameRecorder;
public LotroServer(DeckDAO deckDao, GameHistoryDAO gameHistoryDao, LotroCardBlueprintLibrary library, ChatServer chatServer, boolean test) {
public LotroServer(DeckDAO deckDao, GameHistoryService gameHistoryService, LotroCardBlueprintLibrary library, ChatServer chatServer, boolean test) {
_deckDao = deckDao;
_gameHistoryDao = gameHistoryDao;
_gameHistoryService = gameHistoryService;
_lotroCardBlueprintLibrary = library;
_chatServer = chatServer;
_test = test;
@@ -78,7 +77,7 @@ public class LotroServer extends AbstractServer {
// );
// thr.start();
_gameRecorder = new GameRecorder(_gameHistoryDao);
_gameRecorder = new GameRecorder(_gameHistoryService);
}
public void migrateReplays() {

View File

@@ -2,8 +2,8 @@ package com.gempukku.lotro.server;
import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.common.ApplicationRoot;
import com.gempukku.lotro.db.GameHistoryDAO;
import com.gempukku.lotro.db.vo.GameHistoryEntry;
import com.gempukku.lotro.game.GameHistoryService;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.Player;
@@ -43,7 +43,7 @@ public class ServerResource extends AbstractResource {
@Context
private ChatServer _chatServer;
@Context
private GameHistoryDAO _gameHistoryDao;
private GameHistoryService _gameHistoryService;
public ServerResource() {
if (!_test)
@@ -146,8 +146,8 @@ public class ServerResource extends AbstractResource {
Player resourceOwner = getResourceOwnerSafely(request, participantId);
final List<GameHistoryEntry> playerGameHistory = _gameHistoryDao.getGameHistoryForPlayer(resourceOwner, start, count);
int recordCount = _gameHistoryDao.getGameHistoryForPlayerCount(resourceOwner);
final List<GameHistoryEntry> playerGameHistory = _gameHistoryService.getGameHistoryForPlayer(resourceOwner, start, count);
int recordCount = _gameHistoryService.getGameHistoryForPlayerCount(resourceOwner);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
@@ -196,8 +196,8 @@ public class ServerResource extends AbstractResource {
} else {
sb.append("You are not logged in, log in below or <button id='clickToRegister'>register</button>.");
sb.append("<div class='status'>Tables count: ").append(_hallServer.getTablesCount()).append(", players in hall: ").append(_chatServer.getChatRoom("Game Hall").getUsersInRoom().size())
.append(", games played in last 24 hours: unknown")//.append(_gameHistoryDao.getGamesPlayedCountInLastMs(1000 * 60 * 60 * 24))
.append(",<br/> active players in last week: unknown")//.append(_gameHistoryDao.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7))
.append(", games played in last 24 hours: unknown").append(_gameHistoryService.getGamesPlayedCountInLastMs(1000 * 60 * 60 * 24))
.append(",<br/> active players in last week: unknown").append(_gameHistoryService.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7))
.append("</div>");
sb.append(getLoginHTML());
}

View File

@@ -4,6 +4,7 @@ import com.gempukku.lotro.chat.ChatServer;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.collection.DeliveryService;
import com.gempukku.lotro.db.*;
import com.gempukku.lotro.game.GameHistoryService;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroServer;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
@@ -24,10 +25,10 @@ import java.lang.reflect.Type;
public class ServerProvider implements InjectableProvider<Context, Type> {
private Injectable<ChatServer> _chatServerInjectable;
private Injectable<LeagueService> _leagueServiceInjectable;
private Injectable<GameHistoryService> _gameHistoryServiceInjectable;
private Injectable<MerchantService> _merchantServiceInjectable;
private Injectable<HallServer> _hallServerInjectable;
private Injectable<LotroServer> _lotroServerInjectable;
// private Injectable<TradeServer> _tradeServerInjectable;
private Injectable<CollectionsManager> _collectionsManagerInjectable;
private Injectable<DeliveryService> _deliveryServiceInjectable;
private Injectable<LotroFormatLibrary> _lotroFormatLibraryInjectable;
@@ -63,10 +64,10 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return getHallServerInjectable();
if (type.equals(LeagueService.class))
return getLeagueServiceInjectable();
if (type.equals(GameHistoryService.class))
return getGameHistoryServiceInjectable();
if (type.equals(CollectionsManager.class))
return getCollectionsManagerInjectable();
// if (type.equals(TradeServer.class))
// return getTradeServerInjectable();
if (type.equals(DeliveryService.class))
return getDeliveryServiceInjectable();
if (type.equals(LotroFormatLibrary.class))
@@ -110,6 +111,19 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
return _leagueServiceInjectable;
}
private synchronized Injectable<GameHistoryService> getGameHistoryServiceInjectable() {
if (_gameHistoryServiceInjectable == null) {
final GameHistoryService gameHistoryServiceleagueService = new GameHistoryService(_gameHistoryDao);
_gameHistoryServiceInjectable = new Injectable<GameHistoryService>() {
@Override
public GameHistoryService getValue() {
return gameHistoryServiceleagueService;
}
};
}
return _gameHistoryServiceInjectable;
}
private synchronized Injectable<MerchantService> getMerchantServiceInjectable() {
if (_merchantServiceInjectable == null) {
final MerchantService merchantService = new MerchantService(_library, getCollectionsManagerInjectable().getValue(), _merchantDao);
@@ -166,9 +180,10 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
// }
//
private synchronized Injectable<LotroServer> getLotroServerInjectable() {
if (_lotroServerInjectable == null) {
final LotroServer lotroServer = new LotroServer(_deckDao, _gameHistoryDao, _library, getChatServerInjectable().getValue(), false);
final LotroServer lotroServer = new LotroServer(_deckDao, getGameHistoryServiceInjectable().getValue(), _library, getChatServerInjectable().getValue(), false);
lotroServer.startServer();
_lotroServerInjectable = new Injectable<LotroServer>() {
@Override