def handle_debug_chat(sender, message): global debug_mode human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] is_debug_chat = False if message == ENTERING_DEBUG_MESSAGE: is_debug_chat = True if sender != human_id: return is_debug_chat # don't chat with bots elif message == 'stop': is_debug_chat = True if debug_mode: chat_human("exiting debug mode") debug_mode = False elif debug_mode: is_debug_chat = True out, err = shell(message) if out: chat_human(WHITE % out) if err: chat_human(RED % err) elif message.startswith('start'): is_debug_chat = True try: player_id = int(message[5:].strip()) except ValueError as e: print e chat_human(str(e)) return True if player_id == fo.playerID(): debug_mode = True # add some variables to scope lines = ['import FreeOrionAI as foAI', 'ai = foAI.foAIstate', 'u = fo.getUniverse()', 'e = fo.getEmpire()' ] shell(';'.join(lines)) # Notify all players this AI entering debug mode fo.sendChatMessage(-1, WHITE % ENTERING_DEBUG_MESSAGE) chat_human(WHITE % "Print 'stop' to exit.") chat_human(WHITE % " Local vars: <u>u</u>(universe), <u>e</u>(empire), <u>ai</u>(aistate)") elif message == 'help': is_debug_chat = True if ais[0] == fo.playerID(): chat_human(WHITE % "Chat commands:") chat_human(WHITE % " <u><rgba 0 255 255 255>start id</rgba></u>: start debug for selected empire") chat_human(WHITE % " <u><rgba 0 255 255 255>stop</rgba></u>: stop debug") chat_human(WHITE % "Empire ids:") for player in fo.allPlayerIDs(): if not fo.playerIsHost(player): chat_human(' <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} empire_name={0.name}</rgba> player_name={1}'.format(fo.getEmpire(fo.playerEmpireID(player)), fo.playerName(player))) return is_debug_chat
def handle_debug_chat(sender, message): human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] if sender != human_id: pass # don't chat with bots elif message == 'stop': global debug_mode debug_mode = False if ais[0] == fo.playerID(): chat_human("exiting debug mode") elif debug_mode: out, err = shell(message) if out: chat_human(WHITE % out) if err: chat_human(RED % err) elif message.startswith('start'): try: player_id = int(message[5:].strip()) except ValueError as e: print e return if player_id == fo.playerID(): global debug_mode debug_mode = True # add some variables to scope lines = [ 'import FreeOrionAI as foAI', 'ai = foAI.foAIstate', 'u = fo.getUniverse()', 'e = fo.getEmpire()' ] shell(';'.join(lines)) chat_human(WHITE % "Entering debug mode. print 'stop' to exit.") chat_human( WHITE % " Local vars: <u>u</u>(universe), <u>e</u>(empire), <u>ai</u>(aistate)" ) elif message == 'help': if ais[0] == fo.playerID(): chat_human(WHITE % "Chat commands:") chat_human( WHITE % " <u><rgba 0 255 255 255>start id</rgba></u>: start debug for selected empire" ) chat_human(WHITE % " <u><rgba 0 255 255 255>stop</rgba></u>: stop debug") chat_human(WHITE % "Empire ids:") for player in fo.allPlayerIDs(): if not fo.playerIsHost(player): chat_human( ' <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} empire_name={0.name}</rgba> player_name={1}' .format(fo.getEmpire(fo.playerEmpireID(player)), fo.playerName(player)))
def chat_human(message): """ Send chat message to human and print it to log. Log message cleared form tags. """ human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] fo.sendChatMessage(human_id, message) print "\n%s\n" % remove_tags(message)
def chat_human(message): """ Send chat message to human and print it to log. Log message cleared form tags. """ human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] fo.sendChatMessage(human_id, message) print "\nChat Message to human: %s" % remove_tags(message)
def _ai_should_respond(): """ Return true if AI should respond to message. To avoid chat pollution only one AI should respond to the message. AI player with the smallest id is selected. """ ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] return ais[0] == fo.playerID()
def chat_human(message): """ Send chat message to human and print it to log. Log message cleared form tags. """ human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] message = str(message) fo.sendChatMessage(human_id, message) debug("Chat Message to human: %s", remove_tags(message))
def handle_debug_chat(sender, message): human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] if sender != human_id: pass # don't chat with bots elif message == 'stop': global debug_mode debug_mode = False if ais[0] == fo.playerID(): chat_human("exiting debug mode") elif debug_mode: out, err = shell(message) if out: chat_human(WHITE % out) if err: chat_human(RED % err) elif message.startswith('start'): try: player_id = int(message[5:].strip()) except ValueError as e: print e return if player_id == fo.playerID(): global debug_mode debug_mode = True # add some variables to scope lines = ['import FreeOrionAI as foAI', 'ai = foAI.foAIstate', 'u = fo.getUniverse()', 'e = fo.getEmpire()' ] shell(';'.join(lines)) chat_human(WHITE % "Entering debug mode. print 'stop' to exit.") chat_human(WHITE % " Local vars: <u>u</u>(universe), <u>e</u>(empire), <u>ai</u>(aistate)") elif message == 'help': if ais[0] == fo.playerID(): chat_human(WHITE % "Chat commands:") chat_human(WHITE % " <u><rgba 0 255 255 255>start id</rgba></u>: start debug for selected empire") chat_human(WHITE % " <u><rgba 0 255 255 255>stop</rgba></u>: stop debug") chat_human(WHITE % "Empire ids:") for player in fo.allPlayerIDs(): if not fo.playerIsHost(player): chat_human(' <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} name={0.name}</rgba>'.format(fo.getEmpire(player)))
def chat_human(message, send_to_logs=True): """ Send chat message to human and print it to log. Log message cleared form tags. """ human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] message = str(message) fo.sendChatMessage(human_id, message) if send_to_logs: debug("Chat Message to human: %s", remove_tags(message))
def process_message(self, sender_id: int, message: str) -> bool: host_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] if sender_id != host_id: return False # only user can send debug message if self._debug_mode: return self._handle_debug_commands(message) else: return self._handle_chat_commands(message)
def print_error(exception, location=None, trace=True): """ Sends error to host chat and print its to log. :param exception: message text or exception :type exception: Exception :param location: text that describes error location :param trace: flag if print traceback """ print "possible recipients host status: %s" % [(x, fo.playerIsHost(x)) for x in fo.allPlayerIDs()] if location: message = '%s in "%s": "%s"' % (UserString('AI_ERROR_MSG', 'AI_Error: AI script error'), location, exception) else: message = '%s: "%s"' % (fo.userString('AI_ERROR_MSG'), exception) chat_human(RED % message) if trace: sys.stderr.write(format_exc())
def print_error(msg, location=None, trace=True): """ Sends error to host chat and print its to log. :param msg: message text :param location: text that describes error location :param trace: flag if print traceback """ recipient_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] if location: message = '%s in "%s": "%s"' % (UserString('AI_ERROR_MSG', 'AI_Error: AI script error'), location, msg) else: message = '%s: "%s"' % (fo.userString('AI_ERROR_MSG'), msg) fo.sendChatMessage(recipient_id, message) print "\n%s\n" % message if trace: print format_exc()
def emit(self, record): """Emit a record. If a formatter is specified, it is used to format the record and then sent to human players. """ try: human_ids = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)] if not human_ids: return msg = self.format(record) for human_id in human_ids: fo.sendChatMessage(human_id, msg) except (KeyboardInterrupt, SystemExit): raise # Hide errors from within the ConsoleLogHandler except: self.handleError(record)
def _handle_help(self): if not self._ai_should_respond(): return help_message = [ self._formatter.white("Chat commands:"), self._formatter.white( " " + self._formatter.underline(self._formatter.blue('start <id>')) + ": start debug for selected empire"), ] for player_id in fo.allPlayerIDs(): if not fo.playerIsHost(player_id): help_message.append(self._get_empire_string(player_id)) self._formatter.white( " " + self._formatter.underline(self._formatter.blue('stop')) + ": stop debug"), chat_human('\n'.join(help_message))
def handle_debug_chat(sender, message): global debug_mode human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] is_debug_chat = False if message == ENTERING_DEBUG_MESSAGE: is_debug_chat = True if sender != human_id: return is_debug_chat # don't chat with bots elif message == 'stop': is_debug_chat = True if debug_mode: chat_human("exiting debug mode") debug_mode = False elif debug_mode: print '>', message, is_debug_chat = True out, err = [x.strip('\n') for x in shell(message)] if out: chat_human(WHITE % out) if err: chat_human(RED % err) elif message.startswith('start'): is_debug_chat = True try: player_id = int(message[5:].strip()) except ValueError as e: print e chat_human(str(e)) return True if player_id == fo.playerID(): debug_mode = True initial_code = [ 'import FreeOrionAI as foAI', ] # add some variables to scope: (name, help text, value) scopes_variable = ( ('ai', 'aistate', 'foAI.foAIstate'), ('u', 'universe', 'fo.getUniverse()'), ('e', 'empire', 'fo.getEmpire()'), ) for var, _, code in scopes_variable: initial_code.append('%s = %s' % (var, code)) shell(';'.join(initial_code)) variable_template = '<u><rgba 255 255 0 255>%s</rgba></u>%s %s' variables = (variable_template % (var, ' ' * (3 - len(var)), name) for var, name, _ in scopes_variable) chat_human(WHITE % "%s\n" "Print <rgba 255 255 0 255>'stop'</rgba> to exit.\n" "Local variables:\n" " %s" % (ENTERING_DEBUG_MESSAGE, '\n '.join(variables))) elif message == 'help': is_debug_chat = True if ais[0] == fo.playerID(): chat_human(WHITE % "Chat commands:") chat_human( WHITE % " <u><rgba 0 255 255 255>start id</rgba></u>: start debug for selected empire" ) chat_human(WHITE % " <u><rgba 0 255 255 255>stop</rgba></u>: stop debug") chat_human(WHITE % "Empire ids:") for player in fo.allPlayerIDs(): if not fo.playerIsHost(player): chat_human( ' <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} empire_name={0.name}</rgba> player_name={1}' .format(fo.getEmpire(fo.playerEmpireID(player)), fo.playerName(player))) return is_debug_chat
def is_host(self, player_id): return fo.playerIsHost(player_id)
def handle_debug_chat(sender, message): global debug_mode human_id = [x for x in fo.allPlayerIDs() if fo.playerIsHost(x)][0] ais = [x for x in fo.allPlayerIDs() if not fo.playerIsHost(x)] is_debug_chat = False if message == ENTERING_DEBUG_MESSAGE: is_debug_chat = True if sender != human_id: return is_debug_chat # don't chat with bots elif message == 'stop': is_debug_chat = True if debug_mode: chat_human("exiting debug mode") debug_mode = False elif debug_mode: print('>', message, end='') is_debug_chat = True out, err = [x.strip('\n') for x in shell(message)] if out: chat_human(WHITE % out) if err: chat_human(RED % err) elif message.startswith('start'): is_debug_chat = True try: player_id = int(message[5:].strip()) except ValueError as e: error(e) chat_human(str(e)) return True if player_id == fo.playerID(): debug_mode = True initial_code = [ 'from aistate_interface import get_aistate', ] # add some variables to scope: (name, help text, value) scopes_variable = ( ('ai', 'aistate', 'get_aistate()'), ('u', 'universe', 'fo.getUniverse()'), ('e', 'empire', 'fo.getEmpire()'), ) for var, _, code in scopes_variable: initial_code.append('%s = %s' % (var, code)) shell(';'.join(initial_code)) variable_template = '<u><rgba 255 255 0 255>%s</rgba></u>%s %s' variables = (variable_template % (var, ' ' * (3 - len(var)), name) for var, name, _ in scopes_variable) chat_human(WHITE % "%s\n" "Print <rgba 255 255 0 255>'stop'</rgba> to exit.\n" "Local variables:\n" " %s" % (ENTERING_DEBUG_MESSAGE, '\n '.join(variables))) elif message == 'help': is_debug_chat = True if ais[0] == fo.playerID(): chat_human(WHITE % "Chat commands:") chat_human(WHITE % " <u><rgba 0 255 255 255>start id</rgba></u>: start debug for selected empire") chat_human(WHITE % " <u><rgba 0 255 255 255>stop</rgba></u>: stop debug") chat_human(WHITE % "Empire ids:") for player in fo.allPlayerIDs(): if not fo.playerIsHost(player): chat_human(' <rgba {0.colour.r} {0.colour.g} {0.colour.b} {0.colour.a}>id={0.empireID} empire_name={0.name}</rgba> player_name={1}'.format(fo.getEmpire(fo.playerEmpireID(player)), fo.playerName(player))) return is_debug_chat