forked from Alan01252/Shoreditch-SampleAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
88 lines (74 loc) · 2.11 KB
/
player.py
File metadata and controls
88 lines (74 loc) · 2.11 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
#############
### Sample AI
#############
from bottle import post, put, delete
from bottle import request, abort, run
from storage import use_db
import logic
import config
import thread
from game import Game
def use_game(f):
def inner_func(db, session_id, *args, **kwargs):
g = db.get(session_id)
if not g or not g.get('type') == 'game':
abort(400, 'Not a valid game')
if not 'json' in kwargs:
if not request.json:
abort(400, 'Must use Content-type of application/json')
kwargs['json'] = request.json
if not 'player' in kwargs['json']:
abort(400, 'Must pass "player"')
player = kwargs['json']['player']
del kwargs['json']
game = Game(g, player)
return f(db, game, *args, **kwargs)
return inner_func
@put('/game/:session_id')
@use_db
def start_game(db, session_id):
game = {"type": "game", "id": session_id, "storage": {}, "endpoint": request.json['endpoint']}
db.save(game)
return {"status": "success"}
@post('/game/:session_id/start_turn')
@use_db
@use_game
def start_turn(db, game):
def run_turn():
game.turn = True
logic.start_turn(db, game, game.actions)
thread.start_new_thread(run_turn, ())
return {"status": "success"}
@post('/game/:session_id/trade')
@use_db
@use_game
def incoming_trade(db, game):
if not 'offering' in request.json:
abort(400, 'Must offer something')
if not 'requesting' in request.json:
abort(400, 'Must request something')
if not logic.incoming_trade(db, game, request.json['player'], request.json['offering'], request.json['requesting']):
abort(500, "No deal")
return {"status": "success"}
@delete('/game/:session_id')
@use_db
@use_game
def end_game(db, game):
if request.json and 'error' in request.json:
def run_end():
logic.end_game(db,game,request.json['error'])
else:
def run_end():
logic.end_game(db,game)
thread.start_new_thread(run_end, ())
return {"status": "success"}
@post('/game/:session_id/end_turn')
@use_db
@use_game
def end_turn(db, game):
def run_end_turn():
game.turn = False
logic.time_up(db,game)
thread.start_new_thread(run_end_turn, ())
return {"status": "success"}
run(host='localhost', port=config.PORT, reloader=False)