1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-20 07:01:22 +01:00

Fixing duplicated points records in database

This commit is contained in:
Zrips 2020-10-29 14:29:12 +02:00
parent 9adcc1593e
commit 383bf5da2d
4 changed files with 58 additions and 27 deletions

View File

@ -112,7 +112,7 @@ public class JobsPlayer {
public void setPoints(PlayerPoints points) { public void setPoints(PlayerPoints points) {
getPointsData().setPoints(points.getCurrentPoints()); getPointsData().setPoints(points.getCurrentPoints());
getPointsData().setTotalPoints(points.getTotalPoints()); getPointsData().setTotalPoints(points.getTotalPoints());
getPointsData().setNewEntry(points.isNewEntry()); getPointsData().setDbId(points.getDbId());
} }
public boolean havePoints(double points) { public boolean havePoints(double points) {

View File

@ -4,15 +4,12 @@ public class PlayerPoints {
private double current = 0D; private double current = 0D;
private double total = 0D; private double total = 0D;
private boolean newEntry = false; private int dbId = 0;
public PlayerPoints() { public PlayerPoints() {
newEntry = true;
} }
public PlayerPoints(double points, double total) { public PlayerPoints(double points, double total) {
if (points == 0D && total == 0D)
newEntry = true;
this.current = points; this.current = points;
this.total = total; this.total = total;
} }
@ -43,15 +40,15 @@ public class PlayerPoints {
return total; return total;
} }
public boolean isNewEntry() {
return newEntry;
}
public void setNewEntry(boolean newEntry) {
this.newEntry = newEntry;
}
public void setTotalPoints(double total) { public void setTotalPoints(double total) {
this.total = total; this.total = total;
} }
public int getDbId() {
return dbId;
}
public void setDbId(int dbId) {
this.dbId = dbId;
}
} }

View File

@ -1929,19 +1929,52 @@ public abstract class JobsDAO {
JobsConnection conn = getConnection(); JobsConnection conn = getConnection();
if (conn == null) if (conn == null)
return; return;
PreparedStatement prest = null;
try { PlayerPoints pointInfo = jPlayer.getPointsData();
PlayerPoints pointInfo = jPlayer.getPointsData();
prest = conn.prepareStatement("INSERT INTO `" + DBTables.PointsTable.getTableName() + "` (`" + PointsTableFields.totalpoints.getCollumn() + "`, `" + PointsTableFields.currentpoints.getCollumn() if (pointInfo.getDbId() == 0) {
+ "`, `" + PointsTableFields.userid.getCollumn() + "`) VALUES (?, ?, ?);"); // This needs to exist, removing existing entry by user id unless we have actual line id
prest.setDouble(1, pointInfo.getTotalPoints()); PreparedStatement prest2 = null;
prest.setDouble(2, pointInfo.getCurrentPoints()); try {
prest.setInt(3, jPlayer.getUserId()); prest2 = conn.prepareStatement("DELETE FROM `" + DBTables.PointsTable.getTableName() + "` WHERE `" + PointsTableFields.userid.getCollumn() + "` = ?;");
prest.execute(); prest2.setInt(1, jPlayer.getUserId());
} catch (SQLException e) { prest2.execute();
e.printStackTrace(); } catch (SQLException e) {
} finally { e.printStackTrace();
close(prest); } 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(); res = prest.executeQuery();
if (res.next()) { if (res.next()) {
player.getPointsData().setDbId(res.getInt("id"));
player.getPointsData().setPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn())); player.getPointsData().setPoints(res.getDouble(PointsTableFields.currentpoints.getCollumn()));
player.getPointsData().setTotalPoints(res.getDouble(PointsTableFields.totalpoints.getCollumn())); player.getPointsData().setTotalPoints(res.getDouble(PointsTableFields.totalpoints.getCollumn()));
} }

View File

@ -27,7 +27,7 @@ public class PointsData {
PlayerPoints pi = Jobs.getPlayerManager().getJobsPlayer(uuid).getPointsData(); PlayerPoints pi = Jobs.getPlayerManager().getJobsPlayer(uuid).getPointsData();
pi.setPoints(points.getCurrentPoints()); pi.setPoints(points.getCurrentPoints());
pi.setTotalPoints(points.getTotalPoints()); pi.setTotalPoints(points.getTotalPoints());
pi.setNewEntry(points.isNewEntry()); pi.setDbId(points.getDbId());
} }
public void addPoints(UUID uuid, Double points) { public void addPoints(UUID uuid, Double points) {