def print_action(self, *, when=-1): emit = self.emitlog[when] assert emit[0] == 'message', 'Wrong Type' assert emit[1]['type'] == 'action', 'Wrong Type' if emit[1]['resp']['type'] == Action.Clue.value: who = emit[1]['resp']['target'] if self.game is not None: who = self.game.players[emit[1]['resp']['target']].name if emit[1]['resp']['clue']['type'] == Clue.Suit.value: scolor = emit[1]['resp']['clue']['value'] color = Suit(scolor) print('Clued color {} to {}'.format(color.name, who)) elif emit[1]['resp']['clue']['type'] == Clue.Rank.value: value = emit[1]['resp']['clue']['value'] print('Clued value {} to {}'.format(value, who)) else: assert False elif emit[1]['resp']['type'] == Action.Play.value: position = 'Unknown' if (self.bot is not None and emit[1]['resp']['target'] in self.bot.hand): position = self.bot.hand.index(emit[1]['resp']['target']) print('Played deckIdx {}, position {}'.format( emit[1]['resp']['target'], position)) elif emit[1]['resp']['type'] == Action.Discard.value: position = 'Unknown' if (self.bot is not None and emit[1]['resp']['target'] in self.bot.hand): position = self.bot.hand.index(emit[1]['resp']['target']) print('Discarded deckIdx {}, position {}'.format( emit[1]['resp']['target'], position)) else: assert False
def card_played(self, deckidx: int, suit: int, rank: int) -> None: self._card_shown(deckidx, suit, rank) color = Suit(suit).color(self.variant) self.playedCards[color].append(self.deck[deckidx]) player: Player = self.players[self.currentPlayer] pos: int = player.hand.index(deckidx) if self.currentPlayer == self.botPosition: self.bot.card_played(deckidx, pos) else: self.bot.someone_played(self.currentPlayer, deckidx, pos) player.played_card(deckidx)
def assert_clue_color(self, who, color, *, when=-1): emit = self.emitlog[when] suit = color.suit(self.game.variant) assert emit[0] == 'message', 'Wrong Type' assert emit[1]['type'] == 'action', 'Wrong Type' assert emit[1]['resp']['type'] == Action.Clue.value, 'Not Cluing' assert emit[1]['resp']['target'] == who, 'Clued wrong person' assert emit[1]['resp']['clue']['type'] == Clue.Suit.value,\ 'Clued a Number, not color' assert emit[1]['resp']['clue']['value'] == suit.value,\ 'Clued {}, Not {}'.format( Suit(emit[1]['resp']['clue']['value']).name, color.full_name(self.game.variant))
def deck_draw(self, player: int, deckidx: int, color: Optional[Color], value: Optional[Value]) -> None: if player == self.botPosition: card = self.bot.create_own_card(deckidx) self.deck[deckidx] = card self.bot.drew_card(deckidx) else: color = Suit(color).color(self.variant) value = Rank(value).value_() card = self.bot.create_player_card(player, deckidx, color, value) self.deck[deckidx] = card self.players[player].drew_card(deckidx) self.bot.someone_drew(player, deckidx)
def color_clue_sent(self, from_: int, to: int, suit: int, deckidxs: List[int]) -> None: color: Color = Suit(suit).color(self.variant) positions: List[int] = [] i: int h: int for i, h in enumerate(self.players[to].hand): if h in deckidxs: self.deck[h].got_positive_color(color) positions.append(i) else: self.deck[h].got_negative_color(color) if to == self.botPosition: self.bot.got_color_clue(from_, color, positions) else: self.bot.someone_got_color(from_, to, color, positions)
def message_sent(self, mtype, data): if mtype == 'game_start': pass elif mtype == 'init': self.game = Game(self.connection, Variant(data['variant']), data['names'], self.position, self.botcls, **self.botkwargs) self.bot = self.game.bot self.connection.game = self.game self.connection.bot = self.bot elif self.game is not None: if mtype == 'notify' and data['type'] == 'draw': card = TestCard(Suit(data['suit']), Rank(data['rank']), data['order']) self.deck[data['order']] = card self.game.received(mtype, data) elif mtype == 'notify' and data['type'] == 'reveal': pass elif mtype == 'action': pass else: print(mtype, data) raise Exception()
def _card_shown(self, deckidx: int, suit: int, rank: int) -> None: card: Card = self.deck[deckidx] color: Color = Suit(suit).color(self.variant) value: Value = Rank(rank).value_() card.suit = color card.rank = value
def clue_player(self, player: int, target: int, type: int, value: int) -> None: if self.isGameComplete(): raise GameException('Game is complete') if self.lastAction == player: raise GameException('Player already made a move', player) if self.currentPlayer != player: raise GameException('Wrong Player Turn', player) if player == target: raise GameException('Cannot clue self') if self.clues == 0: raise GameException('Cannot Clue') if target >= len(self.players): raise GameException('Target does not exist', target) rank: Rank suit: Suit positions: List[int] cards: List[int] card: ServerCard i: int h: int if type == Clue.Rank.value: rank = Rank(value) if not rank.valid(): raise GameException('Invalid rank value', value) positions = [] cards = [] for i, h in enumerate(self.hands[target]): card = self.deck[h] if card.rank == rank: positions.insert(0, len(self.hands[target]) - i) cards.append(h) if not cards and not self.allowNullClues: raise GameException('No Cards Clued') self.send( 'notify', { 'type': 'clue', 'giver': player, 'target': target, 'clue': { 'type': type, 'value': value }, 'list': cards }) self.clues -= 1 self.lastAction = player self.print( "{} tells {} about {} {}'s".format(names[player], names[target], numberWords[len(cards)], rank.value), "{} tells {} about {} {}'s in slots {}".format( names[player], names[target], numberWords[len(cards)], rank.value, ', '.join(str(p) for p in positions))) elif type == Clue.Suit.value: suit = Suit(value) if not suit.valid(self.variant): raise GameException('Invalid suit value', value) positions = [] cards = [] for i, h in enumerate(self.hands[target]): card = self.deck[h] if card.suit == suit: positions.insert(0, len(self.hands[target]) - i) cards.append(h) if self.variant == Variant.Rainbow and card.suit == Suit.Extra: cards.append(h) if not cards and not self.allowNullClues: raise GameException('No Cards Clued') self.send( 'notify', { 'type': 'clue', 'giver': player, 'target': target, 'clue': { 'type': type, 'value': value }, 'list': cards }) self.clues -= 1 self.lastAction = player self.print( "{} tells {} about {} {}'s".format( names[player], names[target], numberWords[len(cards)], suit.full_name(self.variant)), "{} tells {} about {} {}'s in slots {}".format( names[player], names[target], numberWords[len(cards)], suit.full_name(self.variant), ', '.join(str(p) for p in positions))) else: raise GameException('Invalid clue type', type)