fix bot wait and add commands

This commit is contained in:
Alberto Xamin 2020-12-06 19:29:40 +01:00
parent 6abd3bfffe
commit c356917ec2
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
3 changed files with 26 additions and 13 deletions

View File

@ -119,19 +119,30 @@ def join_room(sid, room):
@sio.event @sio.event
def chat_message(sid, msg): 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}') if len(msg) > 0:
if '/addbot' in msg and not ses.game.started: if msg[0] == '/':
if len(msg.split()) > 1: if '/addbot' in msg and not ses.game.started:
for _ in range(int(msg.split()[1])): if len(msg.split()) > 1:
ses.game.add_player(Player(f'AI_{random.randint(0,100)}', 'bot', sio, bot=True)) 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:
if any([p.is_bot for p in ses.game.players]):
[p for p in ses.game.players if p.is_bot][-1].disconnect()
elif '/suicide' in msg and ses.game.started:
ses.lives = 0
ses.notify_self()
elif '/cancelgame' in msg and ses.game.started:
ses.game.reset()
elif '/gameinfo' in msg:
sio.emit('chat_message', room=sid, data=f'info: {ses.game.__dict__}')
elif '/meinfo' in msg:
sio.emit('chat_message', room=sid, data=f'info: {ses.__dict__}')
else:
sio.emit('chat_message', room=sid, data=f'{msg} COMMAND NOT FOUND')
else: else:
ses.game.add_player(Player(f'AI_{random.randint(0,100)}', 'bot', sio, bot=True)) sio.emit('chat_message', room=ses.game.name, data=f'[{ses.name}]: {msg}')
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()
elif '/suicide' in msg and ses.game.started:
ses.lives = 0
ses.notify_self()
@sio.event @sio.event
def start_game(sid): def start_game(sid):

View File

@ -45,6 +45,8 @@ class Game:
self.notify_room() self.notify_room()
def add_player(self, player: players.Player): def add_player(self, player: players.Player):
if player.is_bot and len(self.players) >= 8:
return
if player in self.players or len(self.players) >= 10: if player in self.players or len(self.players) >= 10:
return return
if len(self.players) > 7: if len(self.players) > 7:

View File

@ -174,7 +174,7 @@ class Player:
def bot_logic(self): def bot_logic(self):
if self.game.shutting_down: return if self.game.shutting_down: return
if self.pending_action != PendingAction.WAIT: if self.pending_action != None and self.pending_action != PendingAction.WAIT:
eventlet.sleep(uniform(0.6, 1.5)) eventlet.sleep(uniform(0.6, 1.5))
else: else:
return return