-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
279 lines (230 loc) · 7.24 KB
/
game.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
272
273
274
275
276
277
278
279
from utils.io import (
load_location_from_configuration,
load_configuration_from_json,
write_save_as_json,
load_save_from_json
)
from entities.player import Player
from entities.equipment import Key
from location.location import Location
from utils.player_input import (
choose_item_from_dict,
choose_string,
player_input
)
from utils.format import print_formatted_list
from typing import List, Dict
import functools
class Game:
"""
Game class
Contains attributes:
:param game: Name of the chosen game, defaults to 'Dungeons and Dragons'
:type game: str, optional
:param player: Holds information about player, defaults to None
:type player: Player, optional
:param locations: List of locations discovered by player, defaults to None
:type locations: List[Location], optional
:param level: The number of location where the player is now, defaults to 1
:type level: int, optional
"""
def __init__(self,
game: str = 'Dungeons and Dragons',
player: Player = None,
locations: List[Location] = None,
level: int = 1):
"""
Initialize Game
"""
self.set_game(game)
self.set_player(player)
self.set_locations(locations)
self.set_level(level)
self._exit = False
def as_dict(self) -> Dict:
"""
Get Game as a dictionary
:return: Dictionary with game's data
:rtype: Dict
"""
return {
'game': self._game,
'player': self._player.as_dict(),
'locations': [location.as_dict() for location in self._locations],
'level': self._level
}
@staticmethod
def from_dict(dictionary: Dict) -> 'Game':
"""
Get new Game instance loaded from dictionary
:param dictionary: Dictionary with game's data
:type dictionary: Dict
:return: Game loaded from dictionary
:rtype: Game
"""
return Game(
dictionary['game'],
Player.from_dict(dictionary['player']),
[Location.from_dict(location)
for location in dictionary['locations']],
dictionary['level']
)
# Getters and setters
def game(self) -> str:
"""
Get game
:return: Name of the chosen game
:rtype: str
"""
return self._game
def set_game(self, game: str):
"""
Set game
:param game: Name of the chosen game
:type game: str
"""
self._game = game
def player(self) -> Player:
"""
Get player
:return: Information about player
:rtype: Player
"""
return self._player
def set_player(self, player: Player):
"""
Set player
:param player: Information about player
:type player: Player
"""
if player:
self._player = player
else:
self._player = load_configuration_from_json(
self._game, 'player', Player)
def locations(self) -> List[Location]:
"""
Get locations
:return: List of locations discovered by player
:rtype: List[Location]
"""
return self._locations
def set_locations(self, locations: List[Location]):
"""
Set locations
:param locations: List of locations discovered by player
:type locations: List[Location]
"""
if locations:
self._locations = locations
else:
self._locations = []
self.add_location('lvl1')
def level(self) -> int:
"""
Get level
:return: The number of location where the player is now
:rtype: int
"""
return self._level
def set_level(self, level: int):
"""
Set level
:param level: The number of location where the player is now
:type level: int
"""
self._level = level
def end(self) -> bool:
"""
Indicates whether to end the game or start another round
:return: Value of the exit propery
:rtype: bool
"""
return self._exit
def exit(self):
"""
Exit the game - set exit property True
"""
self._exit = True
def location(self) -> Location:
"""
Get location where player currently is
:return: Current location
:rtype: Location
"""
return self._locations[self._level-1]
def __eq__(self, other) -> bool:
"""
Check if other is equal to self
:param other: Might be any object
:type other: Any
:return: Whether self is equal to other
:rtype: bool
"""
return isinstance(other, Game) and self.as_dict() == other.as_dict()
def add_location(self, filename: str):
"""
Load location from configuration directory based on filename
:param filename: Name of loaded location
:type filename: str
"""
location = load_location_from_configuration(
self._game, filename, len(self._locations) + 1)
self._locations.append(location)
def save(self):
"""
Save game state
"""
print('Enter Save Name:')
filename = player_input(choose_string)
write_save_as_json(self._game, filename, self)
def round(self):
"""
Round is a single round of the game
This method is called over and over again in a loop
During a round player is given set of functions he can call
"""
player = self.player()
location = self.location()
field = location.current_field()
player_methods = player.available_methods()
print('\n')
print('Player Methods:')
print_formatted_list(player_methods.keys())
field_methods = field.available_methods(player, self)
print('Field Methods:')
print_formatted_list(field_methods.keys())
location_methods = location.available_methods()
location_methods = {
key: functools.partial(item, self._player)
for key, item in location_methods.items()}
print('Location Methods:')
print_formatted_list(location_methods.keys())
print('Game Methods:')
game_methods = self.available_methods()
print_formatted_list(game_methods.keys())
game_methods.update(player_methods)
game_methods.update(field_methods)
game_methods.update(location_methods)
result = player_input(functools.partial(
choose_item_from_dict, game_methods))()
if isinstance(result, Key):
field.open_with_key(result, self)
@staticmethod
def load(game: str, filename: str) -> 'Game':
"""
Load Game from saves directory
:return: Name of the save
:rtype: Game
"""
return load_save_from_json(game, filename, Game)
def available_methods(self) -> Dict:
"""
Get the methods available now to the user
:return: Dictionary with save and exit functions
:rtype: Dict
"""
return {
'save': self.save,
'exit': self.exit
}