Skip to content

Commit 214c1bf

Browse files
author
XenGi
committed
controller ids are now uuids
1 parent 8f43fa7 commit 214c1bf

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
lines changed

pymlgame/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
__email__ = '[email protected]'
1010
__status__ = 'Development'
1111

12-
# from pymlgame.locals import *
13-
# from pymlgame.screen import Screen
14-
# from pymlgame.clock import Clock
15-
# from pymlgame.surface import Surface
12+
from pymlgame.locals import *
13+
from pymlgame.screen import Screen
14+
from pymlgame.clock import Clock
15+
from pymlgame.surface import Surface
1616
from pymlgame.controller import Controller
1717

1818
_ctlr = Controller()

pymlgame/controller.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pymlgame - Controller
55
"""
66

7+
from uuid import uuid4
78
import time
89
from datetime import datetime
910
import socket
@@ -19,8 +20,6 @@ class Controller(Thread):
1920
A controller can be a game controller attached to the system or any other input that can trigger the controller
2021
functions like a smartphone app.
2122
"""
22-
_next_uid = 0
23-
2423
def __init__(self, host='0.0.0.0', port=1338):
2524
"""
2625
Creates a controller deamon
@@ -37,29 +36,25 @@ def _new_controller(self, addr, port):
3736
"""
3837
Get an uid for your controller.
3938
"""
40-
#print(datetime.now(), '### new controller at', addr, ':', port)
4139
for uid, controller in self.controllers.items():
4240
if controller[0] == addr:
43-
#print(datetime.now(), '### duplicate address. not adding this one.')
4441
# duplicate address. sending the uid again
45-
#print(datetime.now(), '>>> /uid/{}'.format(uid), addr, port)
42+
print('/uid/{} => {}:{}'.format(uid, addr, port))
4643
self.sock.sendto('/uid/{}'.format(uid).encode('utf-8'), (addr, port))
4744
return False
4845

4946
# get an uid and add the controller to the game
50-
uid = self._next_uid
51-
self._next_uid += 1
47+
uid = str(uuid4())
5248
self.controllers[uid] = [addr, port, '00000000000000', time.time()]
5349

5450
# tell the controller about it
55-
#print(datetime.now(), '>>> /uid/{}'.format(uid), addr, port)
51+
print('/uid/{} => {}:{}'.format(uid, addr, port))
5652
self.sock.sendto('/uid/{}'.format(uid).encode('utf-8'), (addr, port))
5753

5854
# create event for pymlgame
5955
e = Event(uid, E_NEWCTLR)
6056
self.queue.put_nowait(e)
6157

62-
#print(datetime.now(), '### controller added with uid', uid)
6358
return uid
6459

6560
def _del_controller(self, uid):
@@ -68,7 +63,6 @@ def _del_controller(self, uid):
6863
"""
6964
try:
7065
self.controllers.pop(uid)
71-
#print(datetime.now(), '### controller', uid, 'deleted')
7266
e = Event(uid, E_DISCONNECT)
7367
self.queue.put_nowait(e)
7468
except KeyError:
@@ -97,20 +91,19 @@ def _update_states(self, uid, states):
9791
"""
9892
#TODO: use try and catch all exceptions
9993
# test if uid exists
100-
#print(datetime.now(), '### Checking states', states, 'for controller', uid)
10194
if self.controllers[uid]:
10295
# test if states have correct lenght
10396
if len(states) == 14:
10497
old_states = self.controllers[uid][2]
10598
if old_states != states:
106-
#print(datetime.now(), '### checking old states', old_states, 'against new states', states)
10799
for key in range(14):
108100
if int(old_states[key]) > int(states[key]):
109101
e = Event(uid, E_KEYUP, key)
110102
self.queue.put_nowait(e)
111103
elif int(old_states[key]) < int(states[key]):
112104
e = Event(uid, E_KEYDOWN, key)
113105
self.queue.put_nowait(e)
106+
self.controllers[uid][2] = states
114107
self.controllers[uid][3] = time.time()
115108

116109
def _got_message(self, uid, text):
@@ -135,16 +128,14 @@ def send(self, uid, event, payload):
135128
addr = self.controllers[uid][0]
136129
port = self.controllers[uid][1]
137130
if event == E_MESSAGE:
138-
#print(datetime.now(), '>>> /message/{}'.format(payload), addr, port)
131+
print('/message/{} => {}:{}'.format(payload, addr, port))
139132
return sock.sendto('/message/{}'.format(payload).encode('utf-8'), (addr, port))
140133
elif event == E_RUMBLE:
141-
#print(datetime.now(), '>>> /rumble/{}'.format(payload), addr, port)
134+
print('/rumble/{} => {}:{}'.format(payload, addr, port))
142135
return sock.sendto('/rumble/{}'.format(payload).encode('utf-8'), (addr, port))
143136
else:
144-
#print(datetime.now(), '### Unknown event type.')
145137
pass
146138
else:
147-
#print(datetime.now(), '### This UID ({}) doesn\'t exist.'.format(uid))
148139
pass
149140
return False
150141

@@ -156,15 +147,14 @@ def run(self):
156147
data, sender = self.sock.recvfrom(1024)
157148
addr = sender[0]
158149
msg = data.decode('utf-8')
159-
#print(datetime.now(), '<<<', msg)
150+
print('New msg: ' + msg)
160151
if msg.startswith('/controller/'):
161152
try:
162153
uid = msg.split('/')[2]
163154
if uid == 'new':
164155
port = int(msg.split('/')[3])
165156
self._new_controller(addr, port)
166157
else:
167-
uid = int(uid)
168158
cmd = msg.split('/')[3]
169159
if cmd == 'ping':
170160
port = msg.split('/')[3]
@@ -176,17 +166,15 @@ def run(self):
176166
self._update_states(uid, states)
177167
elif cmd == 'text':
178168
# /controller/<uid>/text/<text>
179-
text = msg[12 + len(str(uid)) + 6:]
169+
text = msg[12 + len(uid) + 6:]
180170
self._got_message(uid, text)
181171
except IndexError or KeyError:
182-
#print(datetime.now(), '### Error in coitus protocol.')
183172
pass
184173
else:
185-
#print(datetime.now(), '### This thing doesn\'t fit:', msg)
186174
pass
187175

188176
# find unused controllers and delete them
189177
ctlrs = self.controllers.items()
190178
for uid, state in ctlrs:
191179
if state[3] < time.time() - 60:
192-
self.controllers.pop(uid)
180+
self.controllers.pop(uid)

pymlgame/event.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Event(object):
1111
def __init__(self, uid, type, data=None):
1212
self.uid = uid
1313
self.type = type
14-
if type == E_KEYDOWN or E_KEYUP:
14+
if type == E_KEYDOWN or type == E_KEYUP:
1515
self.button = data
1616
else:
17-
self.data = data
17+
self.data = data

0 commit comments

Comments
 (0)