diff options
| author | Billy Brown | 2017-12-02 20:40:46 +0000 |
|---|---|---|
| committer | Billy Brown | 2017-12-02 20:40:46 +0000 |
| commit | 28f4d872a2be2467fdbd494356f11f518110c6a5 (patch) | |
| tree | e2a3c8e9b897d9488992090a60a6acd0c974f31f | |
| parent | Ball is reset after goal (diff) | |
| download | longpong-28f4d872a2be2467fdbd494356f11f518110c6a5.tar.gz longpong-28f4d872a2be2467fdbd494356f11f518110c6a5.zip | |
Supporting multiple messages at once
| -rw-r--r-- | client/client.py | 40 | ||||
| -rw-r--r-- | client/game.py | 8 | ||||
| -rwxr-xr-x | server/server.js | 30 |
3 files changed, 46 insertions, 32 deletions
diff --git a/client/client.py b/client/client.py index 5614bb9..167a73e 100644 --- a/client/client.py +++ b/client/client.py @@ -14,22 +14,29 @@ class Client: def listen(self): try: - msg = self.sock.recv(4096) - jsonMSG = json.loads(msg.decode()) - print(jsonMSG) - if 'command' in jsonMSG: - val = jsonMSG['command'] - if val == 'start': - self.started = True - self.startupMessage = jsonMSG - self.msgs.append(jsonMSG) - elif val =='stop': - self.stopped = True - self.stoppedMessage = jsonMSG - else: + msg = self.sock.recv(4096).decode() + for message in msg.split('|'): + if len(message) <= 0: + continue + try: + jsonMSG = json.loads(message) + except json.decoder.JSONDecodeError as e: + print(message) + sys.exit(1) + print(jsonMSG) + if 'command' in jsonMSG: + val = jsonMSG['command'] + if val == 'start': + self.started = True + self.startupMessage = jsonMSG self.msgs.append(jsonMSG) - else: - print("It really shouldn't be here") + elif val =='stop': + self.stopped = True + self.stoppedMessage = jsonMSG + else: + self.msgs.append(jsonMSG) + else: + print("It really shouldn't be here") except socket.error as e: pass @@ -60,7 +67,8 @@ class Client: def send_ballchange(self, msg): msg['command'] = "bchange" jsonVal = json.dumps(msg) - self.sock.sendall(str(jsonVal).encode()) + message = str(jsonVal) + "|" + self.sock.sendall(message.encode()) def has_started(self): return self.started diff --git a/client/game.py b/client/game.py index 36e2208..656e813 100644 --- a/client/game.py +++ b/client/game.py @@ -133,8 +133,9 @@ class Game: # bounce the balls off the paddles for paddle in self.paddle_list: if ball.is_touching(paddle): - ball.vel = (-1.1 * ball.vel[0], - ball.vel[1] + 1 * ((paddle.pos[1] + (paddle.size[1] / 2)) - (ball.pos[1] + (ball.size[1] / 2)))) + paddle_centre = paddle.pos[1] + paddle.size[1] / 2 + ball_centre = ball.pos[1] + ball.size[1] / 2 + ball.vel = (-1.1 * ball.vel[0], ball.vel[1] - 10 * (paddle_centre - ball_centre)) msg = { 'ball_id': i, 'vel': { @@ -160,8 +161,7 @@ class Game: b.reset() b.vel = (b.vel[0] * direction, b.vel[1]) # self.events.has_key - paddle_speed = 0.2 * deltaT - print(self.paddle_list[0].pos[1]) + paddle_speed = 0.4 * deltaT if len(self.paddle_list) >= 1: if self.events.has_key(sdl2.SDLK_w): if self.paddle_list[0].pos[1] > 0: diff --git a/server/server.js b/server/server.js index 7c413f7..de62da3 100755 --- a/server/server.js +++ b/server/server.js @@ -44,7 +44,7 @@ function forward(d) { clients.clear(); default: if (!d.time) d.time = (new Date()).getTime(); - clients.forEach(x => x.write(JSON.stringify(d))); + clients.forEach(x => x.write(JSON.stringify(d) + '|')); } } @@ -58,18 +58,24 @@ let server = net.createServer((s) => { console.log('new client: ' + s.remoteAddress); s.on('data', (data) => { - var d = JSON.parse(data.toString()); - console.log(d); + let messages = data.toString().split('|') + messages.forEach((message, index) => { + if (message.length <= 0) { + return; + } + var d = JSON.parse(message.toString()); + console.log(d); - console.log(d.info); - console.log(d); - if (d.command) { - forward(d); - } else if (d.info && !running) { - update(s, d.info); - } else { - console.log('not expecting ' + JSON.stringify(d)); - } + console.log(d.info); + console.log(d); + if (d.command) { + forward(d); + } else if (d.info && !running) { + update(s, d.info); + } else { + console.log('not expecting ' + JSON.stringify(d)); + } + }); }); }); |
