SteamWar/BungeeCore
Archiviert
13
2

Add statistics endpoints
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-05-30 18:54:57 +02:00
Ursprung 14052c5d01
Commit e4551e9521
16 geänderte Dateien mit 474 neuen und 76 gelöschten Zeilen

Datei anzeigen

@ -31,7 +31,6 @@ public class AuthUtils {
public static Optional<Token> 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();
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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();
});
}
}
}

Datei anzeigen

@ -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)
```

Datei anzeigen

@ -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) -> {
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();
});
return jsonObject;
}
}

Datei anzeigen

@ -23,14 +23,13 @@ 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) -> {
public JsonObject result(Request request, Response response) {
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray = new JsonArray();
SteamwarUser.getServerTeam().forEach(steamwarUser -> {
@ -41,9 +40,7 @@ public class ServerTeamEndPoint implements EndPoint {
});
jsonObject.add("", jsonArray);
response.type("application/json");
response.status(200);
return jsonObject.toString();
});
return jsonObject;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Map<String, Integer>> 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;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Double> result = Statistics.getHoursContributedPerPlayer();
JsonObject jsonObject = new JsonObject();
result.forEach(jsonObject::addProperty);
response.status(200);
return jsonObject;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Double> result = Statistics.getHoursPlayedPerDay();
JsonObject jsonObject = new JsonObject();
result.forEach(jsonObject::addProperty);
response.status(200);
return jsonObject;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Map<String, Integer>> 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;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Integer> result = Statistics.getUniqueJoinsPerDay();
JsonObject jsonObject = new JsonObject();
result.forEach(jsonObject::addProperty);
response.status(200);
return jsonObject;
}
}

Datei anzeigen

@ -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) -> {
public JsonObject result(Request request, Response response) {
Optional<Token> optionalToken = AuthUtils.isAuthorized(request, response);
if (!optionalToken.isPresent()) {
return "{}";
return null;
}
Token token = optionalToken.get();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("uuid", token.getUuid().toString());
jsonObject.addProperty("username", SteamwarUser.get(token.getUuid()).getUserName());
response.type("application/json");
response.status(200);
return jsonObject.toString();
});
return jsonObject;
}
}

Datei anzeigen

@ -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) -> {
public JsonObject result(Request request, Response response) {
Optional<Token> optionalToken = AuthUtils.isAuthorized(request, response);
if (!optionalToken.isPresent()) {
return "{}";
return null;
}
Token token = optionalToken.get();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("username", SteamwarUser.get(token.getUuid()).getUserName());
jsonObject.addProperty("uuid", token.getUuid().toString());
response.type("application/json");
response.status(200);
return jsonObject.toString();
});
return jsonObject;
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Map<String, Integer>> getFightsPerDayPerGameMode() {
return fightsPerDayPerGameMode.select(rs -> {
Map<String, Map<String, Integer>> 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<String, Integer> getJoinsPerDay() {
return joinsPerDay.select(rs -> {
Map<String, Integer> 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<String, Integer> getUniqueJoinsPerDay() {
return uniqueJoinsPerDay.select(rs -> {
Map<String, Integer> 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<String, Double> getHoursPlayedPerDay() {
return hoursPlayedPerDay.select(rs -> {
Map<String, Double> 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<String, Double> getHoursContributedPerPlayer() {
return hoursContributedPerPlayer.select(rs -> {
Map<String, Double> 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<String, Map<String, Integer>> getAcceptedSchematicsPerDayPerGameMode() {
return acceptedSchematicsPerDayPerGameMode.select(rs -> {
Map<String, Map<String, Integer>> 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;
});
}
}