From 9eb5ab1818b6b19501fd7209db1987c9e06cc919 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Tue, 9 Jun 2026 05:59:20 -0400 Subject: first commit --- internal/combat/state.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 internal/combat/state.go (limited to 'internal/combat/state.go') diff --git a/internal/combat/state.go b/internal/combat/state.go new file mode 100644 index 0000000..b0e7dfe --- /dev/null +++ b/internal/combat/state.go @@ -0,0 +1,89 @@ +package combat + +import "sync" + +type State struct { + PlayerName string + MobID string + MobAttacks int // attacks mob has made (3-hit flee rule) + PlayerDamage int // total damage player dealt this combat + Active bool +} + +var ( + mu sync.Mutex + combatants = make(map[string]*State) // player name -> state + mobTargets = make(map[string]string) // mob ID -> player name +) + +func EnterCombat(playerName, mobID string) { + mu.Lock() + defer mu.Unlock() + combatants[playerName] = &State{ + PlayerName: playerName, + MobID: mobID, + Active: true, + } + mobTargets[mobID] = playerName +} + +func LeaveCombat(playerName string) { + mu.Lock() + defer mu.Unlock() + state, ok := combatants[playerName] + if !ok { + return + } + delete(mobTargets, state.MobID) + delete(combatants, playerName) +} + +func GetCombat(playerName string) *State { + mu.Lock() + defer mu.Unlock() + return combatants[playerName] +} + +func IsMobInCombat(mobID string) bool { + mu.Lock() + defer mu.Unlock() + _, ok := mobTargets[mobID] + return ok +} + +func RecordMobAttack(playerName string) { + mu.Lock() + defer mu.Unlock() + if state, ok := combatants[playerName]; ok { + state.MobAttacks++ + } +} + +func CanFlee(playerName string) bool { + mu.Lock() + defer mu.Unlock() + state, ok := combatants[playerName] + if !ok { + return true + } + return state.MobAttacks >= 3 +} + +func RecordPlayerDamage(playerName string, dmg int) { + mu.Lock() + defer mu.Unlock() + if state, ok := combatants[playerName]; ok { + state.PlayerDamage += dmg + } +} + +func GetTotalDamage(playerName string) int { + mu.Lock() + defer mu.Unlock() + if state, ok := combatants[playerName]; ok { + d := state.PlayerDamage + state.PlayerDamage = 0 + return d + } + return 0 +} -- cgit v1.2.3