Beispiel #1
0
    def join_game(self, board_constructor=None):
        self.sock = self.connection.get_socket()

        # Send join message, blocks until a new game is ready
        Message.serialized(self.sock, Message.TYPE_CONTROL, 'JOIN')

        m = Message(self.sock, Message.TYPE_ID)
        self.player_id = m.message_object

        m = Message(self.sock, Message.TYPE_BOARD, board_constructor)
        board = m.message_object

        m = Message(self.sock, Message.TYPE_CONTROL)
        if m.message_object != 'WAIT':
            raise NotImplementedError, "Unknown CONTROL message " + str(m)

        return self.player_id, board
Beispiel #2
0
    def join_game(self, board_constructor=None):
        self.sock = self.connection.get_socket()

        # Send join message, blocks until a new game is ready
        Message.serialized(self.sock, Message.TYPE_CONTROL, 'JOIN')

        m = Message(self.sock, Message.TYPE_ID)
        self.player_id = m.message_object

        m = Message(self.sock, Message.TYPE_BOARD, board_constructor)
        board = m.message_object

        m = Message(self.sock, Message.TYPE_CONTROL)
        if m.message_object != 'WAIT':
            raise NotImplementedError, "Unknown CONTROL message " + str(m)

        return self.player_id, board
Beispiel #3
0
 def game_loop(self, bot):
     m = Message(self.sock)
     if m.message_type is Message.TYPE_CONTROL:
         if m.message_object == 'TURN':
             Message.serialized(self.sock, Message.TYPE_MOVE, bot.get_move())
         elif m.message_object == 'END':
             return False
         else:
             raise NotImplementedError, "Unknown control message " + str(m)
     elif m.message_type is Message.TYPE_MOVE:
         bot.report_move(m.message_object)
     elif m.message_type is Message.TYPE_STATUS:
         if m.message_object[0] == Bot.STATUS_GAME_OVER:
             return False
         bot.report_status(m.message_object[0], m.message_object[1])
     else:
         raise NotImplementedError, "Unknown message type " + str(m)
     return True
Beispiel #4
0
 def game_loop(self, bot):
     m = Message(self.sock)
     if m.message_type is Message.TYPE_CONTROL:
         if m.message_object == 'TURN':
             Message.serialized(self.sock, Message.TYPE_MOVE,
                                bot.get_move())
         elif m.message_object == 'END':
             return False
         else:
             raise NotImplementedError, "Unknown control message " + str(m)
     elif m.message_type is Message.TYPE_MOVE:
         bot.report_move(m.message_object)
     elif m.message_type is Message.TYPE_STATUS:
         if m.message_object[0] == Bot.STATUS_GAME_OVER:
             return False
         bot.report_status(m.message_object[0], m.message_object[1])
     else:
         raise NotImplementedError, "Unknown message type " + str(m)
     return True
Beispiel #5
0
    def player(self, player_id):
        l = threading.local()
        l.is_first_move = True

        with self.lock:
            l.sock, l.user = self.server.get_connection()
            self.socks[player_id] = l.sock

        m = Message(l.sock)
        if not m.match(Message.TYPE_CONTROL, "JOIN"):
            raise
        print "Got JOIN from %s (assigned player_id %d)" % (l.user, player_id)

        Message.serialized(l.sock, Message.TYPE_ID, player_id)
        Message.serialized(l.sock, Message.TYPE_BOARD, self.board)

        Message.serialized(l.sock, Message.TYPE_CONTROL, "WAIT")

        self.arrival_sem.release()

        while True:
            self.go_sem[player_id].acquire()

            if self.done:
                break

            if not self.skips[player_id]:
                if len(self.board.get_remaining_piece_ids(player_id)) == 0:
                    move = Move.skip(player_id)
                else:
                    try:
                        Message.serialized(l.sock, Message.TYPE_CONTROL,
                                           "TURN")

                        m = Message(l.sock, Message.TYPE_MOVE)
                        move = m.message_object
                    except IOError:
                        move = Move.dropped_skip(player_id)

                    if not self.board.is_valid_move(move):
                        Message.serialized(l.sock, Message.TYPE_STATUS,\
                                [Bot.STATUS_SKIPPED, "Illegal Move"])
                        print 'ILLEGAL:', move
                        print ' REASON:', self.board._valid_reason
                        move = Move.illegal(player_id)
                    else:
                        l.is_first_move = False

                if move.is_skip():
                    self.skips[player_id] = True

                    if sum(self.skips) == 4:
                        self.game_logger.add_move(move)
                        print "=================="
                        print "4 skips. Game Over"
                        for s in self.socks:
                            Message.serialized(s,
                                               Message.TYPE_MOVE,
                                               move,
                                               suppress_err=True)
                        self.done = True
                        for s in self.go_sem:
                            s.release()
                        break

                self.game_logger.add_move(move)

                for s in self.socks:
                    Message.serialized(s,
                                       Message.TYPE_MOVE,
                                       move,
                                       suppress_err=True)

            self.go_sem[(player_id + 1) % 4].release()

        Message.serialized(l.sock, Message.TYPE_STATUS,\
                [Bot.STATUS_GAME_OVER, "This game has ended"])
Beispiel #6
0
    def player(self, player_id):
        l = threading.local()
        l.is_first_move = True

        with self.lock:
            l.sock, l.user = self.server.get_connection()
            self.socks[player_id] = l.sock

        m = Message(l.sock)
        if not m.match(Message.TYPE_CONTROL, "JOIN"):
            raise
        print "Got JOIN from %s (assigned player_id %d)" % (l.user, player_id)

        Message.serialized(l.sock, Message.TYPE_ID, player_id)
        Message.serialized(l.sock, Message.TYPE_BOARD, self.board)

        Message.serialized(l.sock, Message.TYPE_CONTROL, "WAIT")

        self.arrival_sem.release()

        while True:
            self.go_sem[player_id].acquire()

            if self.done:
                break

            if not self.skips[player_id]:
                if len(self.board.get_remaining_piece_ids(player_id)) == 0:
                    move = Move.skip(player_id)
                else:
                    try:
                        Message.serialized(l.sock, Message.TYPE_CONTROL, "TURN")

                        m = Message(l.sock, Message.TYPE_MOVE)
                        move = m.message_object
                    except IOError:
                        move = Move.dropped_skip(player_id)

                    if not self.board.is_valid_move(move):
                        Message.serialized(l.sock, Message.TYPE_STATUS,\
                                [Bot.STATUS_SKIPPED, "Illegal Move"])
                        print 'ILLEGAL:',move
                        print ' REASON:',self.board._valid_reason
                        move = Move.illegal(player_id)
                    else:
                        l.is_first_move = False

                if move.is_skip():
                    self.skips[player_id] = True

                    if sum(self.skips) == 4:
                        self.game_logger.add_move(move)
                        print "=================="
                        print "4 skips. Game Over"
                        for s in self.socks:
                            Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True)
                        self.done = True
                        for s in self.go_sem:
                            s.release()
                        break

                self.game_logger.add_move(move)

                for s in self.socks:
                    Message.serialized(s, Message.TYPE_MOVE, move, suppress_err=True)

            self.go_sem[(player_id+1) % 4].release()

        Message.serialized(l.sock, Message.TYPE_STATUS,\
                [Bot.STATUS_GAME_OVER, "This game has ended"])