1
- #!/usr/bin/env python2.7
1
+ #!/usr/bin/env python2
2
2
# -*- coding: utf-8 -*-
3
3
4
4
"""
18
18
19
19
import pygame
20
20
21
+ DEBUG = True
22
+
21
23
# define some constants
22
24
E_UID = pygame .USEREVENT + 1
23
25
E_DOWNLOAD = pygame .USEREVENT + 2
@@ -29,7 +31,7 @@ class ReceiverThread(Thread):
29
31
"""
30
32
This thread will listen on a UDP port for packets from the game.
31
33
"""
32
- def __init__ (self , host = '0.0.0.0' , port = 11337 ):
34
+ def __init__ (self , host = '0.0.0.0' , port = 1338 ):
33
35
"""
34
36
Creates the socket and binds it to the given host and port.
35
37
"""
@@ -48,24 +50,26 @@ def run(self):
48
50
logging .warning ('example warning' )
49
51
logging .error ('example error' )
50
52
logging .critical ('example critical' )
51
- #print(datetime.now(), '<<< {}'.format(data))
52
53
if data .startswith ('/uid/' ):
53
- #print(datetime.now(), '### uid', data[5:], 'received')
54
54
e = pygame .event .Event (E_UID , {'uid' : int (data [5 :])})
55
55
pygame .event .post (e )
56
+ if DEBUG : logging .info ('uid received: {}' .format (data [5 :]))
56
57
elif data .startswith ('/download/' ):
57
58
e = pygame .event .Event (E_DOWNLOAD , {'url' : str (data [10 :])})
58
59
pygame .event .post (e )
60
+ if DEBUG : logging .info ('download of {} triggered' .format (data [10 :]))
59
61
elif data .startswith ('/play/' ):
60
62
e = pygame .event .Event (E_PLAY , {'filename' : str (data [6 :])})
61
63
pygame .event .post (e )
64
+ if DEBUG : logging .info ('playback of {} triggered' .format (data [6 :]))
62
65
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 :])})
64
67
pygame .event .post (e )
68
+ if DEBUG : logging .info ('request rumble for {}ms' .format (data [8 :]))
65
69
66
70
67
71
class 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 ):
69
73
self .game_host = game_host # Host of Mate Light
70
74
self .game_port = game_port # Port of Mate Light
71
75
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
98
102
self .rumble_active = False
99
103
self .uid = None
100
104
101
- self ._receiver = ReceiverThread (host , port )
105
+ self ._receiver = ReceiverThread (self . host , self . port )
102
106
self ._receiver .setDaemon (True )
103
107
self ._receiver .start ()
104
108
105
109
def ping (self ):
106
110
if self .uid :
111
+ if DEBUG : logging .info ('sending ping' )
107
112
msg = '/controller/{}/ping/{}' .format (self .uid , self ._receiver .port )
108
113
self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
109
- #print(datetime.now(), '>>>', msg)
110
114
111
115
def send_keys (self ):
112
116
# alternative states creation: [1 if k else 0 for k in self.keys]
113
117
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 ])))
114
119
self .sock .sendto (states .encode ('utf-8' ), (self .game_host , self .game_port ))
115
- #print(datetime.now(), '>>>' + states)
116
120
self .timeout = time .time ()
117
121
118
122
def send_message (self , msg ):
123
+ if DEBUG : logging .info ('sending of messages not yet implemented' )
119
124
pass
120
125
121
126
def disconnect (self ):
127
+ if DEBUG : logging .info ('disconnecting from game' )
122
128
msg = '/controller/{}/kthxbye' .format (self .uid )
123
129
self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
124
130
125
131
def connect (self ):
132
+ if DEBUG : logging .info ('connecting to game' )
126
133
msg = '/controller/new/{}' .format (self .port )
127
134
self .sock .sendto (msg .encode ('utf-8' ), (self .game_host , self .game_port ))
128
135
129
136
def rumble (self , duration ):
137
+ if DEBUG : logging .info ('rumble not yet implemented' )
130
138
pass
131
139
132
140
def download_sound (self , url ):
141
+ if DEBUG : logging .info ('downloading of media files not yet implemented' )
133
142
pass
134
143
135
144
def play_sound (self , filename ):
145
+ if DEBUG : logging .info ('playing media files not yet implemented' )
136
146
pass
137
147
138
148
def handle_inputs (self ):
@@ -147,11 +157,9 @@ def handle_inputs(self):
147
157
elif event .type == pygame .MOUSEBUTTONUP :
148
158
pygame .event .post (pygame .event .Event (pygame .QUIT ))
149
159
elif event .type == E_UID :
150
- #print(datetime.now(), '### UID event received', event.uid)
151
160
self .uid = event .uid
152
161
153
162
if self .uid is not None :
154
- #print(datetime.now(), '### UID set. checking other events')
155
163
if event .type == E_DOWNLOAD :
156
164
self .download_sound (event .url )
157
165
elif event .type == E_PLAY :
@@ -162,22 +170,19 @@ def handle_inputs(self):
162
170
try :
163
171
button = self .mapping [event .key ]
164
172
if event .type == pygame .KEYDOWN :
165
- #print('{} | {}'.format(event.key, button))
166
173
self .keys [button ] = 1
167
174
elif event .type == pygame .KEYUP :
168
- #print('{} | {}'.format(event.key, button))
169
175
self .keys [button ] = 0
170
176
self .send_keys ()
171
177
except KeyError :
172
178
break
173
179
else :
174
- #print(datetime.now(), '### UID not set. connecting to game.')
175
180
self .connect ()
176
181
time .sleep (1 )
177
182
178
183
179
184
if __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 )
181
186
try :
182
187
while True :
183
188
ctlr .handle_inputs ()
0 commit comments