From 383bf5da2d3f33da9503726fcba4046998473bdc Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 29 Oct 2020 14:29:12 +0200 Subject: [PATCH] Fixing duplicated points records in database --- .../gamingmesh/jobs/container/JobsPlayer.java | 2 +- .../jobs/container/PlayerPoints.java | 21 +++---- .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 60 +++++++++++++++---- .../gamingmesh/jobs/economy/PointsData.java | 2 +- 4 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java index fa34710f..038ebb72 100644 --- a/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java +++ b/src/main/java/com/gamingmesh/jobs/container/JobsPlayer.java @@ -112,7 +112,7 @@ public class JobsPlayer { public void setPoints(PlayerPoints points) { getPointsData().setPoints(points.getCurrentPoints()); getPointsData().setTotalPoints(points.getTotalPoints()); - getPointsData().setNewEntry(points.isNewEntry()); + getPointsData().setDbId(points.getDbId()); } public boolean havePoints(double points) { diff --git a/src/main/java/com/gamingmesh/jobs/container/PlayerPoints.java b/src/main/java/com/gamingmesh/jobs/container/PlayerPoints.java index dfe9aa46..77258061 100644 --- a/src/main/java/com/gamingmesh/jobs/container/PlayerPoints.java +++ b/src/main/java/com/gamingmesh/jobs/container/PlayerPoints.java @@ -4,15 +4,12 @@ public class PlayerPoints { private double current = 0D; private double total = 0D; - private boolean newEntry = false; + private int dbId = 0; public PlayerPoints() { - newEntry = true; } public PlayerPoints(double points, double total) { - if (points == 0D && total == 0D) - newEntry = true; this.current = points; this.total = total; } @@ -43,15 +40,15 @@ public class PlayerPoints { return total; } - public boolean isNewEntry() { - return newEntry; - } - - public void setNewEntry(boolean newEntry) { - this.newEntry = newEntry; - } - public void setTotalPoints(double total) { this.total = total; } + + public int getDbId() { + return dbId; + } + + public void setDbId(int dbId) { + this.dbId = dbId; + } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 40a3ee7c..2f131f6b 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -1929,19 +1929,52 @@ public abstract class JobsDAO { JobsConnection conn = getConnection(); if (conn == null) return; - PreparedStatement prest = null; - try { - PlayerPoints pointInfo = jPlayer.getPointsData(); - prest = conn.prepareStatement("INSERT INTO `" + DBTables.PointsTable.getTableName() + "` (`" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints.getCollumn() - + "`, `" + PointsTableFields.userid.getCollumn() + "`) VALUES (?, ?, ?);"); - prest.setDouble(1, pointInfo.getTotalPoints()); - prest.setDouble(2, pointInfo.getCurrentPoints()); - prest.setInt(3, jPlayer.getUserId()); - prest.execute(); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - close(prest); + + PlayerPoints pointInfo = jPlayer.getPointsData(); + + if (pointInfo.getDbId() == 0) { + // This needs to exist, removing existing entry by user id unless we have actual line id + PreparedStatement prest2 = null; + try { + prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;"); + prest2.setInt(1, jPlayer.getUserId()); + prest2.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest2); + } + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("INSERT INTO `" + DBTables.PointsTable.getTableName() + "` (`" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints + .getCollumn() + + "`, `" + PointsTableFields.userid.getCollumn() + "`) VALUES (?, ?, ?);"); + prest.setDouble(1, pointInfo.getTotalPoints()); + prest.setDouble(2, pointInfo.getCurrentPoints()); + prest.setInt(3, jPlayer.getUserId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } else { + + PreparedStatement prest = null; + try { + prest = conn.prepareStatement("UPDATE `" + DBTables.PointsTable.getTableName() + "` SET `" + PointsTableFields.totalpoints.getCollumn() + + "` = ?, `" + PointsTableFields.currentpoints.getCollumn() + + "` = ? WHERE `id` = ?;"); + prest.setDouble(1, pointInfo.getTotalPoints()); + prest.setDouble(2, pointInfo.getCurrentPoints()); + prest.setInt(3, pointInfo.getDbId()); + prest.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + close(prest); + } + } } @@ -1958,6 +1991,7 @@ public abstract class JobsDAO { res = prest.executeQuery(); if (res.next()) { + player.getPointsData().setDbId(res.getInt("id")); player.getPointsData().setPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn())); player.getPointsData().setTotalPoints(res.getDouble(PointsTableFields.totalpoints.getCollumn())); } diff --git a/src/main/java/com/gamingmesh/jobs/economy/PointsData.java b/src/main/java/com/gamingmesh/jobs/economy/PointsData.java index ffcf63a8..e5a78613 100644 --- a/src/main/java/com/gamingmesh/jobs/economy/PointsData.java +++ b/src/main/java/com/gamingmesh/jobs/economy/PointsData.java @@ -27,7 +27,7 @@ public class PointsData { PlayerPoints pi = Jobs.getPlayerManager().getJobsPlayer(uuid).getPointsData(); pi.setPoints(points.getCurrentPoints()); pi.setTotalPoints(points.getTotalPoints()); - pi.setNewEntry(points.isNewEntry()); + pi.setDbId(points.getDbId()); } public void addPoints(UUID uuid, Double points) {