-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathadventure.py
More file actions
141 lines (109 loc) · 3.75 KB
/
adventure.py
File metadata and controls
141 lines (109 loc) · 3.75 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
import hashlib
import json
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request, render_template
from pusher import Pusher
from decouple import config
from room import Room
from player import Player
from world import World
# Look up decouple for config variables
pusher = Pusher(app_id=config('PUSHER_APP_ID'), key=config('PUSHER_KEY'), secret=config('PUSHER_SECRET'), cluster=config('PUSHER_CLUSTER'))
world = World()
app = Flask(__name__)
def get_player_by_header(world, auth_header):
if auth_header is None:
return None
auth_key = auth_header.split(" ")
if auth_key[0] != "Token" or len(auth_key) != 2:
return None
player = world.get_player_by_auth(auth_key[1])
return player
@app.route('/api/registration/', methods=['POST'])
def register():
values = request.get_json()
required = ['username', 'password1', 'password2']
if not all(k in values for k in required):
response = {'message': "Missing Values"}
return jsonify(response), 400
username = values.get('username')
password1 = values.get('password1')
password2 = values.get('password2')
response = world.add_player(username, password1, password2)
if 'error' in response:
return jsonify(response), 500
else:
return jsonify(response), 200
@app.route('/api/login/', methods=['POST'])
def login():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/init/', methods=['GET'])
def init():
player = get_player_by_header(world, request.headers.get("Authorization"))
if player is None:
response = {'error': "Malformed auth header"}
return jsonify(response), 500
response = {
'title': player.current_room.name,
'description': player.current_room.description,
}
return jsonify(response), 200
@app.route('/api/adv/move/', methods=['POST'])
def move():
player = get_player_by_header(world, request.headers.get("Authorization"))
if player is None:
response = {'error': "Malformed auth header"}
return jsonify(response), 500
values = request.get_json()
required = ['direction']
if not all(k in values for k in required):
response = {'message': "Missing Values"}
return jsonify(response), 400
direction = values.get('direction')
if player.travel(direction):
response = {
'title': player.current_room.name,
'description': player.current_room.description,
}
return jsonify(response), 200
else:
response = {
'error': "You cannot move in that direction.",
}
return jsonify(response), 500
@app.route('/api/adv/take/', methods=['POST'])
def take_item():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/drop/', methods=['POST'])
def drop_item():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/inventory/', methods=['GET'])
def inventory():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/buy/', methods=['POST'])
def buy_item():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/sell/', methods=['POST'])
def sell_item():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
@app.route('/api/adv/rooms/', methods=['GET'])
def rooms():
# IMPLEMENT THIS
response = {'error': "Not implemented"}
return jsonify(response), 400
# Run the program on port 5000
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)