async def command_messages(): # form_list = CommandMessagesForm(data={"items": [{"message_name": "thing", 'command_message': "reee"}, # {'message_name': 'thingy2', 'command_message': 'acquire'}]}) form_data = await get_form_data() messages = [] for key, value in app.overlord.messages.items(): messages.append({'message_name': key, "command_message": value}) form_list = CommandMessagesForm(form_data, data={"items": messages}) test_command_form = TestCommandForm(form_data, data={'user_points': 10000}) if request.method == 'POST': if form_list.validate(): new_messages = {} saved_messages = {} default_messages = load_message_templates() for res in form_list.items.data: new_messages[res['message_name']] = res['command_message'] if res['message_name'] in default_messages: if res['command_message'] != default_messages[res['message_name']]: print(f"Saved {res['command_message']} because: {res['command_message']} is different than {default_messages[res['message_name']]}") saved_messages[res['message_name']] = res['command_message'] app.overlord.messages = new_messages session = database.Session() # session.query(database.Settings).get('messages').value = json.dumps(app.overlord.messages) if messages_db := session.query(database.Settings).get('messages'): messages_db.value = json.dumps(saved_messages) else: session.add(database.Settings(key='messages', value=json.dumps(saved_messages))) session.commit() await flash("Command Outputs saved successfully.")
def load_age(self, session: database.Session): age = session.query(database.Settings).get('age') if age is None: if os.path.exists('lib/age'): with open('lib/age', "r") as f: age = f.read() session.add(database.Settings(key='age', value=age)) session.commit() os.remove('lib/age') else: session.add(database.Settings(key='age', value='0')) session.commit() age = 0 else: age = age.value self.months = int(age)
def currency_system(self): session = database.Session() currency_system_db = session.query(database.Settings).get('currency_system') if currency_system_db is not None: res = currency_system_db.value else: res = '' session.add(database.Settings(key='currency_system', value='')) session.commit() return res
async def command_names(): form_data = await get_form_data() # pre_made_choices = [{'command': ('buy', None), 'alias': 'acquire', 'group': 'all'}] form_list = CommandNamesForm(form_data, data={'items': get_choices_for_command_names()}) test_command_form = TestCommandForm(form_data, data={'user_points': 10000}) if request.method == 'POST': if "add_field" in form_data: field_path = form_data.get("add_field").split("-") field = getattr_chain(form_list, field_path) if field is not None: field.append_entry() elif "delete_field" in form_data: *field_path, field_num = form_data.get("delete_field").split("-") field = getattr_chain(form_list, field_path) if field is not None: field.entries = [ entry for entry in field.entries if not entry.id.endswith(f"-{field_num}") ] else: if form_list.validate(): res = {} for result in form_list.items.data: if result['alias'].lower() == 'none': continue result['command'] = ast.literal_eval(result['command']) if result['group'] == 'None': result['group'] = None if result['command'][1] is not None and result['group'] is None: result['command'] = (f'{result["command"][1]} {result["command"][0]}', result['command'][1]) res[(result['alias'], result['group'])] = result['command'][0] # print(f"Added {result} to new thingies") app.overlord.api.command_names = BidirectionalMap(res) default_command_names = load_command_names() saved_result = {} for key in res: if key in default_command_names: if res[key] != default_command_names[key]: saved_result[key] = res[key] else: saved_result[key] = res[key] # print(f"\ndefault: {default_command_names}\nres: {res}\ncurrent_command_names: {app.overlord.api.command_names}\nsaved_result: {saved_result}") session = database.Session() if command_names_db := session.query(database.Settings).get("command_names"): command_names_db.value = repr(saved_result) else: session.add(database.Settings(key='command_names', value=repr(saved_result))) session.commit() # print(f"New command_names: {res}") form_list = CommandNamesForm(data={'items': get_choices_for_command_names()}) await flash("Command Aliases saved successfully.")
def load_key(self, key_name, session=None): if session is None: session = database.Session() key_db = session.query(database.Settings).get(key_name) if key_db: setattr(self, key_name, key_db.value) else: if os.path.exists(f'lib/{key_name}'): with open(f'lib/{key_name}', 'rb') as f: key = pickle.load(f) session.add(database.Settings(key=f'{key_name}', value=key)) session.commit() os.remove(f'lib/{key_name}')
def stream_elements_id(self): if self.tokens_ready: session = database.Session() stream_elements_id_db = session.query(database.Settings).get('stream_elements_id') if stream_elements_id_db: return stream_elements_id_db.value url = f'https://api.streamelements.com/kappa/v2/channels/{self.name}' headers = {'accept': 'application/json'} res = requests.get(url, headers=headers) if res.status_code == 200: stream_elements_id = res.json()['_id'] session.add(database.Settings(key='stream_elements_id', value=stream_elements_id)) return stream_elements_id
async def save_token(token, token_name, length, session=None): if token and len(token[0]) == length: token = token[0] setattr(app.overlord.api, token_name, token) if session is None: session = database.Session() token_db = session.query(database.Settings).get(token_name) if token_db: token_db.value = token else: token_db = database.Settings(key=token_name, value=token) session.add(token_db) session.commit() return f"{token_name} saved successfully"
async def announcements(): form_data = await get_form_data() # form_list = AnnouncementForm(form_data, data={'element_list': [{'name': 'thing', 'contents': 'thingy', 'randomize_from': True}], 'result': '{thing}'}) form_list = AnnouncementForm(form_data, data=app.overlord.announcements) if request.method == 'POST': if "add_field" in form_data: field_path = form_data.get("add_field").split("-") field = getattr_chain(form_list, field_path) if field is not None: field.append_entry() elif "delete_field" in form_data: *field_path, field_num = form_data.get("delete_field").split("-") field = getattr_chain(form_list, field_path) if field is not None: field.entries = [ entry for entry in field.entries if not entry.id.endswith(f"-{field_num}") ] else: formatter = AnnouncementDict.from_list(form_list.element_list.data) result = Announcement(form_list.result.data) try: formatter.validate(result) except ValueError as e: form_list.element_list.errors = [e] # print(form_list.element_list.errors, e) else: # print(str(result).format_map(formatter)) default_announcements = load_announcements() announcements_saved = {'element_list': form_list.element_list.data, 'result': form_list.result.data} session = database.Session() if announcements_db := session.query(database.Settings).get('announcements'): if announcements_saved != default_announcements: announcements_db.value = repr(announcements_saved) elif not announcements_db and announcements_saved != default_announcements: session.add(database.Settings(key='announcements', value=repr(announcements_saved))) session.commit() app.overlord.announcements = announcements_saved await flash("Announcements saved successfully.")
async def load_stream_elements_token(): stream_elements = request.args.getlist('access_token') session = database.Session() await save_token(token=stream_elements, token_name='stream_elements_key', length=22, session=session) expires_in = request.args.getlist('expires_in') if expires_in: expires_at = str(int(time.time()) + int(expires_in[0])) expires_at_db = session.query(database.Settings).get('stream_elements_key_expires_at') if expires_at_db: expires_at_db.value = expires_at else: session.add(database.Settings(key='stream_elements_key_expires_at', value=expires_at)) session.commit() if app.overlord.api.stream_elements_key_just_refreshed: app.overlord.api.stream_elements_key_just_refreshed = False print("Stream_elements Token refreshed.") return redirect(url_for('home')) return redirect(url_for('token_settings'))
def load_names(self, session: database.Session = None): if session is None: session = database.Session() companies = session.query(Company).all() company_names_db = session.query( database.Settings).get('company_names') if company_names_db: self.names = { item["abbv"]: item["company_name"] for item in json.loads(company_names_db.value) } else: self.names = { item["abbv"]: item["company_name"] for item in load_default_names() } session.add( database.Settings(key='company_names', value=json.dumps(load_default_names()))) session.commit() for company in companies: self.names.pop(company.abbv, None) session.commit()
"A company has like stock_price under 0, I have absolutely no idea how was this possible, please tell Razbi about it" ) if message not in res: res.append(message) return res, owners @CachedProperty def currency_name(self): session = database.Session() if currency_name := session.query( database.Settings).get('currency_name'): currency_name = currency_name.value else: currency_name = 'points' session.add( database.Settings(key='currency_name', value=currency_name)) session.commit() return currency_name def mark_dirty(self, setting): if setting in self._cache: del self._cache[setting] @staticmethod def display_credits(): print(f""" -------------------------------------------------------------------------- Welcome to {colored('Stocks of Razbia', 'green')} This program is open-source and you can read the code at {colored('https://github.com/TheRealRazbi/Stocks-of-Razbia', 'yellow')} In fact you can even read the code by opening {colored('lib/code/', 'green')} ALL the minigame files are stored in the same folder as the executable, in the {colored('lib/', 'green')}