From 8d6c499cd131cf3a7290a13ba0049c8bb2cb540c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 31 May 2021 08:01:21 +0200 Subject: [PATCH 1/2] Storing replays in the database --- .../src/de/steamwar/sql/EventFight.java | 5 +++ .../src/de/steamwar/sql/Fight.java | 37 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java index 8d5bb86..5b5d4f5 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/EventFight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/EventFight.java @@ -57,6 +57,11 @@ public class EventFight { SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, fightID); } + public void setFight(int fight){ + //Fight.FightID, not EventFight.FightID + SQL.update("UPDATE EventFight SET Fight = ? WHERE FightID = ?", fight, fightID); + } + public int getTeamBlue() { return teamBlue; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index c0448dc..089de5a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -19,6 +19,9 @@ package de.steamwar.sql; +import java.io.InputStream; +import java.io.OutputStream; +import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; @@ -27,8 +30,12 @@ public class Fight { private Fight(){} public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ - SQL.update("INSERT INTO Fight (GameMode, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - gamemode, arena, starttime, duration, blueleader, redleader,blueschem, redschem, win, wincondition); + return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); + } + + public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ + SQL.update("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); try{ if(!rs.next()) @@ -39,4 +46,30 @@ public class Fight { throw new SecurityException(e); } } + + public static InputStream getReplay(int fightID) { + ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); + try { + rs.next(); + Blob replay = rs.getBlob("Replay"); + if(replay == null) + throw new SecurityException("Replay null"); + return replay.getBinaryStream(); + } catch (SQLException e) { + throw new SecurityException(e); + } + } + + public static OutputStream setReplay(int fightID) { + ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); + try { + rs.next(); + Blob replay = rs.getBlob("Replay"); + if(replay == null) + throw new SecurityException("Replay null"); + return replay.setBinaryStream(1); + } catch (SQLException e) { + throw new SecurityException(e); + } + } } From 77766621f8a2711d53f2cfd8955ba4b25f91975c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 31 May 2021 08:14:46 +0200 Subject: [PATCH 2/2] Fix replay saving --- SpigotCore_Main/src/de/steamwar/sql/Fight.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Fight.java b/SpigotCore_Main/src/de/steamwar/sql/Fight.java index 089de5a..7604b12 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Fight.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Fight.java @@ -20,7 +20,6 @@ package de.steamwar.sql; import java.io.InputStream; -import java.io.OutputStream; import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; @@ -60,16 +59,13 @@ public class Fight { } } - public static OutputStream setReplay(int fightID) { - ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); + public static void setReplay(int fightID, byte[] data) { + Blob blob = SQL.blob(); try { - rs.next(); - Blob replay = rs.getBlob("Replay"); - if(replay == null) - throw new SecurityException("Replay null"); - return replay.setBinaryStream(1); + blob.setBytes(1, data); } catch (SQLException e) { throw new SecurityException(e); } + SQL.update("UPDATE Fight SET Replay = ? WHERE FightID = ?", blob, fightID); } }