-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_old.py
272 lines (243 loc) · 9.78 KB
/
game_old.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import os
import board
import berserk_gui
import numpy.random as rng
import network
from kivy.clock import Clock
from cards.card import *
# Config.set('graphics', 'fullscreen', 'auto')
class Game:
def __init__(self, server_ip=None, server_port=None):
""""
cards_on_board is a cards list
"""
self.board = board.Board(self)
self.curr_game_state = GameStates.VSKRYTIE
self.turn_count = 1
self.turn = 1
self.current_active_player = 1
self.curr_priority = 1
self.in_stack = False
self.stack = []
self.passed_1 = False
self.passed_2 = False
self.passed_once = False
self.cards_on_board1 = []
self.cards_on_board2 = []
self.mode = 'offline'
self.server_ip = server_ip
self.server_port = server_port
def set_cards(self, cards_on_board1, cards_on_board2, gui):
new_cards1 = []
new_cards2 = []
for card in cards_on_board1:
nc = card.__class__(card.player, card.loc, gui)
for i, a in enumerate(nc.abilities):
a.index = i
new_cards1.append(nc)
for card in cards_on_board2:
nc = card.__class__(card.player, card.loc, gui)
for i, a in enumerate(nc.abilities):
a.index = i
new_cards2.append(nc)
self.input_cards1 = new_cards1
self.input_cards2 = new_cards2
self.populate_cards()
def populate_cards(self):
self.board.populate_board(self.input_cards1)
self.board.populate_board(self.input_cards2)
def player_passed(self, *args):
if self.in_stack:
if self.curr_priority == 1:
self.passed_1 = True
else:
self.passed_2 = True
self.switch_priority()
def get_roll_result(self, count):
res = []
if self.mode == 'online':
res = network.get_rolls(self.server_ip, self.server_port, count)
else:
for _ in range(count):
res.append(rng.randint(1, 7))
return res
def get_fight_result(self, roll1, roll2):
"""
returns fight simulation, accounts for blessings/curses etc.
roll1 - attacker,
roll2 - defender
"""
res_dict = {
1: [(1, 0)],
2: [(2, 1), (1, 0)],
3: [(2, 0)],
4: [(3, 1), (2, 0)],
5: [(3, 0)],
-1: [(1, 0)],
-2: [(0, 0)],
-3: [(0, 1)],
-4: [(1, 2), (0, 1)],
-5: [(0, 2)],
}
score = roll1 - roll2
if score < -5:
res = [(0, 2)]
elif score > 5:
res = [(3, 0)]
elif score == 0 and roll1 <= 4:
res = [(1, 0)]
elif score == 0 and roll1 > 4:
res = [(0, 1)]
else:
res = res_dict[score]
return res # (attack, defence) list
def on_step_start(self):
if self.curr_game_state == GameStates.END_PHASE:
self.switch_curr_active_player()
if self.curr_game_state == GameStates.OPENING_PHASE:
self.on_start_opening_phase()
def next_game_state(self, *args):
if self.stack:
self.gui.process_stack()
self.passed_1 = False
self.passed_2 = False
self.passed_once = False
next_state = self.curr_game_state.next_after_start()
if next_state != GameStates.MAIN_PHASE:
self.gui.disable_all_non_instant_actions()
if next_state == GameStates.MAIN_PHASE:
cb = self.board.get_all_cards_with_callback(Condition.START_MAIN_PHASE)
for c, a in cb:
if c.player == self.current_active_player and a.check():
if self.mode != 'online' or (self.mode == 'online' and c.player == self.gui.pow):
self.gui.start_stack_action(a, c, c, 0, 0)
# self.gui.check_all_passed()
# if self.backend.mode == 'online' and self.pow != self.backend.current_active_player:
# self.eot_button.disabled = True
# Clock.schedule_once(self.gui.process_stack)
if self.is_state_active(next_state) or next_state == GameStates.MAIN_PHASE:
self.curr_game_state = next_state
self.on_step_start()
self.gui.check_all_passed(None)
else:
self.curr_game_state = next_state
self.on_step_start()
self.next_game_state()
def switch_priority(self, *args):
if self.curr_priority == 1:
self.curr_priority = 2
else:
self.curr_priority = 1
self.gui.buttons_on_priority_switch()
def switch_curr_active_player(self):
if self.current_active_player == 1:
self.current_active_player = 2
self.curr_priority = 2
else:
self.current_active_player = 1
self.curr_priority = 1
if self.gui.pow == self.current_active_player and self.mode == 'online':
self.gui.eot_button.disabled = False
elif self.gui.pow != self.current_active_player and self.mode == 'online':
self.gui.eot_button.disabled = True
self.on_start_new_turn()
def on_start_new_turn(self):
self.turn_count += 1
for card in self.board.get_all_cards():
if card.player != self.current_active_player and CardEffect.NETTED in card.active_status:
card.active_status.remove(CardEffect.NETTED)
if not card.tapped:
card.actions_left = 1
self.gui.add_defence_signs(card)
if self.turn_count > 1 and card.hidden:
self.gui.unhide(card)
#game.gui.on_new_turn()
def on_start_opening_phase(self):
self.gui.on_new_turn()
def is_state_active(self, state):
ret = False
for card in self.board.get_all_cards():
for ability in card.abilities:
if not card.tapped and not isinstance(ability, TriggerBasedCardAction) and state in ability.state_of_action:
ret = True
return ret
def start(self, *args):
Clock.schedule_once(lambda x: self.gui.start_timer(self.gui.turn_duration, True))
if not self.is_state_active(GameStates.VSKRYTIE):
self.next_game_state()
def card_to_state(self, card):
res = {}
res['id'] = card.id_on_board
if hasattr(card, 'life'):
res['life'] = card.life
if hasattr(card, 'curr_life'):
res['curr_life'] = card.curr_life
if hasattr(card, 'move'):
res['move'] = card.move
if hasattr(card, 'curr_move'):
res['curr_move'] = card.curr_move
if hasattr(card, 'curr_fishka'):
res['curr_fishka'] = card.curr_fishka
if hasattr(card, 'red_fishka'): # TODO для перераспределения
res['red_fishka'] = card.curr_fishka
if hasattr(card, 'pic'):
res['pic'] = card.pic
if hasattr(card, 'loc'):
res['loc'] = card.loc
if hasattr(card, 'player'):
res['player'] = card.player
if hasattr(card, 'zone'):
res['zone'] = card.zone
return res
def form_state_obj(
self): # what is displayed now: position, picture, life(curr/max), movement(curr/max), fishka/red fishka, buttons, red arrows, timer restart!
state = {}
# game_board, extra1, extra2, symb1, symb2, grave1, grave2, deck1, deck2 = self.board.get_state() # карты во всех зонах
all_cards = self.board.get_all_cards() + self.board.get_grave()
cards = {}
for card in all_cards:
cards[card.id] = self.card_to_state(card)
markers = {} # клетка на поле или существо в доп зоне ? отправляем только одному игроку
state['cards'] = cards
state['markers'] = markers
state['timer'] = {'restart': False, 'duration': 30}
return state
def handle_input(self):
pass
def send_state(self):
pass
if __name__ == '__main__':
# hack to import all cards
filedir = 'cards/set_1'
modules = [f[:-3] for f in os.listdir(filedir) if
os.path.isfile(os.path.join(filedir, f)) and f.endswith('.py') and f != '__init__.py']
imports = [f"from cards.set_1 import {module}\nfrom cards.set_1.{module} import *" for module in sorted(modules)]
for imp in imports:
exec(imp)
WINDOW_SIZE = (960, 540) # (1920, 1080) #
STACK_DURATION = 5
TURN_DURATION = 5
game = Game()
# game.mode = 'online'
gui = berserk_gui.BerserkApp(game, WINDOW_SIZE, STACK_DURATION, TURN_DURATION, pow=1)
game.gui = gui
cards1 = [Elfiyskiy_voin_1(player=1, location=27, gui=gui),
Lovets_dush_1(player=1, location=13, gui=gui),
# Lovets_dush_1(player=1, location=0, gui=gui),
# Necromant_1(player=1, location=21, gui=gui),
# Ar_gull_1(player=1, location=12, gui=gui),
Cobold_1(player=1, location=14),
Otshelnik_1(player=1, location=4, gui=gui),
Gnom_basaarg_1(player=1, location=15, gui=gui),
# Draks_1(player=1, location=5, gui=gui)
]
cards2 = [
# Bjorn_1(player=2, location=13),
Ovrajnii_gnom_1(player=2, location=22, gui=gui),
Necromant_1(player=2, location=19, gui=gui),
# Lovets_dush_1(player=2, location=12, gui=gui),
Ar_gull_1(player=2, location=16, gui=gui),
# Voin_hrama_1(player=2, location=22, gui=gui), Draks_1(player=2, location=25, gui=gui)
]
game.set_cards(cards1, cards2, gui)
game.gui.run()