-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
175 lines (133 loc) · 5.25 KB
/
Copy pathplayer.py
File metadata and controls
175 lines (133 loc) · 5.25 KB
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
#!/usr/bin/env python3.8
import os
import pickle
import random
import sys
from datetime import datetime
from pathlib import Path
from monster import Monster
class Player:
save_dir = "player_saves"
def __init__(self, name: str, monster: Monster):
self.created = datetime.now()
self.name = name
self.monster = monster
self.gold = 0
self.blessing = datetime.now()
self.savepath = Path(f"{self.save_dir}/{self.name}")
@classmethod
def interactive(cls):
"""Provide interactive constructor for player class"""
while True:
try:
name = input("Please enter a name for your adventure: ")
if name.lower() == "quit":
sys.exit()
savepath = Path(f"{cls.save_dir}/{name}")
if cls.save_exists(cls, savepath):
response = None
while not response in ["l", "o", "s"]:
try:
response = input(
"\nThere appears to be a save with that name.\n\nWould you like to (L)oad the game, (O)verwrite, or (S)tart again? "
)
reponse = response.lower()
except KeyboardInterrupt:
sys.exit()
except:
pass
if response == "l":
player = cls.load_player(cls, savepath)
return player
if response == "s":
print()
continue
print(
f"\nWelcome, {name}! Now it's time to select your monsterly companion...\n"
)
monster = cls.select_monster()
print(f"\nYou have selected the {monster.name}!")
if name and monster:
return cls(name, monster)
except KeyboardInterrupt:
sys.exit()
except:
pass
@staticmethod
def select_monster():
"""Provide interactive interface for user to choose from three randomly selected
'Common' rarity monsters"""
# Select the three elements for the player and generate those monsters
monster_data = [
Monster.random_monster_filter("Common", element)
for element in random.sample(Monster.available_elements(), 3)
]
# Set up the column and row values
column_titles = ["Monster 1", " Monster 2", "Monster 3"]
row_titles = ["Name", "Element", None, "Strength", "Defense", "Hit Points"]
# Define the table header formatting and render the table
row_format = "{:<10} " + "{:^20}" * (len(column_titles))
print(row_format.format("", *column_titles))
print()
for row in row_titles:
if row is None:
print()
else:
if row == "Hit Points":
row = "HP"
if row == "Element":
print(
row_format.format(
f"{row}:",
*[
f"{monster.element} ({monster.rarity})"
for monster in monster_data
],
)
)
else:
print(
row_format.format(
f"{row}:",
*[
getattr(monster, row.lower())
for monster in monster_data
],
)
)
print()
while True:
response = input("Please enter the number of your chosen companion: ")
if response.isnumeric() and 1 <= int(response) <= 3:
return monster_data[int(response) - 1]
elif response.lower() == "quit":
exit()
else:
print(
"Enter a number between 1 and 3 that corresponds with your selection."
)
continue
def save_player(self):
"""Save current player object to disk so that it can be used later"""
savepath = Path(f"{self.save_dir}/{self.name}")
savepath.parent.mkdir(exist_ok=True)
try:
savepath.rename(savepath.with_suffix(".bak"))
except:
pass
with open(savepath, "wb") as fp:
pickle.dump(self, fp)
def load_player(self, savepath: str = None):
"""Attempt to load and return an existing player object that has been saved"""
if not savepath:
savepath = self.savepath
if self.save_exists(self, savepath):
with open(savepath, "rb") as fp:
player = pickle.load(fp)
return player
def save_exists(self, savepath: str = None):
"""Verify the existance of the save file given the file name and path"""
if not savepath:
savepath = self.savepath
save_file = Path(savepath)
return save_file.is_file()