reduce memory allocations for checks

This commit is contained in:
Alberto Xamin 2023-01-06 17:24:16 +00:00
parent 0668fbf670
commit 352f7f006f
6 changed files with 22 additions and 22 deletions

View File

@ -216,7 +216,7 @@ class Bang(Card):
def play_card(self, player, against, _with=None): def play_card(self, player, against, _with=None):
if player.game.check_event(ceh.Sermone) and not self.number == 42: # 42 gold rush if player.game.check_event(ceh.Sermone) and not self.number == 42: # 42 gold rush
return False 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 return False
elif against != None: elif against != None:
import bang.characters as chars import bang.characters as chars
@ -377,7 +377,7 @@ class Mancato(Card):
def play_card(self, player, against, _with=None): def play_card(self, player, against, _with=None):
import bang.characters as chars import bang.characters as chars
if against != None and player.character.check(player.game, chars.CalamityJanet): 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 return False
if player.game.check_event(ceh.Sermone): if player.game.check_event(ceh.Sermone):
return False return False

View File

@ -52,7 +52,7 @@ class Deck:
self.game.notify_event_card() self.game.notify_event_card()
def fill_gold_rush_shop(self): 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 return
for i in range(3): for i in range(3):
if self.shop_cards[i] == None: if self.shop_cards[i] == None:

View File

@ -527,7 +527,7 @@ class Game:
print(f'{self.name}: {self.players[self.turn]} is dead, next turn') print(f'{self.name}: {self.players[self.turn]} is dead, next turn')
return self.next_turn() return self.next_turn()
self.player_bangs = 0 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() self.deck.flip_event()
if len(self.deck.event_cards) > 0 and self.deck.event_cards[0] != None: 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}') print(f'{self.name}: flip new event {self.deck.event_cards[0].name}')

View File

@ -303,10 +303,10 @@ class Player:
self.draw('') self.draw('')
elif self.pending_action == PendingAction.PLAY: 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)] 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))) 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] 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] 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: if self.game.debug:
print(f'hand: {self.hand}') print(f'hand: {self.hand}')
@ -315,7 +315,7 @@ class Player:
print(f'misc: {misc}') print(f'misc: {misc}')
print(f'need_target: {need_target}') print(f'need_target: {need_target}')
print(f'green_cards: {green_cards}') 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)): for i in range(len(self.game.deck.shop_cards)):
if self.game.deck.shop_cards[i].number <= self.gold_nuggets: if self.game.deck.shop_cards[i].number <= self.gold_nuggets:
self.game.rpc_log.append(f'{self.name};buy_gold_rush_card;{i}') 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.available_cards = [self.character, self.not_chosen_character]
self.choose_text = 'choose_nuova_identita' self.choose_text = 'choose_nuova_identita'
self.pending_action = PendingAction.CHOOSE 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.is_giving_life = False
self.pending_action = PendingAction.PICK self.pending_action = PendingAction.PICK
else: else:
@ -609,7 +609,7 @@ class Player:
self.game.next_player().equipment.append(self.equipment.pop(i)) self.game.next_player().equipment.append(self.equipment.pop(i))
self.game.next_player().notify_self() self.game.next_player().notify_self()
break 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() self.notify_self()
return return
for i in range(len(self.equipment)): for i in range(len(self.equipment)):
@ -659,7 +659,7 @@ class Player:
playable_cards = [] playable_cards = []
for i in range(len(self.hand)): for i in range(len(self.hand)):
card = self.hand[i] 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 continue
elif isinstance(card, cs.Birra) and self.lives >= self.max_lives: elif isinstance(card, cs.Birra) and self.lives >= self.max_lives:
continue continue
@ -1309,13 +1309,13 @@ class Player:
if self.game.check_event(ce.LeggeDelWest) and len(must_be_used_cards) > 0: if self.game.check_event(ce.LeggeDelWest) and len(must_be_used_cards) > 0:
card = must_be_used_cards[0] card = must_be_used_cards[0]
print(f'Legge del west card: {card.name}') 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))) 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 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): if isinstance(card, cs.Bang):
return True return True
else: else:
return len(self.equipment) == 0 # se non ho carte equipaggiamento 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 return True
elif isinstance(card, cs.Mancato) or (card.need_with and len(self.hand) < 2): elif isinstance(card, cs.Mancato) or (card.need_with and len(self.hand) < 2):
return True return True

View File

@ -21,7 +21,7 @@ class Sheriff(Role):
def on_player_death(self, alive_players: list, initial_players: int, dead_role=None, attacker_role=None): 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: if initial_players == 3 and len(alive_players) == 1:
return True 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!") print("The Sheriff won!")
return True return True
return False return False
@ -42,7 +42,7 @@ class Vice(Role):
return True return True
elif initial_players == 3 and attacker_role != None: elif initial_players == 3 and attacker_role != None:
return isinstance(dead_role, Renegade) and isinstance(attacker_role, Vice) 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!") print("The Vice won!")
return True return True
return False return False
@ -62,9 +62,9 @@ class Outlaw(Role):
return True return True
elif initial_players == 3 and attacker_role != None: elif initial_players == 3 and attacker_role != None:
return isinstance(dead_role, Vice) and isinstance(attacker_role, Outlaw) 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])) 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]) 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)): or any((isinstance(p.role, Renegade) for p in alive_players)) and len(alive_players) > 1)):
print("The Outlaw won!") print("The Outlaw won!")
return True return True
return False return False

View File

@ -131,7 +131,7 @@ def get_me(sid, room):
join_room(sid, room) join_room(sid, room)
elif len(de_games) == 1 and de_games[0].started: elif len(de_games) == 1 and de_games[0].started:
print('room exists') 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') 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 = [p for p in de_games[0].players if p.is_bot and p.name == room['username'] ][0]
bot.sid = sid bot.sid = sid
@ -164,7 +164,7 @@ def get_me(sid, room):
sio.emit('me', data={'error':'Wrong password/Cannot connect'}, room=sid) sio.emit('me', data={'error':'Wrong password/Cannot connect'}, room=sid)
else: else:
sio.emit('me', data=sio.get_session(sid).name, room=sid) 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) sio.emit('change_username', room=sid)
else: else:
sio.emit('chat_message', room=sio.get_session(sid).game.name, data=f"_change_username|{sio.get_session(sid).name}|{room['username']}") 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'}) sio.emit('chat_message', room=ses.game.name, data={'color': f'red','text':f'Only 1 bot at the time'})
else: else:
bot = Player(f'AI_{random.randint(0,10)}', 'bot', sio, bot=True) 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) bot = Player(f'AI_{random.randint(0,10)}', 'bot', sio, bot=True)
ses.game.add_player(bot) ses.game.add_player(bot)
bot.bot_spin() bot.bot_spin()
@ -409,7 +409,7 @@ def chat_message(sid, msg, pl=None):
ses.game.start_game(int(msg.split()[1])) ses.game.start_game(int(msg.split()[1]))
return return
elif '/removebot' in msg and not ses.game.started: 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() [p for p in ses.game.players if p.is_bot][-1].disconnect()
return return
elif '/togglecomp' in msg and ses.game: elif '/togglecomp' in msg and ses.game: