def __init__(self,tcp): ## create states self.WAIT_UI_START = CommClient.WAIT_UI_START() self.LOOK_GAME = CommClient.LOOK_GAME() self.WAIT_MYTURN = CommClient.WAIT_MYTURN() self.WAIT_UI = CommClient.WAIT_UI() ## state variable & initial state self._state = self.WAIT_UI_START ## layers self.tcp = tcp self.port = None self.ui = None self.codec_server = ServerPduCodec(self) self.codec_client = ClientPduCodec(self) ## variables self.lock = threading.Lock() tcp.set_ind(self)
class CommClient(IGameCommClientReq,IGameCommClientPdu, EntityMix): def __init__(self,tcp): ## create states self.WAIT_UI_START = CommClient.WAIT_UI_START() self.LOOK_GAME = CommClient.LOOK_GAME() self.WAIT_MYTURN = CommClient.WAIT_MYTURN() self.WAIT_UI = CommClient.WAIT_UI() ## state variable & initial state self._state = self.WAIT_UI_START ## layers self.tcp = tcp self.port = None self.ui = None self.codec_server = ServerPduCodec(self) self.codec_client = ClientPduCodec(self) ## variables self.lock = threading.Lock() tcp.set_ind(self) def set_ui(self,ui): self.ui = ui class State: # default actions def name(self): return self.__class__.__name__ # add methods with error-messages for each state # waiting for game to starts class WAIT_UI_START(State): def look_game_req(self, ctx): #ctx.port = ctx.tcp.port(addr,port,ctx) data = ctx.codec_server.look_game_pdu() ctx.goto(ctx.LOOK_GAME) ctx.tcp.req_send(data) def game_end_pdu(self, ctx, player): sys.exit(0) # class WAIT_CONN_IND(State): # def new_connection_ind(self, ctx, port): # assert ctx.port == port # data = ctx.codec_server.look_game_pdu() # ctx.port.req_send(data) # ctx.goto(ctx.LOOK_GAME) # def close_connection_ind(self, ctx, port): # ctx.goto(ctx.WAIT_UI_START) # def network_error_ind(self, ctx, port, code, reason): # ctx.port = None # ctx.goto(ctx.WAIT_UI_START) # client waiting for connection to server class LOOK_GAME(State): # when second player found goto WAIT_MY_TURN def start_game_pdu(self, ctx): ctx.ui.start_game_ind() ctx.goto(ctx.WAIT_MYTURN) # player waiting for turn or server calculating changes class WAIT_MYTURN(State): # board is updated after opponent's m_place or rotate def update_board_pdu(self, ctx, board_info): ctx.ui.update_board_ind(board_info) ctx.goto(ctx.WAIT_MYTURN) # player's turn starts or it's time to rotate def your_turn_pdu(self, ctx): ctx.ui.your_turn_ind() ctx.goto(ctx.WAIT_UI) # game tells that the move was invalid def invalid_move_pdu(self, ctx): ctx.ui.invalid_move_ind() ctx.goto(ctx.WAIT_UI) # game ends - victory, loss, or draw def game_end_pdu(self, ctx, end_status): ctx.ui.game_end_ind(end_status) #ctx.goto(ctx.WAIT_UI_START) # client waiting for user input class WAIT_UI(State): # user places marble def m_place_req(self, ctx, x, y): data = ctx.codec_server.m_place_pdu(x, y) ctx.tcp.req_send(data) ctx.goto(ctx.WAIT_UI) # board is updated after current user's input def update_board_pdu(self, ctx, board_info): ctx.ui.update_board_ind(board_info) ctx.goto(ctx.WAIT_UI) # user rotates board def rotate_board_req(self, ctx, board, direction): ctx.goto(ctx.WAIT_MYTURN) data = ctx.codec_server.rotate_board_pdu(board, direction) ctx.tcp.req_send(data) # game tells that the move was invalid def invalid_move_pdu(self, ctx): ctx.ui.invalid_move_ind() ctx.goto(ctx.WAIT_UI) # game ends - victory, loss, or draw def game_end_pdu(self, ctx, end_status): ctx.ui.game_end_ind(end_status) ctx.goto(ctx.WAIT_UI_START) # Inputs that are sent to states ## GUI inputs def look_game_req(self): # STATES self._state.look_game_req(self) def m_place_req(self, x, y): self._state.m_place_req(self, x, y) def rotate_board_req(self, board, direction): self._state.rotate_board_req(self, board, direction) ## peer inputs def start_game_pdu(self): self._state.start_game_pdu(self) def update_board_pdu(self, board_info): self._state.update_board_pdu(self, board_info) def invalid_move_pdu(self): self._state.invalid_move_pdu(self) def game_end_pdu(self, end_status): self._state.game_end_pdu(self, end_status) def your_turn_pdu(self): self._state.your_turn_pdu(self) ## tcp inputs def received_ind(self, port, data): self.codec_client.decode(data) def new_connection_ind(self, port): self._state.ind_connect(self, port) def close_connection_ind(self, port): self._state.close_connection_ind(self, port) def networ_error_ind(self, port, code, reason): self._state.ind_error(self, port, code, reason)