-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_engine.py
70 lines (51 loc) · 1.92 KB
/
mini_engine.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
import random
import json
from engine import GameState
from queue import Queue
from paho.mqtt import client as mqttclient
q = Queue()
class GameEngine:
def __init__(self):
self.game_state = GameState()
def connect_mqtt(self):
# Set Connecting Client ID
client = mqttclient.Client(f'python-mqtt-{random.randint(0, 1000)}')
client.on_connect = self.on_connect
# client.username_pw_set(username, password)
client.connect('broker.emqx.io', 1883)
# client.connect('116.15.202.85', 1883)
return client
def on_message(self, client, userdata, message):
q.put(json.loads(message.payload.decode("utf-8")))
def on_connect(self,client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
def run(self):
mqttclient = self.connect_mqtt()
mqttclient.loop_start()
mqttclient.subscribe("lasertag/vizhit")
mqttclient.on_message = self.on_message
while True:
if not q.empty():
# Get data from ext comms
y = q.get()
# ASSUME THAT EVAL_SERVER REPLIES HERE FROM EXT COMMS
valid = self.game_state.update(y)
print("received:" + json.dumps(y))
if (valid): # only draw valid actions
action = y["action"]
else:
action = "none"
x = {
"type": "UPDATE",
"player_id": y["player_id"],
"action": action,
"isHit": y["isHit"],
"game_state": self.game_state.get_dict()
}
mqttclient.publish("lasertag/vizgamestate", json.dumps(x))
print("sent UPDATE:" + json.dumps(x))
engine = GameEngine()
engine.run()