diff --git a/src/de/steamwar/bungeecore/api/AuthUtils.java b/src/de/steamwar/bungeecore/api/AuthUtils.java index 2a055230..c500334f 100644 --- a/src/de/steamwar/bungeecore/api/AuthUtils.java +++ b/src/de/steamwar/bungeecore/api/AuthUtils.java @@ -31,7 +31,6 @@ public class AuthUtils { public static Optional isAuthorized(Request request, Response response) { String authorization = request.headers("Authorization"); if (authorization == null) { - response.type("application/json"); response.status(401); return Optional.empty(); } @@ -39,7 +38,6 @@ public class AuthUtils { try { return Optional.of(Token.decrypt(authorization)); } catch (SecurityException e) { - response.type("application/json"); response.status(401); return Optional.empty(); } diff --git a/src/de/steamwar/bungeecore/api/EndPoint.java b/src/de/steamwar/bungeecore/api/EndPoint.java index 02304f82..14665a19 100644 --- a/src/de/steamwar/bungeecore/api/EndPoint.java +++ b/src/de/steamwar/bungeecore/api/EndPoint.java @@ -19,6 +19,14 @@ package de.steamwar.bungeecore.api; +import com.google.gson.JsonObject; +import spark.Request; +import spark.Response; + public interface EndPoint { - void ignite(); + + default String path() { + return this.getClass().getTypeName().substring(27).replace("EndPoint", "").replaceAll("([A-Z])", "_\1").toLowerCase(); + } + JsonObject result(Request request, Response response); } diff --git a/src/de/steamwar/bungeecore/api/WebAPI.java b/src/de/steamwar/bungeecore/api/WebAPI.java index 6d9dd430..1f06ebac 100644 --- a/src/de/steamwar/bungeecore/api/WebAPI.java +++ b/src/de/steamwar/bungeecore/api/WebAPI.java @@ -19,21 +19,46 @@ package de.steamwar.bungeecore.api; -import de.steamwar.bungeecore.api.v1.ServerStatusEndPoint; -import de.steamwar.bungeecore.api.v1.ServerTeamEndPoint; -import de.steamwar.bungeecore.api.v1.user.GetUUIDEndPoint; -import de.steamwar.bungeecore.api.v1.user.GetUserNameEndPoint; +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.v1.ServerstatusEndPoint; +import de.steamwar.bungeecore.api.v1.ServerteamEndPoint; +import de.steamwar.bungeecore.api.v1.statistics.*; +import de.steamwar.bungeecore.api.v1.user.GetUuidEndPoint; +import de.steamwar.bungeecore.api.v1.user.GetUsernameEndPoint; +import de.steamwar.bungeecore.api.v1.website.LoginEndPoint; +import de.steamwar.bungeecore.api.v1.website.LogoutEndPoint; import static spark.Spark.port; +import static spark.Spark.post; public class WebAPI { + private static EndPoint[] endPoints = new EndPoint[] { + new FightsEndPoint(), + new HoursContributedEndPoint(), + new HoursPlayedEndPoint(), + new SchematicsAcceptedEndPoint(), + new UniqueJoinsEndPoint(), + new GetUsernameEndPoint(), + new GetUuidEndPoint(), + new LoginEndPoint(), + new LogoutEndPoint(), + new ServerstatusEndPoint(), + new ServerteamEndPoint(), + }; + public static void start() { port(1024); - new ServerStatusEndPoint().ignite(); - new ServerTeamEndPoint().ignite(); - new GetUserNameEndPoint().ignite(); - new GetUUIDEndPoint().ignite(); + for (EndPoint endPoint : endPoints) { + post(endPoint.path(), (request, response) -> { + JsonObject result = endPoint.result(request, response); + if (result == null) { + result = new JsonObject(); + } + response.type("application/json"); + return result.toString(); + }); + } } } diff --git a/src/de/steamwar/bungeecore/api/doc.txt b/src/de/steamwar/bungeecore/api/doc.txt new file mode 100644 index 00000000..ce125d01 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/doc.txt @@ -0,0 +1,38 @@ +``` +SteamWar Endpoints: + +Serverteam-Endpoint: +- UUID, Rang, InGame-Name + +User-Endpoints: +- UUID -> skin.png +- UUID -> head.png +- Token -> UUID +- Token -> head.png +- Token -> skin.png +- Token -> InGame-Name + +Website-User-Endpoints: +- (UserName, hashedPW) -> Token +- (Token) -> Void // Abmeldung + +Serverstatus-Endpoint: +- PlayerSize, MaxPlayerSize + Response -> Status + +Ranked-Endpoint: +- (Modus, Season?) -> Ranglist +- (UUID) -> Ränge + Ranglist-Endpoint: + - Modi // Alle Modi, welche eine Ranglist haben + Schem-Endpoint: + - (Modus, Season?) -> Public-Ranglist + - (Modus, Season?, Token) -> Privat-Ranglist + +Statistiken-Endpoints: +- Modi, AnzahlFights // Anzahl Fights der letzten 7 Tage +- AnzahlJoins // Anzahl Joins der letzten 7 Tage (total) +- UniqueJoins // Anzahl uniquer PlayerJoins der letzten 7 Tage (total) +- HoursPlayed // Anzahl gespielter Stunden für alle Spieler der letzten 7 Tage (total) + +``` \ No newline at end of file diff --git a/src/de/steamwar/bungeecore/api/v1/ServerStatusEndPoint.java b/src/de/steamwar/bungeecore/api/v1/ServerstatusEndPoint.java similarity index 62% rename from src/de/steamwar/bungeecore/api/v1/ServerStatusEndPoint.java rename to src/de/steamwar/bungeecore/api/v1/ServerstatusEndPoint.java index b21d70e4..ee7d7e4b 100644 --- a/src/de/steamwar/bungeecore/api/v1/ServerStatusEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/ServerstatusEndPoint.java @@ -22,21 +22,18 @@ package de.steamwar.bungeecore.api.v1; import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.EndPoint; import net.md_5.bungee.BungeeCord; +import spark.Request; +import spark.Response; -import static spark.Spark.post; - -public class ServerStatusEndPoint implements EndPoint { +public class ServerstatusEndPoint implements EndPoint { @Override - public void ignite() { - post("/v1/serverstatus", (request, response) -> { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("players", BungeeCord.getInstance().getPlayers().size()); - jsonObject.addProperty("maxPlayers", BungeeCord.getInstance().getConfig().getPlayerLimit()); + public JsonObject result(Request request, Response response) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("players", BungeeCord.getInstance().getPlayers().size()); + jsonObject.addProperty("maxPlayers", BungeeCord.getInstance().getConfig().getPlayerLimit()); - response.type("application/json"); - response.status(200); - return jsonObject.toString(); - }); + response.status(200); + return jsonObject; } } diff --git a/src/de/steamwar/bungeecore/api/v1/ServerTeamEndPoint.java b/src/de/steamwar/bungeecore/api/v1/ServerteamEndPoint.java similarity index 57% rename from src/de/steamwar/bungeecore/api/v1/ServerTeamEndPoint.java rename to src/de/steamwar/bungeecore/api/v1/ServerteamEndPoint.java index a0599e8f..9c371f03 100644 --- a/src/de/steamwar/bungeecore/api/v1/ServerTeamEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/ServerteamEndPoint.java @@ -23,27 +23,24 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.EndPoint; import de.steamwar.bungeecore.sql.SteamwarUser; +import spark.Request; +import spark.Response; -import static spark.Spark.post; - -public class ServerTeamEndPoint implements EndPoint { +public class ServerteamEndPoint implements EndPoint { @Override - public void ignite() { - post("/v1/serverteam", (request, response) -> { - JsonObject jsonObject = new JsonObject(); - JsonArray jsonArray = new JsonArray(); - SteamwarUser.getServerTeam().forEach(steamwarUser -> { - JsonObject user = new JsonObject(); - user.addProperty("name", steamwarUser.getUserName()); - user.addProperty("rank", steamwarUser.getUserGroup().name()); - jsonArray.add(user); - }); - jsonObject.add("", jsonArray); - - response.type("application/json"); - response.status(200); - return jsonObject.toString(); + public JsonObject result(Request request, Response response) { + JsonObject jsonObject = new JsonObject(); + JsonArray jsonArray = new JsonArray(); + SteamwarUser.getServerTeam().forEach(steamwarUser -> { + JsonObject user = new JsonObject(); + user.addProperty("name", steamwarUser.getUserName()); + user.addProperty("rank", steamwarUser.getUserGroup().name()); + jsonArray.add(user); }); + jsonObject.add("", jsonArray); + + response.status(200); + return jsonObject; } } diff --git a/src/de/steamwar/bungeecore/api/v1/statistics/FightsEndPoint.java b/src/de/steamwar/bungeecore/api/v1/statistics/FightsEndPoint.java new file mode 100644 index 00000000..72c29a75 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/statistics/FightsEndPoint.java @@ -0,0 +1,45 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.api.v1.statistics; + +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.EndPoint; +import de.steamwar.bungeecore.sql.Statistics; +import spark.Request; +import spark.Response; + +import java.util.Map; + +public class FightsEndPoint implements EndPoint { + + @Override + public JsonObject result(Request request, Response response) { + Map> result = Statistics.getFightsPerDayPerGameMode(); + JsonObject jsonObject = new JsonObject(); + result.forEach((s, stringIntegerMap) -> { + JsonObject singleResult = new JsonObject(); + stringIntegerMap.forEach(singleResult::addProperty); + jsonObject.add(s, singleResult); + }); + + response.status(200); + return jsonObject; + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/statistics/HoursContributedEndPoint.java b/src/de/steamwar/bungeecore/api/v1/statistics/HoursContributedEndPoint.java new file mode 100644 index 00000000..55065451 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/statistics/HoursContributedEndPoint.java @@ -0,0 +1,41 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.api.v1.statistics; + +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.EndPoint; +import de.steamwar.bungeecore.sql.Statistics; +import spark.Request; +import spark.Response; + +import java.util.Map; + +public class HoursContributedEndPoint implements EndPoint { + + @Override + public JsonObject result(Request request, Response response) { + Map result = Statistics.getHoursContributedPerPlayer(); + JsonObject jsonObject = new JsonObject(); + result.forEach(jsonObject::addProperty); + + response.status(200); + return jsonObject; + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/statistics/HoursPlayedEndPoint.java b/src/de/steamwar/bungeecore/api/v1/statistics/HoursPlayedEndPoint.java new file mode 100644 index 00000000..6f8a3931 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/statistics/HoursPlayedEndPoint.java @@ -0,0 +1,41 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.api.v1.statistics; + +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.EndPoint; +import de.steamwar.bungeecore.sql.Statistics; +import spark.Request; +import spark.Response; + +import java.util.Map; + +public class HoursPlayedEndPoint implements EndPoint { + + @Override + public JsonObject result(Request request, Response response) { + Map result = Statistics.getHoursPlayedPerDay(); + JsonObject jsonObject = new JsonObject(); + result.forEach(jsonObject::addProperty); + + response.status(200); + return jsonObject; + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/statistics/SchematicsAcceptedEndPoint.java b/src/de/steamwar/bungeecore/api/v1/statistics/SchematicsAcceptedEndPoint.java new file mode 100644 index 00000000..9114acb9 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/statistics/SchematicsAcceptedEndPoint.java @@ -0,0 +1,45 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.api.v1.statistics; + +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.EndPoint; +import de.steamwar.bungeecore.sql.Statistics; +import spark.Request; +import spark.Response; + +import java.util.Map; + +public class SchematicsAcceptedEndPoint implements EndPoint { + + @Override + public JsonObject result(Request request, Response response) { + Map> result = Statistics.getAcceptedSchematicsPerDayPerGameMode(); + JsonObject jsonObject = new JsonObject(); + result.forEach((s, stringIntegerMap) -> { + JsonObject singleResult = new JsonObject(); + stringIntegerMap.forEach(singleResult::addProperty); + jsonObject.add(s, singleResult); + }); + + response.status(200); + return jsonObject; + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/statistics/UniqueJoinsEndPoint.java b/src/de/steamwar/bungeecore/api/v1/statistics/UniqueJoinsEndPoint.java new file mode 100644 index 00000000..c79d221a --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/statistics/UniqueJoinsEndPoint.java @@ -0,0 +1,41 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.api.v1.statistics; + +import com.google.gson.JsonObject; +import de.steamwar.bungeecore.api.EndPoint; +import de.steamwar.bungeecore.sql.Statistics; +import spark.Request; +import spark.Response; + +import java.util.Map; + +public class UniqueJoinsEndPoint implements EndPoint { + + @Override + public JsonObject result(Request request, Response response) { + Map result = Statistics.getUniqueJoinsPerDay(); + JsonObject jsonObject = new JsonObject(); + result.forEach(jsonObject::addProperty); + + response.status(200); + return jsonObject; + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java b/src/de/steamwar/bungeecore/api/v1/user/GetUsernameEndPoint.java similarity index 63% rename from src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java rename to src/de/steamwar/bungeecore/api/v1/user/GetUsernameEndPoint.java index bf1b0a72..4fbcef4f 100644 --- a/src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/user/GetUsernameEndPoint.java @@ -23,28 +23,28 @@ import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.AuthUtils; import de.steamwar.bungeecore.api.EndPoint; import de.steamwar.bungeecore.api.Token; +import de.steamwar.bungeecore.sql.SteamwarUser; +import spark.Request; +import spark.Response; import java.util.Optional; import static spark.Spark.post; -public class GetUUIDEndPoint implements EndPoint { +public class GetUsernameEndPoint implements EndPoint { @Override - public void ignite() { - post("/v1/user/token_to_uuid", (request, response) -> { - Optional optionalToken = AuthUtils.isAuthorized(request, response); - if (!optionalToken.isPresent()) { - return "{}"; - } - Token token = optionalToken.get(); + public JsonObject result(Request request, Response response) { + Optional optionalToken = AuthUtils.isAuthorized(request, response); + if (!optionalToken.isPresent()) { + return null; + } + Token token = optionalToken.get(); - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("uuid", token.getUuid().toString()); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("username", SteamwarUser.get(token.getUuid()).getUserName()); - response.type("application/json"); - response.status(200); - return jsonObject.toString(); - }); + response.status(200); + return jsonObject; } } diff --git a/src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java b/src/de/steamwar/bungeecore/api/v1/user/GetUuidEndPoint.java similarity index 58% rename from src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java rename to src/de/steamwar/bungeecore/api/v1/user/GetUuidEndPoint.java index 9b5a848b..c87405a2 100644 --- a/src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/user/GetUuidEndPoint.java @@ -23,29 +23,25 @@ import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.AuthUtils; import de.steamwar.bungeecore.api.EndPoint; import de.steamwar.bungeecore.api.Token; -import de.steamwar.bungeecore.sql.SteamwarUser; +import spark.Request; +import spark.Response; import java.util.Optional; -import static spark.Spark.post; - -public class GetUserNameEndPoint implements EndPoint { +public class GetUuidEndPoint implements EndPoint { @Override - public void ignite() { - post("/v1/user/token_to_username", (request, response) -> { - Optional optionalToken = AuthUtils.isAuthorized(request, response); - if (!optionalToken.isPresent()) { - return "{}"; - } - Token token = optionalToken.get(); + public JsonObject result(Request request, Response response) { + Optional optionalToken = AuthUtils.isAuthorized(request, response); + if (!optionalToken.isPresent()) { + return null; + } + Token token = optionalToken.get(); - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("username", SteamwarUser.get(token.getUuid()).getUserName()); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("uuid", token.getUuid().toString()); - response.type("application/json"); - response.status(200); - return jsonObject.toString(); - }); + response.status(200); + return jsonObject; } } diff --git a/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java b/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java index c1103b4c..e4ceec29 100644 --- a/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java @@ -19,12 +19,15 @@ package de.steamwar.bungeecore.api.v1.website; +import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.EndPoint; +import spark.Request; +import spark.Response; public class LoginEndPoint implements EndPoint { @Override - public void ignite() { - + public JsonObject result(Request request, Response response) { + return null; } } diff --git a/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java b/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java index c1db3bbe..bd30ac21 100644 --- a/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java +++ b/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java @@ -19,12 +19,15 @@ package de.steamwar.bungeecore.api.v1.website; +import com.google.gson.JsonObject; import de.steamwar.bungeecore.api.EndPoint; +import spark.Request; +import spark.Response; public class LogoutEndPoint implements EndPoint { @Override - public void ignite() { - + public JsonObject result(Request request, Response response) { + return null; } } diff --git a/src/de/steamwar/bungeecore/sql/Statistics.java b/src/de/steamwar/bungeecore/sql/Statistics.java new file mode 100644 index 00000000..fdeb0368 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/Statistics.java @@ -0,0 +1,120 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * 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 . + */ + +package de.steamwar.bungeecore.sql; + +import java.util.HashMap; +import java.util.Map; + +public class Statistics { + + private static final Statement fightsPerDayPerGameMode = new Statement("SELECT * FROM Punishments WHERE UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1"); + + public static Map> getFightsPerDayPerGameMode() { + return fightsPerDayPerGameMode.select(rs -> { + Map> counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + String gameMode = rs.getString("GameMode"); + int fightCount = rs.getInt("Fights"); + + counts.computeIfAbsent(date, s -> new HashMap<>()).put(gameMode, fightCount); + } + return counts; + }); + } + + /* + private static final Statement joinsPerDay = new Statement("SELECT * FROM Punishments WHERE UserId = ? AND Type = ? ORDER BY PunishmentId DESC LIMIT 1"); + + public static Map getJoinsPerDay() { + return joinsPerDay.select(rs -> { + Map counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + int joins = rs.getInt("Joins"); + + counts.put(date, joins); + } + return counts; + }); + } + */ + + private static final Statement uniqueJoinsPerDay = new Statement("SELECT DATE(StartTime) as Date, COUNT(distinct UserID) as UniqueJoins FROM Session WHERE DATE(StartTime) > DATE(CURRENT_TIME) - 7 GROUP BY DATE(StartTime)"); + + public static Map getUniqueJoinsPerDay() { + return uniqueJoinsPerDay.select(rs -> { + Map counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + int joins = rs.getInt("Joins"); + + counts.put(date, joins); + } + return counts; + }); + } + + private static final Statement hoursPlayedPerDay = new Statement("SELECT DATE(StartTime) as Date, SUM(UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime)) / 60.0 / 60.0 as Hours FROM Session WHERE DATE(StartTime) > DATE(CURRENT_TIME) - 7 GROUP BY DATE(StartTime)"); + + public static Map getHoursPlayedPerDay() { + return hoursPlayedPerDay.select(rs -> { + Map counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + double hours = rs.getInt("Hours"); + + counts.put(date, hours); + } + return counts; + }); + } + + private static final Statement hoursContributedPerPlayer = new Statement("SELECT UserName, SUM(UNIX_TIMESTAMP(EndTime) - UNIX_TIMESTAMP(StartTime)) / 60.0 / 60.0 as Hours FROM Session INNER JOIN UserData UD on Session.UserID = UD.id WHERE DATE(StartTime) > DATE(CURRENT_TIME) - 7 GROUP BY UserID ORDER BY HOURS DESC LIMIT 10"); + + public static Map getHoursContributedPerPlayer() { + return hoursContributedPerPlayer.select(rs -> { + Map counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + double hours = rs.getInt("Hours"); + + counts.put(date, hours); + } + return counts; + }); + } + + private static final Statement acceptedSchematicsPerDayPerGameMode = new Statement("SELECT DATE(StartTime) as Date, NodeType, COUNT(*) as Count FROM CheckedSchematic INNER JOIN SchematicNode SN on CheckedSchematic.NodeId = SN.NodeId WHERE DeclineReason = 'freigegeben' GROUP BY DATE(StartTime), NodeType"); + + public static Map> getAcceptedSchematicsPerDayPerGameMode() { + return acceptedSchematicsPerDayPerGameMode.select(rs -> { + Map> counts = new HashMap<>(); + while (rs.next()) { + String date = rs.getString("Date"); + String gameMode = rs.getString("NodeType"); + int acceptedCount = rs.getInt("Count"); + + counts.computeIfAbsent(date, s -> new HashMap<>()).put(gameMode, acceptedCount); + } + return counts; + }); + } +}