-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
226 lines (194 loc) · 7.42 KB
/
__init__.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
import pickle
import requests
from player import Player
from monsters import create_the_monsters
from utils import printer, create_activity, reset_base_url
import settings
"""
Persist game_data via pickle
info include
player the player instance, should you set/unset you must call
save_game_data() afterwards
"""
game_data = {}
__monsters = create_the_monsters()
__current_monster = None
# find a monster to fight with.
def find_monster():
global __current_monster
global game_data
if 'player' not in game_data:
printer('You are not a registered player!')
printer('Use world.enter() to enter the World.')
return
elif game_data['player'].level > len(__monsters):
printer('There are no monsters left in the world!')
printer('Congratulations {}! You have finished the game.'.format(
game_data['player'].name))
# log activity: finished game
create_activity(text="Our hero {} finished all the monsters!".format(
game_data['player'].name), kind="player-finished")
return
elif __current_monster:
printer('You are already fighting a monster currently.')
printer('Use world.attack() to try defeating the monster.')
return
# log activity: found monster
create_activity(text="{} is searching for monsters ...".format(
game_data['player'].name),
kind="monster-search")
game_data['player'].life = game_data['player'].level + 5
i = game_data['player'].level - 1
monster = __monsters[i]
__current_monster = monster
# log activity: found monster
create_activity(text="{} found {}".format(
game_data['player'].name, __current_monster.name),
kind="monster-found")
__current_monster.introduction()
game_data['player'].display_stats()
# evaluate the answer given the current monster being fought
def attack(answer):
global __current_monster
global game_data
if not __current_monster:
printer('You are not fighting any monster as of now.')
printer('Use world.find_monster() to fight one.')
return
printer('You attacked {0} with your answer.'.format(
__current_monster.name))
player = game_data['player']
# log activity: attacks monster
create_activity(text="{} attacks {}!".format(player.name,
__current_monster.name),
kind="monster-attack")
result = __current_monster.evaluate(answer)
if result:
printer('Your answer is correct!')
# log activity: defeat monster
create_activity(text="{} defeated {}!".format(player.name,
__current_monster.name),
kind="monster-defeat")
__current_monster.defeat()
__current_monster = None
player.level_up()
# log activity: level up
create_activity(text="{} leveled up! Now level {}".format(
player.name, player.level), kind="player-level-up")
save_game_data()
else:
printer('Your answer is wrong!')
__current_monster.attack()
player.life -= __current_monster.level
player.display_stats()
# log activity: lost to monster
create_activity(text="{} dominates {}!".format(__current_monster.name,
player.name),
kind="monster-success")
if player.life <= 0:
__current_monster = None
# log activity: lost to monster
create_activity(text="{} fainted!".format(player.name),
kind="player-fainted")
printer('You fainted because of your incompetence.')
printer('What a loser.')
printer('Unless you try again...')
save_game_data()
# register player to server
def register(player):
if player.registered:
return True
global game_data
printer('Registering to server...', False)
try:
r = requests.post(settings.register_url,
data={'username': player.name})
except Exception, e:
printer(e)
printer('... failed! :( . Will try again later.')
return False
if r.status_code == 403:
printer('... failed! with code {} :( .\
Username is already taken.'.format(r.status_code))
return False
elif r.status_code != 200:
printer('... failed! with code {} :( .'.format(r.status_code))
return False
if 'username' in r.json() and r.json()['username'] == player.name:
player.registered = True
game_data['player'] = player
save_game_data() # save game data
printer('... success for user {} ! :) .'.format(player.name))
return True
# unregister player to server
def unregister(player):
r = requests.post(settings.deactivate_url, data={'username': player.name})
if r.status_code == 200:
return True
return False
# get existing player or create a new player
def enter():
global game_data
try:
# load existing
load_game_data()
game_data['player']
except (IOError, KeyError):
# create new
printer('Alas, for the intro. What is your name again?')
name = str(raw_input())
player = Player(name=name)
game_data['player'] = player # save player instance
save_game_data()
player = game_data['player']
if not player.registered:
register(player)
# log activity: enter world
create_activity(text="{} has entered the world!".format(player.name),
kind="world-enter")
printer('{}! Our chosen one. We are pleased to meet you.'.format(
player.name))
global __current_monster
__current_monster = None
# reset game_data, and unregister player to server
# much like an apocalypse but the host survives, (this time the world)
def reset_world():
message = 'Are you sure you want to leave this world? (y/n) '
if (raw_input(message).lower() == 'y'):
global __current_monster
global game_data
# log activity: exit world
player = game_data['player']
create_activity(text="{} has left the world!".format(player.name),
kind="world-exit")
game_data = {} # this is so creepy! :-o
save_game_data() # reset game data
printer('okay :(')
__current_monster = None
else:
printer("That's the spirit!")
# print help
def ask_help():
printer('Our hero, here are the functions you can call from this World.')
printer('world.find_monster() -> Find a monster to fight.')
printer('world.attack(answer) -> Attack the monster with your answer.')
printer('world.ask_help() -> Print this help section.')
printer('world.enter() -> Register yourself into the World.')
printer('world.reset_world() -> Reset everything about you.')
# save game data
def save_game_data():
global game_data
with open(r'gamedat.pkl', 'wb') as f: # reset game_data
pickle.dump(game_data, f)
# load game data
def load_game_data():
global game_data
with open(r'gamedat.pkl', 'rb') as f:
game_data = pickle.load(f)
# run this function upon import just to keep things tidy
def main():
printer('Greetings young adventurer!\nWelcome to... The World!')
enter()
ask_help()
reset_base_url()
main()