def connect(self, host, user, password, db, charset, cursorclass=pymysql.cursors.DictCursor): """ Checkout PyMYSQL documentation for complete walkthrough """ try: self.connection = pymysql.connect(host=host, user=user, password=password, db=db, charset=charset, cursorclass=cursorclass) self.cursor = self.connection.cursor() if self._debug: if self.error_handling == 'print': print('threaded_mysql: [SUCCES] connection was succesfully established.') else: echo_console('threaded_mysql: [SUCCES] connection was succesfully established.') self.connection_method = 1 except: if self._debug: if self.error_handling == 'print': print('threaded_mysql: [ERROR] Not possible to make a connection.') else: echo_console('threaded_mysql: [ERROR] Not possible to make a connection.')
def server_arcjail_add_credits(command): try: userid = command[1] credits = command[2] except IndexError: echo_console("Usage: arcjail_add_credits <userid> <credits>") return try: userid = int(userid) except ValueError: echo_console("Error: userid should be an integer") return try: credits = int(credits) except ValueError: echo_console("Error: credits should be an integer") return try: arcjail_user = arcjail_user_manager.get_by_userid(userid) if arcjail_user is None: raise KeyError except (KeyError, OverflowError, ValueError): echo_console("Couldn't find ArcjailUser (userid={})".format(userid)) return earn_credits(arcjail_user.player, credits, strings_module['reason']) echo_console("Added {} credits to {}'s account".format( credits, arcjail_user.player.name))
def connect_use(self, connection): """ If you created your connection elsewhere in your code, you can pass it to Threaded MySQL :param connection: Your connection socket :return: """ try: self.connection = connection self.cursor = self.connection.cursor() if self._debug: if self.error_handling == 'print': print( 'threaded_mysql: [SUCCES] Cursor created succesfully for your connection.' ) else: echo_console( 'threaded_mysql: [SUCCES] Cursor created succesfully for your connection.' ) self.connection_method = 2 except: if self._debug: if self.error_handling == 'print': print( 'threaded_mysql: [ERROR] Not possible to create cursor.' ) else: echo_console( 'threaded_mysql: [ERROR] Not possible to create cursor.' )
def callback(command_info): session = Session() j2template_txt.stream(rows=session.query(DB_ServerMap).all(), dumpdate=time()).dump(DBDUMP_TXT_PATH) echo_console("Dump written to {}".format(DBDUMP_TXT_PATH)) session.close()
def cmd_mc_launch_vote(command): if status.vote_status != VoteStatus.NOT_STARTED: echo_console( "Can't launch the vote as it has already started or ended") return launch_vote(scheduled=False) echo_console("Map vote has been launched")
def close_all_sessions(self, error=None): if error is not None: for session in self._sessions.values(): try: session.error(error) except Exception: echo_console(EXCEPTION_HEADER) echo_console(format_exc()) self._sessions.clear()
def send_ws_data(data): try: data_encoded = json.dumps({ 'status': "OK", 'custom_data': data, }).encode('utf-8') except (TypeError, UnicodeEncodeError): echo_console(EXCEPTION_HEADER) echo_console(format_exc()) else: self.send_data(data_encoded)
def callback(command_info): session = Session() for db_server_map in session.query(DB_ServerMap).all(): db_server_map.detected = 0 session.commit() echo_console("Operation succeeded.") session.close()
def new_callback(command): index = WORLD_ENTITY_INDEX current_time = time() client_time = self.client_timestamps.get(index, 0) if current_time - client_time < self.timeout: echo_console(strings['anti_spam_message'].get_string()) else: callback(command) self.client_timestamps[index] = current_time
def callback(command_info): from ..map_cycle import build_json_from_mapcycle_txt try: build_json_from_mapcycle_txt() except FileNotFoundError: echo_console("Error: No mapcycle.txt nor mapcycle_default.txt found " "in /cfg directory. You can create one automatically by " "typing 'mc scan_maps_folder'." "") else: echo_console("mapcycle.json was rebuild")
def fire(self, event_name, event_var): exceptions = [] for handler in self.get(event_name, ()): try: handler(**event_var) except Exception as e: exceptions.append(e) echo_console(format_exc()) if exceptions: echo_console("{} exceptions were raised during handling of " "'{}' event".format(len(exceptions), event_name))
def send_command_response(message, sender, multiline): if sender: if ping_on_reply: # TODO: Reply to a specific message using ":<message id>" if multiline: message = message + "\n@" + sender.name else: message = "@" + sender.name + " " + message if announce_se_command_output: # Not sure how messy this will be for multi-line output SayText2("\x07" + se_color + "[SE] TF2Goat\x01: " + message).send() room.send_message(message) else: echo_console(message)
def callback(command_info): echo_console("""mc help: > mc help Shows this help message > mc reload_mapcycle Reloads mapcycle.json > mc rebuild_mapcycle Creates new mapcycle.json based on mapcycle.txt (mapcycle_default.txt) > mc db show [<starting ID>] Prints contents of database.sqlite3. If the starting ID is given, shows the contents only beginning from this ID. > mc db dump_html Dumps contents of the database to an HTML page: <mod folder>/logs/source-python/map_cycle/databasedump.html > mc db dump_txt Dumps contents of the database to a text file: <mod folder>/logs/source-python/map_cycle/databasedump.txt > mc db save Saves current maps list from memory to the database > mc db load Reloads data from the database into memory > mc db set_old <map filename> Marks the given map as old (no NEW! postfix) > mc db set_old_all Marks all known maps as old (no NEW! postfix) > mc db forget_map <map filename> Removes the given map from the database, doesn't remove the map from the mapcycle. Map will be added to the database again if it's still in mapcycle. > mc scan_maps_folder [<map prefix> ...] Scans contents of ../maps folder and puts scanned maps in mapcycle.txt. You can then convert that mapcycle.txt to mapcycle.json by typing 'mc rebuild_mapcycle'. If map prefixes are given, only maps that start with that prefix will be added to the list. Example: mc scan_maps_folder de_ cs_ gg_ """)
def _log(self, level, msg, *args, **kwargs): """Main logging method.""" # Does the message need logged? if self.level > level: # If not, simply return return # Get the areas to be used areas = self.areas # Print to main log file? if MAIN_LOG & areas: # Import engine_server # This is done here to fix an ImportError from engines.server import engine_server # Create the record record = self.logger.makeRecord( self.logger.name, level, '(unknown file)', 0, msg, args, None) # Get the message to send message = _main_log_formatter.format(record) # Print to the main log engine_server.log_print(message + '\n') # Print to the console? if CONSOLE & areas: # If not, print to the console # If <engine>.log_print is called with logging being on, # the console is already echoed with the message. from core import echo_console echo_console(msg) # Print to the script's log file? if SCRIPT_LOG & areas and self.root != _sp_logger: # Print message to the log file self.logger.log(level, msg, *args, **kwargs) # Print to the main SP log file? if SP_LOG & areas: # Print to the SP log file _sp_logger.logger.log(level, msg, *args, **kwargs)
def callback(command_info, map_name: str): session = Session() db_server_map = session.query(DB_ServerMap).filter_by( filename=map_name).first() if db_server_map is None: echo_console("Unknown map: {}".format(map_name)) else: session.delete(db_server_map) session.commit() echo_console("Operation succeeded.") session.close()
def get_session_for_data_transmission(self, session_id): if session_id not in self._sessions: return None session = self._sessions[session_id] for session_ in self._sessions.values(): if session_.id != session.id: try: session_.error(SessionError.TAKEN_OVER) except Exception: echo_console(EXCEPTION_HEADER) echo_console(format_exc()) self._sessions.clear() self._sessions[session_id] = session return session
def set_throwing_mode(command_info, userid: int, mode_int: int): """Server command used for setting the grenade throwing mode for a player. Args: userid (int): Userid of the player. mode_int (int): Throwing mode. Examples: >>> cf_set_mode 5 1 Player with userid 5 can change their throwing mode by inspecting their weapon (F by default). >>> cf_set_mode 5 0 Player with userid 5 will throw flashbangs normally. >>> cf_set_mode 3 2 Player with userid 3 will always throw curving flashbangs. """ command_str = command_info.command[0] try: # Is this a valid throwing mode? mode = ThrowingMode(mode_int) except ValueError: echo_console(f'{command_str}: invalid mode -> {mode_int}') return try: # Try to get a PlayerCF instance. player = PlayerCF.from_userid(userid) except ValueError: echo_console(f'{command_str}: invalid userid -> {userid}') return # Are we going back to default game behavior? if mode == ThrowingMode.NORMAL: player.should_curve_flash = False # Or should all flashbangs curve? elif mode == ThrowingMode.ALWAYS_CURVE: player.should_curve_flash = True player.throwing_mode = mode
def callback(command_info): from ..map_cycle import (load_maps_from_db, reload_map_list, reload_mapcycle_json) try: reload_mapcycle_json() echo_console("Loaded JSON from mapcycle.json") except FileNotFoundError: echo_console("Error: Missing mapcycle.json, please rebuild it first") return try: reload_map_list() except RuntimeError as e: echo_console("Error: {}".format(e)) return echo_console("Reloaded maps list from JSON") if load_maps_from_db(): echo_console("Data from the database was reloaded")
def callback(command_info, *prefixes: str): if prefixes: prefixes = list(map(lambda prefix: prefix.lower(), prefixes)) echo_console("Scanning maps only with the " "following prefixes:\n{}".format(','.join(prefixes))) def is_valid_map(path): if path.ext.lower() != ".bsp": return False map_name = path.namebase.lower() for prefix in prefixes: if map_name.startswith(prefix): return True return False else: echo_console("Scanning all maps...") def is_valid_map(path): return path.ext.lower() == ".bsp" rs = [] for map_path in MAPS_DIR.files(): if is_valid_map(map_path): rs.append(map_path.namebase.lower()) if WORKSHOP_DIR.isdir(): echo_console( "Found /maps/workshop dir! Scanning Steam Workshop maps...") for subdir_path in WORKSHOP_DIR.dirs(): subdir_name = subdir_path.namebase.lower() for map_path in subdir_path.files(): map_name = map_path.namebase.lower() if is_valid_map(map_path): rs.append(f"workshop/{subdir_name}/{map_name}") with open(MAPCYCLE_TXT_PATH1, 'w') as f: for map_name in rs: f.write(map_name + '\n') echo_console("{} maps were scanned and written to " "mapcycle.txt".format(len(rs)))
def echo_to_console(text): echo_console("{} ({})".format(text, strftime('%X')))
def save_local_tlds_list(tlds): with open(TLDS_TXT_PATH, 'w') as f: f.write("\n".join(tlds)) def load_local_tlds_list(): try: with open(TLDS_TXT_PATH) as f: return f.read().split('\n') except OSError: return None echo_console("[AdPurge] Attempting to get TLD " "list from {url}...".format(url=TLDS_SOURCE_URL)) tlds = download_tlds_list() if tlds is None: echo_console("[AdPurge] Couldn't get TLD list from the given url, " "obtaining local copy...") tlds = load_local_tlds_list() if tlds is None: raise TLDListUnavailableError( "Top Level Domain list couldn't be obtained") else: echo_console("[AdPurge] {number} domains were obtained from " "the given url".format(number=len(tlds))) save_local_tlds_list(tlds)
def server_arcjail_give_item(command): try: userid = command[1] class_id = command[2] instance_id = command[3] amount = command[4] except IndexError: echo_console("Usage: arcjail_give_item <userid> <class_id> " "<instance_id> <amount>") return try: userid = int(userid) except ValueError: echo_console("Error: userid should be an integer") return item_instance = get_item_instance(class_id, instance_id) if item_instance is None: echo_console("Couldn't find ItemInstance (class_id={}, " "instance_id={})".format(class_id, instance_id)) return try: amount = int(amount) except ValueError: echo_console("Error: amount should be an integer") return try: arcjail_user = arcjail_user_manager.get_by_userid(userid) if arcjail_user is None: raise KeyError except (KeyError, OverflowError, ValueError): echo_console("Couldn't find ArcjailUser (userid={})".format(userid)) return item = arcjail_user.give_item(class_id, instance_id, amount=amount, async=False) echo_console("Given item ID: {}".format(item.id))
def save(): save_maps_to_db() echo_console("Data was saved to the database")
def print_stages(self): echo_console(dumps(self._stage_groups, indent=2))
def callback(command_info, start_id: int = 0): session = Session() echo_console("+----+--------------------------------+--------------+-" "------------------+") echo_console("| ID | Map File Name (w/o .bsp) | Detected | " "Likes/Total |") echo_console("+----+--------------------------------+--------------+-" "------------------+") db_server_maps = session.query(DB_ServerMap).order_by( DB_ServerMap.detected)[start_id:start_id + DB_SHOW_CAP] for db_server_map in db_server_maps: echo_console("| {}| {}| {}| {}|".format( str(db_server_map.id).ljust(3)[:3], db_server_map.filename.ljust(31)[:31], datetime.fromtimestamp( db_server_map.detected).strftime('%x').ljust(13)[:13], "{:.2f}".format( db_server_map.likes / (db_server_map.likes + db_server_map.dislikes)).ljust(18)[:18] if (db_server_map.likes + db_server_map.dislikes != 0) else "n/a".ljust(18), )) echo_console("+----+--------------------------------+--------------+-" "------------------+") echo_console("* Only showing rows from {} to {}".format( start_id + 1, start_id + DB_SHOW_CAP)) session.close()
def on_data_received(self, data): try: message = json.loads(data.decode('utf-8')) except (JSONDecodeError, UnicodeDecodeError): self.stop() return try: action = message['action'] except KeyError: self.stop() return if action == "set-identity": try: steamid = message['steamid'] session_id = message['session_id'] new_salt = message['new_salt'] request_type = message['request_type'] except KeyError: self.stop() return if self.motdplayer is not None: self.stop() return try: motdplayer = motdplayer_dictionary.from_steamid64(steamid) except ValueError: self.send_message(status="ERROR_UNKNOWN_STEAMID") self.stop() return self.motdplayer = motdplayer session = motdplayer.get_session_for_data_transmission(session_id) if session is None: self.send_message(status="ERROR_SESSION_CLOSED_1") self.stop() return self.session = session self.page_request_type = { 'INIT': PageRequestType.INIT, 'AJAX': PageRequestType.AJAX, 'WEBSOCKET': PageRequestType.WEBSOCKET, }[request_type] if self.page_request_type == PageRequestType.WEBSOCKET: if not self.session.ws_allowed: self.send_message(status="ERROR_NO_WS_SUPPORT") self.stop() return def send_ws_data(data): try: data_encoded = json.dumps({ 'status': "OK", 'custom_data': data, }).encode('utf-8') except (TypeError, UnicodeEncodeError): echo_console(EXCEPTION_HEADER) echo_console(format_exc()) else: self.send_data(data_encoded) def stop_ws_transmission(status): self.send_message(status=status) self.stop() self.session.set_ws_callbacks(send_ws_data, stop_ws_transmission) if (new_salt is not None and not motdplayer.confirm_new_salt(new_salt)): self.send_message(status="ERROR_SALT_REFUSED") self.stop() return self.send_message(status="OK") return if action == "switch": try: new_page_id = message['new_page_id'] except KeyError: self.stop() return if self.motdplayer is None: self.stop() return plugin_id = self.session.plugin_id try: new_page_class = _pages_mapping[plugin_id][new_page_id] except KeyError: self.send_message(status="ERROR_UNKNOWN_PAGE") self.stop() return try: allow_switch = self.session.request_switch(new_page_id) except SessionClosedException: self.send_message(status="ERROR_SESSION_CLOSED_2") self.stop() return except Exception: echo_console(EXCEPTION_HEADER) echo_console(format_exc()) self.send_message(status="ERROR_SWITCH_CALLBACK_RAISED") self.stop() return if not allow_switch: self.send_message(status="ERROR_SWITCH_REFUSED") self.stop() return self.session.init_page(new_page_class) self.send_message(status="OK") return if action == "custom-data": try: custom_data = message['custom_data'] except KeyError: self.stop() return if self.motdplayer is None: self.stop() return if self.page_request_type == PageRequestType.WEBSOCKET: try: self.session.receive_ws(custom_data) except SessionClosedException: self.send_message(status="ERROR_SESSION_CLOSED_2") self.stop() return except Exception: echo_console(EXCEPTION_HEADER) echo_console(format_exc()) # Note that we don't stop communication because of general # exceptions else: try: answer = self.session.receive(custom_data, self.page_request_type) except SessionClosedException: self.send_message(status="ERROR_SESSION_CLOSED_3") self.stop() return except Exception: echo_console(EXCEPTION_HEADER) echo_console(format_exc()) self.send_message(status="ERROR_DATA_CALLBACK_RAISED_2") self.stop() return if answer is None: answer = dict() try: answer_encoded = json.dumps({ 'status': "OK", 'custom_data': answer, }).encode('utf-8') except (TypeError, UnicodeEncodeError): echo_console(EXCEPTION_HEADER) echo_console(format_exc()) self.send_message( status="ERROR_DATA_CALLBACK_INVALID_ANSWER") self.stop() return self.send_data(answer_encoded)
def _log(self, level, msg, *args, **kwargs): """Main logging method.""" # Does the message need logged? if self.level > level: # If not, simply return return # Get the areas to be used areas = self.areas # Get wether we should prepend prefix prepend_prefix = kwargs.pop('prepend_prefix', self.prefix is not None) # Print to main log file? if MAIN_LOG & areas: # Import engine_server # This is done here to fix an ImportError from engines.server import engine_server # Create the record record = self.logger.makeRecord(self.logger.name, level, '(unknown file)', 0, msg, args, None) # Get the message to send message = _main_log_formatter.format(record) # Prepend prefix if prepend_prefix: message = self.prefix + message # Print to the main log engine_server.log_print(message + '\n') # Print to the console? if CONSOLE & areas: # If not, print to the console # If <engine>.log_print is called with logging being on, # the console is already echoed with the message. from core import echo_console # Prepend prefix if prepend_prefix: msg = self.prefix + msg echo_console(msg) # Print to the script's log file? if SCRIPT_LOG & areas and self.root != _sp_logger: # Print message to the log file self.logger.log(level, msg, *args, **kwargs) # Print to the main SP log file? if SP_LOG & areas: # Get the given extra dictionary extra = kwargs.setdefault('extra', dict()) # Set the logger name extra.setdefault('logger_name', self.logger.name) # Print to the SP log file _sp_logger.logger.log(level, msg, *args, **kwargs)
def complete_task(self, worker, prio=None): query = worker[0] args = worker[1] callback = worker[2] data_pack = worker[3] get_info = worker[4] query_type = worker[5] try: if get_info: get_info['time'] = timestamp() - get_info['time'] if args: self.cursor.execute(query, args) else: self.cursor.execute(query) if query_type == 0: if get_info: if callback: if data_pack: callback(data_pack, get_info) else: callback(get_info) else: if callback: if data_pack: callback(data_pack) else: callback() if query_type == 1: data = self.cursor.fetchone() if get_info: if callback: if data_pack: callback(data, data_pack, get_info) else: callback(data, get_info) else: if callback: if data_pack: callback(data, data_pack) else: callback(data) if query_type == 2: data = self.cursor.fetchall() if get_info: if callback: if data_pack: callback(data, data_pack, get_info) else: callback(data, get_info) else: if callback: if data_pack: callback(data, data_pack) else: callback(data) if prio: self._p_queue.task_done() else: self._r_queue.task_done() except Exception as SQL_ERROR: # Possible errors class_error, actual_error, traceback = exc_info() format_error = '-' * 64 + '\nExceptions probable cause (SQL Query: {0})\n{1}\nActual Error:\n{2}'.format( query, class_error, SQL_ERROR) if self.error_handling == 'print': print(format_error) print('-' * 64) else: echo_console(format_error) echo_console('-' * 64) logging_error(traceback_format_exc())
def server_command_test(command): decalmanager.refresh() echo_console("Refreshed the decals to paint!")
def load(): load_maps_from_db() echo_console("Data from the database was reloaded")