aboutsummaryrefslogtreecommitdiff
path: root/internal/combat/state.go
blob: 9c8d346f460bb62f4b5219e7780b71c53e520637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package combat

import "sync"

type State struct {
	PlayerName string
	MobID      string
	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 GetMobTarget(mobID string) string {
	mu.Lock()
	defer mu.Unlock()
	return mobTargets[mobID]
}