From 4496cd8b0a4a4aba70b1363156a2317f63248aa9 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 4 Mar 2021 03:06:34 +0100 Subject: [PATCH] Changing to Schemnodes --- .../bungeecore/commands/CheckCommand.java | 74 ++-- .../bungeecore/inventory/SWListInv.java | 23 -- .../bungeecore/listeners/CheckListener.java | 6 +- .../bungeecore/sql/CheckedSchematic.java | 20 +- .../steamwar/bungeecore/sql/NodeMember.java | 87 +++++ src/de/steamwar/bungeecore/sql/Schematic.java | 141 ------- .../bungeecore/sql/SchematicMember.java | 134 ------- .../bungeecore/sql/SchematicNode.java | 359 ++++++++++++++++++ 8 files changed, 497 insertions(+), 347 deletions(-) create mode 100644 src/de/steamwar/bungeecore/sql/NodeMember.java delete mode 100644 src/de/steamwar/bungeecore/sql/Schematic.java delete mode 100644 src/de/steamwar/bungeecore/sql/SchematicMember.java create mode 100644 src/de/steamwar/bungeecore/sql/SchematicNode.java diff --git a/src/de/steamwar/bungeecore/commands/CheckCommand.java b/src/de/steamwar/bungeecore/commands/CheckCommand.java index 9aaed515..bd268ec5 100644 --- a/src/de/steamwar/bungeecore/commands/CheckCommand.java +++ b/src/de/steamwar/bungeecore/commands/CheckCommand.java @@ -22,7 +22,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.listeners.ConnectionListener; import de.steamwar.bungeecore.sql.CheckedSchematic; -import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.ChatColor; @@ -66,7 +66,7 @@ public class CheckCommand extends BasicCommand { super("check", ConnectionListener.CHECK_PERMISSION); ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { - List schematics = getSchemsToCheck(); + List schematics = getSchemsToCheck(); if(schematics.size() != currentCheckers.size()) Message.team("CHECK_REMINDER", schematics.size() - currentCheckers.size()); }, 10, 10, TimeUnit.MINUTES); @@ -105,33 +105,33 @@ public class CheckCommand extends BasicCommand { } } - private static List getSchemsToCheck(){ - List schematicList = new LinkedList<>(); + private static List getSchemsToCheck(){ + List schematicList = new LinkedList<>(); for (SchematicType type : SchematicType.values()) { if (type.check()) - schematicList.addAll(Schematic.getAllSchemsOfType(type)); + schematicList.addAll(SchematicNode.getAllSchematicsOfType(type.toDB())); } return schematicList; } private void list(ProxiedPlayer player) { - List schematicList = getSchemsToCheck(); + List schematicList = getSchemsToCheck(); Message.sendPrefixless("CHECK_LIST_HEADER", player, schematicList.size()); - for (Schematic schematic : schematicList) { - CheckSession current = currentSchems.get(schematic.getSchemID()); + for (SchematicNode schematic : schematicList) { + CheckSession current = currentSchems.get(schematic.getId()); if (current == null) { Message.sendPrefixless("CHECK_LIST_TO_CHECK", player, Message.parse("CHECK_LIST_TO_CHECK_HOVER", player), - new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check schematic " + schematic.getSchemID()), - schematic.getSchemType().getKuerzel(), SteamwarUser.get(schematic.getSchemOwner()).getUserName(), schematic.getSchemName()); + new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check schematic " + schematic.getId()), + schematic.getSchemtype().getKuerzel(), SteamwarUser.get(schematic.getOwner()).getUserName(), schematic.getName()); } else { Message.sendPrefixless("CHECK_LIST_CHECKING", player, Message.parse("CHECK_LIST_CHECKING_HOVER", player), new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/join " + current.checker.getName()), - schematic.getSchemType().getKuerzel(), SteamwarUser.get(schematic.getSchemOwner()).getUserName(), schematic.getSchemName(), current.checker.getName()); + schematic.getSchemtype().getKuerzel(), SteamwarUser.get(schematic.getOwner()).getUserName(), schematic.getName(), current.checker.getName()); } } } @@ -142,11 +142,11 @@ public class CheckCommand extends BasicCommand { return; } - Schematic schem = Schematic.getSchemFromDB(Integer.parseInt(schemID)); - if(!schem.getSchemType().check()){ + SchematicNode schem = SchematicNode.getSchematicNode(Integer.parseInt(schemID)); + if(!schem.getSchemtype().check()){ ProxyServer.getInstance().getLogger().log(Level.SEVERE, player.getName() + " tried to check an uncheckable schematic!"); return; - }else if(schem.getSchemOwner() == SteamwarUser.get(player.getUniqueId()).getId()) { + }else if(schem.getOwner() == SteamwarUser.get(player.getUniqueId()).getId()) { Message.send("CHECK_SCHEMATIC_OWN", player); return; } @@ -212,15 +212,15 @@ public class CheckCommand extends BasicCommand { private static class CheckSession{ private final ProxiedPlayer checker; - private final Schematic schematic; + private final SchematicNode schematic; private final Timestamp startTime; private final ListIterator checkList; - private CheckSession(ProxiedPlayer checker, Schematic schematic){ + private CheckSession(ProxiedPlayer checker, SchematicNode schematic){ this.checker = checker; this.schematic = schematic; this.startTime = Timestamp.from(Instant.now()); - this.checkList = checkQuestions.get(schematic.getSchemType()).listIterator(); + this.checkList = checkQuestions.get(schematic.getSchemtype()).listIterator(); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { for (Subserver subserver : Subserver.getServerList()) { @@ -230,11 +230,11 @@ public class CheckCommand extends BasicCommand { } } - ArenaMode mode = ArenaMode.getByCheckSchemType(schematic.getSchemType().toDB()); - SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getSchemID()); + //ArenaMode mode = ArenaMode.getByCheckSchemType(schematic.getSchemtype().toDB()); + //SubserverSystem.startTestServer(checker, mode, FightCommand.getMap(checker, mode, "Random"), schematic.getId()); currentCheckers.put(checker.getUniqueId(), this); - currentSchems.put(schematic.getSchemID(), this); - for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic.getSchemName(), schematic.getSchemOwner())) + currentSchems.put(schematic.getId(), this); + for(CheckedSchematic previous : CheckedSchematic.previousChecks(schematic.getId())) Message.sendPrefixless("CHECK_SCHEMATIC_PREVIOUS", checker, previous.getEndTime(), SteamwarUser.get(previous.getValidator()).getUserName(), previous.getReason()); next(0); }); @@ -253,8 +253,8 @@ public class CheckCommand extends BasicCommand { if(checkList.hasNext()){ next.setText(Message.parse("CHECK_NEXT", checker)); next.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/check next")); - }else if(ranks.containsKey(schematic.getSchemType())){ - List r = ranks.get(schematic.getSchemType()); + }else if(ranks.containsKey(schematic.getSchemtype())){ + List r = ranks.get(schematic.getSchemtype()); for(int i = 0; i < r.size(); i++){ Message.sendPrefixless("CHECK_RANK", checker, Message.parse("CHECK_RANK_HOVER", checker), @@ -276,43 +276,43 @@ public class CheckCommand extends BasicCommand { } private void accept(int rank){ - if(ranks.containsKey(schematic.getSchemType())){ - if(rank <= 0 || ranks.get(schematic.getSchemType()).size() < rank){ + if(ranks.containsKey(schematic.getSchemtype())){ + if(rank <= 0 || ranks.get(schematic.getSchemtype()).size() < rank){ Message.send("CHECK_INVALID_RANK", checker); return; } schematic.setRank(rank); } - schematic.setSchemType(schematic.getSchemType().fightType()); - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); - SteamwarUser user = SteamwarUser.get(schematic.getSchemOwner()); + schematic.setType(schematic.getSchemtype().fightType().toDB()); + CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "freigegeben"); + SteamwarUser user = SteamwarUser.get(schematic.getOwner()); ProxiedPlayer player = ProxyServer.getInstance().getPlayer(user.getUuid()); if(player != null) - Message.send("CHECK_ACCEPTED", player, schematic.getSchemType().name(), schematic.getSchemName()); - Message.team("CHECK_ACCEPTED_TEAM", schematic.getSchemName(), user.getUserName()); + Message.send("CHECK_ACCEPTED", player, schematic.getSchemtype().name(), schematic.getName()); + Message.team("CHECK_ACCEPTED_TEAM", schematic.getName(), user.getUserName()); stop(); } private void decline(String reason){ - schematic.setSchemType(SchematicType.Normal); - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); - SteamwarUser user = SteamwarUser.get(schematic.getSchemOwner()); + schematic.setType(SchematicType.Normal.toDB()); + CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), reason); + SteamwarUser user = SteamwarUser.get(schematic.getOwner()); ProxiedPlayer player = ProxyServer.getInstance().getPlayer(user.getUuid()); if(player != null) - Message.send("CHECK_DECLINED", player, schematic.getSchemType().name(), schematic.getSchemName(), reason); - Message.team("CHECK_DECLINED_TEAM", schematic.getSchemName(), user.getUserName(), reason); + Message.send("CHECK_DECLINED", player, schematic.getSchemtype().name(), schematic.getName(), reason); + Message.team("CHECK_DECLINED_TEAM", schematic.getName(), user.getUserName(), reason); stop(); } private void abort(){ - CheckedSchematic.create(schematic.getSchemName(), schematic.getSchemOwner(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "Prüfvorgang abgebrochen"); + CheckedSchematic.create(schematic.getId(), SteamwarUser.get(checker.getUniqueId()).getId(), startTime, Timestamp.from(Instant.now()), "Prüfvorgang abgebrochen"); stop(); } private void stop(){ currentCheckers.remove(checker.getUniqueId()); - currentSchems.remove(schematic.getSchemID()); + currentSchems.remove(schematic.getId()); ProxyServer.getInstance().getScheduler().runAsync(BungeeCore.get(), () -> { for (Subserver subserver : Subserver.getServerList()) { if (subserver.getType() == Servertype.BAUSERVER && ((Bauserver) subserver).getOwner().equals(checker.getUniqueId())) { diff --git a/src/de/steamwar/bungeecore/inventory/SWListInv.java b/src/de/steamwar/bungeecore/inventory/SWListInv.java index 1161260c..8636054d 100644 --- a/src/de/steamwar/bungeecore/inventory/SWListInv.java +++ b/src/de/steamwar/bungeecore/inventory/SWListInv.java @@ -19,8 +19,6 @@ package de.steamwar.bungeecore.inventory; -import de.steamwar.bungeecore.sql.Schematic; -import de.steamwar.bungeecore.sql.SchematicType; import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -103,27 +101,6 @@ public class SWListInv extends SWInventory { return onlinePlayers; } - public static List> getSchemList(SchematicType type, int steamwarUserId){ - List> schemList = new ArrayList<>(); - - List schems; - if(type == null) - schems = Schematic.getSchemsAccessibleByUser(steamwarUserId); - else - schems = Schematic.getSchemsOfType(steamwarUserId, type); - - for(Schematic s : schems){ - String m; - if(s.getSchemItem().isEmpty()) - m = "CAULDRON_ITEM"; - else - m = s.getSchemItem(); - SWItem item = new SWItem(m, "§e" + s.getSchemName()); - schemList.add(new SWListEntry<>(item, s)); - } - return schemList; - } - public static class SWListEntry{ final SWItem item; final T object; diff --git a/src/de/steamwar/bungeecore/listeners/CheckListener.java b/src/de/steamwar/bungeecore/listeners/CheckListener.java index f4aec741..1370a0db 100644 --- a/src/de/steamwar/bungeecore/listeners/CheckListener.java +++ b/src/de/steamwar/bungeecore/listeners/CheckListener.java @@ -21,7 +21,7 @@ package de.steamwar.bungeecore.listeners; import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.commands.CheckCommand; -import de.steamwar.bungeecore.sql.Schematic; +import de.steamwar.bungeecore.sql.SchematicNode; import de.steamwar.bungeecore.sql.SchematicType; import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -40,10 +40,10 @@ public class CheckListener extends BasicListener { ProxiedPlayer player = e.getPlayer(); SteamwarUser user = SteamwarUser.get(player.getUniqueId()); - List uncheckedSchematics = new LinkedList<>(); + List uncheckedSchematics = new LinkedList<>(); for(SchematicType type : SchematicType.values()){ if(type.check()) - uncheckedSchematics.addAll(Schematic.getSchemsOfType(user.getId(), type)); + uncheckedSchematics.addAll(SchematicNode.getAllSchematicsOfType(user.getId(), type.toDB())); } if(!uncheckedSchematics.isEmpty()) diff --git a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java index 0424e4aa..ed2160cb 100644 --- a/src/de/steamwar/bungeecore/sql/CheckedSchematic.java +++ b/src/de/steamwar/bungeecore/sql/CheckedSchematic.java @@ -27,8 +27,7 @@ import java.util.List; public class CheckedSchematic { - private final String schemName; - private final int schemOwner; + private final int nodeId; private final int validator; private final Timestamp startTime; @@ -36,21 +35,20 @@ public class CheckedSchematic { private final String reason; private CheckedSchematic(ResultSet rs) throws SQLException { - schemName = rs.getString("SchemName"); - schemOwner = rs.getInt("SchemOwner"); + nodeId = rs.getInt("NodeId"); validator = rs.getInt("Validator"); startTime = rs.getTimestamp("StartTime"); endTime = rs.getTimestamp("EndTime"); reason = rs.getString("DeclineReason"); } - public static void create(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String reason){ - SQL.update("INSERT INTO CheckedSchematic (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", - schemName, schemOwner, validator, startTime, endTime, reason); + public static void create(int nodeId, int validator, Timestamp startTime, Timestamp endTime, String reason){ + SQL.update("INSERT INTO CheckedSchematic (NodeId, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?)", + nodeId, validator, startTime, endTime, reason); } - public static List previousChecks(String schemName, int schemOwner){ - ResultSet rs = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemName = ? AND SchemOwner = ? ORDER BY EndTime ASC", schemName, schemOwner); + public static List previousChecks(int nodeId){ + ResultSet rs = SQL.select("SELECT * FROM CheckedSchematic WHERE NodeId = ? ORDER BY EndTime ASC", nodeId); List schematics = new ArrayList<>(); try { while(rs.next()) @@ -72,4 +70,8 @@ public class CheckedSchematic { public String getReason() { return reason; } + + public int getNodeId() { + return nodeId; + } } diff --git a/src/de/steamwar/bungeecore/sql/NodeMember.java b/src/de/steamwar/bungeecore/sql/NodeMember.java new file mode 100644 index 00000000..76c91e4b --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/NodeMember.java @@ -0,0 +1,87 @@ +/* + 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.Set; + +public class NodeMember { + public static NodeMember getNodeMember(int node, int member) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + try { + if(!set.next()) + return null; + return new NodeMember(set); + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + public static Set getNodeMembers(int node) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE NodeId = ?", node); + try { + Set members = new HashSet<>(); + while (set.next()) + members.add(new NodeMember(set)); + return members; + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + public static Set getSchematics(int member) { + ResultSet set = SQL.select("SELECT * FROM NodeMember WHERE UserId = ?", member); + try { + Set members = new HashSet<>(); + while (set.next()) + members.add(new NodeMember(set)); + return members; + } catch (SQLException e) { + throw new SecurityException("Could not load NodeMember", e); + } + } + + public static NodeMember createNodeMember(int node, int member) { + SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + return getNodeMember(node, member); + } + + final int node; + final int member; + + private NodeMember(ResultSet set) throws SQLException { + node = set.getInt("NodeId"); + member = set.getInt("UserId"); + } + + public int getNode() { + return node; + } + + public int getMember() { + return member; + } + + public void delete() { + SQL.update("DELETE FROM NodeMember WHERE NodeId = ? AND UserId = ?", node, member); + } +} diff --git a/src/de/steamwar/bungeecore/sql/Schematic.java b/src/de/steamwar/bungeecore/sql/Schematic.java deleted file mode 100644 index 9d25a384..00000000 --- a/src/de/steamwar/bungeecore/sql/Schematic.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - 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.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class Schematic { - private final int schemID; - private final String schemName; - private final int schemOwner; - private SchematicType schemType; - private String schemItem; - - private Schematic(ResultSet rs) throws SQLException { - this.schemID = rs.getInt("SchemID"); - this.schemName = rs.getString("SchemName"); - this.schemOwner = rs.getInt("SchemOwner"); - this.schemType = SchematicType.fromDB(rs.getString("SchemType")); - this.schemItem = rs.getString("Item"); - } - - public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ - return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static Schematic getSchemFromDB(String schemName, int schemOwner){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); - try { - if(schematic == null || !schematic.next()){ - return null; - } - return new Schematic(schematic); - } catch (SQLException e) { - throw new SecurityException("Failed loading schematic", e); - } - } - - public static Schematic getSchemFromDB(int schemID){ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemID = ?", schemID); - try { - if(!schematic.next()) - throw new SecurityException("Failed loading schematic " + schemID); - return new Schematic(schematic); - } catch (SQLException e) { - throw new SecurityException("Failed loading schematic", e); - } - } - - public static List getAllSchemsOfType(SchematicType schemType){ - try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ?", schemType.toDB()); - List schematics = new ArrayList<>(); - while(schematic.next()){ - schematics.add(new Schematic(schematic)); - } - return schematics; - }catch(SQLException e){ - throw new SecurityException("Failed loading all schems of type", e); - } - } - - public static List getSchemsOfType(int userId, SchematicType schemType){ - try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, SchemType, Item FROM Schematic WHERE SchemType = ? AND SchemOwner = ?", schemType.toDB(), userId); - List schematics = new ArrayList<>(); - while(schematic.next()){ - schematics.add(new Schematic(schematic)); - } - return schematics; - }catch(SQLException e){ - throw new SecurityException("Failed loading schems of type", e); - } - } - - public static List getSchemsAccessibleByUser(int schemOwner){ - try{ - ResultSet schematic = SQL.select("SELECT SchemID, SchemName, SchemOwner, Item, SchemType, Rank, SchemFormat, Item FROM Schematic WHERE SchemOwner = ? ORDER BY SchemName", schemOwner); - List schematics = new ArrayList<>(); - while(schematic.next()){ - schematics.add(new Schematic(schematic)); - } - List addedSchems = SchematicMember.getAccessibleSchems(schemOwner); - for(SchematicMember schem : addedSchems){ - schematics.add(getSchemFromDB(schem.getSchemName(), schem.getSchemOwner())); - } - return schematics; - }catch(SQLException e){ - throw new SecurityException("Failed listing schematics", e); - } - } - - public int getSchemID() { - return schemID; - } - - public String getSchemName() { - return schemName; - } - - public int getSchemOwner() { - return schemOwner; - } - - public String getSchemItem() { - return schemItem; - } - - public SchematicType getSchemType() { - return schemType; - } - - public void setSchemType(SchematicType schemType) { - this.schemType = schemType; - SQL.update("UPDATE Schematic SET SchemType = ? WHERE SchemID = ?", schemType.toDB(), schemID); - } - - public void setRank(int rank) { - SQL.update("UPDATE Schematic SET Rank = ? WHERE SchemID = ?", rank, schemID); - } -} diff --git a/src/de/steamwar/bungeecore/sql/SchematicMember.java b/src/de/steamwar/bungeecore/sql/SchematicMember.java deleted file mode 100644 index 85b84855..00000000 --- a/src/de/steamwar/bungeecore/sql/SchematicMember.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - 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.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class SchematicMember { - private final int schemOwner; - private final String schemName; - private final int member; - - private SchematicMember(String schemName, int schemOwner, int schemMember, boolean updateDB){ - this.schemOwner = schemOwner; - member = schemMember; - this.schemName = schemName; - if(updateDB) - updateDB(); - } - - public SchematicMember(String schemName, int schemOwner, int schemMember){ - this(schemName, schemOwner, schemMember, true); - } - - public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ - this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); - } - - private void updateDB(){ - SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ - return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); - } - - public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get schemmember", e); - } - } - - public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); - try { - if(schematicMember == null || !schematicMember.next()){ - return null; - } - int schemOwner = schematicMember.getInt("SchemOwner"); - return new SchematicMember(schemName, schemOwner, schemMember, false); - } catch (SQLException e) { - throw new SecurityException("Could not get member", e); - } - } - - public static List getSchemMembers(String schemName, UUID schemOwner){ - return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); - } - - public static List getSchemMembers(String schemName, int schemOwner){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); - try { - List schematicMembers = new ArrayList<>(); - while(schematicMember.next()){ - int schemMember = schematicMember.getInt("Member"); - schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); - } - return schematicMembers; - } catch (SQLException e) { - throw new SecurityException("Could not get schemmembers", e); - } - } - - public static List getAccessibleSchems(UUID schemMember){ - return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); - } - - public static List getAccessibleSchems(int schemMember){ - ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = ?", schemMember); - try { - List schematicMembers = new ArrayList<>(); - while(schematicMember.next()){ - String schemName = schematicMember.getString("SchemName"); - int schemOwner = schematicMember.getInt("SchemOwner"); - schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); - } - return schematicMembers; - } catch (SQLException e) { - throw new SecurityException("Could not get accessible schems", e); - } - } - - public int getSchemOwner() { - return schemOwner; - } - - public String getSchemName() { - return schemName; - } - - public int getMember() { - return member; - } - - public void remove(){ - SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); - } -} diff --git a/src/de/steamwar/bungeecore/sql/SchematicNode.java b/src/de/steamwar/bungeecore/sql/SchematicNode.java new file mode 100644 index 00000000..85c0bf66 --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/SchematicNode.java @@ -0,0 +1,359 @@ +/* + 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.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; + +public class SchematicNode { + + public static SchematicNode createSchematic(int owner, String name, Integer parent) { + return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); + } + + public static SchematicNode createSchematicDirectory(int owner, String name, Integer parent) { + return createSchematicNode(owner, name, parent, null, ""); + } + + public static SchematicNode createSchematicNode(int owner, String name, Integer parent, String type, String item) { + if(parent == 0) + parent = null; + SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", + name, owner, parent, type, item); + return getSchematicNode(owner, name, parent); + } + + public static SchematicNode getSchematicNode(int owner, String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode is NULL", owner, name); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ?", owner, name, parent); + } + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(!node.isDir()) + return node; + } + List nodes = getSchematicNodeInNode(parent); + for (SchematicNode node:nodes) { + if(!node.isDir() && node.name.equals(name) && NodeMember.getNodeMember(node.getId(), owner) != null) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static SchematicNode getSchematicNode(int owner, String name, SchematicNode parent) { + return getSchematicNode(owner, name, parent.getId()); + } + + public static List getSchematicNodeInNode(Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode is NULL"); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE ParentNode = ?", parent); + } + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getSchematicNodeInNode(SchematicNode parent) { + return getSchematicNodeInNode(parent.getId()); + } + + public static SchematicNode getSchematicDirectory(String name, SchematicNode parent) { + return getSchematicDirectory(name, parent.getId()); + } + + public static SchematicNode getSchematicDirectory(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(node.isDir()) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static SchematicNode getSchematicInParent(String name, Integer parent) { + if(parent != null && parent == 0) + parent = null; + ResultSet set; + if(parent == null) { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode is NULL", name, parent); + }else { + set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + } + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(!node.isDir()) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static SchematicNode getSchematicNode(int id) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId = ?", id); + try { + if(!set.next()) + return null; + return new SchematicNode(set); + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getAllSchematicsOfType(int owner, String schemType) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?", owner, schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getAllSchematicsOfType(String schemType) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeType = ?", schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + + public static List getSchematicsOfType(int owner, String schemType, Integer parent) { + if(parent != null && parent == 0) + parent = null; + List schems = getAllSchematicsOfType(owner, schemType); + Map nodesInParent = new LinkedHashMap<>(); + for (SchematicNode schematicNode : schems) { + SchematicNode currentNode = schematicNode; + while (currentNode.getParent() != parent) { + currentNode = currentNode.getParentNode(); + } + nodesInParent.putIfAbsent(currentNode.getId(), currentNode); + } + return new ArrayList<>(nodesInParent.values()); + } + + public static List getSchematicsAccessibleByUser(int user, Integer parent) { + if(parent != null && parent != 0) { + SchematicNode node = SchematicNode.getSchematicNode(parent); + boolean isAdded = false; + while (node.getId() != 0) { + for (NodeMember member:node.getMembers()) { + if (member.getMember() == user) { + isAdded = true; + break; + } + } + node = SchematicNode.getSchematicNode(node.getParent()); + } + if(isAdded) + return getSchematicNodeInNode(parent); + } else { + ResultSet set = SQL.select("SELECT * FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + return null; + } + + public static List getAllSchematicsAccessibleByUser(int user) { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + + private final int id; + private final int owner; + private String name; + private int parent; + private String item; + private String type; + private boolean schemFormat; + private int rank; + private boolean isDir; + + private SchematicNode(ResultSet set) throws SQLException { + id = set.getInt("NodeId"); + owner = set.getInt("NodeOwner"); + name = set.getString("NodeName"); + parent = set.getInt("ParentNode"); + item = set.getString("NodeItem"); + type = set.getString("NodeType"); + if(type != null) { + isDir = false; + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("NodeFormat"); + }else { + isDir = true; + } + } + + public int getId() { + return id; + } + + public int getOwner() { + return owner; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + updateDB(); + } + + public int getParent() { + return parent; + } + + public void setParent(int parent) { + this.parent = parent; + updateDB(); + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + updateDB(); + } + + public String getType() { + if(isDir) + throw new SecurityException("Node is Directory"); + return type; + } + + public void setType(String type) { + if(isDir) + throw new SecurityException("Node is Directory"); + this.type = type; + updateDB(); + } + + public boolean isDir() { + return isDir; + } + + public boolean getSchemFormat() { + if(isDir) + throw new SecurityException("Node is Directory"); + return schemFormat; + } + + public int getRank() { + if(isDir) + throw new SecurityException("Node is Directory"); + return rank; + } + + public void setRank(int rank) { + if(isDir) + throw new SecurityException("Node is Directory"); + this.rank = rank; + } + + public SchematicType getSchemtype() { + if(isDir()) + throw new RuntimeException("Is Directory"); + return SchematicType.fromDB(type); + } + + public SchematicNode getParentNode() { + return SchematicNode.getSchematicNode(parent); + } + + public boolean accessibleByUser(int user) { + return NodeMember.getNodeMember(id, user) != null; + } + + public Set getMembers() { + return NodeMember.getNodeMembers(id); + } + + private void updateDB() { + SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?", + name, owner, parent, item, type, rank, id); + } + + public void delete() { + if(isDir()) { + getSchematicNodeInNode(getId()).forEach(SchematicNode::delete); + } + SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); + } +}