From 877aaea1dff3d682a6f7de240c121716780676f3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 1 Feb 2022 13:29:07 +0100 Subject: [PATCH] WIP standalone SpigotCore --- .../src/de/steamwar/core/Core.java | 15 +- .../src/de/steamwar/sql/BauweltMember.java | 53 +++--- .../src/de/steamwar/sql/CheckedSchematic.java | 51 ++---- SpigotCore_Main/src/de/steamwar/sql/Elo.java | 11 +- .../src/de/steamwar/sql/Event.java | 29 ++-- .../src/de/steamwar/sql/EventFight.java | 38 ++--- .../src/de/steamwar/sql/Fight.java | 21 +-- .../src/de/steamwar/sql/FightPlayer.java | 4 +- .../src/de/steamwar/sql/NodeDownload.java | 5 +- .../src/de/steamwar/sql/Provider.java | 51 ++++++ .../src/de/steamwar/sql/SQLProvider.java | 157 ++++++++++++++++++ .../de/steamwar/sql/StandaloneProvider.java | 91 ++++++++++ .../src/de/steamwar/sql/Statement.java | 54 +++--- 13 files changed, 411 insertions(+), 169 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Provider.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 8a47d30..7caba9d 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -31,6 +31,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.logging.Level; @@ -66,6 +67,8 @@ public class Core extends JavaPlugin{ } private ErrorHandler errorHandler; + private File sqlConfig; + private boolean standalone; @Override public void onLoad() { @@ -74,6 +77,8 @@ public class Core extends JavaPlugin{ @Override public void onEnable() { + sqlConfig = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); + standalone = !sqlConfig.exists(); errorHandler = new ErrorHandler(); Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); @@ -84,7 +89,7 @@ public class Core extends JavaPlugin{ AuthlibInjector.inject(); TinyProtocol.init(); try { - Bukkit.getLogger().log(Level.INFO, "Running on: " + new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream())).readLine()); + getLogger().log(Level.INFO, "Running on: " + new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream())).readLine()); } catch (IOException e) { throw new SecurityException("Could not load Hostname", e); } @@ -97,6 +102,14 @@ public class Core extends JavaPlugin{ Statement.close(); } + public static File sqlConfig() { + return instance.sqlConfig; + } + + public static boolean standalone() { + return instance.standalone; + } + public static Core getInstance() { return instance; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java index 305aac5..564b916 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/BauweltMember.java @@ -23,28 +23,11 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; -public class BauweltMember{ - - private static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); - private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?"); - - private final int bauweltID; - private final int memberID; - private final boolean worldEdit; - private final boolean world; - - private static final List members = new ArrayList<>(); +public class BauweltMember { + private static final List memberCache = new ArrayList<>(); public static void clear() { - members.clear(); - } - - private BauweltMember(int ownerID, int memberID, boolean worldEdit, boolean world) { - bauweltID = ownerID; - this.memberID = memberID; - this.worldEdit = worldEdit; - this.world = world; - members.add(this); + memberCache.clear(); } public static BauweltMember getBauMember(UUID ownerID, UUID memberID){ @@ -52,14 +35,10 @@ public class BauweltMember{ } public static BauweltMember getBauMember(int ownerID, int memberID){ - for(BauweltMember member : members) - if(member.memberID == memberID) + for(BauweltMember member : memberCache) + if(member.getMemberID() == memberID) return member; - return getMember.select(rs -> { - if(!rs.next()) - return null; - return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World")); - }, ownerID, memberID); + return Provider.impl.getBauMember(ownerID, memberID); } public static List getMembers(UUID bauweltID){ @@ -67,12 +46,20 @@ public class BauweltMember{ } public static List getMembers(int bauweltID){ - return getMembers.select(rs -> { - List members = new ArrayList<>(); - while(rs.next()) - members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"))); - return members; - }, bauweltID); + return Provider.impl.getMembers(bauweltID); + } + + private final int bauweltID; + private final int memberID; + private final boolean worldEdit; + private final boolean world; + + public BauweltMember(int bauweltID, int memberID, boolean worldEdit, boolean world) { + this.bauweltID = bauweltID; + this.memberID = memberID; + this.worldEdit = worldEdit; + this.world = world; + memberCache.add(this); } public int getBauweltID() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java index e130c70..3c19e4e 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/CheckedSchematic.java @@ -20,45 +20,17 @@ package de.steamwar.sql; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.List; import java.util.UUID; public class CheckedSchematic { - private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); - private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); - - private final Integer node; - private final int validator; - private final Timestamp startTime; - private final Timestamp endTime; - private final String declineReason; - - private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ - this.node = node; - this.validator = validator; - this.startTime = startTime; - this.endTime = endTime; - this.declineReason = declineReason; - } - public static List getLastDeclinedOfNode(SchematicNode node){ return getLastDeclinedOfNode(node.getId()); } public static List getLastDeclinedOfNode(int node){ - return nodeHistory.select(rs -> { - List lastDeclined = new ArrayList<>(); - while(rs.next()){ - int validator = rs.getInt("Validator"); - Timestamp startTime = rs.getTimestamp("StartTime"); - Timestamp endTime = rs.getTimestamp("EndTime"); - String declineReason = rs.getString("DeclineReason"); - lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason)); - } - return lastDeclined; - }, node); + return Provider.impl.getLastDeclinedOfNode(node); } public static List getLastDeclined(UUID uuid){ @@ -66,12 +38,21 @@ public class CheckedSchematic { } public static List getLastDelined(int schemOwner){ - return checkHistory.select(rs -> { - List history = new ArrayList<>(); - while(rs.next()) - history.add(new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"))); - return history; - }, schemOwner); + return Provider.impl.getLastDelined(schemOwner); + } + + private final Integer node; + private final int validator; + private final Timestamp startTime; + private final Timestamp endTime; + private final String declineReason; + + public CheckedSchematic(Integer node, int validator, Timestamp startTime, Timestamp endTime, String declineReason) { + this.node = node; + this.validator = validator; + this.startTime = startTime; + this.endTime = endTime; + this.declineReason = declineReason; } public int getValidator() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Elo.java b/SpigotCore_Main/src/de/steamwar/sql/Elo.java index 6fa6504..8eeeab7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Elo.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Elo.java @@ -22,18 +22,11 @@ package de.steamwar.sql; public class Elo { private Elo(){} - private static final Statement get = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?"); - private static final Statement set = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); - public static int getElo(int userId, String gameMode){ - return get.select(rs -> { - if(rs.next()) - return rs.getInt("Elo"); - return 1000; - }, userId, gameMode); + return Provider.impl.getElo(userId, gameMode); } public static void setElo(int userId, String gameMode, int elo){ - set.update(userId, gameMode, elo); + Provider.impl.setElo(userId, gameMode, elo); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Event.java b/SpigotCore_Main/src/de/steamwar/sql/Event.java index 12ab5dd..a08ad8f 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Event.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Event.java @@ -19,13 +19,13 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Timestamp; public class Event { - private static final Statement get = new Statement("SELECT * FROM Event WHERE EventID = ?"); + public static Event get(int eventID){ + return Provider.impl.getEvent(eventID); + } private final int eventID; private final String eventName; @@ -35,21 +35,14 @@ public class Event { private final boolean publicSchemsOnly; private final boolean spectateSystem; - private Event(ResultSet rs) throws SQLException{ - this.eventID = rs.getInt("EventID"); - this.eventName = rs.getString("EventName"); - this.start = rs.getTimestamp("Start"); - this.end = rs.getTimestamp("End"); - this.maximumTeamMembers = rs.getInt("MaximumTeamMembers"); - this.publicSchemsOnly = rs.getBoolean("PublicSchemsOnly"); - this.spectateSystem = rs.getBoolean("SpectateSystem"); - } - - public static Event get(int eventID){ - return get.select(rs -> { - rs.next(); - return new Event(rs); - }, eventID); + public Event(int eventID, String eventName, Timestamp start, Timestamp end, int maximumTeamMembers, boolean publicSchemsOnly, boolean spectateSystem) { + this.eventID = eventID; + this.eventName = eventName; + this.start = start; + this.end = end; + this.maximumTeamMembers = maximumTeamMembers; + this.publicSchemsOnly = publicSchemsOnly; + this.spectateSystem = spectateSystem; } public int getEventID() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 684cd7a..056e38a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -19,14 +19,13 @@ package de.steamwar.sql; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.util.function.BiConsumer; public class EventFight { - private static final Statement get = new Statement("SELECT * FROM EventFight WHERE FightID = ?"); - private static final Statement setResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?"); - private static final Statement setFight = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?"); + public static EventFight get(int fightID) { + return Provider.impl.getEventFight(fightID); + } private final int eventID; private final int fightID; @@ -34,30 +33,27 @@ public class EventFight { private final int teamRed; private final int kampfleiter; private final int ergebnis; + private final BiConsumer setErgebnis; + private final BiConsumer setFight; - private EventFight(ResultSet rs) throws SQLException{ - this.eventID = rs.getInt("EventID"); - this.fightID = rs.getInt("FightID"); - this.teamBlue = rs.getInt("TeamBlue"); - this.teamRed = rs.getInt("TeamRed"); - this.kampfleiter = rs.getInt("Kampfleiter"); - this.ergebnis = rs.getInt("Ergebnis"); - } - - public static EventFight get(int fightID){ - return get.select(rs -> { - rs.next(); - return new EventFight(rs); - }, fightID); + public EventFight(int eventID, int fightID, int teamBlue, int teamRed, int kampfleiter, int ergebnis, BiConsumer setErgebnis, BiConsumer setFight) { + this.eventID = eventID; + this.fightID = fightID; + this.teamBlue = teamBlue; + this.teamRed = teamRed; + this.kampfleiter = kampfleiter; + this.ergebnis = ergebnis; + this.setErgebnis = setErgebnis; + this.setFight = setFight; } public void setErgebnis(int winner){ - setResult.update(winner, fightID); + setErgebnis.accept(this, winner); } public void setFight(int fight){ //Fight.FightID, not EventFight.FightID - setFight.update(fight, fightID); + setFight.accept(this, fight); } public int getTeamBlue() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index eb5cd30..4f46ca6 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -27,32 +27,19 @@ import java.util.function.Consumer; public class Fight { private Fight(){} - private static final Statement create = new Statement("INSERT INTO Fight (GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition, ReplayLock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - private static final Statement lastId = new Statement("SELECT LAST_INSERT_ID() AS FightID"); - private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?"); - private static final Statement setReplay = new Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?"); - public static int create(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ - return create(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, Timestamp.from(Instant.now())); + return create(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, Timestamp.from(Instant.now())); } public static int create(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock){ - create.update(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock); - return lastId.select(rs -> { - rs.next(); - return rs.getInt("FightID"); - }); + return Provider.impl.createFight(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock); } public static void getReplay(int fightID, Consumer reader) { - getReplay.select(rs -> { - rs.next(); - reader.accept(rs.getBinaryStream("Replay")); - return null; - }, fightID); + Provider.impl.getReplay(fightID, reader); } public static void setReplay(int fightID, InputStream data) { - setReplay.update(data, fightID); + Provider.impl.setReplay(fightID, data); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java index 4814560..df65d54 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java +++ b/SpigotCore_Main/src/de/steamwar/sql/FightPlayer.java @@ -22,9 +22,7 @@ package de.steamwar.sql; public class FightPlayer { private FightPlayer(){} - private static final Statement create = new Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); - public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){ - create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut); + Provider.impl.createFightPlayer(fightID, userID, blue, kit, kills, isOut); } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java index b19348d..2ffc7d9 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeDownload.java @@ -26,8 +26,7 @@ import java.security.NoSuchAlgorithmException; import java.time.Instant; public class NodeDownload { - - private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + private NodeDownload() {} private static final String BASE = "https://steamwar.de/download.php?schem="; @@ -43,7 +42,7 @@ public class NodeDownload { digest.reset(); digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes()); String hash = BaseEncoding.base16().encode(digest.digest()); - createLink.update(schem.getId(), hash); + Provider.impl.createDownloadLink(schem.getId(), hash); return BASE + hash; } } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Provider.java b/SpigotCore_Main/src/de/steamwar/sql/Provider.java new file mode 100644 index 0000000..ff0a613 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Provider.java @@ -0,0 +1,51 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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; + +import de.steamwar.core.Core; + +import java.io.InputStream; +import java.sql.Timestamp; +import java.util.List; +import java.util.function.Consumer; + +public interface Provider { + Provider impl = Core.standalone() ? new StandaloneProvider() : new SQLProvider(); + + BauweltMember getBauMember(int ownerID, int memberID); + List getMembers(int bauweltID); + + List getLastDeclinedOfNode(int node); + List getLastDelined(int schemOwner); + + int getElo(int userId, String gameMode); + void setElo(int userId, String gameMode, int elo); + + Event getEvent(int eventID); + EventFight getEventFight(int fightID); + + int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock); + void getReplay(int fightID, Consumer reader); + void setReplay(int fightID, InputStream data); + + void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut); + + void createDownloadLink(int nodeId, String hash); +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java new file mode 100644 index 0000000..d21338e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -0,0 +1,157 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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; + +import java.io.InputStream; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class SQLProvider implements Provider { + + private static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?"); + @Override + public BauweltMember getBauMember(int ownerID, int memberID) { + return getMember.select(rs -> rs.next() ? newBauweltMember(rs) : null, ownerID, memberID); + } + + private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?"); + @Override + public List getMembers(int bauweltID) { + return getMembers.select(rs -> { + List members = new ArrayList<>(); + while(rs.next()) + members.add(newBauweltMember(rs)); + return members; + }, bauweltID); + } + + private BauweltMember newBauweltMember(ResultSet rs) throws SQLException { + return new BauweltMember(rs.getInt("BauweltID"), rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World")); + } + + private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC"); + @Override + public List getLastDeclinedOfNode(int node) { + return nodeHistory.select(rs -> { + List lastDeclined = new ArrayList<>(); + while(rs.next()) + lastDeclined.add(newCheckedSchematic(rs)); + return lastDeclined; + }, node); + } + + private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC"); + @Override + public List getLastDelined(int schemOwner) { + return checkHistory.select(rs -> { + List history = new ArrayList<>(); + while(rs.next()) + history.add(newCheckedSchematic(rs)); + return history; + }, schemOwner); + } + + private CheckedSchematic newCheckedSchematic(ResultSet rs) throws SQLException { + 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) { + return getElo.select(rs -> { + if(rs.next()) + return rs.getInt("Elo"); + return 1000; + }, userId, gameMode); + } + + private static final Statement setElo = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)"); + @Override + public void setElo(int userId, String gameMode, int elo) { + setElo.update(userId, gameMode, elo); + } + + private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?"); + @Override + public Event getEvent(int eventID) { + return getEvent.select(rs -> { + rs.next(); + return new Event(rs.getInt("EventID"), rs.getString("EventName"), rs.getTimestamp("Start"), rs.getTimestamp("End"), rs.getInt("MaximumTeamMembers"), rs.getBoolean("PublicSchemsOnly"), rs.getBoolean("SpectateSystem")); + }, eventID); + } + + private static final Statement getEventFight = new Statement("SELECT * FROM EventFight WHERE FightID = ?"); + private static final Statement setEventFightResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?"); + private static final Statement setEventFightFightID = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?"); + @Override + public EventFight getEventFight(int fightID) { + return getEventFight.select(rs -> { + rs.next(); + return new EventFight( + rs.getInt("EventID"), rs.getInt("FightID"), rs.getInt("TeamBlue"), rs.getInt("TeamRed"), rs.getInt("Kampfleiter"), rs.getInt("Ergebnis"), + (eventFight, winner) -> setEventFightResult.update(winner, eventFight.getFightID()), + (eventFight, fight) -> setEventFightFightID.update(fight, eventFight.getFightID()) + ); + }, fightID); + } + + private static final Statement createFight = new Statement("INSERT INTO Fight (GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition, ReplayLock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + private static final Statement getLastFightID = new Statement("SELECT LAST_INSERT_ID() AS FightID"); + @Override + public int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock) { + createFight.update(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock); + return getLastFightID.select(rs -> { + rs.next(); + return rs.getInt("FightID"); + }); + } + + private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?"); + @Override + public void getReplay(int fightID, Consumer reader) { + getReplay.select(rs -> { + rs.next(); + reader.accept(rs.getBinaryStream("Replay")); + return null; + }, fightID); + } + + private static final Statement setReplay = new Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?"); + @Override + public void setReplay(int fightID, InputStream data) { + setReplay.update(data, fightID); + } + + private static final Statement create = new Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)"); + @Override + public void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut) { + create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut); + } + + private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)"); + @Override + public void createDownloadLink(int nodeId, String hash) { + createLink.update(nodeId, hash); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java new file mode 100644 index 0000000..2fa3b2d --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -0,0 +1,91 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +import java.io.InputStream; +import java.sql.Timestamp; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +public class StandaloneProvider implements Provider { + @Override + public BauweltMember getBauMember(int ownerID, int memberID) { + OfflinePlayer player = Bukkit.getOfflinePlayer(SteamwarUser.get(memberID).getUUID()); + return new BauweltMember(ownerID, memberID, player.isOp(), player.isOp()); + } + + @Override + public List getMembers(int bauweltID) { + return Bukkit.getOnlinePlayers().stream().map(player -> getBauMember(bauweltID, SteamwarUser.get(player.getUniqueId()).getId())).collect(Collectors.toList()); + } + + @Override + public List getLastDeclinedOfNode(int node) { + return new ArrayList<>(); + } + + @Override + public List getLastDelined(int schemOwner) { + return new ArrayList<>(); + } + + @Override + public int getElo(int userId, String gameMode) { + return 1000; + } + + @Override + public void setElo(int userId, String gameMode, int elo) {} + + @Override + public Event getEvent(int eventID) { + return new Event(eventID, "DummyEvent", Timestamp.from(Instant.now()), Timestamp.from(Instant.now()), 6, false, false); + } + + @Override + public EventFight getEventFight(int fightID) { + return new EventFight(0, fightID, 0, 0, 0, 0, (eventFight, winner) -> {}, (eventFight, fightId) -> {}); + } + + @Override + public int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock) { + return 0; + } + + @Override + public void getReplay(int fightID, Consumer reader) {} + + @Override + public void setReplay(int fightID, InputStream data) {} + + @Override + public void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut) {} + + @Override + public void createDownloadLink(int nodeId, String hash) {} + + +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/Statement.java b/SpigotCore_Main/src/de/steamwar/sql/Statement.java index 48719e6..13eec67 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Statement.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Statement.java @@ -40,7 +40,7 @@ public class Statement { private static final List statements = new ArrayList<>(); static { - File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml"); + File file = Core.sqlConfig(); YamlConfiguration config = YamlConfiguration.loadConfiguration(file); if(!file.exists()) @@ -63,10 +63,12 @@ public class Statement { public static void close() { for (Statement statement : statements) { - try { - statement.st.close(); - } catch (SQLException e) { - Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); + if (statement.st != null) { + try { + statement.st.close(); + } catch (SQLException e) { + Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e); + } } } @@ -80,14 +82,6 @@ public class Statement { private static void reset() { close(); connect(); - - try { - for (Statement statement : statements) { - statement.init(); - } - } catch (SQLException ex) { - throw new SecurityException("Could not reprepare SQL Statements", ex); - } } public static boolean connectionStable() { @@ -101,31 +95,22 @@ public class Statement { private final String sql; private PreparedStatement st; - Statement(String sql) { + public Statement(String sql) { this.sql = sql; - try { - init(); - } catch (SQLException e) { - throw new SecurityException("Could not init statement", e); - } statements.add(this); } - private synchronized void init() throws SQLException { - st = con.prepareStatement(sql); - } - - T select(ResultSetUser user, Object... objects) { + public T select(ResultSetUser user, Object... objects) { return prepare(() -> { - ResultSet rs = st.executeQuery(); + ResultSet rs = getSt().executeQuery(); T result = user.use(rs); rs.close(); return result; }, objects); } - void update(Object... objects) { - prepare(st::executeUpdate, objects); + public void update(Object... objects) { + prepare(getSt()::executeUpdate, objects); } private synchronized T prepare(SQLRunnable runnable, Object... objects) { @@ -143,13 +128,24 @@ public class Statement { } } + private PreparedStatement getSt() { + if(st == null) { + try { + st = con.prepareStatement(sql); + } catch (SQLException e) { + throw new SecurityException("Could not prepare statement", e); + } + } + return st; + } + private void setObjects(Object... objects) throws SQLException { for (int i = 0; i < objects.length; i++) { - st.setObject(i + 1, objects[i]); + getSt().setObject(i + 1, objects[i]); } } - interface ResultSetUser { + public interface ResultSetUser { T use(ResultSet rs) throws SQLException; }