From d5937f8da4b4fd6bc96b400c244185b868b3aba4 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Wed, 18 Apr 2012 22:59:49 +0000 Subject: [PATCH] Caching game history. --- .../java/com/gempukku/lotro/DateUtils.java | 5 ++ .../lotro/game/GameHistoryService.java | 72 +++++++++++++++++++ .../com/gempukku/lotro/game/GameRecorder.java | 9 ++- .../com/gempukku/lotro/game/LotroServer.java | 9 ++- .../gempukku/lotro/server/ServerResource.java | 12 ++-- .../lotro/server/provider/ServerProvider.java | 23 ++++-- 6 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/DateUtils.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/DateUtils.java index 70bd002e1..132e2d151 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/DateUtils.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/DateUtils.java @@ -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"); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java new file mode 100644 index 000000000..d604cc93d --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameHistoryService.java @@ -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 _playerGameCount = new ConcurrentHashMap(); + + private Map _activeCountCachedTimes = new ConcurrentHashMap(); + private Map _activeCountCachedValues = new ConcurrentHashMap(); + + private Map _gamesPlayedCountCachedTimes = new ConcurrentHashMap(); + private Map _gamesPlayedCountCachedValues = new ConcurrentHashMap(); + + 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 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; + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java index eb727f9e1..7e9f02940 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/GameRecorder.java @@ -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 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()); } }; } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java index 0ea8c5636..7d47aa959 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroServer.java @@ -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() { diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java index 600083a74..76a652148 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -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 playerGameHistory = _gameHistoryDao.getGameHistoryForPlayer(resourceOwner, start, count); - int recordCount = _gameHistoryDao.getGameHistoryForPlayerCount(resourceOwner); + final List 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 ."); sb.append("
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(",
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(",
active players in last week: unknown").append(_gameHistoryService.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7)) .append("
"); sb.append(getLoginHTML()); } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java index 7186844b3..f7a248816 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/provider/ServerProvider.java @@ -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 { private Injectable _chatServerInjectable; private Injectable _leagueServiceInjectable; + private Injectable _gameHistoryServiceInjectable; private Injectable _merchantServiceInjectable; private Injectable _hallServerInjectable; private Injectable _lotroServerInjectable; - // private Injectable _tradeServerInjectable; private Injectable _collectionsManagerInjectable; private Injectable _deliveryServiceInjectable; private Injectable _lotroFormatLibraryInjectable; @@ -63,10 +64,10 @@ public class ServerProvider implements InjectableProvider { 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 { return _leagueServiceInjectable; } + private synchronized Injectable getGameHistoryServiceInjectable() { + if (_gameHistoryServiceInjectable == null) { + final GameHistoryService gameHistoryServiceleagueService = new GameHistoryService(_gameHistoryDao); + _gameHistoryServiceInjectable = new Injectable() { + @Override + public GameHistoryService getValue() { + return gameHistoryServiceleagueService; + } + }; + } + return _gameHistoryServiceInjectable; + } + private synchronized Injectable getMerchantServiceInjectable() { if (_merchantServiceInjectable == null) { final MerchantService merchantService = new MerchantService(_library, getCollectionsManagerInjectable().getValue(), _merchantDao); @@ -166,9 +180,10 @@ public class ServerProvider implements InjectableProvider { // } // + private synchronized Injectable 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() { @Override