-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameactions.go
110 lines (87 loc) · 2.87 KB
/
gameactions.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"strings"
"sync"
"github.com/AEPi-AK/game-server/models"
log "github.com/Sirupsen/logrus"
)
var mutex = &sync.Mutex{}
func playersDone() bool {
return player1Attacked && player2Attacked || player1Attacked && strings.EqualFold(state.Player2.ID, "") || player2Attacked && strings.EqualFold(state.Player1.ID, "")
}
func PerformAttack(attack models.Attack) models.State {
mutex.Lock()
if strings.EqualFold(state.Player1.ID, attack.Target) {
state.Player1.Hitpoints = state.Player1.Hitpoints - attack.Damage
} else if strings.EqualFold(state.Player2.ID, attack.Target) {
state.Player2.Hitpoints = state.Player2.Hitpoints - attack.Damage
} else if strings.EqualFold(state.Monster.ID, attack.Target) {
state.Monster.Hitpoints = state.Monster.Hitpoints - attack.Damage
}
if strings.EqualFold(state.Player1.ID, attack.Attacker) {
player1Attacked = true
state.Player1.LastAttackUsed = attack.Attack
} else if strings.EqualFold(state.Player2.ID, attack.Attacker) {
player2Attacked = true
state.Player2.LastAttackUsed = attack.Attack
} else if strings.EqualFold(state.Monster.ID, attack.Attacker) {
monsterTurn = false
state.Monster.LastAttackUsed = attack.Attack
}
if playersDone() {
monsterTurn = true
}
mutex.Unlock()
return state
}
func PerformHello(hello models.Hello) models.State {
mutex.Lock()
if hello.PlayerNum == 1 && strings.EqualFold(state.Player1.ID, "") {
state.Player1 = hello.Player
} else if hello.PlayerNum == 2 && strings.EqualFold(state.Player2.ID, "") {
state.Player2 = hello.Player
}
if hello.PlayerNum == 1 && !strings.EqualFold(state.Player1.ID, "") {
state.Player1.Hitpoints = hello.Player.Hitpoints
} else if hello.PlayerNum == 2 && !strings.EqualFold(state.Player2.ID, "") {
state.Player2.Hitpoints = hello.Player.Hitpoints
}
mutex.Unlock()
return state
}
func PerformPoll(poll models.Poll) PollResponse {
mutex.Lock()
isMonster := false
if strings.EqualFold(poll.ID, state.Monster.ID) {
isMonster = true
}
canAttack := false
if isMonster && monsterTurn {
canAttack = true
log.Warn("putting false in poll")
player1Attacked = false
player2Attacked = false
} else if !isMonster && (strings.EqualFold(poll.ID, state.Player1.ID) && !player1Attacked) {
canAttack = true
} else if !isMonster && (strings.EqualFold(poll.ID, state.Player2.ID) && !player2Attacked) {
canAttack = true
}
mutex.Unlock()
return PollResponse{CanAttack: canAttack, State: state}
}
func PerformHelloMonster(helloMonster models.HelloMonster) models.State {
mutex.Lock()
state.Monster = helloMonster.Monster
state.Player1.ID = ""
state.Player2.ID = ""
state.Player1.Hitpoints = 0
state.Player2.Hitpoints = 0
state.Player1.LastAttackUsed = ""
state.Player2.LastAttackUsed = ""
monsterTurn = false
player1Attacked = false
player2Attacked = false
log.Warn("putting false in hello monster")
mutex.Unlock()
return state
}