From d470e951f865534844e33f21b8d0789ac95838ab Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 9 Mar 2022 14:44:56 +0100 Subject: [PATCH] Add Season Update Elo for later rework in FightSystem and BungeeCore --- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 6 +++- .../src/de/steamwar/sql/Provider.java | 21 ++++++++++- .../src/de/steamwar/sql/SQLProvider.java | 17 ++++++--- .../src/de/steamwar/sql/Season.java | 36 +++++++++++++++++++ .../de/steamwar/sql/StandaloneProvider.java | 7 +++- 5 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Season.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index 8eeeab7..07e8c47 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -23,7 +23,11 @@ public class Elo { private Elo(){} public static int getElo(int userId, String gameMode){ - return Provider.impl.getElo(userId, gameMode); + return Provider.impl.getCurrentElo(userId, gameMode); + } + + public static int getElo(int season, int userId, String gameMode){ + return Provider.impl.getElo(season, userId, gameMode); } public static void setElo(int userId, String gameMode, int elo){ diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java index 56737f5..af1209a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Provider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -39,7 +39,26 @@ public interface Provider { List getLastDeclinedOfNode(int node); List getLastDelined(int schemOwner); - int getElo(int userId, String gameMode); + int getCurrentSeason(); // returns -1 is in standalone mode + default String convertSeasonNumberToString(int season) { + if (season == -1) return ""; + int yearSeason = season % 3; + int year = (season - yearSeason) / 3; + return String.format("%d-%d", year, yearSeason); + } + default int convertSeasonStringToNumber(String season) { + if (season.isEmpty()) return -1; + String[] split = season.split("-"); + try { + return Integer.parseInt(split[0]) * 3 + Integer.parseInt(split[1]); + } catch (NumberFormatException e) { + return -1; + } + } + default int getCurrentElo(int userId, String gameMode) { + return getElo(getCurrentSeason(), userId, gameMode); + } + int getElo(int season, int userId, String gameMode); void setElo(int userId, String gameMode, int elo); Event getEvent(int eventID); diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index 916f460..acb3d4a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -78,20 +78,27 @@ public class SQLProvider implements Provider { return new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason")); } - private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); @Override - public int getElo(int userId, String gameMode) { + public int getCurrentSeason() { + Calendar calendar = Calendar.getInstance(); + int yearIndex = calendar.get(Calendar.MONTH) / 3; + return (calendar.get(Calendar.YEAR) * 3 + yearIndex); + } + + private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ? AND Season = ?"); + @Override + public int getElo(int season, int userId, String gameMode) { return getElo.select(rs -> { if(rs.next()) return rs.getInt("Elo"); return 1000; - }, userId, gameMode); + }, userId, gameMode, season); } - private static final Statement setElo = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); + private static final Statement setElo = new Statement("UPDATE Elo SET Elo = ? WHERE Season = ? AND UserID = ? AND GameMode = ?"); @Override public void setElo(int userId, String gameMode, int elo) { - setElo.update(userId, gameMode, elo); + setElo.update(elo, getCurrentSeason(), userId, gameMode); } private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?"); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Season.java b/SpigotCore_Main/src/de/steamwar/sql/Season.java new file mode 100644 index 0000000..9075ab4 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Season.java @@ -0,0 +1,36 @@ +/* + * 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.sql; + +public class Season { + private Season() {} + + public static int getSeason() { + return Provider.impl.getCurrentSeason(); + } + + public static String convertSeasonToString(int season){ + return Provider.impl.convertSeasonNumberToString(season); + } + + public static int convertSeasonToNumber(String season){ + return Provider.impl.convertSeasonStringToNumber(season); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 9c422f3..0499d99 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -65,7 +65,12 @@ public class StandaloneProvider implements Provider { } @Override - public int getElo(int userId, String gameMode) { + public int getCurrentSeason() { + return -1; + } + + @Override + public int getElo(int season, int userId, String gameMode) { return 1000; }