|
|
@@ -5,9 +5,15 @@ Competitive tetris server
|
|
|
"""
|
|
|
import random
|
|
|
import pygame
|
|
|
+import zmq
|
|
|
+import flatbuffers
|
|
|
|
|
|
import tetrominos
|
|
|
import game
|
|
|
+import wire_protocol.Command
|
|
|
+import wire_protocol.CommandRequest
|
|
|
+import wire_protocol.CommandResponse
|
|
|
+import wire_protocol.CommandResponseError
|
|
|
|
|
|
pygame.font.init()
|
|
|
|
|
|
@@ -102,6 +108,12 @@ def main(win):
|
|
|
frame_time = 0
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
|
+ zmq_context = zmq.Context()
|
|
|
+ socket = zmq_context.socket(zmq.REP)
|
|
|
+ socket.bind("tcp://*:5555")
|
|
|
+ poller = zmq.Poller()
|
|
|
+ poller.register(socket, zmq.POLLIN)
|
|
|
+
|
|
|
def _next_piece_func():
|
|
|
return random.choice(range(len(tetrominos.SHAPES)))
|
|
|
game_state = game.initial_game_state(_next_piece_func(),
|
|
|
@@ -117,15 +129,31 @@ def main(win):
|
|
|
run = False
|
|
|
pygame.display.quit()
|
|
|
|
|
|
- if event.type == pygame.KEYDOWN:
|
|
|
- if event.key == pygame.K_LEFT:
|
|
|
- game_state = game.game_state_shift(game_state, -1)
|
|
|
- if event.key == pygame.K_RIGHT:
|
|
|
- game_state = game.game_state_shift(game_state, 1)
|
|
|
- if event.key == pygame.K_UP:
|
|
|
- game_state = game.game_state_rotate(game_state)
|
|
|
- if event.key == pygame.K_DOWN:
|
|
|
- game_state = game.game_state_land(game_state)
|
|
|
+ for sock, _ in poller.poll(timeout=0):
|
|
|
+ req_buf = sock.recv()
|
|
|
+ req = wire_protocol.CommandRequest.CommandRequest.GetRootAs(req_buf)
|
|
|
+
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
+
|
|
|
+ if req.Command() == wire_protocol.Command.Command.ShiftLeft:
|
|
|
+ game_state = game.game_state_shift(game_state, -1)
|
|
|
+ elif req.Command() == wire_protocol.Command.Command.ShiftRight:
|
|
|
+ game_state = game.game_state_shift(game_state, 1)
|
|
|
+ elif req.Command() == wire_protocol.Command.Command.Rotate:
|
|
|
+ game_state = game.game_state_rotate(game_state)
|
|
|
+ elif req.Command() == wire_protocol.Command.Command.Land:
|
|
|
+ game_state = game.game_state_land(game_state)
|
|
|
+ else:
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.UNKNOWN_COMMAND
|
|
|
+
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
+ builder.Finish(rsp)
|
|
|
+ socket.send(builder.Output())
|
|
|
+
|
|
|
+
|
|
|
|
|
|
is_game_running = True
|
|
|
if frame_time >= 500:
|