Repository navigation
feat: bind game money to character and guild ledgers #42
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f9dd73c
feat: bind game money to character and guild ledgers
ryanbarlow97 4d23148
fix: persist payment intent directory entries before posting
ryanbarlow97 c98222c
fix: retry guild table migration after a failed incarnation lookup
ryanbarlow97 7da9e48
fix: do not quarantine tables for payout legs that move no money
ryanbarlow97 c5b8f97
fix: stop re-reading unloadable table files on deferred passes
ryanbarlow97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/net/tfminecraft/games/guild/GuildTableMigration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package net.tfminecraft.games.guild; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.UUID; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParser; | ||
| import net.tfminecraft.games.persistence.DurableFiles; | ||
|
|
||
| /** Stamp an old table's guild incarnation once; a deleted guild never adopts a later namesake. */ | ||
| public final class GuildTableMigration { | ||
| private GuildTableMigration() {} | ||
|
|
||
| public static boolean prepare(Path tableFile, Path migrationFolder) throws IOException { | ||
| JsonObject data = JsonParser.parseString(Files.readString(tableFile)).getAsJsonObject(); | ||
| if (data.has("ownerGuildMigrated") && data.get("ownerGuildMigrated").getAsBoolean()) return true; | ||
| if (!data.has("ownerGuildId") || data.get("ownerGuildId").isJsonNull() | ||
| || data.get("ownerGuildId").getAsString().isBlank()) return true; | ||
| if (!GuildTables.ready()) return false; | ||
| Path backup = migrationFolder.resolve("backup").resolve(tableFile.getFileName()); | ||
| Path manifest = migrationFolder.resolve(tableFile.getFileName()); | ||
| JsonObject migrated; | ||
| if (Files.exists(manifest)) { | ||
| // A complete plan is written before the table. Replay the exact incarnation, never resolve again. | ||
| migrated = JsonParser.parseString(Files.readString(manifest)).getAsJsonObject(); | ||
| } else { | ||
| GuildTables.Lookup lookup = GuildTables.resolveIncarnation(data.get("ownerGuildId").getAsString()); | ||
| // A failed lookup is not a deleted guild: stamp nothing and let a later pass retry. | ||
| if (lookup.failed()) return false; | ||
| if (!Files.exists(backup)) DurableFiles.write(backup, Files.readAllBytes(tableFile)); | ||
| migrated = data.deepCopy(); | ||
| UUID incarnation = lookup.incarnation(); | ||
| if (incarnation != null) migrated.addProperty("ownerGuildIncarnation", incarnation.toString()); | ||
| migrated.addProperty("ownerGuildMigrated", true); | ||
| DurableFiles.write(manifest, migrated.toString()); | ||
| } | ||
| DurableFiles.write(tableFile, migrated.toString()); | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/net/tfminecraft/games/persistence/DurableFiles.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package net.tfminecraft.games.persistence; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.nio.file.StandardOpenOption; | ||
|
|
||
| /** Publish whole, flushed files before money or inventory changes. */ | ||
| public final class DurableFiles { | ||
| private DurableFiles() {} | ||
|
|
||
| public static void write(Path target, String text) throws IOException { | ||
| write(target, text.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| public static void write(Path target, byte[] bytes) throws IOException { | ||
| target = target.toAbsolutePath(); | ||
| Files.createDirectories(target.getParent()); | ||
| Path temp = Files.createTempFile(target.getParent(), ".pending-", ".tmp"); | ||
| try { | ||
| try (FileChannel channel = FileChannel.open(temp, StandardOpenOption.WRITE)) { | ||
| ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||
| while (buffer.hasRemaining()) channel.write(buffer); | ||
| channel.force(true); | ||
| } | ||
| Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); | ||
| // Parent entries also need flushing when an intent creates its first directory. | ||
| for (Path directory = target.getParent(); directory != null; directory = directory.getParent()) sync(directory); | ||
| } finally { | ||
| Files.deleteIfExists(temp); | ||
| } | ||
| } | ||
|
|
||
| public static void delete(Path target) throws IOException { | ||
| Files.deleteIfExists(target); | ||
| sync(target.getParent()); | ||
| } | ||
|
|
||
| private static void sync(Path directory) throws IOException { | ||
| try (FileChannel channel = FileChannel.open(directory, StandardOpenOption.READ)) { | ||
| channel.force(true); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.