Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update database pixel info and user pixel counts simultaneously #667

Draft
wants to merge 1 commit into
base: old
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/space/pxls/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,9 @@ public static void putPixel(int x, int y, int color, User user, boolean mod_acti
virginmap[x + y * width] = (byte) 0x00;
pixelLogger.log(Level.INFO, String.format("%s\t%d\t%d\t%d\t%s", userName, x, y, color, action));
if (updateDatabase) {
database.placePixel(x, y, color, user, mod_action);
if (!mod_action) {
user.increasePixelCounts();
var counts = database.placePixelAndUpdateCounts(x, y, color, user, mod_action);
if (counts != null) {
user.setPixelCounts(counts);
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/space/pxls/data/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ public Database() {
* @param color The pixel's color.
* @param who Who placed the pixel.
* @param mod_action Whether or not the pixel is a mod action.
* @return An instance of {@Link DBUserPixelCounts}.
*/
public Integer placePixel(int x, int y, int color, User who, boolean mod_action) {
public DBUserPixelCounts placePixelAndUpdateCounts(int x, int y, int color, User who, boolean mod_action) {
return jdbi.withHandle(handle -> {
handle.begin();
Optional<Integer> second_id = handle.select("SELECT id FROM pixels AS pp WHERE pp.x = :x AND pp.y = :y AND pp.most_recent ORDER BY id DESC LIMIT 1")
.bind("x", x)
.bind("y", y)
Expand All @@ -296,7 +298,25 @@ public Integer placePixel(int x, int y, int color, User who, boolean mod_action)
.bind("second_id", second_id)
.bind("mod", mod_action)
.execute();
return rowID;

DBUserPixelCounts counts = null;
if (who != null && !mod_action) {
int increaseCurrent = App.getConfig().getBoolean("pixelCounts.countTowardsCurrent") ? 1 : 0;
int increaseAllTime = App.getConfig().getBoolean("pixelCounts.countTowardsAlltime") ? 1 : 0;

if (increaseCurrent + increaseAllTime > 0) {
counts = handle.createQuery("UPDATE users SET pixel_count = pixel_count + :current_amount, pixel_count_alltime = pixel_count_alltime + :alltime_amount WHERE id = :who RETURNING pixel_count, pixel_count_alltime")
.bind("who", whoID)
.bind("current_amount", increaseCurrent)
.bind("alltime_amount", increaseAllTime)
.map(new DBUserPixelCounts.Mapper())
.findFirst()
.orElse(null);
}
}

handle.commit();
return counts;
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/space/pxls/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ public void tickStack(boolean sendRes) {
}
}

public void setPixelCounts(DBUserPixelCounts newCounts) {
this.pixelCount = newCounts.pixelCount;
this.pixelCountAllTime = newCounts.pixelCountAllTime;
}

private void modifyPixelCounts(int amount) {
boolean increaseCurrent = App.getConfig().getBoolean("pixelCounts.countTowardsCurrent");
boolean increaseAllTime = App.getConfig().getBoolean("pixelCounts.countTowardsAlltime");
Expand All @@ -603,8 +608,7 @@ private void modifyPixelCounts(int amount) {
}

DBUserPixelCounts newCounts = App.getDatabase().modifyPixelCounts(this.id, amount, increaseCurrent, increaseAllTime);
this.pixelCount = newCounts.pixelCount;
this.pixelCountAllTime = newCounts.pixelCountAllTime;
this.setPixelCounts(newCounts);
}

public void increasePixelCounts() {
Expand Down
Loading