-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.py
138 lines (120 loc) · 4.26 KB
/
attack.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
import os
import json
import aiohttp
from aiohttp import web
BOARD_API_URL = os.environ.get('BOARD_API_URL')
COMBAT_API_URL = os.environ.get('COMBAT_API_URL')
async def test_handler(request):
return web.json_response(
{"my": "json"},
status=200
)
async def hex_attack(request):
data = await request.json()
attacker_id = data.get('attacker').get('hex_id')
defender_id = data.get('defender').get('hex_id')
attacker = await get_hex(attacker_id)
defender = await get_hex(defender_id)
attacker = json.loads(attacker.text)
defender = json.loads(defender.text)
connection = await check_connection(attacker_id, defender_id)
connection = json.loads(connection.text)
if connection.get('Connection') is False:
return web.json_response(
connection,
status=200
)
attacker = {
"hex_id": attacker_id,
"player_id": attacker.get('player_id'),
"tokens": attacker.get('tokens')
}
defender = {
"hex_id": defender_id,
"player_id": defender.get('player_id'),
"tokens": defender.get('tokens')
}
report = await basic_combat(attacker, defender)
if report['combatReport'].get('success'):
await change_ownership(
attacker.get('player_id'),
defender.get('hex_id')
)
await update_tokens(
report['attackingTIle'].get('currentTokens'),
report['defendingTile'].get('id')
)
return web.json_response(
report,
status=200
)
else:
await update_tokens(
report['defendingTile'].get('currentTokens'),
report['defendingTile'].get('id')
)
await update_tokens(0, report['attackingTIle'].get('id'))
return web.json_response(
report,
status=200
)
async def check_connection(from_id, to_id):
async with aiohttp.ClientSession() as session:
url = f'{BOARD_API_URL}/v0/check-connection?from={from_id}&to={to_id}'
async with session.get(url) as resp:
if resp.status == 200:
return web.json_response(
await resp.json(),
)
return web.json_response(
{"error": "check your hex IDs"},
status=404
)
async def change_ownership(player_id, hex_id):
async with aiohttp.ClientSession() as session:
url = f'{BOARD_API_URL}/v0/change-ownership'
data = {
'player_id': player_id,
'hex_id': hex_id
}
data = json.dumps(data)
async with session.patch(url, data=data) as resp:
if resp.status == 200:
return web.json_response(
{'good': 'job'},
status=200)
async def update_tokens(tokens, hex_id):
async with aiohttp.ClientSession() as session:
url = f'{BOARD_API_URL}/v0/update-tokens'
data = {
'tokens': tokens,
'hex_id': hex_id
}
data = json.dumps(data)
async with session.patch(url, data=data) as resp:
if resp.status == 200:
return web.json_response(
{'winner': 'eeeeeee'},
status=200)
else:
return web.json_response(
{'error': 'something went wrong'},
)
async def get_hex(hex_id):
async with aiohttp.ClientSession() as session:
url = f'{BOARD_API_URL}/v0/get-hex?id={hex_id}'
async with session.get(url) as resp:
return web.json_response(
await resp.json(),
)
async def basic_combat(attacker, defender):
data = {
"attacker": attacker,
"defender": defender
}
data = json.dumps(data)
async with aiohttp.ClientSession() as session:
url = f'{COMBAT_API_URL}/v0/basic-combat'
async with session.post(url, data=data) as resp:
data = await resp.json()
return data