forked from aleX3645/PythonServerOfGroupProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (50 loc) · 1.97 KB
/
app.py
File metadata and controls
66 lines (50 loc) · 1.97 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
from flask import Flask, request, jsonify
from flask_restful import Api, Resource
from BotMapper import BotMapper
from GameServer import GameServer
app = Flask(__name__)
api = Api(app)
array = []
class Fight(Resource):
def post(self):
return self.start_game_by_json(request.get_json())
def start_game_by_json(self, req):
field = self.get_int_field((req['arena'])['field'])
temp_pos = self.get_position_for(field, 1)
temp = req['robot1']
bot_stat1 = BotMapper(temp['charge'], temp['weight'], temp['speed'],
temp['tiredness'], temp['damage'], temp['range'],
temp_pos[0], temp_pos[1], field)
bot1_string = (temp['botAlgorithm'])['algorithm']
temp_pos = self.get_position_for(field, 2)
temp = req['robot2']
bot_stat2 = BotMapper(temp['charge'], temp['weight'], temp['speed'],
temp['tiredness'], temp['damage'],
temp['range'], temp_pos[0], temp_pos[1], field)
bot2_string = (temp['botAlgorithm'])['algorithm']
game = GameServer(bot1_string, bot2_string, bot_stat1, bot_stat2, field)
res = game.start()
return jsonify(
{"winner": res[0], "fight_map": res[1]}
)
def get_position_for(self, field, t: int):
for x in range(len(field)):
for y in range(len(field[x])):
if field[x][y] == t:
return [x, y]
def get_int_field(self, field):
temp_field = field.split("\n")
f_res = []
for x in range(len(temp_field)):
list = []
for y in temp_field[x]:
if y == '0':
list.append(0)
elif y == '1':
list.append(1)
elif y == '2':
list.append(2)
f_res.append(list)
print(f_res)
return f_res
api.add_resource(Fight, '/fight/')