-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (96 loc) · 3.06 KB
/
main.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
import shelve
from flask import Flask, request, redirect, render_template, abort
from utils.map import Map
from utils.player import Player
DEBUG = True
PLAYER_DB = None
APP = Flask(__name__)
@APP.route('/')
def index():
return render_template('index.html')
@APP.route('/login', methods=['POST'])
def login():
id = request.values['_id']
if id.startswith('16DRL_'):
if id not in PLAYER_DB['player']:
abort(404)
else:
# Init Map
map = Map(13)
player = Player(id)
map.reset(player)
showMap = map.show_map(player)
level = map.show_total_map(player)
# Save Map
save(player, map)
id = player.hashID
return redirect("/map/" + id, code=303)
@APP.route('/map/<id>')
def map(id):
player, map = loadData(id)
return render_template('map_base.html', player=player, map=map)
@APP.route('/wear/<id>')
def wear(id):
player, map = loadData(id)
return render_template('map_items.html',
player=player, map=map,
usage='wear',
objs=player.getWearable())
@APP.route('/wear/<id>/<int:index>')
def wear_it(id, index):
player, map = loadData(id)
player.wear(index, map)
map.update(player)
save(player, map)
return redirect("/map/" + player.hashID, code=303)
@APP.route('/use/<id>')
def use(id):
player, map = loadData(id)
return render_template('map_items.html',
player=player, map=map,
usage='use',
objs=player.getUsable())
@APP.route('/use/<id>/<int:index>')
def use_it(id, index):
player, map = loadData(id)
player.use(index, map)
map.update(player)
save(player, map)
return redirect("/map/" + player.hashID, code=303)
@APP.route('/move/<id>/<dir>')
def move(id, dir):
player, map = loadData(id)
map.player_move(player, dir)
map.update(player)
save(player, map)
if player.finished:
if player.hp == 0:
map.logs.append('You died!')
return render_template('map_finished.html',
player=player, map=map, message='Game Over')
else:
map.logs.append('You win!')
return render_template('map_finished.html',
player=player, map=map, message='You Win')
else:
return redirect("/map/" + player.hashID, code=303)
def save(player, map):
PLAYER_DB['player'][player.hashID] = (player, map)
def loadData(id):
if id in PLAYER_DB['player']:
return PLAYER_DB['player'][id]
else:
abort(404)
if __name__ == "__main__":
# Main Function
try:
PLAYER_DB = shelve.open('storage/db.shelf', writeback=True)
if 'player' not in PLAYER_DB:
PLAYER_DB['player'] = {}
APP.run(host='0.0.0.0', port='5555', debug=DEBUG)
except Exception as e:
print(str(e))
finally:
if PLAYER_DB is not None:
PLAYER_DB.sync()
PLAYER_DB.close()