def website_data_get(key): try: return DbCore.session.query(WebsiteData).filter_by(key=key).first() except exc.SQLAlchemyError as e: out("SQL Failure to get website_data:", e) DbCore.reconnect() return None
def find_quote(text): try: result = None result = DbCore.session.query(Chat).filter_by(id=text).first() return result except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None
def create_chat_entry(player_id, name, data): try: entry = Chat(player_id=player_id, name=name, data=data, created=text("NOW()")) DbCore.session.add(entry) DbCore.commit() return entry except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None
def check_word(word): try: result = None result = DbCore.session.query(ChatFilter).filter_by(word=word).first() if result: return result return None except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None
def remove_word(text): try: result = DbCore.session.query(ChatFilter).filter_by(word=text).first() if result is not None: DbCore.session.query(ChatFilter).filter_by(word=text).delete() DbCore.session.commit() return text else: return False except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None
def add_word(text): try: result = DbCore.session.query(ChatFilter).filter_by(word=text).first() if result is None: entry = ChatFilter(word=text) DbCore.session.add(entry) DbCore.session.commit() return entry else: return False except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None
def getPlayerCount(active=False): try: if active is False: result = DbCore.session.query(Player) else: result = DbCore.session.query(Player).filter( func.unix_timestamp(Player.last_seen) >= time.time() - Config.active_player_timeframe ) return result.count() except exc.SQLAlchemyError as e: out("SQL Failure - getPlayerCount:", e) DbCore.reconnect() return None
def website_data_set(key, value): try: data = DbCore.session.query(WebsiteData).filter_by(key=key).first() except exc.SQLAlchemyError as e: out("SQL Failure - website_data set:", e) DbCore.reconnect() return False if not data: data = WebsiteData() data.key = key data.value = value DbCore.session.add(data) DbCore.commit() return True
def find_player(steam_id=None, steam_name=None, name=None, exact_match=True): """Find player by steam_name, name or steam id Searches steam_id, steam_name and name. In that order. exact_match: If true - On any name search only return if 1 match Args: steam_name: Default None steam_id: Default None name: Default None exact_match: Bool. Default True. Returns: Player object or None """ try: player = None if steam_id: player = DbCore.session.query(Player).filter_by(steam_id=steam_id).first() elif steam_name: players = DbCore.session.query(Player).filter_by(steam_name=steam_name) if exact_match: if players.count() == 1: player = players.first() else: player = players.first() elif name: players = DbCore.session.query(Player).filter_by(name=name) if exact_match: if players.count() == 1: player = players.first() else: player = players.first() else: out("ERROR: No search parameters in DbCore.find_player") if player: return player return None except exc.SQLAlchemyError as e: out("SQL Failure to find player:", e) DbCore.reconnect() return None
def find_player_wildcard(player_name=None, steam_name=None): """Find player by LIKE %name% Args: player_name: String, default None steam_name: String, default None Returns: Player object or None """ try: player = None wildcard = "%{}%".format(player_name) if player_name: player = DbCore.session.query(Player).filter(Player.name.like(wildcard)).first() elif steam_name: player = DbCore.session.query(Player).filter(Player.steam_name.like(wildcard)).first() else: out("ERROR: No search params. DbCore.find_player_wildcard") return player except exc.SQLAlchemyError as e: out("SQL Failure - find player wildcard:", e) DbCore.reconnect() return None