-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (130 loc) · 3.94 KB
/
main.py
File metadata and controls
177 lines (130 loc) · 3.94 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import json
import random
from tile import *
from position import *
from board import Board
from bathysEncoder import BathysEncoder
from sub import Sub
from quart import jsonify, request
from quart import Quart, send_from_directory
from asyncio import Event
app = Quart(__name__, static_folder='static')
app.json_encoder = BathysEncoder
random.seed("bathys")
board = Board(25, 10)
sub = Sub((0, board.size_y-1))
# sub_loc = (0, board_y_size-1)
def gen_positions():
navigator = Position()
navigator.uniq = "navigator"
navigator.name = "Navigator"
operator = Position()
operator.uniq = "operator"
operator.name = "Sonar Operator"
return [navigator, operator]
players = []
positions = gen_positions()
position_to_player = {}
for position in positions:
position_to_player[position.uniq] = None
board.reveal_tile(tup=sub.loc)
request_position_events = {}
def common_move_eval(x, y):
new_loc = (x, y)
board.reveal_tile(x, y)
sub.loc = new_loc
def moveSubDown():
global sub
x, y = sub.loc
if y < board.size_y-1:
y += 1
common_move_eval(x, y)
def moveSubUp():
global sub
x, y = sub.loc
if y > 0:
y -= 1
common_move_eval(x, y)
def moveSubLeft():
global sub
x, y = sub.loc
if x > 0:
x -= 1
common_move_eval(x, y)
def moveSubRight():
global sub
x, y = sub.loc
if x < board.size_x-1:
x += 1
common_move_eval(x, y)
@app.route("/registerPlayer",methods=['GET','POST'])
async def registerPlayer():
content = await request.json
player_id = content["playerId"]
players.append(player_id)
request_position_events[player_id] = Event()
return jsonify(True)
@app.route("/requestPosition",methods=['GET','POST'])
async def requestPosition():
content = await request.json
player_id = content["playerId"]
position_uniq = content["positionUniq"]
if position_to_player[position_uniq] is None:
position_to_player[position_uniq] = player_id
print("Assigning "+player_id+" to "+position_uniq)
for other_player_id in players:
if other_player_id != player_id:
request_position_events[other_player_id].set()
request_position_events[other_player_id].clear()
return jsonify(True)
else:
return jsonify(False)
@app.route("/moveSub", methods=['GET','POST'])
async def moveSub():
move_map = {"DOWN": moveSubDown, "UP": moveSubUp, "LEFT": moveSubLeft, "RIGHT": moveSubRight}
content = await request.json
direction = content["direction"]
move_map[direction]()
x, y = sub.loc
return jsonify({"x": x, "y": y, 'new_tile': board[(x, y)]})
@app.route("/getSubLoc")
async def getSubLoc():
global sub
x, y = sub.loc
return jsonify({"x": x, "y": y})
@app.route("/getPositions")
async def getPositions():
global positions
return jsonify(positions)
@app.route("/getPositionMappingLong",methods=['GET','POST'])
async def getPositionMappingLong():
global position_to_player
global request_position_events
content = await request.json
player_id = content["playerId"]
await request_position_events[player_id].wait()
return jsonify(position_to_player)
@app.route("/getPositionMapping")
async def getPositionMapping():
global position_to_player
return jsonify(position_to_player)
@app.route("/favicon.ico")
async def favicon():
return await send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico')
@app.route("/board")
async def board_func():
return jsonify(board)
@app.route('/bundle.js')
async def bundle():
return await app.send_static_file('./bundle.js')
@app.route('/index.css')
async def style():
return await app.send_static_file('./index.css')
@app.route('/')
async def root():
return await app.send_static_file('./index.html')
if __name__ == '__main__':
test = Board.GenTree(10,25)
print(test)
# app.run(host='localhost', port=5000)