|
@@ -15,6 +15,7 @@ import wire_protocol.CommandRequest
|
|
|
import wire_protocol.CommandResponse
|
|
import wire_protocol.CommandResponse
|
|
|
import wire_protocol.Playfield
|
|
import wire_protocol.Playfield
|
|
|
import wire_protocol.CommandResponseError
|
|
import wire_protocol.CommandResponseError
|
|
|
|
|
+import wire_protocol.GameState
|
|
|
|
|
|
|
|
pygame.font.init()
|
|
pygame.font.init()
|
|
|
|
|
|
|
@@ -104,6 +105,111 @@ def draw_game_state_to_window(surface, state):
|
|
|
draw_grid(surface, field)
|
|
draw_grid(surface, field)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def handle_shift_left(state):
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
+ next_state = state.shift(-1)
|
|
|
|
|
+ if next_state is None:
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
+ else:
|
|
|
|
|
+ state = next_state
|
|
|
|
|
+
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_shift_right(state):
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
+ next_state = state.shift(1)
|
|
|
|
|
+ if next_state is None:
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
+ else:
|
|
|
|
|
+ state = next_state
|
|
|
|
|
+
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_rotate(state):
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
+ next_state = state.rotate()
|
|
|
|
|
+ if next_state is None:
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
+ else:
|
|
|
|
|
+ state = next_state
|
|
|
|
|
+
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_land(state):
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
+ next_state = state.land()
|
|
|
|
|
+ if next_state is None:
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
+ else:
|
|
|
|
|
+ state = next_state
|
|
|
|
|
+
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_other(state):
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(
|
|
|
|
|
+ builder,
|
|
|
|
|
+ wire_protocol.CommandResponseError.CommandResponseError.UNKNOWN_COMMAND)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def handle_get_game_state(state):
|
|
|
|
|
+ error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
+ builder = flatbuffers.Builder()
|
|
|
|
|
+
|
|
|
|
|
+ wire_protocol.GameState.StartGridVector(builder, state.data.grid.size)
|
|
|
|
|
+ builder.head -= state.data.grid.size
|
|
|
|
|
+ builder.Bytes[builder.head:(builder.head + state.data.grid.size)] = state.data.grid.tobytes()
|
|
|
|
|
+ grid_vector = builder.EndVector(state.data.grid.size)
|
|
|
|
|
+
|
|
|
|
|
+ wire_protocol.GameState.Start(builder)
|
|
|
|
|
+ wire_protocol.GameState.AddRows(builder, state.data.grid.shape[0])
|
|
|
|
|
+ wire_protocol.GameState.AddCols(builder, state.data.grid.shape[1])
|
|
|
|
|
+ wire_protocol.GameState.AddNextPiece(builder, state.data.next_piece)
|
|
|
|
|
+ wire_protocol.GameState.AddCurrentPiece(builder, state.data.current_piece)
|
|
|
|
|
+ wire_protocol.GameState.AddPieceRotation(builder, state.data.rotation)
|
|
|
|
|
+ wire_protocol.GameState.AddPieceRow(builder, state.data.row)
|
|
|
|
|
+ wire_protocol.GameState.AddPieceCol(builder, state.data.col)
|
|
|
|
|
+ wire_protocol.GameState.AddScore(builder, state.data.score)
|
|
|
|
|
+ wire_protocol.GameState.AddGrid(builder, grid_vector)
|
|
|
|
|
+ game_state_msg = wire_protocol.GameState.End(builder)
|
|
|
|
|
+
|
|
|
|
|
+ wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
+ wire_protocol.CommandResponse.AddGameState(builder, game_state_msg)
|
|
|
|
|
+ rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
+ builder.Finish(rsp)
|
|
|
|
|
+
|
|
|
|
|
+ return state, builder
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def main(win):
|
|
def main(win):
|
|
|
run = True
|
|
run = True
|
|
|
frame_time = 0
|
|
frame_time = 0
|
|
@@ -132,59 +238,21 @@ def main(win):
|
|
|
for sock, _ in poller.poll(timeout=0):
|
|
for sock, _ in poller.poll(timeout=0):
|
|
|
req_buf = sock.recv()
|
|
req_buf = sock.recv()
|
|
|
req = wire_protocol.CommandRequest.CommandRequest.GetRootAs(req_buf)
|
|
req = wire_protocol.CommandRequest.CommandRequest.GetRootAs(req_buf)
|
|
|
-
|
|
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.OK
|
|
|
|
|
- cells = None
|
|
|
|
|
|
|
+ builder = None
|
|
|
|
|
|
|
|
if req.Command() == wire_protocol.Command.Command.ShiftLeft:
|
|
if req.Command() == wire_protocol.Command.Command.ShiftLeft:
|
|
|
- next_state = state.shift(-1)
|
|
|
|
|
- if next_state is None:
|
|
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
- else:
|
|
|
|
|
- state = next_state
|
|
|
|
|
|
|
+ state, builder = handle_shift_left(state)
|
|
|
elif req.Command() == wire_protocol.Command.Command.ShiftRight:
|
|
elif req.Command() == wire_protocol.Command.Command.ShiftRight:
|
|
|
- next_state = state.shift(1)
|
|
|
|
|
- if next_state is None:
|
|
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
- else:
|
|
|
|
|
- state = next_state
|
|
|
|
|
|
|
+ state, builder = handle_shift_right(state)
|
|
|
elif req.Command() == wire_protocol.Command.Command.Rotate:
|
|
elif req.Command() == wire_protocol.Command.Command.Rotate:
|
|
|
- next_state = state.rotate()
|
|
|
|
|
- if next_state is None:
|
|
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
- else:
|
|
|
|
|
- state = next_state
|
|
|
|
|
|
|
+ state, builder = handle_rotate(state)
|
|
|
elif req.Command() == wire_protocol.Command.Command.Land:
|
|
elif req.Command() == wire_protocol.Command.Command.Land:
|
|
|
- next_state = state.land()
|
|
|
|
|
- if next_state is None:
|
|
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.INVALID_COMMAND
|
|
|
|
|
- else:
|
|
|
|
|
- state = next_state
|
|
|
|
|
|
|
+ state, builder = handle_land(state)
|
|
|
elif req.Command() == wire_protocol.Command.Command.GetGameState:
|
|
elif req.Command() == wire_protocol.Command.Command.GetGameState:
|
|
|
- cells = state.get_field()
|
|
|
|
|
|
|
+ state, builder = handle_get_game_state(state)
|
|
|
else:
|
|
else:
|
|
|
- error_code = wire_protocol.CommandResponseError.CommandResponseError.UNKNOWN_COMMAND
|
|
|
|
|
-
|
|
|
|
|
- builder = flatbuffers.Builder()
|
|
|
|
|
- playfield = None
|
|
|
|
|
- if cells is not None:
|
|
|
|
|
- wire_protocol.Playfield.StartCellsVector(builder, cells.size)
|
|
|
|
|
- builder.head -= cells.size
|
|
|
|
|
- builder.Bytes[builder.head:(builder.head + cells.size)] = cells.tobytes()
|
|
|
|
|
- cells_vector = builder.EndVector(cells.size)
|
|
|
|
|
-
|
|
|
|
|
- wire_protocol.Playfield.Start(builder)
|
|
|
|
|
- wire_protocol.Playfield.AddRows(builder, cells.shape[0])
|
|
|
|
|
- wire_protocol.Playfield.AddCols(builder, cells.shape[1])
|
|
|
|
|
- wire_protocol.Playfield.AddCells(builder, cells_vector)
|
|
|
|
|
- playfield = wire_protocol.Playfield.End(builder)
|
|
|
|
|
-
|
|
|
|
|
- wire_protocol.CommandResponse.Start(builder)
|
|
|
|
|
- wire_protocol.CommandResponse.AddError(builder, error_code)
|
|
|
|
|
- if playfield is not None:
|
|
|
|
|
- wire_protocol.CommandResponse.AddPlayfield(builder, playfield)
|
|
|
|
|
- rsp = wire_protocol.CommandResponse.End(builder)
|
|
|
|
|
- builder.Finish(rsp)
|
|
|
|
|
|
|
+ state, builder = handle_other(state)
|
|
|
|
|
+
|
|
|
socket.send(builder.Output())
|
|
socket.send(builder.Output())
|
|
|
|
|
|
|
|
is_game_running = True
|
|
is_game_running = True
|