N3/P5: a fight that knows whose turn it is

A solo fight is a conversation: the player types, the engine answers, and
nothing happens in between. A party fight is a queue, and three things follow.

Turn ownership. Only the seat on the clock may act, so beginCombatTurn resolves
the sender's seat and refuses the rest before anyone spends a slot or burns an
item. A fight lock, because three members typing !attack at once took three
different user locks and the check would have passed for all three: it takes the
fight's lock (keyed on seat 0) then the member's own, always in that order, and
a solo fight -- whose owner is the sender, and sync.Mutex is not reentrant --
takes exactly the one lock it always took. And a turn deadline, because one
member who wanders off must not freeze the other two for the hour it takes the
session reaper to wake up.

The deadline is three minutes, not the plan's sixty seconds. The sweep rides the
existing one-minute ticker, so any deadline really fires in [d, d+1m); and
expeditions here run for days, so the asymmetry favours patience over robbing
someone of their boss turn while they read the room on their phone. A lapse
latches that seat onto the auto-picker for the rest of the fight, so an absent
member costs the party one wait rather than one per round. Typing anything hands
the wheel back. Solo is never swept.

Three seat-0 leaks fixed on the way past, all of which would have surfaced as
the leader quietly doing everyone's business:

  - mid-fight buffs folded into the session's embedded ActorStatuses, so a
    member casting Shield on themselves would have armoured the leader;
  - pickAutoCombatAction read sess.PlayerHP and Statuses.ConcentrationDmg, so
    playing an away member's turn would have healed the wrong person and
    re-armed the wrong aura;
  - runCombatRound rested on any player_turn, and a downed seat still holds one
    -- the round would have come to rest on a corpse and waited for a dead
    member to type !attack. settleCombatSession drains it. beginCombatTurn
    settles before reading the clock, which also fixes a latent solo bug: a
    fight interrupted mid enemy_turn resumed parked there and silently ate the
    player's next !attack.

The narration turned out to be written in the second person -- "You score 9
damage", "A hit gets through your guard" -- so swapping a name per seat would
have told three people they each landed the same blow. A round is rendered once
per reader instead: your own events go through the untouched flavor pool, your
allies' through a terse third-person summary. CombatEvent carries the seat to
make that possible, stamped once per phase step rather than at the twenty-odd
append sites in the primitives, which emit against the cursor and know nothing
of seats.

Closing out fans along the seam the data model already cut. Threat, the
zone-kill record, the boss-defeat drop and the run teardown all resolve through
getActiveExpedition or getActiveZoneRun, and a member owns neither row -- so
they fire once, for the owner. Fanning them out would have tripled the threat a
single kill costs. HP, XP, loot and death are the character's, and every seat
gets their own. A member can be dead in a fight the party won, so death is read
per seat off HP, not off the session's status.

The reaper stays attack-only. Finishing an abandoned fight should not quietly
burn the player's spell slots and potions; the deadline latch does use the
picker, because that member is mid-fight with a party waiting on them.

startPartyCombatSession has no production caller yet -- handleFightCmd still
opens a solo session. P6 seats the party.

TestCombatCharacterization is byte-identical: solo balance did not move.
This commit is contained in:
prosolis
2026-07-09 22:07:20 -07:00
parent d7d0230223
commit e8d06195ac
12 changed files with 1709 additions and 171 deletions

View File

@@ -874,16 +874,46 @@ var narrativeCloseLoss = []string{
// handful of events, and across a long boss fight the picker resetting each
// round reads as variety rather than staleness.
func RenderTurnRound(events []CombatEvent, playerName, enemyName string) string {
return RenderPartyTurnRound(events, []string{playerName}, enemyName, 0)
}
// RenderPartyTurnRound renders a round for one member of a party — the reader at
// viewerSeat.
//
// The turn narration pool is written in the second person: "You score 9 damage",
// "A hit gets through your guard". That is exactly right for the reader's own
// events and exactly wrong for everybody else's, so this splits on the event's
// Seat. The reader's own events go through the same flavor pool a solo fight
// uses, untouched; an ally's are summarised third-person by renderAllySeatEvent.
//
// A solo fight has one seat, every event belongs to it, and the reader is it —
// so it renders the same bytes it always has.
func RenderPartyTurnRound(events []CombatEvent, seatNames []string, enemyName string, viewerSeat int) string {
if len(seatNames) == 0 {
seatNames = []string{"You"}
}
seatName := func(seat int) string {
if seat > 0 && seat < len(seatNames) {
return seatNames[seat]
}
return seatNames[0]
}
picker := newActionPicker()
var lines []string
for _, e := range events {
line := renderTurnEvent(e, playerName, enemyName, picker)
var line string
if e.Seat == viewerSeat {
line = renderTurnEvent(e, seatName(e.Seat), enemyName, picker)
if roll := rollAnnotation(e); line != "" && roll != "" {
line += " " + roll
}
} else {
line = renderAllySeatEvent(e, seatName(e.Seat), enemyName)
}
if line == "" {
continue
}
if roll := rollAnnotation(e); roll != "" {
line += " " + roll
}
lines = append(lines, line)
}
if len(lines) == 0 {
@@ -892,6 +922,80 @@ func RenderTurnRound(events []CombatEvent, playerName, enemyName string) string
return strings.Join(lines, "\n")
}
// renderAllySeatEvent summarises one event belonging to somebody else's
// character, for a party member's DM. Terse on purpose: the reader wants the
// shape of the round, not three sentences of flavor about their friend's pet.
// Anything not worth a line in someone else's DM renders as "".
func renderAllySeatEvent(e CombatEvent, name, enemyName string) string {
who := "**" + name + "**"
down := func(line string) string {
if e.PlayerHP <= 0 {
return line + " " + who + " is down."
}
return line
}
switch e.Action {
case "hit":
if e.Actor == "player" {
return fmt.Sprintf("%s hits %s for %d.", who, enemyName, e.Damage)
}
return down(fmt.Sprintf("%s hits %s for %d.", enemyName, who, e.Damage))
case "crit":
if e.Actor == "player" {
return fmt.Sprintf("%s crits %s for %d!", who, enemyName, e.Damage)
}
return down(fmt.Sprintf("%s crits %s for %d!", enemyName, who, e.Damage))
case "miss":
if e.Actor == "player" {
return fmt.Sprintf("%s misses.", who)
}
return fmt.Sprintf("%s misses %s.", enemyName, who)
case "block":
// renderEvent's convention: Actor "player" means the *enemy* blocked the
// player's swing, and vice versa.
if e.Actor == "player" {
return fmt.Sprintf("%s blocks %s (%d).", enemyName, who, e.Damage)
}
return fmt.Sprintf("%s blocks (%d).", who, e.Damage)
case "spell_cast":
label := e.Desc
if label == "" {
label = "a spell"
}
return fmt.Sprintf("%s casts %s.", who, label)
case "use_consumable":
label := e.Desc
if label == "" {
label = "an item"
}
return fmt.Sprintf("%s uses %s.", who, label)
case "pet_attack":
return fmt.Sprintf("🐾 %s's pet strikes for %d.", who, e.Damage)
case "spirit_weapon_strike":
return fmt.Sprintf("%s's spirit weapon strikes for %d.", who, e.Damage)
case "concentration_tick":
return fmt.Sprintf("%s's aura burns %s for %d.", who, enemyName, e.Damage)
case "poison_tick":
return down(fmt.Sprintf("☠️ %s takes %d from poison.", who, e.Damage))
case "environmental":
return down(fmt.Sprintf("%s takes %d from the room.", who, e.Damage))
case "flat_damage":
return fmt.Sprintf("%s deals %d.", who, e.Damage)
case "heal_item", "misty_heal":
return fmt.Sprintf("%s recovers %d.", who, e.Damage)
case "death_save":
return fmt.Sprintf("%s clings on.", who)
case "stun", "stunned":
return fmt.Sprintf("%s is stunned.", who)
case "flee":
return fmt.Sprintf("%s breaks off.", who)
default:
// Ward absorbs, spore misses, reflects, enrage cues: real, but noise in
// somebody else's DM. The owner sees them in full in their own.
return ""
}
}
func renderTurnEvent(e CombatEvent, playerName, enemyName string, picker *actionPicker) string {
switch e.Action {
case "flee":