From 352f7f006ffe6a70f3acd46fdabe6b5c773b63fd Mon Sep 17 00:00:00 2001 From: Alberto Xamin Date: Fri, 6 Jan 2023 17:24:16 +0000 Subject: [PATCH] reduce memory allocations for checks --- backend/bang/cards.py | 4 ++-- backend/bang/deck.py | 2 +- backend/bang/game.py | 2 +- backend/bang/players.py | 18 +++++++++--------- backend/bang/roles.py | 10 +++++----- backend/server.py | 8 ++++---- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/backend/bang/cards.py b/backend/bang/cards.py index e8a09ab..1c6a2b5 100644 --- a/backend/bang/cards.py +++ b/backend/bang/cards.py @@ -216,7 +216,7 @@ class Bang(Card): def play_card(self, player, against, _with=None): if player.game.check_event(ceh.Sermone) and not self.number == 42: # 42 gold rush return False - if ((player.has_played_bang and not self.number == 42) and (not any([isinstance(c, Volcanic) for c in player.equipment]) or player.game.check_event(ce.Lazo)) and against != None): # 42 gold rush: + if ((player.has_played_bang and not self.number == 42) and (not any((isinstance(c, Volcanic) for c in player.equipment)) or player.game.check_event(ce.Lazo)) and against != None): # 42 gold rush: return False elif against != None: import bang.characters as chars @@ -377,7 +377,7 @@ class Mancato(Card): def play_card(self, player, against, _with=None): import bang.characters as chars if against != None and player.character.check(player.game, chars.CalamityJanet): - if player.has_played_bang and (not any([isinstance(c, Volcanic) for c in player.equipment]) or player.game.check_event(ce.Lazo)): + if player.has_played_bang and (not any((isinstance(c, Volcanic) for c in player.equipment)) or player.game.check_event(ce.Lazo)): return False if player.game.check_event(ceh.Sermone): return False diff --git a/backend/bang/deck.py b/backend/bang/deck.py index 26a207a..cb138e1 100644 --- a/backend/bang/deck.py +++ b/backend/bang/deck.py @@ -52,7 +52,7 @@ class Deck: self.game.notify_event_card() def fill_gold_rush_shop(self): - if not any([c == None for c in self.shop_cards]): + if not any((c == None for c in self.shop_cards)): return for i in range(3): if self.shop_cards[i] == None: diff --git a/backend/bang/game.py b/backend/bang/game.py index f45b2c1..ce12c68 100644 --- a/backend/bang/game.py +++ b/backend/bang/game.py @@ -527,7 +527,7 @@ class Game: print(f'{self.name}: {self.players[self.turn]} is dead, next turn') return self.next_turn() self.player_bangs = 0 - if isinstance(self.players[self.turn].role, roles.Sheriff) or ((self.initial_players == 3 and isinstance(self.players[self.turn].role, roles.Vice) and not self.players[self.turn].is_ghost) or (self.initial_players == 3 and any([p for p in self.players if p.is_dead and p.role.name == 'Vice']) and isinstance(self.players[self.turn].role, roles.Renegade))): + if isinstance(self.players[self.turn].role, roles.Sheriff) or ((self.initial_players == 3 and isinstance(self.players[self.turn].role, roles.Vice) and not self.players[self.turn].is_ghost) or (self.initial_players == 3 and any((p for p in self.players if p.is_dead and p.role.name == 'Vice')) and isinstance(self.players[self.turn].role, roles.Renegade))): self.deck.flip_event() if len(self.deck.event_cards) > 0 and self.deck.event_cards[0] != None: print(f'{self.name}: flip new event {self.deck.event_cards[0].name}') diff --git a/backend/bang/players.py b/backend/bang/players.py index b3efa50..4eec5fa 100644 --- a/backend/bang/players.py +++ b/backend/bang/players.py @@ -303,10 +303,10 @@ class Player: self.draw('') elif self.pending_action == PendingAction.PLAY: non_blocked_cards = [card for card in self.hand if (not self.game.check_event(ceh.Manette) or card.suit == self.committed_suit_manette)] - equippables = [c for c in non_blocked_cards if (c.is_equipment or c.usable_next_turn) and not isinstance(c, cs.Prigione) and not any([type(c) == type(x) and not (c.is_weapon and c.must_be_used) for x in self.equipment])] + equippables = [c for c in non_blocked_cards if (c.is_equipment or c.usable_next_turn) and not isinstance(c, cs.Prigione) and not any((type(c) == type(x) and not (c.is_weapon and c.must_be_used) for x in self.equipment))] misc = [c for c in non_blocked_cards if not c.need_target and (isinstance(c, cs.WellsFargo) or isinstance(c, cs.Indiani) or isinstance(c, cs.Gatling) or isinstance(c, cs.Diligenza) or isinstance(c, cs.Emporio) or ((isinstance(c, cs.Birra) and self.lives < self.max_lives or c.must_be_used) and not self.game.check_event(ceh.IlReverendo)) or (c.need_with and len(self.hand) > 1 and not (isinstance(c, csd.Whisky) and self.lives == self.max_lives))) and not (not c.can_be_used_now and self.game.check_event(ce.IlGiudice)) and not c.is_equipment] - need_target = [c for c in non_blocked_cards if c.need_target and c.can_be_used_now and not (c.need_with and len(self.hand) < 2) and not (type(c) == type(cs.Bang) and (self.game.check_event(ceh.Sermone) or (self.has_played_bang and (not any([isinstance(c, cs.Volcanic) for c in self.equipment]) or self.game.check_event(ce.Lazo))))) and not (isinstance(c, cs.Prigione) and self.game.check_event(ce.IlGiudice)) or isinstance(c, cs.Duello) or isinstance(c, cs.CatBalou) or isinstance(c, csd.Pugno)] + need_target = [c for c in non_blocked_cards if c.need_target and c.can_be_used_now and not (c.need_with and len(self.hand) < 2) and not (type(c) == type(cs.Bang) and (self.game.check_event(ceh.Sermone) or (self.has_played_bang and (not any((isinstance(c, cs.Volcanic) for c in self.equipment)) or self.game.check_event(ce.Lazo))))) and not (isinstance(c, cs.Prigione) and self.game.check_event(ce.IlGiudice)) or isinstance(c, cs.Duello) or isinstance(c, cs.CatBalou) or isinstance(c, csd.Pugno)] green_cards = [c for c in self.equipment if not self.game.check_event(ce.Lazo) and not isinstance(c, cs.Mancato) and c.usable_next_turn and c.can_be_used_now] if self.game.debug: print(f'hand: {self.hand}') @@ -315,7 +315,7 @@ class Player: print(f'misc: {misc}') print(f'need_target: {need_target}') print(f'green_cards: {green_cards}') - if self.gold_nuggets > 0 and any([c.number <= self.gold_nuggets for c in self.game.deck.shop_cards]): + if self.gold_nuggets > 0 and any((c.number <= self.gold_nuggets for c in self.game.deck.shop_cards)): for i in range(len(self.game.deck.shop_cards)): if self.game.deck.shop_cards[i].number <= self.gold_nuggets: self.game.rpc_log.append(f'{self.name};buy_gold_rush_card;{i}') @@ -465,7 +465,7 @@ class Player: self.available_cards = [self.character, self.not_chosen_character] self.choose_text = 'choose_nuova_identita' self.pending_action = PendingAction.CHOOSE - elif not self.game.check_event(ce.Lazo) and any([isinstance(c, cs.Dinamite) or isinstance(c, cs.Prigione) or isinstance(c, tvosc.SerpenteASonagli) for c in self.equipment]): + elif not self.game.check_event(ce.Lazo) and any((isinstance(c, cs.Dinamite) or isinstance(c, cs.Prigione) or isinstance(c, tvosc.SerpenteASonagli) for c in self.equipment)): self.is_giving_life = False self.pending_action = PendingAction.PICK else: @@ -609,7 +609,7 @@ class Player: self.game.next_player().equipment.append(self.equipment.pop(i)) self.game.next_player().notify_self() break - if any([isinstance(c, cs.Dinamite) or isinstance(c, cs.Prigione) for c in self.equipment]): + if any((isinstance(c, cs.Dinamite) or isinstance(c, cs.Prigione) for c in self.equipment)): self.notify_self() return for i in range(len(self.equipment)): @@ -659,7 +659,7 @@ class Player: playable_cards = [] for i in range(len(self.hand)): card = self.hand[i] - if isinstance(card, cs.Bang) and self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment]): + if isinstance(card, cs.Bang) and self.has_played_bang and not any((isinstance(c, cs.Volcanic) for c in self.equipment)): continue elif isinstance(card, cs.Birra) and self.lives >= self.max_lives: continue @@ -1309,13 +1309,13 @@ class Player: if self.game.check_event(ce.LeggeDelWest) and len(must_be_used_cards) > 0: card = must_be_used_cards[0] print(f'Legge del west card: {card.name}') - print(self.has_played_bang and not (any([isinstance(c, cs.Volcanic) for c in self.equipment]) and type(card) == type(cs.Bang))) - if card.suit == cs.Suit.DIAMONDS and card.need_target and len([p for p in self.game.get_alive_players() if (not p.character.check(self.game, chd.ApacheKid) and not any([isinstance(c, grc.Calumet) for c in p.gold_rush_equipment]))]) == 0: + print(self.has_played_bang and not (any((isinstance(c, cs.Volcanic) for c in self.equipment)) and type(card) == type(cs.Bang))) + if card.suit == cs.Suit.DIAMONDS and card.need_target and len([p for p in self.game.get_alive_players() if (not p.character.check(self.game, chd.ApacheKid) and not any((isinstance(c, grc.Calumet) for c in p.gold_rush_equipment)))]) == 0: if isinstance(card, cs.Bang): return True else: return len(self.equipment) == 0 # se non ho carte equipaggiamento - elif (isinstance(card, cs.Bang) or (isinstance(card, cs.Mancato) and self.character.check(self.game, chars.CalamityJanet))) and self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment]) or len([p for p in self.game.get_visible_players(self) if self.get_sight() >= p['dist']]) == 0: + elif (isinstance(card, cs.Bang) or (isinstance(card, cs.Mancato) and self.character.check(self.game, chars.CalamityJanet))) and self.has_played_bang and not any((isinstance(c, cs.Volcanic) for c in self.equipment)) or len([p for p in self.game.get_visible_players(self) if self.get_sight() >= p['dist']]) == 0: return True elif isinstance(card, cs.Mancato) or (card.need_with and len(self.hand) < 2): return True diff --git a/backend/bang/roles.py b/backend/bang/roles.py index 32eb94d..9032e40 100644 --- a/backend/bang/roles.py +++ b/backend/bang/roles.py @@ -21,7 +21,7 @@ class Sheriff(Role): def on_player_death(self, alive_players: list, initial_players: int, dead_role=None, attacker_role=None): if initial_players == 3 and len(alive_players) == 1: return True - elif initial_players != 3 and not any([isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players]): + elif initial_players != 3 and not any((isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players)): print("The Sheriff won!") return True return False @@ -42,7 +42,7 @@ class Vice(Role): return True elif initial_players == 3 and attacker_role != None: return isinstance(dead_role, Renegade) and isinstance(attacker_role, Vice) - elif initial_players != 3 and not any([isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players]): + elif initial_players != 3 and not any((isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players)): print("The Vice won!") return True return False @@ -62,9 +62,9 @@ class Outlaw(Role): return True elif initial_players == 3 and attacker_role != None: return isinstance(dead_role, Vice) and isinstance(attacker_role, Outlaw) - elif (initial_players != 3 and (not any([isinstance(p.role, Sheriff) for p in alive_players])) - and (any([isinstance(p.role, Outlaw) for p in alive_players]) - or any([isinstance(p.role, Renegade) for p in alive_players]) and len(alive_players) > 1)): + elif (initial_players != 3 and (not any((isinstance(p.role, Sheriff) for p in alive_players))) + and (any((isinstance(p.role, Outlaw) for p in alive_players)) + or any((isinstance(p.role, Renegade) for p in alive_players)) and len(alive_players) > 1)): print("The Outlaw won!") return True return False diff --git a/backend/server.py b/backend/server.py index 8ced009..1190157 100644 --- a/backend/server.py +++ b/backend/server.py @@ -131,7 +131,7 @@ def get_me(sid, room): join_room(sid, room) elif len(de_games) == 1 and de_games[0].started: print('room exists') - if room['username'] != None and any([p.name == room['username'] for p in de_games[0].players if p.is_bot]): + if room['username'] != None and any((p.name == room['username'] for p in de_games[0].players if p.is_bot)): print('getting inside the bot') bot = [p for p in de_games[0].players if p.is_bot and p.name == room['username'] ][0] bot.sid = sid @@ -164,7 +164,7 @@ def get_me(sid, room): sio.emit('me', data={'error':'Wrong password/Cannot connect'}, room=sid) else: sio.emit('me', data=sio.get_session(sid).name, room=sid) - if room['username'] == None or any([p.name == room['username'] for p in sio.get_session(sid).game.players]): + if room['username'] == None or any((p.name == room['username'] for p in sio.get_session(sid).game.players)): sio.emit('change_username', room=sid) else: sio.emit('chat_message', room=sio.get_session(sid).game.name, data=f"_change_username|{sio.get_session(sid).name}|{room['username']}") @@ -381,7 +381,7 @@ def chat_message(sid, msg, pl=None): sio.emit('chat_message', room=ses.game.name, data={'color': f'red','text':f'Only 1 bot at the time'}) else: bot = Player(f'AI_{random.randint(0,10)}', 'bot', sio, bot=True) - while any([p for p in ses.game.players if p.name == bot.name]): + while any((p for p in ses.game.players if p.name == bot.name)): bot = Player(f'AI_{random.randint(0,10)}', 'bot', sio, bot=True) ses.game.add_player(bot) bot.bot_spin() @@ -409,7 +409,7 @@ def chat_message(sid, msg, pl=None): ses.game.start_game(int(msg.split()[1])) return elif '/removebot' in msg and not ses.game.started: - if any([p.is_bot for p in ses.game.players]): + if any((p.is_bot for p in ses.game.players)): [p for p in ses.game.players if p.is_bot][-1].disconnect() return elif '/togglecomp' in msg and ses.game: