-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
163 lines (119 loc) · 4.32 KB
/
engine.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from collections import namedtuple
class Player:
def __init__(self):
self.max_grenades = 2
self.max_shields = 3
self.hp_bullet = 10
self.hp_grenade = 30
self.max_shield_health = 30
self.max_bullets = 6
self.max_hp = 100
self.num_deaths = 0
self.hp = self.max_hp
self.num_bullets = self.max_bullets
self.num_grenades = self.max_grenades
self.hp_shield = 0
self.num_shield = self.max_shields
def get_dict(self):
data = dict()
data['hp'] = self.hp
data['bullets'] = self.num_bullets
data['grenades'] = self.num_grenades
data['shield_hp'] = self.hp_shield
data['deaths'] = self.num_deaths
data['shields'] = self.num_shield
return data
def damage(self, damage):
if self.hp_shield > 0:
self.hp_shield -= damage
# bleedthrough
if self.hp_shield < 0:
self.hp += self.hp_shield
self.hp_shield = 0
else:
self.hp -= damage
if self.hp <= 0:
self.num_deaths += 1
self.hp = self.max_hp
self.num_bullets = self.max_bullets
self.num_grenades = self.max_grenades
self.hp_shield = 0
self.num_shield = self.max_shields
def shoot(self, defender, isHit):
if self.num_bullets > 0:
self.num_bullets -= 1
if isHit:
defender.damage(self.hp_bullet)
else:
pass
def grenade(self, defender, isHit):
if self.num_grenades > 0:
self.num_grenades -= 1
if isHit:
defender.damage(self.hp_grenade)
else:
pass
def shield(self):
if self.num_shield > 0 and self.hp_shield == 0:
self.hp_shield = self.max_shield_health
self.num_shield -= 1
else:
pass
def reload(self):
if self.num_bullets > 0:
pass
else:
self.num_bullets = self.max_bullets
def generic_action(self, defender, isHit):
if isHit:
defender.damage(self.hp_bullet)
else:
pass
class GameState:
def __init__(self):
self.p1 = Player()
self.p2 = Player()
def get_dict(self):
data = {'p1': self.p1.get_dict(), 'p2': self.p2.get_dict()}
return data
def __str__(self):
return str(self.get_dict())
def update(self, msg):
player_id = msg["player_id"]
action = msg["action"]
isHit = msg["isHit"]
if player_id == "1":
attacker = self.p1
defender = self.p2
else:
attacker = self.p2
defender = self.p1
if action == "gun":
attacker.shoot(defender, isHit)
elif action == "grenade":
attacker.grenade(defender, isHit)
elif action == "shield":
attacker.shield()
elif action == "reload":
attacker.reload()
elif action in {"web", "portal", "punch", "hammer", "spear"}:
attacker.generic_action(defender, isHit)
elif action == "logout":
# logout
pass
else:
# invalid
pass
def overwrite(self, eval_server_game_state):
self.p1.hp = eval_server_game_state["p1"]["hp"]
self.p1.num_bullets = eval_server_game_state["p1"]["bullets"]
self.p1.num_grenades = eval_server_game_state["p1"]["grenades"]
self.p1.num_shield = eval_server_game_state["p1"]["shields"]
self.p1.hp_shield = eval_server_game_state["p1"]["shield_hp"]
self.p1.num_deaths = eval_server_game_state["p1"]["deaths"]
self.p2.hp = eval_server_game_state["p2"]["hp"]
self.p2.num_bullets = eval_server_game_state["p2"]["bullets"]
self.p2.num_grenades = eval_server_game_state["p2"]["grenades"]
self.p2.num_shield = eval_server_game_state["p2"]["shields"]
self.p2.hp_shield = eval_server_game_state["p2"]["shield_hp"]
self.p2.num_deaths = eval_server_game_state["p2"]["deaths"]