Repository navigation
Add lockGuild and the death-guild index for character-owned locks #161
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
61 changes: 61 additions & 0 deletions
61
src/main/java/net/tfminecraft/simplefactions/identity/death/LockGuildIndex.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,61 @@ | ||
| package net.tfminecraft.simplefactions.identity.death; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonPrimitive; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| /** Character ID to the guild incarnation a dead owner's GUILD and FACTION locks follow. */ | ||
| final class LockGuildIndex { | ||
| private final Map<UUID, UUID> entries = new HashMap<>(); | ||
|
|
||
| /** Only each character's most recent journal counts; two journals at the same moment deny. */ | ||
| void rebuild(List<DeathJournal> journals) { | ||
| Map<UUID, List<DeathJournal>> latest = new HashMap<>(); | ||
| for (DeathJournal journal : journals) { | ||
| List<DeathJournal> winners = latest.get(journal.characterId); | ||
| if (winners == null || journal.at > winners.getFirst().at) { | ||
| latest.put(journal.characterId, new ArrayList<>(List.of(journal))); | ||
| } else if (journal.at == winners.getFirst().at) { | ||
| winners.add(journal); | ||
| } | ||
| } | ||
| entries.clear(); | ||
| latest.forEach((character, winners) -> { | ||
| UUID incarnation = winners.size() == 1 ? incarnation(winners.getFirst()) : null; | ||
| if (incarnation != null) entries.put(character, incarnation); | ||
| }); | ||
| } | ||
|
|
||
| /** A guildless death removes the entry an earlier death left. */ | ||
| void capture(DeathJournal journal) { | ||
| if (journal.lockGuildIncarnation == null) entries.remove(journal.characterId); | ||
| else entries.put(journal.characterId, journal.lockGuildIncarnation); | ||
| } | ||
|
|
||
| Optional<UUID> get(UUID characterId) { | ||
| return Optional.ofNullable(entries.get(characterId)); | ||
| } | ||
|
|
||
| /** | ||
| * Journals written before the field existed use the first guild listing the | ||
| * character in the rights snapshots, in their order, without civil-war mapping. | ||
| */ | ||
| static UUID incarnation(DeathJournal journal) { | ||
| if (journal.lockGuildIncarnation != null) return journal.lockGuildIncarnation; | ||
| JsonPrimitive key = new JsonPrimitive(journal.characterId.toString()); | ||
| for (DeathJournal.Step step : journal.steps) { | ||
| if (!step.kind.equals("rights") || !step.plan.has("guilds")) continue; | ||
| for (JsonElement guild : step.plan.getAsJsonArray("guilds")) { | ||
| if (guild.getAsJsonObject().getAsJsonArray("members").contains(key)) { | ||
| return UUID.fromString(guild.getAsJsonObject().get("incarnation").getAsString()); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/test/java/net/tfminecraft/simplefactions/identity/death/LockGuildIndexTest.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,94 @@ | ||
| package net.tfminecraft.simplefactions.identity.death; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonObject; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class LockGuildIndexTest { | ||
| final UUID character = UUID.randomUUID(); | ||
| final String key = character.toString(); | ||
| final LockGuildIndex index = new LockGuildIndex(); | ||
|
|
||
| DeathJournal journal(long at, JsonObject... rights) { | ||
| DeathJournal journal = new DeathJournal(); | ||
| journal.deathId = UUID.randomUUID(); | ||
| journal.ownerId = UUID.randomUUID(); | ||
| journal.characterId = character; | ||
| journal.at = at; | ||
| for (JsonObject plan : rights) journal.steps.add(new DeathJournal.Step("rights:" + UUID.randomUUID(), "rights", plan)); | ||
| journal.steps.add(new DeathJournal.Step("online", "online", new JsonObject())); | ||
| return journal; | ||
| } | ||
|
|
||
| static JsonObject guild(UUID incarnation, String... members) { | ||
| JsonObject guild = new JsonObject(); | ||
| guild.addProperty("incarnation", incarnation.toString()); | ||
| JsonArray list = new JsonArray(); | ||
| for (String member : members) list.add(member); | ||
| guild.add("members", list); | ||
| guild.add("invites", new JsonArray()); | ||
| return guild; | ||
| } | ||
|
|
||
| static JsonObject realm(JsonObject... guilds) { | ||
| JsonObject realm = new JsonObject(); | ||
| realm.addProperty("realm", UUID.randomUUID().toString()); | ||
| JsonArray list = new JsonArray(); | ||
| for (JsonObject guild : guilds) list.add(guild); | ||
| realm.add("guilds", list); | ||
| return realm; | ||
| } | ||
|
|
||
| @Test void olderJournalsUseTheFirstGuildListingTheCharacterInSnapshotOrder() { | ||
| UUID invitedOnly = UUID.randomUUID(), first = UUID.randomUUID(), later = UUID.randomUUID(); | ||
| JsonObject invited = guild(invitedOnly, "someone"); | ||
| invited.getAsJsonArray("invites").add(key); | ||
| DeathJournal journal = journal(1, new JsonObject(), | ||
| realm(invited, guild(first, "someone", key)), realm(guild(later, key))); | ||
| index.rebuild(List.of(journal)); | ||
| assertEquals(Optional.of(first), index.get(character)); | ||
| } | ||
|
|
||
| @Test void recordedIncarnationWinsOverTheSnapshot() { | ||
| UUID recorded = UUID.randomUUID(); | ||
| DeathJournal journal = journal(1, realm(guild(UUID.randomUUID(), key))); | ||
| journal.lockGuildIncarnation = recorded; | ||
| index.rebuild(List.of(journal)); | ||
| assertEquals(Optional.of(recorded), index.get(character)); | ||
| } | ||
|
|
||
| @Test void onlyTheMostRecentJournalPerCharacterCounts() { | ||
| UUID guild = UUID.randomUUID(); | ||
| DeathJournal inGuild = journal(1, realm(guild(guild, key))); | ||
| DeathJournal guildless = journal(2, realm(guild(UUID.randomUUID(), "someone"))); | ||
| index.rebuild(List.of(guildless, inGuild)); | ||
| assertTrue(index.get(character).isEmpty(), "a later guildless death leaves no entry"); | ||
| index.rebuild(List.of(journal(3, realm(guild(guild, key))), guildless)); | ||
| assertEquals(Optional.of(guild), index.get(character)); | ||
| } | ||
|
|
||
| @Test void journalsAtTheSameMomentDeny() { | ||
| UUID guild = UUID.randomUUID(); | ||
| index.rebuild(List.of(journal(1, realm(guild(UUID.randomUUID(), key))), | ||
| journal(5, realm(guild(guild, key))), journal(5, realm(guild(guild, key))))); | ||
| assertTrue(index.get(character).isEmpty()); | ||
| } | ||
|
|
||
| @Test void rebuildReplacesEarlierEntriesAndCaptureKeepsThemCurrent() { | ||
| UUID first = UUID.randomUUID(), second = UUID.randomUUID(); | ||
| index.rebuild(List.of(journal(1, realm(guild(first, key))))); | ||
| index.rebuild(List.of()); | ||
| assertTrue(index.get(character).isEmpty()); | ||
| DeathJournal captured = journal(2); | ||
| captured.lockGuildIncarnation = second; | ||
| index.capture(captured); | ||
| assertEquals(Optional.of(second), index.get(character)); | ||
| index.capture(journal(3)); | ||
| assertTrue(index.get(character).isEmpty(), "a guildless capture deletes the entry"); | ||
| } | ||
| } |
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.