def change_mask_label(message, lang): key, content = get_key_and_content_from_message(message) s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) new_mask_label_og = get_args_from_content(content) if char_info[PLAYBOOK] != JANUS: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.no_playbook')(get_translation(lang, f'playbooks.inverted_names.{JANUS}')) label_does_not_exist = validate_labels(lang, [new_mask_label_og]) if label_does_not_exist: return label_does_not_exist new_mask_label = get_translation(lang, f'inverted_labels.{new_mask_label_og}') if new_mask_label == char_info[MASK_LABEL]: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.already_mask_label')(new_mask_label_og) new_label_value = char_info[LABELS][new_mask_label][VALUE] if new_label_value == MAX_LABEL_VALUE: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.already_max')(new_label_value, new_mask_label_og) char_info[LABELS][new_mask_label][VALUE] = new_label_value + 1 char_info[MASK_LABEL] = new_mask_label upload_to_s3(char_info, key, s3_client) return format_labels(char_info[LABELS], lang)
def add_one_to_two_labels(message, lang): key, content = get_key_and_content_from_message(message) s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) if char_info[PLAYBOOK] != DELINQUENT: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.no_playbook')(get_translation(lang, f'playbooks.inverted_names.{DELINQUENT}')) labels_to_upgrade = get_args_from_content(content) labels = char_info[LABELS] labels_do_not_exist = validate_labels(lang, labels_to_upgrade) if labels_do_not_exist: return labels_do_not_exist for label_name in labels_to_upgrade: label_to_increase = get_translation(lang, f'inverted_labels.{label_name}') value = int(labels[label_to_increase][VALUE]) if value == MAX_LABEL_VALUE: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.already_max')(value, label_name) labels[label_to_increase][VALUE] = 1 + value upload_to_s3(char_info, key, s3_client) return format_labels(labels, lang)
def lock_label(message, lang): key, content = get_key_and_content_from_message(message) label_to_lock_name_og = get_args_from_content(content) label_to_lock_name = get_translation( lang, f'inverted_labels.{label_to_lock_name_og}') s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) if not char_info: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.no_character') labels = char_info[LABELS] label_does_not_exist = validate_labels(lang, [label_to_lock_name_og]) if label_does_not_exist: return label_does_not_exist label_to_lock = labels[label_to_lock_name] if label_to_lock[LOCKED]: return get_translation( lang, f'{PLAYBOOK_INTERACTIONS}.already_locked')(get_translation( lang, f'labels.{label_to_lock_name}')) labels[label_to_lock_name][LOCKED] = True upload_to_s3(char_info, key, s3_client) return format_labels(labels, lang)
def messages(): labels = [label.to_dict() for label in LabelMessage.query.all()] msgs = [message.to_dict() for message in Message.query.all()] if request.method == 'GET': is_admin = current_app.config['ADMIN_USER'] return render_template('messages.html', labels=labels, messages=msgs, is_admin=is_admin) else: fields = request.form.to_dict() for field in fields: # form field name format: 'status_valve' or 'battery_sensor' message = fields[field] mess_name = field.split('_')[0] mess_type = field.split('_')[1] mess_labels = validate_message(message) if not mess_labels and mess_labels != []: flash( f'Incorrect message format for {mess_name.upper()} {mess_type.upper()}' ) else: if not validate_labels(labels, mess_labels): flash( f'Invalid labels for {mess_name.upper()} {mess_type.upper()} message' ) continue db_message = Message.query.filter_by( name=mess_name, mess_type=mess_type).first() db_message.message = message db.session.commit() return redirect(url_for('main.messages'))
def create_character_slash(ctx, lang, playbook_name, character_name, player_name, label_to_increase_og): key = get_key_from_ctx(ctx) s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) if char_info: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.existing_character') label_does_not_exist = validate_labels(lang, [label_to_increase_og]) if label_does_not_exist: return label_does_not_exist label_to_increase = get_translation(lang, f'inverted_labels.{label_to_increase_og}') translated_name = get_translation(lang, f'playbooks.names.{playbook_name}') file_list = get_files_from_dir('playbooks', s3_client) template_key = f'playbooks/{translated_name}' matching_files = list(filter(lambda file_info: file_info["Key"] == f'{template_key}.json', file_list["Contents"])) if not matching_files: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.no_template')(playbook_name) template = info_from_s3(template_key, s3_client) template[LABELS][label_to_increase][VALUE] = template[LABELS][label_to_increase][VALUE] + 1 template['characterName'] = character_name template['playerName'] = player_name upload_to_s3(template, key, s3_client) formated_playbook_name = playbook_name.capitalize() return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.congrats_on_creation')(character_name, formated_playbook_name)
def rearrange_labels(message, lang): key, content = get_key_and_content_from_message(message) s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) new_label_values = get_args_from_content(content) labels = char_info[LABELS] total_sum = 0 new_sum = 0 labels_do_not_exist = validate_labels(lang, labels) if labels_do_not_exist: return labels_do_not_exist for value in new_label_values: int_value = int(value) if int_value < MIN_LABEL_VALUE: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.less_than_min')( MIN_LABEL_VALUE, int_value) if int_value > MAX_LABEL_VALUE: return get_translation( lang, f'{PLAYBOOK_INTERACTIONS}.greater_than_max')(MAX_LABEL_VALUE, int_value) new_sum = new_sum + int_value for label in labels: total_sum = total_sum + int(labels[label][VALUE]) if total_sum + 1 != new_sum: difference = abs(new_sum - total_sum) if new_sum - total_sum > 0: direction = get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.more') elif total_sum == new_sum: difference = '' direction = get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.equal') else: direction = get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.less') return get_translation( lang, f'{PLAYBOOK_INTERACTIONS}.add_one_to_label')(difference, direction) index = 0 for label in labels: labels[label][VALUE] = int(new_label_values[index]) index += 1 upload_to_s3(char_info, key, s3_client) return format_labels(labels, lang)
def edit_labels(message, lang): key, content = get_key_and_content_from_message(message) label_to_increase_name_og, label_to_decrease_name_og = get_args_from_content( content) if label_to_increase_name_og == label_to_decrease_name_og: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.different_labels') label_to_increase_name = get_translation( lang, f'inverted_labels.{label_to_increase_name_og}') label_to_decrease_name = get_translation( lang, f'inverted_labels.{label_to_decrease_name_og}') s3_client = get_s3_client() char_info = info_from_s3(key, s3_client) if not char_info: return get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.no_character') labels = char_info[LABELS] labels_do_not_exist = validate_labels( lang, [label_to_increase_name_og, label_to_decrease_name_og]) if labels_do_not_exist: return labels_do_not_exist label_to_increase = labels[label_to_increase_name] label_to_increase_value = label_to_increase[VALUE] label_to_decrease = labels[label_to_decrease_name] label_to_decrease_value = label_to_decrease[VALUE] if label_is_not_editable(label_to_increase, MAX_LABEL_VALUE): up = get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.up') return get_label_has_border_value_text(label_to_increase_name, label_to_increase, up, lang) if label_is_not_editable(label_to_decrease, MIN_LABEL_VALUE): down = get_translation(lang, f'{PLAYBOOK_INTERACTIONS}.down') return get_label_has_border_value_text(label_to_decrease_name, label_to_decrease, down, lang) labels[label_to_increase_name][VALUE] = label_to_increase_value + 1 labels[label_to_decrease_name][VALUE] = label_to_decrease_value - 1 upload_to_s3(char_info, key, s3_client) return format_labels(labels, lang)