From 2bf53b0cbd51c1a54be917276ffdeca4b2133ca7 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Mon, 9 Jul 2012 14:34:17 +0000 Subject: [PATCH] Adding stats. --- .../com/gempukku/lotro/db/GameHistoryDAO.java | 18 ++--- .../lotro/game/GameHistoryService.java | 31 ++------ .../gempukku/lotro/server/ServerResource.java | 70 +++++++++++++++++-- 3 files changed, 78 insertions(+), 41 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java index baa96943c..bfb0d76a5 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/GameHistoryDAO.java @@ -120,16 +120,17 @@ public class GameHistoryDAO { } } - public int getActivePlayersInLastMs(long ms) { + public int getActivePlayersCount(long from, long duration) { try { Connection connection = _dbAccess.getDataSource().getConnection(); try { PreparedStatement statement = connection.prepareStatement( - "select count(*) from (SELECT winner FROM game_history where end_date>=? union select loser from game_history where end_date>=?) as u"); + "select count(*) from (SELECT winner FROM game_history where end_date>=? and end_date=? and end_date=? and end_date-start_date>" + minTime); + PreparedStatement statement = connection.prepareStatement("select count(*) from game_history where end_date>=? and end_date" + minTime); try { - statement.setLong(1, System.currentTimeMillis() - ms); + statement.setLong(1, from); + statement.setLong(2, from + duration); ResultSet rs = statement.executeQuery(); try { if (rs.next()) 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 index b63513566..6e6eada3a 100644 --- 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 @@ -1,6 +1,5 @@ package com.gempukku.lotro.game; -import com.gempukku.lotro.DateUtils; import com.gempukku.lotro.db.GameHistoryDAO; import com.gempukku.lotro.db.vo.GameHistoryEntry; @@ -13,12 +12,6 @@ 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; } @@ -46,28 +39,12 @@ public class GameHistoryService { 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 getActivePlayersCount(long from, long duration) { + return _gameHistoryDAO.getActivePlayersCount(from, duration); } - 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; + public int getGamesPlayedCount(long from, long duration) { + return _gameHistoryDAO.getGamesPlayedCount(from, duration); } public long getOldestGameHistoryEntry() { 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 37a4dbc05..a4c86143c 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 @@ -3,10 +3,7 @@ package com.gempukku.lotro.server; import com.gempukku.lotro.chat.ChatServer; import com.gempukku.lotro.common.ApplicationRoot; 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; +import com.gempukku.lotro.game.*; import com.gempukku.lotro.hall.HallServer; import com.sun.jersey.spi.resource.Singleton; import org.apache.log4j.Logger; @@ -26,7 +23,12 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.text.DecimalFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; +import java.util.TimeZone; @Singleton @Path("/") @@ -203,9 +205,11 @@ public class ServerResource extends AbstractResource { sb.append(""); } else { sb.append("You are not logged in, log in below or ."); + int day = 1000 * 60 * 60 * 24; + int week = 1000 * 60 * 60 * 24 * 7; 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: ").append(_gameHistoryService.getGamesPlayedCountInLastMs(1000 * 60 * 60 * 24)) - .append(",
active players in last week: ").append(_gameHistoryService.getActivePlayersInLastMs(1000 * 60 * 60 * 24 * 7)) + .append(", games played in last 24 hours: ").append(_gameHistoryService.getGamesPlayedCount(System.currentTimeMillis() - day, day)) + .append(",
active players in last week: ").append(_gameHistoryService.getActivePlayersCount(System.currentTimeMillis() - week, week)) .append("
"); sb.append(getLoginHTML()); } @@ -213,6 +217,60 @@ public class ServerResource extends AbstractResource { return sb.toString(); } + @Path("/stats") + @GET + @Produces(MediaType.APPLICATION_XML) + public Document getGameState( + @QueryParam("startDay") String startDay, + @QueryParam("length") String length, + @QueryParam("participantId") String participantId, + @Context HttpServletRequest request) throws ParserConfigurationException { + Player resourceOwner = getResourceOwnerSafely(request, participantId); + + try { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + format.setTimeZone(TimeZone.getTimeZone("GMT")); + long from = format.parse(startDay).getTime(); + Date to = format.parse(startDay); + if (length.equals("month")) + to.setMonth(to.getMonth() + 1); + else if (length.equals("week")) + to.setDate(to.getDate() + 7); + else if (length.equals("day")) + to.setDate(to.getDate() + 1); + else + sendError(Response.Status.BAD_REQUEST); + long duration = to.getTime() - from; + + int activePlayers = _gameHistoryService.getActivePlayersCount(from, duration); + int gamesCount = _gameHistoryService.getGamesPlayedCount(from, duration); + + GameHistoryStatistics gameHistoryStatistics = _gameHistoryService.getGameHistoryStatistics(from, duration); + + DecimalFormat percFormat = new DecimalFormat("#0.0%"); + + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document doc = documentBuilder.newDocument(); + Element stats = doc.createElement("stats"); + stats.setAttribute("activePlayers", String.valueOf(activePlayers)); + stats.setAttribute("gamesCount", String.valueOf(gamesCount)); + for (GameHistoryStatistics.FormatStat formatStat : gameHistoryStatistics.getFormatStats()) { + Element formatStatElem = doc.createElement("formatStat"); + formatStatElem.setAttribute("format", formatStat.getFormat()); + formatStatElem.setAttribute("count", String.valueOf(formatStat.getCount())); + formatStatElem.setAttribute("perc", percFormat.format(formatStat.getPercentage())); + stats.appendChild(formatStatElem); + } + + doc.appendChild(stats); + return doc; + } catch (ParseException exp) { + sendError(Response.Status.BAD_REQUEST); + return null; + } + } + private void logUser(HttpServletRequest request, String login) { request.getSession().setAttribute("logged", login); }