new bot logic

This commit is contained in:
Alberto Xamin 2020-12-25 21:00:22 +01:00
parent 9781770577
commit 1bfa6ab2b6
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
5 changed files with 30 additions and 19 deletions

View File

@ -166,10 +166,13 @@ def chat_message(sid, msg):
if msg[0] == '/': if msg[0] == '/':
if '/addbot' in msg and not ses.game.started: if '/addbot' in msg and not ses.game.started:
if len(msg.split()) > 1: if len(msg.split()) > 1:
for _ in range(int(msg.split()[1])): # for _ in range(int(msg.split()[1])):
ses.game.add_player(Player(f'AI_{random.randint(0,1000)}', 'bot', sio, bot=True)) # ses.game.add_player(Player(f'AI_{random.randint(0,1000)}', 'bot', sio, bot=True))
sio.emit('chat_message', room=ses.game.name, data={'color': f'red','text':f'Only 1 bot at the time'})
else: else:
ses.game.add_player(Player(f'AI_{random.randint(0,1000)}', 'bot', sio, bot=True)) bot = Player(f'AI_{random.randint(0,1000)}', 'bot', sio, bot=True)
ses.game.add_player(bot)
bot.bot_spin()
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()
@ -260,7 +263,7 @@ def chat_message(sid, msg):
sio.emit('chat_message', room=sid, data={'color': f'','text':f'info: {ses.__dict__}'}) sio.emit('chat_message', room=sid, data={'color': f'','text':f'info: {ses.__dict__}'})
elif '/mebot' in msg: elif '/mebot' in msg:
ses.is_bot = not ses.is_bot ses.is_bot = not ses.is_bot
ses.notify_self() ses.bot_spin()
else: else:
sio.emit('chat_message', room=sid, data={'color': f'','text':f'{msg} COMMAND NOT FOUND'}) sio.emit('chat_message', room=sid, data={'color': f'','text':f'{msg} COMMAND NOT FOUND'})
else: else:

View File

@ -369,10 +369,12 @@ class Game:
if player in self.spectators: if player in self.spectators:
self.spectators.remove(player) self.spectators.remove(player)
return return
if player.is_bot and not self.started:
player.game = None
if self.disconnect_bot and self.started: if self.disconnect_bot and self.started:
player.is_bot = True player.is_bot = True
eventlet.sleep(15) # he may reconnect eventlet.sleep(15) # he may reconnect
player.notify_self() player.bot_spin()
else: else:
self.player_death(player=player, disconnected=True) self.player_death(player=player, disconnected=True)
# else: # else:
@ -488,6 +490,8 @@ class Game:
print('resetting lobby') print('resetting lobby')
self.players.extend(self.spectators) self.players.extend(self.spectators)
self.spectators = [] self.spectators = []
for bot in [p for p in self.players if p.is_bot]:
bot.game = None
self.players = [p for p in self.players if not p.is_bot] self.players = [p for p in self.players if not p.is_bot]
print(self.players) print(self.players)
self.started = False self.started = False

View File

@ -199,7 +199,6 @@ class Player:
ser.pop('sid') ser.pop('sid')
ser.pop('on_pick_cb') ser.pop('on_pick_cb')
ser.pop('on_failed_response_cb') ser.pop('on_failed_response_cb')
# ser.pop('expected_response')
ser.pop('attacker') ser.pop('attacker')
if self.attacker: if self.attacker:
ser['attacker'] = self.attacker.name ser['attacker'] = self.attacker.name
@ -213,22 +212,23 @@ class Player:
self.sio.emit('self', room=self.sid, data=json.dumps( self.sio.emit('self', room=self.sid, data=json.dumps(
ser, default=lambda o: o.__dict__)) ser, default=lambda o: o.__dict__))
self.game.player_death(self) self.game.player_death(self)
elif not self.is_bot: if self.game: # falso quando un bot viene eliminato dalla partita
self.sio.emit('self_vis', room=self.sid, data=json.dumps( self.sio.emit('self_vis', room=self.sid, data=json.dumps(self.game.get_visible_players(self), default=lambda o: o.__dict__))
self.game.get_visible_players(self), default=lambda o: o.__dict__)) self.sio.emit('self', room=self.sid, data=json.dumps(
if not self.is_bot: ser, default=lambda o: o.__dict__))
self.sio.emit('self', room=self.sid, data=json.dumps( self.game.notify_all()
ser, default=lambda o: o.__dict__))
self.game.notify_all() def bot_spin(self):
else: while self.is_bot and self.game != None and not self.game.shutting_down:
self.game.notify_all() eventlet.sleep(uniform(self.game.bot_speed/2-0.1, self.game.bot_speed))
self.bot_logic() if self.lives > 0 or self.is_ghost:
self.game.notify_all() self.bot_logic()
def bot_logic(self): def bot_logic(self):
if self.game.shutting_down: return if self.game.shutting_down: return
if self.pending_action != None and self.pending_action != PendingAction.WAIT: if self.pending_action != None and self.pending_action != PendingAction.WAIT:
eventlet.sleep(uniform(self.game.bot_speed/2-0.1, self.game.bot_speed)) # eventlet.sleep(uniform(self.game.bot_speed/2-0.1, self.game.bot_speed))
pass
else: else:
return return
if self.pending_action == PendingAction.PICK: if self.pending_action == PendingAction.PICK:

View File

@ -48,7 +48,7 @@
</div> </div>
<div v-if="started"> <div v-if="started">
<deck :endTurnAction="()=>{wantsToEndTurn = true}"/> <deck :endTurnAction="()=>{wantsToEndTurn = true}"/>
<player :isEndingTurn="wantsToEndTurn" :cancelEndingTurn="()=>{wantsToEndTurn = false}" :chooseCardFromPlayer="choose"/> <player :isEndingTurn="wantsToEndTurn" :cancelEndingTurn="()=>{wantsToEndTurn = false}" :chooseCardFromPlayer="choose" :cancelChooseCardFromPlayer="()=>{hasToChoose=false}"/>
</div> </div>
</div> </div>
<chat/> <chat/>

View File

@ -66,6 +66,7 @@ export default {
name: 'Player', name: 'Player',
props: { props: {
chooseCardFromPlayer: Function, chooseCardFromPlayer: Function,
cancelChooseCardFromPlayer: Function,
isEndingTurn: Boolean, isEndingTurn: Boolean,
cancelEndingTurn: Function, cancelEndingTurn: Function,
}, },
@ -147,6 +148,9 @@ export default {
this.chooseCardFromPlayer(self.target_p) this.chooseCardFromPlayer(self.target_p)
} else if (this.pending_action == 5) { } else if (this.pending_action == 5) {
this.shouldChooseCard = true this.shouldChooseCard = true
} else {
this.cancelChooseCardFromPlayer()
this.shouldChooseCard = false
} }
}, },
self_vis(vis) { self_vis(vis) {