diff --git a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/ServerStatsRequestHandler.java b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/ServerStatsRequestHandler.java index 53607e14e..782aa3d4e 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/ServerStatsRequestHandler.java +++ b/gemp-lotr/gemp-lotr-async/src/main/java/com/gempukku/lotro/async/handler/ServerStatsRequestHandler.java @@ -1,21 +1,17 @@ package com.gempukku.lotro.async.handler; +import com.alibaba.fastjson.JSON; import com.gempukku.lotro.async.HttpProcessingException; import com.gempukku.lotro.async.ResponseWriter; +import com.gempukku.lotro.common.JSONDefs; import com.gempukku.lotro.game.GameHistoryService; -import com.gempukku.lotro.game.GameHistoryStatistics; import com.gempukku.lotro.game.Player; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.QueryStringDecoder; import org.apache.log4j.Logger; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; import java.lang.reflect.Type; -import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @@ -56,32 +52,14 @@ public class ServerStatsRequestHandler extends LotroServerRequestHandler impleme } long duration = to.getTime() - from; - int activePlayers = _gameHistoryService.getActivePlayersCount(from, duration); - int gamesCount = _gameHistoryService.getGamesPlayedCount(from, duration); + var stats = new JSONDefs.PlayHistoryStats(); + stats.ActivePlayers = _gameHistoryService.getActivePlayersCount(from, duration); + stats.GamesCount = _gameHistoryService.getGamesPlayedCount(from, duration); + stats.StartDate = format.format(new Date(from)); + stats.EndDate = format.format(new Date(from + duration - 1)); + stats.Stats = _gameHistoryService.getGameHistoryStatistics(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)); - stats.setAttribute("start", format.format(new Date(from))); - stats.setAttribute("end", format.format(new Date(from + duration - 1))); - 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); - - responseWriter.writeXmlResponse(doc); + responseWriter.writeJsonResponse(JSON.toJSONString(stats)); } catch (ParseException exp) { logHttpError(_log, 400, request.uri(), exp); throw new HttpProcessingException(400); diff --git a/gemp-lotr/gemp-lotr-async/src/main/web/includes/info.html b/gemp-lotr/gemp-lotr-async/src/main/web/includes/info.html index 1d5f8da10..bcc531b39 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/web/includes/info.html +++ b/gemp-lotr/gemp-lotr-async/src/main/web/includes/info.html @@ -35,7 +35,36 @@ -
+
+
Stats for -
+
Active players:
+
All games count:
+ + + + + + + + + + + +
Competitive Games by Format
Format name# of games% of competitive% of total
+
+ + + + + + + + + + + +
Casual Games by Format
Format name# of games% of casual% of total
+
diff --git a/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/communication.js b/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/communication.js index ee76385ea..5134b27ce 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/communication.js +++ b/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/communication.js @@ -79,7 +79,7 @@ var GempLotrCommunication = Class.extend({ participantId:getUrlParam("participantId") }, success:this.deliveryCheck(callback), error:this.errorCheck(errorMap), - dataType:"xml" + dataType:"json" }); }, diff --git a/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/statsUi.js b/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/statsUi.js index ddd404f60..25f670150 100644 --- a/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/statsUi.js +++ b/gemp-lotr/gemp-lotr-async/src/main/web/js/gemp-022/statsUi.js @@ -35,38 +35,66 @@ var StatsUI = Class.extend({ }) }); }, + + getPercentage:function (num1, num2) { + return Number(num1 / num2).toLocaleString(undefined, {style: 'percent', minimumFractionDigits:2}); + }, - loadedStats:function (xml) { + loadedStats:function (json) { var that = this; - log(xml); - var root = xml.documentElement; - if (root.tagName == 'stats') { - $("#stats").html(""); + log(json); + + var getPercentage = (num1, num2) => Number(num1 / num2).toLocaleString(undefined, {style: 'percent', minimumFractionDigits:2}); - var stats = root; + $("#startDateSpan").html(json["StartDate"]); + $("#endDateSpan").html(json["EndDate"]); + $("#activePlayersStat").html(json["ActivePlayers"]); + $("#gamesCountStat").html(json["GamesCount"]); - var activePlayers = stats.getAttribute("activePlayers"); - var gamesCount = stats.getAttribute("gamesCount"); - var start = stats.getAttribute("start"); - var end = stats.getAttribute("end"); - - $("#stats").append("
Stats for " + start + " - " + end + "
"); - $("#stats").append("
Active players: " + activePlayers + "
"); - $("#stats").append("
All games count: " + gamesCount + "
"); - - var formatStats = stats.getElementsByTagName("formatStat"); - if (formatStats.length > 0) { - $("#stats").append("
Casual games per format
"); - - var table = $("
"); - table.append("Format name# of games% of casual"); - for (var i = 0; i < formatStats.length; i++) { - var formatStat = formatStats[i]; - table.append("" + formatStat.getAttribute("format") + "" + formatStat.getAttribute("count") + "" + formatStat.getAttribute("perc") + ""); + var formatStats = json["Stats"]; + if (formatStats.length > 0) { + + var casualStats = $("#casualStatsTable"); + var compStats = $("#competitiveStatsTable"); + $("#casualStatsTable > tbody").empty(); + $("#competitiveStatsTable > tbody").empty(); + + var casuals = 0; + var comps = 0; + var total = 0; + + json["Stats"].forEach(item => { + if(item.Casual) { + casuals += item.Count; } + else { + comps += item.Count; + } + total += item.Count; + }); + + json["Stats"].forEach(item => { + + var test = getPercentage(item.Count, total); + + if(item.Casual) { + casualStats.append("" + + "" + item.Format + "" + + "" + item.Count + "" + + "" + getPercentage(item.Count, casuals) + "" + + "" + getPercentage(item.Count, total) + "" + + ""); + } + else { + compStats.append("" + + "" + item.Format + "" + + "" + item.Count + "" + + "" + getPercentage(item.Count, comps) + "" + + "" + getPercentage(item.Count, total) + "" + + ""); + } + }); - $("#stats").append(table); - } } } }); \ No newline at end of file diff --git a/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/DBDefs.java b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/DBDefs.java index 12e990a86..b10e8d936 100644 --- a/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/DBDefs.java +++ b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/DBDefs.java @@ -65,4 +65,10 @@ public class DBDefs { public int id; public String name; } + + public static class FormatStats { + public String Format; + public int Count; + public boolean Casual; + } } diff --git a/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/JSONDefs.java b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/JSONDefs.java index 5d0d5e76b..4efa31e0c 100644 --- a/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/JSONDefs.java +++ b/gemp-lotr/gemp-lotr-common/src/main/java/com/gempukku/lotro/common/JSONDefs.java @@ -73,4 +73,12 @@ public class JSONDefs { public Map ErrataIDs; } + public static class PlayHistoryStats { + public List Stats; + public int ActivePlayers; + public int GamesCount; + public String StartDate; + public String EndDate; + } + } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbGameHistoryDAO.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbGameHistoryDAO.java index 5bda87e5d..c020b68a6 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbGameHistoryDAO.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/db/DbGameHistoryDAO.java @@ -174,6 +174,34 @@ public class DbGameHistoryDAO implements GameHistoryDAO { } } + public List GetAllGameFormatData(long from, long duration) { + try { + Sql2o db = new Sql2o(_dbAccess.getDataSource()); + + try (org.sql2o.Connection conn = db.open()) { + String sql = """ + SELECT + COUNT(*) AS Count + ,format_name AS Format + ,CASE WHEN tournament IS NULL OR tournament LIKE 'Casual %' THEN 1 ELSE 0 END AS Casual + FROM game_history + WHERE end_date >= :from + AND end_date < :to + GROUP BY format_name, CASE WHEN tournament IS NULL OR tournament LIKE 'Casual %' THEN 1 ELSE 0 END + """; + List result = conn.createQuery(sql) + .addParameter("from", from) + .addParameter("to", from + duration) + .executeAndFetch(DBDefs.FormatStats.class); + + return result; + } + } catch (Exception ex) { + throw new RuntimeException("Unable to retrieve format stats", ex); + } + } + + public List getCasualPlayerStatistics(Player player) { try { try (Connection connection = _dbAccess.getDataSource().getConnection()) { 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 236cced60..e7626e51a 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 @@ -22,6 +22,8 @@ public interface GameHistoryDAO { public Map getCasualGamesPlayedPerFormat(long from, long duration); + public List GetAllGameFormatData(long from, long duration); + public List getCasualPlayerStatistics(Player player); public List getCompetitivePlayerStatistics(Player player); 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 d271074d7..7868366af 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 @@ -56,10 +56,8 @@ public class GameHistoryService { return _gameHistoryDAO.getGamesPlayedCount(from, duration); } - public GameHistoryStatistics getGameHistoryStatistics(long from, long duration) { - GameHistoryStatistics stats = new GameHistoryStatistics(from, duration); - stats.init(_gameHistoryDAO); - return stats; + public List getGameHistoryStatistics(long from, long duration) { + return _gameHistoryDAO.GetAllGameFormatData(from, duration); } public List getCasualPlayerStatistics(Player player) {