bot improvements

This commit is contained in:
Alberto Xamin 2020-12-05 21:57:53 +01:00
parent 0ed906fc39
commit 46239f9b30
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
3 changed files with 92 additions and 59 deletions

View File

@ -121,10 +121,16 @@ def chat_message(sid, msg):
ses: Player = sio.get_session(sid) ses: Player = sio.get_session(sid)
sio.emit('chat_message', room=ses.game.name, data=f'[{ses.name}]: {msg}') sio.emit('chat_message', room=ses.game.name, data=f'[{ses.name}]: {msg}')
if '/addbot' in msg and not ses.game.started: if '/addbot' in msg and not ses.game.started:
ses.game.add_player(Player(f'AI_{random.randint(0,100)}', 'bot', sio, bot=True)) if len(msg.split()) > 1:
for _ in range(int(msg.split()[1])):
ses.game.add_player(Player(f'AI_{random.randint(0,100)}', 'bot', sio, bot=True))
else:
ses.game.add_player(Player(f'AI_{random.randint(0,100)}', 'bot', sio, bot=True))
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()
elif '/suicide' in msg and ses.game.started:
ses.game.player_death(player=ses)
@sio.event @sio.event
def start_game(sid): def start_game(sid):

View File

@ -246,7 +246,8 @@ class Game:
if self.started: if self.started:
self.sio.emit('chat_message', room=self.name, data=f'_died_role|{player.name}|{player.role.name}') self.sio.emit('chat_message', room=self.name, data=f'_died_role|{player.name}|{player.role.name}')
for p in self.players: for p in self.players:
p.notify_self() if not p.is_bot:
p.notify_self()
self.players_map = {c.name: i for i, c in enumerate(self.players)} self.players_map = {c.name: i for i, c in enumerate(self.players)}
if self.started: if self.started:
print('Check win status') print('Check win status')

View File

@ -114,7 +114,6 @@ class Player:
self.sio.emit('characters', room=self.sid, data=json.dumps( self.sio.emit('characters', room=self.sid, data=json.dumps(
available, default=lambda o: o.__dict__)) available, default=lambda o: o.__dict__))
else: else:
eventlet.sleep(uniform(1, 2))
self.set_character(available[randrange(0, len(available))].name) self.set_character(available[randrange(0, len(available))].name)
def notify_card(self, player, card): def notify_card(self, player, card):
@ -166,66 +165,93 @@ class Player:
self.game.notify_all() self.game.notify_all()
else: else:
self.game.notify_all() self.game.notify_all()
if self.pending_action != PendingAction.WAIT: self.bot_logic()
eventlet.sleep(uniform(0.6, 1.5)) self.game.notify_all()
else:
return def bot_logic(self):
if self.pending_action == PendingAction.PICK: if self.pending_action != PendingAction.WAIT:
self.pick() eventlet.sleep(uniform(0.6, 1.5))
elif self.pending_action == PendingAction.DRAW: else:
self.draw('') return
elif self.pending_action == PendingAction.PLAY: if self.pending_action == PendingAction.PICK:
has_played = False self.pick()
if len([c for c in self.hand if c.is_equipment or c.usable_next_turn]) > 0: elif self.pending_action == PendingAction.DRAW:
for i in range(len(self.hand)): self.draw('')
if self.hand[i].is_equipment or self.hand[i].usable_next_turn: elif self.pending_action == PendingAction.PLAY:
self.play_card(i) has_played = False
has_played = True if len([c for c in self.hand if c.is_equipment or c.usable_next_turn]) > 0:
break for i in range(len(self.hand)):
if len([c for c in self.hand if c.need_target and not (self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment]))]) > 0: if self.hand[i].is_equipment or self.hand[i].usable_next_turn:
for i in range(len(self.hand)): self.play_card(i)
if self.hand[i].need_target and not (self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment])): has_played = True
if self.hand[i].need_with and len(self.hand) < 2: break
continue elif any([isinstance(c, cs.WellsFargo) or isinstance(c, cs.Diligenza) or isinstance(c, cs.Emporio) or isinstance(c, cs.Birra) for c in self.hand]):
_range = self.get_sight() if self.hand[i].name == 'Bang!' or self.hand[i].name == "Pepperbox" else self.hand[i].range for i in range(len(self.hand)):
others = [p for p in self.game.get_visible_players(self) if _range <= p['dist']] c = self.hand[i]
if isinstance(c, cs.WellsFargo) or isinstance(c, cs.Diligenza) or isinstance(c, cs.Emporio) or (isinstance(c, cs.Birra) and self.lives < self.max_lives):
self.play_card(i)
has_played = True
break
elif len([c for c in self.hand if c.need_target and not (self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment]))]) > 0:
for i in range(len(self.hand)):
if self.hand[i].need_target and not (self.has_played_bang and not any([isinstance(c, cs.Volcanic) for c in self.equipment])):
if self.hand[i].need_with and len(self.hand) < 2:
continue
_range = self.get_sight() if self.hand[i].name == 'Bang!' or self.hand[i].name == "Pepperbox" else self.hand[i].range
others = [p for p in self.game.get_visible_players(self) if _range >= p['dist'] and not (isinstance(self.role, r.Vice) and p['is_sheriff'])]
if len(others) == 0:
continue
target = others[randrange(0, len(others))]
if target['is_sheriff'] and isinstance(self.role, r.Renegade):
target = others[randrange(0, len(others))]
if not self.hand[i].need_with:
self.play_card(i, against=target['name'])
else:
self.play_card(i, against=target['name'], _with=sample([j for j in range(len(self.hand)) if j != i], 1)[0])
has_played = True
break
elif any([not isinstance(c, cs.Mancato) and c.usable_next_turn and c.can_be_used_now for c in self.equipment]):
print('hmm', [not isinstance(c, cs.Mancato) and c.usable_next_turn and c.can_be_used_now for c in self.equipment])
for i in range(len(self.equipment)):
c = self.equipment[i]
if not isinstance(c, cs.Mancato) and c.usable_next_turn and c.can_be_used_now:
if not c.need_target:
self.play_card(len(self.hand)+i)
else:
_range = self.get_sight() if c.name == "Pepperbox" else self.hand[i].range
others = [p for p in self.game.get_visible_players(self) if _range >= p['dist'] and not (isinstance(self.role, r.Vice) and p['is_sheriff'])]
if len(others) == 0: if len(others) == 0:
continue continue
target = others[randrange(0, len(others))] target = others[randrange(0, len(others))]
len_before = len(self.hand) if target['is_sheriff'] and isinstance(self.role, r.Renegade):
if not self.hand[i].need_with: target = others[randrange(0, len(others))]
self.play_card(i, against=target['name']) self.play_card(len(self.hand)+i, against=target['name'])
else: has_played = True
self.play_card(i, against=target['name'], _with=sample([j for j in range(len(self.hand)) if j != i], 1)[0])
has_played = True
break
if any([isinstance(c, cs.WellsFargo) or isinstance(c, cs.Diligenza) or isinstance(c, cs.Emporio) or isinstance(c, cs.Birra) for c in self.hand]):
for i in range(len(self.hand)):
c = self.hand[i]
if isinstance(c, cs.WellsFargo) or isinstance(c, cs.Diligenza) or isinstance(c, cs.Emporio) or (isinstance(c, cs.Birra) and self.lives < self.max_lives):
self.play_card(i)
has_played = True
break
if not has_played and len(self.hand) > self.lives:
self.scrap(0)
else:
self.end_turn()
elif self.pending_action == PendingAction.RESPOND:
did_respond = False
for i in range(len(self.hand)):
if self.hand[i].name in self.expected_response:
self.respond(i)
did_respond = True
break break
if not did_respond: if not has_played and len(self.hand) > self.lives:
self.respond(-1) self.scrap(0)
elif self.pending_action == PendingAction.CHOOSE: else:
if not self.target_p: self.end_turn()
self.choose(randrange(0, len(self.available_cards))) elif self.pending_action == PendingAction.RESPOND:
else: did_respond = False
target = self.game.get_player_named(self.target_p) for i in range(len(self.hand)):
self.choose(randrange(0, len(target.hand)+len(target.equipment))) if self.hand[i].name in self.expected_response:
self.game.notify_all() self.respond(i)
did_respond = True
break
for i in range(len(self.equipment)):
if self.equipment[i].name in self.expected_response:
self.respond(len(self.hand)+i)
did_respond = True
break
if not did_respond:
self.respond(-1)
elif self.pending_action == PendingAction.CHOOSE:
if not self.target_p:
self.choose(randrange(0, len(self.available_cards)))
else:
target = self.game.get_player_named(self.target_p)
self.choose(randrange(0, len(target.hand)+len(target.equipment)))
def play_turn(self): def play_turn(self):
if self.lives == 0: if self.lives == 0: