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 '/addbot' in msg and not ses.game.started:
if len(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))
# for _ in range(int(msg.split()[1])):
# 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:
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:
if any([p.is_bot for p in ses.game.players]):
[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__}'})
elif '/mebot' in msg:
ses.is_bot = not ses.is_bot
ses.notify_self()
ses.bot_spin()
else:
sio.emit('chat_message', room=sid, data={'color': f'','text':f'{msg} COMMAND NOT FOUND'})
else:

View File

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

View File

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

View File

@ -48,7 +48,7 @@
</div>
<div v-if="started">
<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>
<chat/>

View File

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