-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
584 lines (494 loc) · 17.4 KB
/
player.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
from entities.equipment import Item
from entities.equipment import (
Weapon,
Armor,
Potion,
Key
)
from utils.format import format_stats, print_list_with_num
from utils.player_input import choose_num_from_list, player_input
from typing import List, Dict
from random import randint
import functools
class PlayerNameError(Exception):
"""
Indicates that given Player's name was empty
:param Exception: Player's name cannot be empty
:type Exception: Exception
"""
def __init__(self):
super().__init__("Player's name cannot be empty")
class EquipmentSizeError(ValueError):
"""
Indicates that equipment size is too small to add another item
:param ValueError: Equipment size is too small
:type ValueError: ValueError
"""
pass
class Player:
"""
Player
Contains attributes:
:param name: Player's name, defaults to 'Nameless'
:type name: str, optional
:param base_health: Base health of the player, defaults to 100
:type base_health: int, optional
:param health: Current health of the player, defaults to 100
:type health: int, optional
:param strength: Strength of the base attack, defaults to 10
:type strength: int, optional
:param weapon: Weapon - item that can increase player's
attack damage, defaults to None
:type weapon: Weapon, optional
:param armor: Armor - item that reduces taken damage, defaults to None
:type armor: Armor, optional
:param equipment_size: Number of slots int the equipment, defaults to 0
:type equipment_size: int, optional
:param equipment: List of items - current player's equipment,
defaults to None
:type equipment: List[Item], optional
"""
def __init__(self,
name: str = 'Nameless',
base_health: int = 100,
health: int = 100,
strength: int = 10,
weapon: Weapon = None,
armor: Armor = None,
equipment_size: int = 0,
equipment: List[Item] = None
):
"""
Initialize Player
:param name: Player's name, defaults to 'Nameless'
:type name: str, optional
:param base_health: Base health of the player, defaults to 100
:type base_health: int, optional
:param health: Current health of the player, defaults to 100
:type health: int, optional
:param strength: Strength of the base attack, defaults to 10
:type strength: int, optional
:param weapon: Weapon - item that can increase player's attack damage,
defaults to None
:type weapon: Weapon, optional
:param armor: Armor - item that reduces taken damage, defaults to None
:type armor: Armor, optional
:param equipment_size: Number of slots int the equipment, defaults to 0
:type equipment_size: int, optional
:param equipment: List of items - current player's equipment,
defaults to None
:type equipment: List[Item], optional
"""
self.set_name(name)
self.set_base_health(base_health)
self.set_health(health)
self.set_strength(strength)
self.set_weapon(weapon)
self.set_armor(armor)
self.set_equipment_size(equipment_size)
self.set_equipment(equipment)
def as_dict(self) -> Dict:
"""
Get Player as a dictionary
:return: Dictionary with player's data
:rtype: Dict
"""
dictionary = {
'name': self._name,
'base_health': self._base_health,
'health': self._health,
'strength': self._strength,
'equipment_size': self._equipment_size,
'equipment': [item.as_dict() for item in self._equipment]
}
if self._weapon:
dictionary['weapon'] = self._weapon.as_dict()
if self._armor:
dictionary['armor'] = self._armor.as_dict()
return dictionary
@staticmethod
def from_dict(dictionary) -> "Player":
"""
Get new Player instance loaded from dictionary
:param dictionary: Dictionary with Player's data
:type dictionary: Dict
:return: Loaded Player
:rtype: Player
"""
return Player(
dictionary.get('name', 'Nameless'),
dictionary.get('base_health', 100),
dictionary.get('health', 100),
dictionary.get('strength', 10),
Weapon.from_dict(dictionary.get('weapon', None)),
Armor.from_dict(dictionary.get('armor', None)),
dictionary.get('equipment_size', 0),
[Item.item_from_dict(item)
for item in dictionary.get('equipment', [])]
)
# Getters and Setters
def name(self) -> str:
"""
Get player's name
:return: Name of the player
:rtype: str
"""
return self._name
def set_name(self, name: str):
"""
Set player's name
:param name: Name of the player
:type name: str
:raises PlayerNameError: Indicates that given name is empty
"""
if not name:
raise PlayerNameError()
self._name = name
def health(self) -> int:
"""
Get current player's health
:return: Value of player's health
:rtype: int
"""
return self._health
def set_health(self, health: int):
"""
Set player's health
:param health: Value of player's health
:type health: int
:raises ValueError: Indicates that given player's health was negative
"""
if health <= 0:
raise ValueError("Player's Health cannot be negative")
if health > self._base_health:
raise ValueError(
"Player's Health cannot be greater than base health")
self._health = health
def base_health(self) -> int:
"""
Get player's base value of health
:return: Base value of player's health
:rtype: int
"""
return self._base_health
def set_base_health(self, base_health: int):
"""
Set player's base health value
:param base_health: Base value of player's health
:type base_health: int
:raises ValueError: Indicates that given player's health was negative
"""
if base_health <= 0:
raise ValueError("Player's Base Health cannot be negative")
self._base_health = base_health
def strength(self) -> int:
"""
Get base value of player's strength
:return: Value of player's strength
:rtype: int
"""
return self._strength
def set_strength(self, strength: int):
"""
Set base value of player's strength
:param strength: Value of player's strength
:type strength: int
:raises ValueError: Indicates that given strength was negative
"""
if strength < 0:
raise ValueError('Strength must be positive')
self._strength = strength
def weapon(self) -> Weapon:
"""
Get weapon - item that can increase player's attack damage
:return: Player's equipped weapon
:rtype: Weapon
"""
return self._weapon
def set_weapon(self, weapon: Weapon):
"""
Set weapon - item that can increase player's attack damage
:param weapon: Player's weapon to equip
:type weapon: Weapon
"""
self._weapon = weapon
def armor(self) -> Armor:
"""
Get armor - item that can decrease player's damage taken
:return: Player's equipped armor
:rtype: Armor
"""
return self._armor
def set_armor(self, armor: Armor):
"""
Set armor - item that can decrease player's damage taken
:param armor: Player's armor to equip
:type armor: Armor
"""
self._armor = armor
def equipment(self) -> List[Item]:
"""
Get list of player's items from the equipment
:return: List of player's items
:rtype: List[Item]
"""
return self._equipment
def set_equipment(self, equipment: List[Item]):
"""
Get list of player's items as the equipment
:param equipment: List of items
:type equipment: List[Item]
:raises ValueError: Indicates that in given list was found item
that didn't inherited from Item class
:raises EquipmentSizeError: Indicates that player couldn't
handle such heavy equipment
"""
if not equipment:
equipment = []
for item in equipment:
if not isinstance(item, Item):
raise ValueError('Equipment must contain only Items')
if len(equipment) > self._equipment_size:
raise EquipmentSizeError('Equipment size is too small')
self._equipment = equipment
def equipment_size(self) -> int:
"""
Get max size of the player's equipment
:return: Max equipment size
:rtype: int
"""
return self._equipment_size
def set_equipment_size(self, size: int):
"""
Set max size of the player's equipment
:param size: Max equipment size
:type size: int
:raises ValueError: Indicates that given size was negative
"""
if size < 0:
raise ValueError('Equipment size must be positive')
self._equipment_size = size
# Custom Methods
def take_damage(self, amount: int) -> bool:
"""
Method that decreases player's health by given amount
:param amount: Amount of the damage given
:type amount: int
:raises ValueError: Indicates that given amount was negative
:return: If player is alive
:rtype: bool
"""
if amount < 0:
raise ValueError('Damage cannot be negative')
if self.armor():
amount = amount * (100 - self.armor().defence()) / 100
amount = int(amount)
self._health -= min(amount, self._health)
if amount != 0:
print('')
print(f'Ouch! You took {amount} points of damage!')
return self._health != 0
def inflict_damage(self, enemy) -> bool:
"""
Method that calculates the amount of damage player will
deal to given enemy
:param enemy: Enemy from field player is standing on
:type enemy: Enemy
:return: Whether enemy is alive
:rtype: bool
"""
damage = self._strength
if self._weapon:
damage += self._weapon.base_strength()
damage += randint(0, self._weapon.random_strength())
return enemy.take_damage(damage)
def regenerate(self, amount: int):
"""
Method that regenerates player's healt by a given amount
Player's health cannot be greater than base health
:param amount: Amount of health to regenerate
:type amount: int
:raises ValueError: Indicates that given amount was negative
"""
if amount < 0:
raise ValueError('Amount cannot be negative')
if self._health != self._base_health:
print('')
print(f'Player has regenerated {amount} points of health.')
self._health = min(self._health + amount, self._base_health)
def use(self, item: Item):
"""
Method that decides based on given item which action should be called
:param item: Item from equipment
:type item: Item
:return: Called method result
:rtype: Key, bool
"""
if isinstance(item, Weapon):
return self.equip_weapon(item)
elif isinstance(item, Armor):
return self.equip_armor(item)
elif isinstance(item, Key):
return self.open(item)
elif isinstance(item, Potion):
return self.drink(item)
def drink(self, potion: Potion) -> bool:
"""
Regenerate player's health and remove potion from inventory
:param potion: Potion of health
:type potion: Potion
:return: Whether potion was drunk or not
:rtype: bool
"""
if self._health != self._base_health:
self._health = min(potion.health() + self._health,
self._base_health)
print('You have drank your potion!')
self._equipment.remove(potion)
return True
print('You have full health already!')
return False
def equip_weapon(self, weapon: Weapon):
"""
Set player's weapon as a choosen one, old weapon hide to inventory
:param weapon: Weapon to equip
:type weapon: Weapon
"""
current_weapon = self._weapon
num = self._equipment.index(weapon)
self.set_weapon(weapon)
print('You have equipped a weapon.')
if current_weapon:
self._equipment[num] = current_weapon
print('Your old weapon is now in the inventory.')
else:
self._equipment.remove(weapon)
def equip_armor(self, armor: Armor):
"""
Set player's armor as a choosen one, old armor hide to inventory
:param armor: Armor to equip
:type armor: Armor
"""
current_armor = self._armor
num = self._equipment.index(armor)
self.set_armor(armor)
print('You have equipped armor.')
if current_armor:
self._equipment[num] = current_armor
print('Your old armor is now in the inventory.')
else:
self._equipment.remove(armor)
def open(self, key: Key) -> Key:
"""
Return key used to open location on this field
:param key: Key to new location
:type key: Key
:return: Key to new location
:rtype: Key
"""
return key
def print_equipment(self):
"""
Print player's equipment as enumerated list
"""
print('\n')
print_list_with_num(self._equipment)
def pickup_item(self, item: Item) -> bool:
"""
Add item to inventory
:param item: Item to add
:type item: Item
:return: Whether item was added or inventory was full
:rtype: bool
"""
if len(self._equipment) != self._equipment_size:
self._equipment.append(item)
print('Item added to equipment!')
return True
print("You don't have enough space in the inventory!")
return False
def drop_item(self) -> Item:
"""
Remove item chosen by user from inventory
:return: Item player has chosen
:rtype: Item
"""
print('Choose item you want to drop?')
self.print_equipment()
print('Enter number:')
item = player_input(functools.partial(
choose_num_from_list, self._equipment))
self._equipment.remove(item)
return item
def prepare_use(self):
"""
Ask player which item he wants to use
"""
print('Choose item you want to use?')
self.print_equipment()
print('Enter number:')
item = player_input(functools.partial(
choose_num_from_list, self._equipment))
return self.use(item)
def search_for_key(self, level: int) -> Key:
"""
Search inventory for approptiate key
:param level: Level of the location key opens
:type level: int
:return: Key that opens given level
:rtype: Key
"""
for item in self._equipment:
if isinstance(item, Key) and item.level() == level:
return item
def available_methods(self) -> Dict:
"""
Get player methods available to player during this round
:return: Dictionary with methods
:rtype: Dict
"""
dictionary = {}
if len(self._equipment):
dictionary['use item'] = self.prepare_use
dictionary['player info'] = self.print_info
if len(self._equipment) != 0:
dictionary['equipment info'] = self.print_equipment
return dictionary
def print_info(self):
"""
Print information about player
"""
print('\n')
print(self)
print('\n')
def info(self) -> str:
"""
:return: Player information
:rtype: str
"""
info = 'PLAYER\n'
info += f'Name: {self.name()}\n'
info += format_stats('Health', self.health(), self.base_health())
strength = self.strength()
base_strength = strength
if self._weapon:
strength += self._weapon.base_strength()
base_strength = strength + self._weapon.random_strength()
info += format_stats('Strength', strength, base_strength)
if self._armor:
info += format_stats('Armor', self._armor.defence(), 100)
return info
def __str__(self) -> str:
"""
:return: Basic player information
:rtype: str
"""
return self.info()
def __eq__(self, other):
"""
:param other: Checked object
:type other: Any
:return: Indicates whether self and other are equal
:rtype: bool
"""
return isinstance(other, Player) and self.as_dict() == other.as_dict()