Skip to content

Commit

Permalink
Flag assignment saving and loading works!
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Aug 22, 2024
1 parent c694350 commit b66efec
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 211 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ tasks {
}

register<Delete>("cleanTests") {
delete(fileTree(mainDir).include("*.tmp.sqlite3"))
delete(fileTree(mainDir).include("tmp/*.tmp.sqlite3"))
}

test {
Expand All @@ -131,7 +131,7 @@ tasks {
"junit.jupiter.testinstance.lifecycle.default" to "per_class"
)

finalizedBy("cleanTests")
// finalizedBy("cleanTests")
}

clean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS player_data (
last_online_time INTEGER NOT NULL,
alerts_enabled INTEGER NOT NULL,
extra_max_claims INTEGER NOT NULL
) STRICT
) WITHOUT ROWID, STRICT
""");

// Chunk data table
Expand Down Expand Up @@ -55,18 +55,12 @@ CREATE TABLE IF NOT EXISTS permission_flags (
flag_name TEXT NOT NULL,
allow_deny INTEGER NOT NULL,
PRIMARY KEY(player_uuid, other_player_uuid, chunk_id, flag_name)
PRIMARY KEY(player_uuid, other_player_uuid, chunk_id, flag_name),
FOREIGN KEY(player_uuid)
REFERENCES player_data(player_uuid)
ON DELETE CASCADE,
FOREIGN KEY(other_player_uuid)
REFERENCES player_data(player_uuid)
ON DELETE CASCADE,
FOREIGN KEY(chunk_id)
REFERENCES chunk_data(chunk_id)
ON DELETE CASCADE
) STRICT
) WITHOUT ROWID, STRICT
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -253,7 +254,9 @@ public void setPermissionFlags(
newFlags.values().stream()
.map(
ignored ->
"(?,?,"
"(?,"
+ (accessor != null ? "?" : "''")
+ ","
+ (chunk != null ? SELECT_CHUNK_ID_SQL : "-1")
+ ",?,?)")
.collect(Collectors.joining(","));
Expand Down Expand Up @@ -282,15 +285,16 @@ ON CONFLICT (
int param = 1;
for (Map.Entry<String, Boolean> entry : newFlags.entrySet()) {
statement.setString(param++, owner.toString());
statement.setString(
param++, accessor == null ? "" : accessor.toString());
if (accessor != null) {
statement.setString(param++, accessor.toString());
}
if (chunk != null) {
param = setChunkPosParams(statement, param, chunk);
}
statement.setString(param++, entry.getKey());
statement.setBoolean(param++, entry.getValue());
}
statement.execute();
statement.executeUpdate();
return null;
}
});
Expand All @@ -304,33 +308,33 @@ public void clearPermissionFlags(
String clauses =
Arrays.stream(flagNames)
.map(ignored -> "flag_name=?")
.collect(Collectors.joining(" OR "));
.collect(Collectors.joining(" OR ", "(", ")"));
String where =
"WHERE player_uuid="
"WHERE player_uuid=? AND other_player_uuid="
+ (accessor == null ? "''" : "?")
+ " AND chunk_id="
+ (chunk == null ? "-1" : SELECT_CHUNK_ID_SQL)
+ " AND ("
+ clauses
+ ")";
+ " AND "
+ clauses;

System.out.println(where);

SqlClosure.sqlExecute(
connection -> {
try (PreparedStatement statement =
connection.prepareStatement(
chunkIdQuery("DELETE FROM permission_flags " + where))) {
connection.prepareStatement("DELETE FROM permission_flags " + where)) {
int param = 1;
statement.setString(param++, owner.toString());
if (accessor != null) {
statement.setString(param, accessor.toString());
statement.setString(param++, accessor.toString());
}
if (chunk != null) {
param = setChunkPosParams(statement, param, chunk);
}
for (String flagName : flagNames) {
statement.setString(param++, flagName);
}
statement.execute();
statement.executeUpdate();
return null;
}
});
Expand All @@ -352,21 +356,21 @@ public List<FullPlayerData> getAllPlayers() {
try (PreparedStatement statement =
connection.prepareStatement(
"""
SELECT player_uuid, other_player_uuid, flag_name, allow_deny, chunk_id
FROM permission_flags
WHERE chunk_id=-1
""")) {
SELECT *
FROM permission_flags
WHERE chunk_id=-1
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {

UUID playerUuid = UUID.fromString(resultSet.getString("player_uuid"));
String otherPlyUuid = resultSet.getString("other_player_uuid");
String flagName = resultSet.getString("flag_name");
boolean allowDeny = resultSet.getBoolean("allow_deny");

FullPlayerData thisPly = playerData.get(playerUuid);
if (thisPly == null) {
Utils.err("Failed to load player %s for permission", playerUuid);
continue;
throw new RuntimeException("Failed to load player " + playerUuid + " for permission " + flagName);
}

UUID otherPly = null;
Expand All @@ -375,7 +379,7 @@ public List<FullPlayerData> getAllPlayers() {
} catch (Exception ignored) {
}

if (otherPlyUuid != null) {
if (otherPly != null) {
thisPly.playerFlags
.computeIfAbsent(otherPly, ignored -> new HashMap<>())
.put(flagName, allowDeny);
Expand All @@ -399,8 +403,7 @@ public Collection<DataChunk> getAllChunks() {
try (PreparedStatement statement =
connection.prepareStatement(
"""
SELECT chunk_world, chunk_x, chunk_z, owner_uuid
FROM chunk_data
SELECT * FROM chunk_data
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Expand All @@ -418,13 +421,11 @@ public Collection<DataChunk> getAllChunks() {
try (PreparedStatement statement =
connection.prepareStatement(
"""
SELECT player_uuid, owner_uuid, other_player_uuid,
chunk_world, chunk_x, chunk_z,
flag_name, allow_deny
FROM permission_flags
LEFT JOIN chunk_data ON permission_flags.chunk_id=chunk_data.chunk_id
WHERE permission_flags.chunk_id!=-1
""")) {
SELECT * FROM permission_flags
LEFT JOIN chunk_data
ON permission_flags.chunk_id=chunk_data.chunk_id
WHERE permission_flags.chunk_id!=-1
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String playerUuid = resultSet.getString("player_uuid");
Expand All @@ -439,13 +440,9 @@ public Collection<DataChunk> getAllChunks() {
String chunkWorld = resultSet.getString("chunk_world");
int chunkX = resultSet.getInt("chunk_x");
int chunkZ = resultSet.getInt("chunk_z");
ChunkPos chunkPos =
ignoredChunkOwnerUuid != null && chunkWorld != null
? new ChunkPos(chunkWorld, chunkX, chunkZ)
: null;
ChunkPos chunkPos = new ChunkPos(chunkWorld, chunkX, chunkZ);
// May be empty!
String otherPlyUuid = resultSet.getString("other_player_uuid");
// Never null
String flagName = resultSet.getString("flag_name");
boolean allowDeny = resultSet.getBoolean("allow_deny");

Expand Down
Loading

0 comments on commit b66efec

Please sign in to comment.