-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehaviour.py
78 lines (66 loc) · 2.27 KB
/
behaviour.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
import os
import json
import aiohttp
from aiohttp import web
BOARD_API_URL = os.environ.get('BOARD_API_URL')
PLAYER_API_URL = os.environ.get('PLAYER_API_URL')
headers = {"Access-Control-Allow-Origin": "*"}
async def update_turn(next_player, board_id):
url = f'{BOARD_API_URL}/v0/update-turn'
data = {
'next': next_player,
'id': board_id
}
data = json.dumps(data)
async with aiohttp.ClientSession() as session:
async with session.patch(url, data=data) as resp:
data = await resp.json()
return web.json_response(
data,
headers=headers,
status=200
)
async def players_on_board(board_id):
url = f'{PLAYER_API_URL}/v0/board-players?id={board_id}'
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
players = await resp.json()
return players
if resp.status == 500:
return {'Error': 'Server error'}
async def get_board(board_id):
url = f'{BOARD_API_URL}/v0/get-board?id={board_id}'
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
board = await resp.json()
return board
async def load_board(request):
params = request.rel_url.query
board_id = params['id']
board = await get_board(board_id)
if 'Error' in board:
return web.json_response(
board,
headers=headers,
status=404
)
return web.json_response(
board,
headers=headers,
status=200
)
async def update_tokens(tokens, defender_id):
async with aiohttp.ClientSession() as session:
url = f'{BOARD_API_URL}/v0/add-tokens'
data = {
'tokens': tokens,
'territory_id': defender_id
}
async with session.patch(url, data=data) as resp:
if resp.status == 200:
return web.Response(
text='we did it', status=200)
else:
return web.json_response(
{'error': 'something went wrong'}, status=400)