mirror of
https://github.com/norohind/jubilant-system.git
synced 2026-11-24 02:26:41 +03:00
tags_diff2str func and appropriate trigger in ru changes hook
This commit is contained in:
2 files changed
+85
-12
No files matched your search
@@ -96,6 +96,7 @@ tag: {squad_info['tag']}
|
||||
created: {squad_info['created']}
|
||||
platform: {squad_info['platform']}
|
||||
owner: {squad_info['ownerName']}
|
||||
tags:\n{utils.humanify_resolved_user_tags(utils.resolve_user_tags(squad_info['userTags']))}
|
||||
activity:
|
||||
previous season sum: {previous_season_sum}
|
||||
current season sum: {current_season_sum}
|
||||
@@ -120,7 +121,8 @@ def detect_important_changes_ru_squads(squad_info: dict, db_conn: sqlite3.Connec
|
||||
3. Detect if squad changed their motd
|
||||
4. Detect if squad changed their ownership
|
||||
5. Detect if squad changed their minor faction
|
||||
6. Notify discord
|
||||
6. Tags changes
|
||||
7. Notify discord
|
||||
"""
|
||||
|
||||
squad_id = squad_info['id']
|
||||
@@ -191,6 +193,18 @@ def detect_important_changes_ru_squads(squad_info: dict, db_conn: sqlite3.Connec
|
||||
else:
|
||||
message = message + f'Minor faction changed: {old_faction} -> {new_faction}\n'
|
||||
|
||||
# let's check tags changes
|
||||
new_tags_raw, old_tags_raw = new_old_diff('user_tags', db_conn, squad_id)
|
||||
new_tags: list = json.loads(new_tags_raw)
|
||||
old_tags: list = json.loads(old_tags_raw)
|
||||
|
||||
if new_tags == old_tags:
|
||||
# nothing changed
|
||||
pass
|
||||
|
||||
else:
|
||||
message += f"```diff\n{tags_diff2str(new_tags, old_tags)}```"
|
||||
|
||||
if len(message) != 0:
|
||||
utils.notify_discord(f'State changing for `{squad_info["name"]}` {squad_info["tag"]}\n'
|
||||
f'platform: {squad_info["platform"]}\nmembers: {squad_info["memberCount"]}\n'
|
||||
@@ -273,6 +287,36 @@ def new_old_news_diff(column: str, db_conn: sqlite3.Connection, squad_id: int) -
|
||||
return new, old
|
||||
|
||||
|
||||
def tags_diff2str(new_tags_ids: list, old_tags_ids: list) -> str:
|
||||
"""Compares two list of tags, new and old, and returns it in diff like str
|
||||
|
||||
:param new_tags_ids: list ids of new tags
|
||||
:param old_tags_ids: list ids of old tags
|
||||
:return: diff like str
|
||||
"""
|
||||
|
||||
resolved_tags: dict[str, list[str]] = dict()
|
||||
|
||||
removed_tags_ids: list = list(set(old_tags_ids) - set(new_tags_ids))
|
||||
added_tags_ids: list = list(set(new_tags_ids) - set(old_tags_ids))
|
||||
|
||||
tags_union_ids: list = list(set(new_tags_ids).union(set(old_tags_ids)))
|
||||
|
||||
for tag_id in tags_union_ids:
|
||||
collection_name, tag_name = utils.resolve_user_tag(tag_id)
|
||||
|
||||
if tag_id in removed_tags_ids:
|
||||
resolved_tags = utils.append_to_list_in_dict(resolved_tags, collection_name, f'- {tag_name}')
|
||||
|
||||
elif tag_id in added_tags_ids:
|
||||
resolved_tags = utils.append_to_list_in_dict(resolved_tags, collection_name, f'+ {tag_name}')
|
||||
|
||||
else: # tag_id not in added_tags_ids and not in removed_tags_ids - nothing changed
|
||||
resolved_tags = utils.append_to_list_in_dict(resolved_tags, collection_name, f' {tag_name}')
|
||||
|
||||
return utils.humanify_resolved_user_tags(resolved_tags, do_tabulate=False)
|
||||
|
||||
|
||||
insert_data_hooks.append(detect_new_ru_squads)
|
||||
insert_data_hooks.append(detect_important_changes_ru_squads)
|
||||
properly_delete_hooks.append(detect_removing_ru_squads)
|
||||
@@ -16,6 +16,9 @@ BASE_URL = 'https://api.orerve.net/2.0/website/squadron/'
|
||||
INFO_ENDPOINT = 'info'
|
||||
NEWS_ENDPOINT = 'news/list'
|
||||
|
||||
with open('available.json', 'r', encoding='utf-8') as available_file:
|
||||
TAG_COLLECTIONS: dict = json.load(available_file)['SquadronTagData']['SquadronTagCollections']
|
||||
|
||||
# proxy: last request time
|
||||
# ssh -C2 -T -n -N -D 2081 patagonia
|
||||
try:
|
||||
@@ -406,24 +409,22 @@ def get_next_hole_id_for_discover(db_conn: sqlite3.Connection) -> int:
|
||||
return sql_req[0]
|
||||
|
||||
|
||||
def resolve_user_tag(single_user_tag: int) -> [str, str]:
|
||||
for tag_collection in TAG_COLLECTIONS:
|
||||
for tag in tag_collection['SquadronTags']:
|
||||
if tag['ServerUniqueId'] == single_user_tag:
|
||||
return tag_collection['localisedCollectionName'], tag['LocalisedString']
|
||||
|
||||
|
||||
def resolve_user_tags(user_tags: list[int]) -> dict[str, list[str]]:
|
||||
"""Function to resolve user_tags list of ints to dict with tag collections as keys and list of tags as value
|
||||
|
||||
:param user_tags: list of ints of tags to resolve
|
||||
:return: dict of tags
|
||||
"""
|
||||
with open('available.json', 'r', encoding='utf-8') as available_file:
|
||||
available = json.load(available_file)
|
||||
|
||||
tag_collections: dict = available['SquadronTagData']['SquadronTagCollections']
|
||||
_resolved_tags: dict[str, list[str]] = dict()
|
||||
|
||||
def resolve_user_tag(single_user_tag: int) -> [str, str]:
|
||||
for tag_collection in tag_collections:
|
||||
for tag in tag_collection['SquadronTags']:
|
||||
if tag['ServerUniqueId'] == single_user_tag:
|
||||
return tag_collection['localisedCollectionName'], tag['LocalisedString']
|
||||
|
||||
for user_tag in user_tags:
|
||||
collection_name, tag_name = resolve_user_tag(user_tag)
|
||||
if collection_name in _resolved_tags: # if key in dict
|
||||
@@ -435,17 +436,45 @@ def resolve_user_tags(user_tags: list[int]) -> dict[str, list[str]]:
|
||||
return _resolved_tags
|
||||
|
||||
|
||||
def humanify_resolved_user_tags(user_tags: dict[str, list[str]]) -> str:
|
||||
def humanify_resolved_user_tags(user_tags: dict[str, list[str]], do_tabulate=True) -> str:
|
||||
"""Function to make result of resolve_user_tags more human readable
|
||||
|
||||
:param do_tabulate: if we should insert tabulation or you already did it in source data, default to True
|
||||
:param user_tags: result of resolve_user_tags function
|
||||
:return: string with human-friendly tags list
|
||||
"""
|
||||
|
||||
result_str: str = str()
|
||||
if do_tabulate:
|
||||
tab = ' '
|
||||
|
||||
else:
|
||||
tab = str()
|
||||
|
||||
for tag_collection_name in user_tags:
|
||||
result_str += f"{tag_collection_name}:\n"
|
||||
|
||||
for tag in user_tags[tag_collection_name]:
|
||||
result_str += f"\t{tag}\n"
|
||||
result_str += f"{tab}{tag}\n"
|
||||
|
||||
return result_str
|
||||
|
||||
|
||||
def append_to_list_in_dict(dict_to_append: dict[str, list[str]], key: str, value: str) -> dict[str, list[str]]:
|
||||
""" function to handle situation when you have a dict with str as keys and lists of strs as values. Sometimes
|
||||
you will face situation when you want to append some value to a list under specified key but this key might even
|
||||
doesn't exists, then... this function exists
|
||||
|
||||
:param dict_to_append: dict, to which you wanna append a value
|
||||
:param key: key under which you wanna append a value
|
||||
:param value: value to append
|
||||
:return: original dict with appended value under specified key
|
||||
"""
|
||||
|
||||
if key in dict_to_append:
|
||||
dict_to_append[key].append(value)
|
||||
|
||||
else:
|
||||
dict_to_append.update({key: [value]})
|
||||
|
||||
return dict_to_append
|
||||
Reference in new issue
Block a user