Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

// ==============================================
Expand All @@ -37,32 +39,40 @@ public SurgeryCompletionHandler(JavaPlugin plugin, SurgeryStateManager stateMana
// Checks if all success conditions are met
// ==============================================
public boolean isSurgerySuccessful(UUID playerId) {
// 1. The ailment has been treated and dressed
if (!stateManager.isCured(playerId)) { return false; }

// 2. Pulse must be "Strong" (LIME)
if (!stateManager.getPulse(playerId).equals("Strong")) { return false; }

// 3. Status must be "Unconscious" (LIME)
if (!stateManager.getStatus(playerId).equals("Unconscious")) { return false; }
// The ailment has been treated and dressed, and nothing else is left to do
return stateManager.isCured(playerId) && remainingSteps(playerId).isEmpty();
}

// 4. Temperature must be at or below the configured success threshold (LIME)
// ==============================================
// What still stands between the surgeon and success, besides the dressing,
// in the order the menu shows it
// ==============================================
public List<String> remainingSteps(UUID playerId) {
List<String> steps = new ArrayList<>();
if (stateManager.getBrokenBones(playerId) != 0 || stateManager.getShatteredBones(playerId) != 0) {
steps.add("set the bones");
}
if (!stateManager.getPulse(playerId).equals("Strong")) {
steps.add("strengthen the pulse");
}
String status = stateManager.getStatus(playerId);
if (!status.equals("Unconscious")) {
steps.add(status.equals("Collapsed") ? "revive the patient" : "keep the patient under");
}
double successThreshold = plugin.getConfig().getDouble("temperature.success-threshold", 100.0);
if (stateManager.getTemperature(playerId) > successThreshold) { return false; }

// 5. Operation site must be "Clean" (LIME)
if (!stateManager.getOperationSite(playerId).equals("Clean")) { return false; }

// 6. Incisions must be 0 (LIME)
if (stateManager.getIncisions(playerId) != 0) { return false; }

// 7. All bones must be set
if (stateManager.getBrokenBones(playerId) != 0 || stateManager.getShatteredBones(playerId) != 0) { return false; }

// 8. No bleeding
if (stateManager.isBleeding(playerId)) { return false; }

return true;
if (stateManager.getTemperature(playerId) > successThreshold) {
steps.add("bring the fever down");
}
if (!stateManager.getOperationSite(playerId).equals("Clean")) {
steps.add("clean the operation site");
}
if (stateManager.getIncisions(playerId) != 0) {
steps.add("close the incisions");
}
if (stateManager.isBleeding(playerId)) {
steps.add("stop the bleeding");
}
return steps;
}

public void handleSuccess(Player surgeon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ private void updateMenu(Player player, SurgeryTool tool) {
if (stateManager.isBleeding(playerId)) {
uiUpdater.sendNumberedMessage(player, uiUpdater.getMessage("bleeding-warning"));
}
// Bleeding has no block of its own; the incisions block shows it
uiUpdater.updateIncisionBlock(menu, playerId, stateManager.getIncisions(playerId));

// ==============================================
// Update skill fail block
Expand Down Expand Up @@ -455,6 +457,8 @@ private String handleArteryForceps(Player player, Inventory menu, UUID playerId,
}
if (stateManager.getIncisions(playerId) > 0 && stateManager.isBleeding(playerId)) {
stateManager.setBleeding(playerId, false);
// Clamping holds a haemorrhage back as long as a sponge would
stateManager.setMovesSinceLastSponge(playerId, 0);
mechanicsManager.updateDynamicTools(player, menu, playerId);
}
return "";
Expand All @@ -474,7 +478,8 @@ private String handleDressing(Player player, Inventory menu, UUID playerId, bool
// Don't show incomplete message if surgery is already successful
if (!completionHandler.isSurgerySuccessful(playerId)) {
uiUpdater.sendNumberedMessage(player, uiUpdater.getMessage("condition-treated-incomplete"));
uiUpdater.sendNumberedMessage(player, uiUpdater.getMessage("check-remaining"));
uiUpdater.sendNumberedMessage(player, uiUpdater.getMessage("remaining-steps", "&eStill to do: %steps%")
.replace("%steps%", String.join(", ", completionHandler.remainingSteps(playerId))));
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,12 @@ private boolean isPatientPresent(Player surgeon) {
// Runs the procedure's complications
// ==============================================
private void runComplications(Player player, Inventory menu, UUID playerId, Procedure procedure) {
if (procedure.has(Complication.HAEMORRHAGE)) {
// A dressed wound no longer haemorrhages, so the surgery can be finished
if (procedure.has(Complication.HAEMORRHAGE) && !stateManager.isCured(playerId)) {
// Forces bleeding somewhere between min and max moves since the last
// sponge: 50/50 each move once min is reached, guaranteed at max
int min = plugin.getConfig().getInt("complications.haemorrhage.bleeding-interval-min", 3);
int max = plugin.getConfig().getInt("complications.haemorrhage.bleeding-interval-max", 4);
// sponge or forceps: 50/50 each move once min is reached, guaranteed at max
int min = plugin.getConfig().getInt("complications.haemorrhage.bleeding-interval-min", 6);
int max = plugin.getConfig().getInt("complications.haemorrhage.bleeding-interval-max", 8);
int movesSinceSponge = stateManager.getMovesSinceLastSponge(playerId);
if (movesSinceSponge >= max || (movesSinceSponge >= min && ThreadLocalRandom.current().nextBoolean())) {
stateManager.setBleeding(playerId, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ public ItemStack createInfoBlock(Material material, String name, java.util.List<
// Keep the existing legacy text representation, formatting, and exact-string comparisons.
@SuppressWarnings("deprecation")
public void updateIncisionBlock(Inventory menu, UUID playerId, int incisions) {
Material incisionColor = SurgeryConstants.getIncisionColor(incisions);
ItemStack incisionBlock = createInfoBlock(incisionColor, ChatColor.GOLD + "Incisions", ChatColor.GRAY + String.valueOf(incisions));
menu.setItem(SurgeryConstants.SLOT_INCISIONS, incisionBlock);
// A bleeding patient turns the block red, since bleeding blocks success too
boolean bleeding = stateManager.isBleeding(playerId);
Material incisionColor = bleeding ? Material.RED_CONCRETE : SurgeryConstants.getIncisionColor(incisions);
java.util.List<String> lore = new java.util.ArrayList<>();
lore.add(ChatColor.GRAY + String.valueOf(incisions));
if (bleeding) {
lore.add(getMessage("incisions-bleeding", "&cThe patient is bleeding"));
}
menu.setItem(SurgeryConstants.SLOT_INCISIONS, createInfoBlock(incisionColor, ChatColor.GOLD + "Incisions", lore));
}

// ==============================================
Expand Down Expand Up @@ -145,7 +151,9 @@ public void updateDiagnosisBlock(Inventory menu, UUID playerId) {
lore.add(ChatColor.GRAY + "Complaint: " + ChatColor.WHITE + ailment);
lore.add(ChatColor.GRAY + "Examine the patient with the stethoscope.");
} else {
color = stateManager.isCured(playerId) ? Material.LIME_CONCRETE : Material.YELLOW_CONCRETE;
// Stays yellow after the dressing: success closes the menu, so green
// here only ever meant "dressed" and read as "done"
color = Material.YELLOW_CONCRETE;
lore.add(ChatColor.GRAY + "The patient suffers from " + ChatColor.WHITE + procedure.name());
lore.add(ChatColor.GRAY + "Ailment: " + ChatColor.WHITE + ailment);
lore.add(ChatColor.GRAY + "Incisions needed: " + ChatColor.WHITE + procedure.requiredIncisions());
Expand All @@ -155,6 +163,7 @@ public void updateDiagnosisBlock(Inventory menu, UUID playerId) {
}
if (stateManager.isCured(playerId)) {
lore.add(ChatColor.GREEN + "Treated and dressed");
lore.add(ChatColor.YELLOW + "Finish the surgery to close it");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ anesthetic:

complications:
haemorrhage:
bleeding-interval-min: 3 # Forces bleeding every 3-4 moves since the last sponge
bleeding-interval-max: 4
bleeding-interval-min: 6 # Forces bleeding every 6-8 moves since the last sponge
bleeding-interval-max: 8 # or forceps, until the wound is dressed
shock:
collapse-chance: 0.20 # Chance per move to collapse while unconscious
sepsis:
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ temperature-reduced: "&aThe tincture brings the fever down."
operation-clean: "&aCarbolic acid has cleaned the site. The fever will not rise."
vision-cleared: "&aVision cleared! Your next move will be steadier."
condition-treated-incomplete: "&eThe ailment is dressed, but the surgery is not yet complete."
check-remaining: "&eCheck: bones, incisions, bleeding, temperature, status"
remaining-steps: "&eStill to do: %steps%"
incisions-bleeding: "&cThe patient is bleeding"
dressing-ready: "&aThe injury is set - apply the linen dressing!"

# Discovery Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ void successRequiresEveryClinicalCondition() {
assertTrue(completion.isSurgerySuccessful(surgeonId));
}

@Test
void remainingStepsListEveryUnmetConditionInMenuOrder() {
state.setStatus(surgeonId, "Unconscious");
state.setOperationSite(surgeonId, "Clean");
assertEquals(List.of(), completion.remainingSteps(surgeonId));
state.setBrokenBones(surgeonId, 1);
state.setPulse(surgeonId, "Weak");
state.setStatus(surgeonId, "Coming to");
state.setTemperature(surgeonId, 101);
state.setOperationSite(surgeonId, "Unclean");
state.setIncisions(surgeonId, 2);
state.setBleeding(surgeonId, true);
assertEquals(List.of("set the bones", "strengthen the pulse", "keep the patient under",
"bring the fever down", "clean the operation site", "close the incisions", "stop the bleeding"),
completion.remainingSteps(surgeonId));
state.setBrokenBones(surgeonId, 0);
state.setShatteredBones(surgeonId, 1);
assertEquals("set the bones", completion.remainingSteps(surgeonId).get(0));
state.setStatus(surgeonId, "Collapsed");
assertTrue(completion.remainingSteps(surgeonId).contains("revive the patient"));
}

@Test
void successfulCureRunsRewardOnceAndClosesOnNextTick() {
injuries.when(() -> HealingInjuries.cure(patient, "leg")).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ void scalpelCreatesDirtyIncisionsWeakensPulseAndRefreshesTools() {
assertEquals(1, state.getIncisions(id));
assertEquals("Unclean", state.getOperationSite(id));
assertEquals("Steady", state.getPulse(id));
verify(ui).updateIncisionBlock(menu, id, 1);
// Once by the scalpel, once by the end-of-move bleeding refresh
verify(ui, times(2)).updateIncisionBlock(menu, id, 1);
verify(mechanics).handleBoneReveal(player, menu, id, 1);
verify(mechanics).updateDynamicTools(player, menu, id);
verify(mechanics).checkForDressing(player, menu, id, 1);
Expand Down Expand Up @@ -406,8 +407,11 @@ void arteryForcepsRequireAnOpenBleedingWound() {
click(SurgeryTool.ARTERY_FORCEPS);
assertTrue(state.isBleeding(id));
state.setIncisions(id, 1);
state.setMovesSinceLastSponge(id, 5);
click(SurgeryTool.ARTERY_FORCEPS);
assertFalse(state.isBleeding(id));
// Clamping restarts the haemorrhage interval like a sponge
assertEquals(0, state.getMovesSinceLastSponge(id));
click(SurgeryTool.ARTERY_FORCEPS);
verify(mechanics, times(1)).updateDynamicTools(player, menu, id);
}
Expand All @@ -419,7 +423,7 @@ void dressingTreatsTheConditionAndOnlyCompletesWhenAllChecksPass() {
assertTrue(state.isCured(id));
assertNull(menu.getItem(SurgeryTool.DRESSING.getSlot()));
verify(ui).sendNumberedMessage(player, "condition-treated-incomplete");
verify(ui).sendNumberedMessage(player, "check-remaining");
verify(ui).sendNumberedMessage(player, "remaining-steps");
verify(completion, never()).handleSuccess(player);
when(completion.isSurgerySuccessful(id)).thenReturn(true);
click(SurgeryTool.DRESSING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,19 @@ void complicationsWaitForExaminationThenHaemorrhageAndShockAddTools() {
verify(ui).sendNumberedMessage(player, "shock-collapse");
}

@Test
void dressedWoundNoLongerHaemorrhages() {
procedure(2, 0, 0, Complication.HAEMORRHAGE);
config.set("complications.haemorrhage.bleeding-interval-min", 1);
config.set("complications.haemorrhage.bleeding-interval-max", 1);
state.setExamined(id, true);
state.setCured(id, true);
move();
move();
assertFalse(state.isBleeding(id));
verify(ui, never()).sendNumberedMessage(player, "haemorrhage");
}

@Test
void haemorrhageDoesNotStartBeforeMinimumInterval() {
procedure(2, 0, 0, Complication.HAEMORRHAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,22 @@ void messagesAndInfoBlocksShowTheCurrentState() throws Exception {
assertTrue(menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getItemMeta().getLore().stream().anyMatch(s -> s.contains("Shattered Bones")));
state.setCured(id, true);
ui.updateDiagnosisBlock(menu, id);
assertEquals(Material.LIME_CONCRETE, menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getType());
assertEquals(Material.YELLOW_CONCRETE, menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getType());
assertTrue(menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getItemMeta().getLore().contains("§aTreated and dressed"));
assertTrue(menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getItemMeta().getLore().contains("§eFinish the surgery to close it"));
state.setAilment(id, "eye", "Eye", new Procedure("Eye repair", 1, 0, 0, Set.of()));
ui.updateDiagnosisBlock(menu, id);
assertFalse(menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getItemMeta().getLore().stream().anyMatch(s -> s.contains("Bones")));
state.setAilment(id, "unknown", "Unknown", null);
ui.updateDiagnosisBlock(menu, id);
assertEquals(Material.RED_CONCRETE, menu.getItem(SurgeryConstants.SLOT_DIAGNOSIS).getType());
state.setBleeding(id, true);
ui.updateIncisionBlock(menu, id, 0);
assertEquals(Material.RED_CONCRETE, menu.getItem(SurgeryConstants.SLOT_INCISIONS).getType());
assertEquals(List.of("§70", "§cThe patient is bleeding"), menu.getItem(SurgeryConstants.SLOT_INCISIONS).getItemMeta().getLore());
state.setBleeding(id, false);
ui.updateIncisionBlock(menu, id, 2);
assertEquals(List.of("§72"), menu.getItem(SurgeryConstants.SLOT_INCISIONS).getItemMeta().getLore());
ui.updateTemperatureBlock(menu, id, 107);
ui.updateOperationSiteBlock(menu, id, "Clean");
ui.updateStatusBlock(menu, id, "Unconscious");
Expand Down