Beispiel #1
0
    def HeartBeat(self):
        # a empty message, only to find if there is new info from the server
        self.heartbeaklock.acquire()

        while not self.allowheartbeat:
            self.heartbeaklock.release()
            sleep(1)
            self.heartbeatlock.acquire()

        self.heartbeaklock.release()

        return dealer_pb2.DealerRequest(user=self.username, command='heartbeat', type=0, pos=self.mypos,
                                        token=self.key, status=self.step)
Beispiel #2
0
    def start(self):
        responses = self.conn.GameStream(self.chat_with_server())
        for res in responses:
            # self.logger.info('res.type %d'%(res.type))
            self._new_response.append(res)
            # if self.stoped:
            #     break
            if res.type == MessageType_GameDecision:
                # server asking for a decision from the client
                self.state.currpos = res.pos
                if res.pos == self.mypos:
                    decision = self.ai(self.mypos, self.state)
                    if not decision.isValid():
                        self.logger.info(
                            '$$$ This client made a invalid decision')
                        print(decision, flush=True)
                        decision.fix()
                        print(decision, flush=True)

                    self.logger.info(
                        '$$$ This client made a decision at pos {}'.format(
                            self.mypos))
                    self.add_request(
                        dealer_pb2.DealerRequest(user=self.username,
                                                 giveup=decision.giveup,
                                                 allin=decision.allin,
                                                 check=decision.check,
                                                 raisebet=decision.raisebet,
                                                 callbet=decision.callbet,
                                                 amount=decision.amount,
                                                 pos=self.mypos,
                                                 type=MessageType_StateUpdate,
                                                 token=self.key))

            elif res.type == MessageType_StateUpdate:
                # server sending an info to the client to modify the state
                self.print_stateupdate(res)
                self.state.currpos = res.pos
                self._decision_so_far.append(res)
                if res.giveup == 1:
                    self.state.player[self.state.currpos].active = False
                    self.state.playernum -= 1
                elif res.check == 1:
                    pass
                elif res.allin == 1:
                    self.state.moneypot += self.state.player[
                        self.state.currpos].money
                    self.state.player[self.state.currpos].allinbet()
                    if self.state.player[
                            self.state.currpos].bet > self.state.minbet:
                        self.state.last_raised = max(
                            self.state.player[self.state.currpos].bet -
                            self.state.minbet, self.state.last_raised)
                        self.state.minbet = self.state.player[
                            self.state.currpos].bet
                elif res.callbet == 1:
                    delta = self.state.minbet - self.state.player[
                        self.state.currpos].bet
                    self.state.player[self.state.currpos].raisebet(delta)
                    self.state.moneypot += delta

                elif res.raisebet == 1:
                    self.state.last_raised = max(
                        res.amount - self.state.minbet, self.state.last_raised)
                    self.state.minbet = res.amount
                    delta = res.amount - self.state.player[
                        self.state.currpos].bet
                    self.state.player[self.state.currpos].raisebet(delta)
                    self.state.moneypot += delta

                else:
                    self.logger.info('impossible condition')
                    # assert(0)

                #**************************Modify here if you want to print some other message to read*******************
                # This information of this player's action is stored in variable 'res'.
                # The information of the game is stored in the variable 'self.state'.
                #*********************************************************************************************************

                self.step += 1

            elif res.type == MessageType_IllegalDecision:
                self.logger.info(
                    'player at pos {} illegalMove and is forced to give up. actionNum {}'
                    .format(res.pos, res.actionNum))
                self.state.player[self.state.currpos].active = False
                self.state.playernum -= 1

            elif res.type == MessageType_StateControl:
                # server send a state control command
                if res.command == 'restore':
                    self.round += 1
                    if res.pos == 1:
                        self.state.restore(res.pos, self.button, self.bigBlind)
                    else:
                        self.state.restore(res.pos, self.button, 0)
                elif res.command == 'update':
                    self.state.update(self.totalPlayer)
                elif res.command == 'givecard':
                    self.state.player[res.pos].cards.append(res.num)
                elif res.command == 'sharedcard':
                    self.state.sharedcards.append(res.num)
                elif res.command == 'setUserMoney':
                    self.state.set_user_money(res.userMoney)
                elif res.command == 'competitionEnd':
                    self.logger.info('The competition finished.')
                    return

            elif res.type == MessageType_ClientInit:
                # for money in res.userMoney:
                #     print("Money {} is ".format(money))
                self.client_reset(self.username, self.ai, self.logger,
                                  self.mypos)
                # client initialize
                assert (self.step == -1)
                s = res.command.split()
                self.initMoney = int(s[0])
                self.bigBlind = int(s[1])
                self.totalPlayer = int(s[2])
                self.button = int(s[3])
                self.key = res.token
                if self.initMoney == -2:
                    self.logger.info('Bad key for this username.')
                    exit()
                if self.initMoney == -4:
                    self.logger.info('Has no money left.')
                    exit()
                if self.initMoney == -3:
                    self.logger.info('Wait for next game begin.')
                    continue

                key_file = self.username + '_key.txt'
                if not os.path.exists(key_file):
                    with open(key_file, 'w') as f:
                        f.write(self.key)

                self.mypos = res.pos
                self.logger.info('This ai is begin at the pos {}'.format(
                    self.mypos))

                ### If the player in current position already connected to the game,
                # then the game server will return msg in which button is -1
                # If the player join the game failed, then the thread exit.
                if self.button == -1:
                    self.logger.info(
                        'Game already started. wait for next game.')
                    # self.stoped = True
                    continue
                self.logger.info(res.extra)
                self.step = 0
                self.state = State(self.logger, self.totalPlayer,
                                   self.initMoney, self.bigBlind, self.button)
                self.state.last_raised = self.bigBlind

                # self.initialized = True

                self.logger.info(
                    '******client initialized****** client:%d step:%d' %
                    (self.mypos, self.step))

            elif res.type == MessageType_GameOver:
                # game over info
                self.logger.info('***********game over***************')
                self.logger.info('sharedcards:%s' %
                                 str(self.state.sharedcards))
                for x in self.state.sharedcards:
                    self.logger.info('%s. ' % printcard(x))
                self.logger.info('cards:%s' %
                                 str(self.state.player[self.mypos].cards))
                for x in self.state.player[self.mypos].cards:
                    self.logger.info('%s. ' % printcard(x))
                self.logger.info('\n')
                self.logger.info('Have money {} left'.format(
                    res.userMoney[self.mypos]))

                self.stoped = True

                # self.client_reset(self.username, self.ai, self.logger, self.mypos)
                if ISTESTING:
                    return