1- #!/usr/bin/env python2.7
1+ #!/usr/bin/env python2
22# -*- coding: utf-8 -*-
33
44"""
1818
1919import pygame
2020
21+ DEBUG = True
22+
2123# define some constants
2224E_UID = pygame .USEREVENT + 1
2325E_DOWNLOAD = pygame .USEREVENT + 2
@@ -29,7 +31,7 @@ class ReceiverThread(Thread):
2931 """
3032 This thread will listen on a UDP port for packets from the game.
3133 """
32- def __init__ (self , host = '0.0.0.0' , port = 11337 ):
34+ def __init__ (self , host = '0.0.0.0' , port = 1338 ):
3335 """
3436 Creates the socket and binds it to the given host and port.
3537 """
@@ -48,24 +50,26 @@ def run(self):
4850 logging .warning ('example warning' )
4951 logging .error ('example error' )
5052 logging .critical ('example critical' )
51- #print(datetime.now(), '<<< {}'.format(data))
5253 if data .startswith ('/uid/' ):
53- #print(datetime.now(), '### uid', data[5:], 'received')
5454 e = pygame .event .Event (E_UID , {'uid' : int (data [5 :])})
5555 pygame .event .post (e )
56+ if DEBUG : logging .info ('uid received: {}' .format (data [5 :]))
5657 elif data .startswith ('/download/' ):
5758 e = pygame .event .Event (E_DOWNLOAD , {'url' : str (data [10 :])})
5859 pygame .event .post (e )
60+ if DEBUG : logging .info ('download of {} triggered' .format (data [10 :]))
5961 elif data .startswith ('/play/' ):
6062 e = pygame .event .Event (E_PLAY , {'filename' : str (data [6 :])})
6163 pygame .event .post (e )
64+ if DEBUG : logging .info ('playback of {} triggered' .format (data [6 :]))
6265 elif data .startswith ('/rumble/' ):
63- e = pygame .event .Event (E_RUMBLE , {'duration' : float (data [8 :]. replace ( ',' , '.' ) )})
66+ e = pygame .event .Event (E_RUMBLE , {'duration' : int (data [8 :])})
6467 pygame .event .post (e )
68+ if DEBUG : logging .info ('request rumble for {}ms' .format (data [8 :]))
6569
6670
6771class Controller (object ):
68- def __init__ (self , game_host = '127.0.0.1' , game_port = 1338 , host = '0.0.0.0' , port = 11337 ):
72+ def __init__ (self , game_host = '127.0.0.1' , game_port = 1338 , host = '0.0.0.0' , port = 1338 ):
6973 self .game_host = game_host # Host of Mate Light
7074 self .game_port = game_port # Port of Mate Light
7175 self .host = host # Host of ReceiverThread
@@ -98,41 +102,47 @@ def __init__(self, game_host='127.0.0.1', game_port=1338, host='0.0.0.0', port=1
98102 self .rumble_active = False
99103 self .uid = None
100104
101- self ._receiver = ReceiverThread (host , port )
105+ self ._receiver = ReceiverThread (self . host , self . port )
102106 self ._receiver .setDaemon (True )
103107 self ._receiver .start ()
104108
105109 def ping (self ):
106110 if self .uid :
111+ if DEBUG : logging .info ('sending ping' )
107112 msg = '/controller/{}/ping/{}' .format (self .uid , self ._receiver .port )
108113 self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
109- #print(datetime.now(), '>>>', msg)
110114
111115 def send_keys (self ):
112116 # alternative states creation: [1 if k else 0 for k in self.keys]
113117 states = '/controller/{}/states/{}' .format (self .uid , '' .join ([str (k ) for k in self .keys ]))
118+ if DEBUG : logging .info ('sending states {}' .format ('' .join ([str (k ) for k in self .keys ])))
114119 self .sock .sendto (states .encode ('utf-8' ), (self .game_host , self .game_port ))
115- #print(datetime.now(), '>>>' + states)
116120 self .timeout = time .time ()
117121
118122 def send_message (self , msg ):
123+ if DEBUG : logging .info ('sending of messages not yet implemented' )
119124 pass
120125
121126 def disconnect (self ):
127+ if DEBUG : logging .info ('disconnecting from game' )
122128 msg = '/controller/{}/kthxbye' .format (self .uid )
123129 self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
124130
125131 def connect (self ):
132+ if DEBUG : logging .info ('connecting to game' )
126133 msg = '/controller/new/{}' .format (self .port )
127134 self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
128135
129136 def rumble (self , duration ):
137+ if DEBUG : logging .info ('rumble not yet implemented' )
130138 pass
131139
132140 def download_sound (self , url ):
141+ if DEBUG : logging .info ('downloading of media files not yet implemented' )
133142 pass
134143
135144 def play_sound (self , filename ):
145+ if DEBUG : logging .info ('playing media files not yet implemented' )
136146 pass
137147
138148 def handle_inputs (self ):
@@ -147,11 +157,9 @@ def handle_inputs(self):
147157 elif event .type == pygame .MOUSEBUTTONUP :
148158 pygame .event .post (pygame .event .Event (pygame .QUIT ))
149159 elif event .type == E_UID :
150- #print(datetime.now(), '### UID event received', event.uid)
151160 self .uid = event .uid
152161
153162 if self .uid is not None :
154- #print(datetime.now(), '### UID set. checking other events')
155163 if event .type == E_DOWNLOAD :
156164 self .download_sound (event .url )
157165 elif event .type == E_PLAY :
@@ -162,22 +170,19 @@ def handle_inputs(self):
162170 try :
163171 button = self .mapping [event .key ]
164172 if event .type == pygame .KEYDOWN :
165- #print('{} | {}'.format(event.key, button))
166173 self .keys [button ] = 1
167174 elif event .type == pygame .KEYUP :
168- #print('{} | {}'.format(event.key, button))
169175 self .keys [button ] = 0
170176 self .send_keys ()
171177 except KeyError :
172178 break
173179 else :
174- #print(datetime.now(), '### UID not set. connecting to game.')
175180 self .connect ()
176181 time .sleep (1 )
177182
178183
179184if __name__ == '__main__' :
180- ctlr = Controller ('127.0.0.1' , 1338 , '0.0.0.0' , 11337 )
185+ ctlr = Controller ('127.0.0.1' , 1338 , '0.0.0.0' , 1338 )
181186 try :
182187 while True :
183188 ctlr .handle_inputs ()
0 commit comments