libs.commands.report.winner
libs/commands/report/winner.py
1""" 2libs/commands/report/winner.py 3""" 4 5from typing import TYPE_CHECKING, Any 6 7import matplotlib.pyplot as plt 8import pandas as pd 9 10import libs.global_value as g 11from libs.domain.datamodels import GameInfo 12from libs.functions import message 13from libs.functions.compose import text_item 14from libs.types import StyleOptions 15from libs.utils import formatter, graphutil, textutil 16 17if TYPE_CHECKING: 18 from integrations.protocols import MessageParserProtocol 19 20 21def plot(m: "MessageParserProtocol") -> None: 22 """ 23 成績上位者を一覧化 24 25 Args: 26 m (MessageParserProtocol): メッセージデータ 27 28 """ 29 # --- データ取得 30 game_info = GameInfo() 31 results_df = g.params.read_data("REPORT_WINNER") 32 if len(results_df) == 0: 33 m.set_headline(message.random_reply(m, "no_hits"), StyleOptions(title="成績上位")) 34 m.status.result = False 35 return 36 37 # 匿名化 38 if g.params.anonymous: 39 name_list: list[Any] = [] 40 for col in [f"name{x}" for x in range(1, 6)]: 41 name_list.extend(results_df[col].unique().tolist()) 42 mapping_dict = formatter.anonymous_mapping(list(set(name_list))) 43 for col in [f"name{x}" for x in range(1, 6)]: 44 results_df[col] = results_df[col].replace(mapping_dict) 45 46 # --- 集計 47 results: dict[str, Any] = {} 48 for _, v in results_df.iterrows(): 49 results[v["collection"]] = {} 50 results[v["collection"]]["集計月"] = v["collection"] 51 for x in range(1, 6): 52 if v.isna()[f"point{x}"]: 53 results[v["collection"]][f"{x}位"] = "該当者なし" 54 else: 55 results[v["collection"]][f"{x}位"] = "{} ({}pt)".format( 56 v[f"name{x}"], 57 str("{:+}".format(v[f"point{x}"])).replace("-", "▲"), 58 ) 59 60 m.set_headline(message.header(game_info, m), StyleOptions(title="成績上位者")) 61 62 # --- グラフ設定 63 match g.adapter.conf.plotting_backend: 64 case "plotly": 65 report_file_path = textutil.save_file_path("report.html") 66 case _: 67 graphutil.setup() 68 report_file_path = textutil.save_file_path("report.png") 69 plt.rcParams["font.size"] = 6 70 71 # 色彩設定 72 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 73 case text_color, bg_color if text_color == "black" and bg_color == "white": 74 line_color1 = "#ffffff" 75 line_color2 = "#dddddd" 76 case text_color, bg_color if text_color == "white" and bg_color == "black": 77 line_color1 = "#000000" 78 line_color2 = "#111111" 79 case _: 80 line_color1 = plt.rcParams["figure.facecolor"] 81 line_color2 = plt.rcParams["figure.facecolor"] 82 83 column_labels = list(results[list(results.keys())[0]].keys()) 84 column_color = ["#000080" for i in column_labels] 85 86 cell_param = [] 87 cell_color = [] 88 line_count = 0 89 for _, val in results.items(): 90 line_count += 1 91 cell_param.append([val[y] for y in column_labels]) 92 if int(line_count % 2): 93 cell_color.append([line_color1 for i in column_labels]) 94 else: 95 cell_color.append([line_color2 for i in column_labels]) 96 97 fig = plt.figure(figsize=(6.5, (len(results) * 0.2) + 0.8), dpi=200, tight_layout=True) 98 ax_dummy = fig.add_subplot(111) 99 ax_dummy.axis("off") 100 101 plt.title("成績上位者", fontsize=12) 102 103 tb = plt.table( 104 colLabels=column_labels, 105 colColours=column_color, 106 cellText=cell_param, 107 cellColours=cell_color, 108 loc="center", 109 ) 110 111 tb.auto_set_font_size(False) 112 for i in range(len(column_labels)): 113 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 114 for i in range(len(results.keys()) + 1): 115 for j in range(len(column_labels)): 116 tb[i, j].set_text_props(ha="center") 117 118 # 追加テキスト 119 remark_text = "".join(text_item.remarks(True)) + text_item.search_word(True) 120 add_text = "{} {}".format( 121 f"[検索範囲:{text_item.search_range()}]", 122 f"[{remark_text}]" if remark_text else "", 123 ) 124 125 fig.text( 126 0.01, 127 0.02, # 表示位置(左下0,0 右下0,1) 128 add_text, 129 transform=fig.transFigure, 130 fontsize=6, 131 ) 132 133 fig.savefig(report_file_path) 134 135 match g.adapter.interface_type: 136 case "slack" | "discord": 137 m.set_message(report_file_path, StyleOptions(title="成績上位者", use_comment=True, header_hidden=True)) 138 case "web": 139 m.set_message(formatter.df_rename(results_df, StyleOptions()), StyleOptions(title="成績上位者")) 140 case _: 141 m.set_message(pd.DataFrame(results).T, StyleOptions())
22def plot(m: "MessageParserProtocol") -> None: 23 """ 24 成績上位者を一覧化 25 26 Args: 27 m (MessageParserProtocol): メッセージデータ 28 29 """ 30 # --- データ取得 31 game_info = GameInfo() 32 results_df = g.params.read_data("REPORT_WINNER") 33 if len(results_df) == 0: 34 m.set_headline(message.random_reply(m, "no_hits"), StyleOptions(title="成績上位")) 35 m.status.result = False 36 return 37 38 # 匿名化 39 if g.params.anonymous: 40 name_list: list[Any] = [] 41 for col in [f"name{x}" for x in range(1, 6)]: 42 name_list.extend(results_df[col].unique().tolist()) 43 mapping_dict = formatter.anonymous_mapping(list(set(name_list))) 44 for col in [f"name{x}" for x in range(1, 6)]: 45 results_df[col] = results_df[col].replace(mapping_dict) 46 47 # --- 集計 48 results: dict[str, Any] = {} 49 for _, v in results_df.iterrows(): 50 results[v["collection"]] = {} 51 results[v["collection"]]["集計月"] = v["collection"] 52 for x in range(1, 6): 53 if v.isna()[f"point{x}"]: 54 results[v["collection"]][f"{x}位"] = "該当者なし" 55 else: 56 results[v["collection"]][f"{x}位"] = "{} ({}pt)".format( 57 v[f"name{x}"], 58 str("{:+}".format(v[f"point{x}"])).replace("-", "▲"), 59 ) 60 61 m.set_headline(message.header(game_info, m), StyleOptions(title="成績上位者")) 62 63 # --- グラフ設定 64 match g.adapter.conf.plotting_backend: 65 case "plotly": 66 report_file_path = textutil.save_file_path("report.html") 67 case _: 68 graphutil.setup() 69 report_file_path = textutil.save_file_path("report.png") 70 plt.rcParams["font.size"] = 6 71 72 # 色彩設定 73 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 74 case text_color, bg_color if text_color == "black" and bg_color == "white": 75 line_color1 = "#ffffff" 76 line_color2 = "#dddddd" 77 case text_color, bg_color if text_color == "white" and bg_color == "black": 78 line_color1 = "#000000" 79 line_color2 = "#111111" 80 case _: 81 line_color1 = plt.rcParams["figure.facecolor"] 82 line_color2 = plt.rcParams["figure.facecolor"] 83 84 column_labels = list(results[list(results.keys())[0]].keys()) 85 column_color = ["#000080" for i in column_labels] 86 87 cell_param = [] 88 cell_color = [] 89 line_count = 0 90 for _, val in results.items(): 91 line_count += 1 92 cell_param.append([val[y] for y in column_labels]) 93 if int(line_count % 2): 94 cell_color.append([line_color1 for i in column_labels]) 95 else: 96 cell_color.append([line_color2 for i in column_labels]) 97 98 fig = plt.figure(figsize=(6.5, (len(results) * 0.2) + 0.8), dpi=200, tight_layout=True) 99 ax_dummy = fig.add_subplot(111) 100 ax_dummy.axis("off") 101 102 plt.title("成績上位者", fontsize=12) 103 104 tb = plt.table( 105 colLabels=column_labels, 106 colColours=column_color, 107 cellText=cell_param, 108 cellColours=cell_color, 109 loc="center", 110 ) 111 112 tb.auto_set_font_size(False) 113 for i in range(len(column_labels)): 114 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 115 for i in range(len(results.keys()) + 1): 116 for j in range(len(column_labels)): 117 tb[i, j].set_text_props(ha="center") 118 119 # 追加テキスト 120 remark_text = "".join(text_item.remarks(True)) + text_item.search_word(True) 121 add_text = "{} {}".format( 122 f"[検索範囲:{text_item.search_range()}]", 123 f"[{remark_text}]" if remark_text else "", 124 ) 125 126 fig.text( 127 0.01, 128 0.02, # 表示位置(左下0,0 右下0,1) 129 add_text, 130 transform=fig.transFigure, 131 fontsize=6, 132 ) 133 134 fig.savefig(report_file_path) 135 136 match g.adapter.interface_type: 137 case "slack" | "discord": 138 m.set_message(report_file_path, StyleOptions(title="成績上位者", use_comment=True, header_hidden=True)) 139 case "web": 140 m.set_message(formatter.df_rename(results_df, StyleOptions()), StyleOptions(title="成績上位者")) 141 case _: 142 m.set_message(pd.DataFrame(results).T, StyleOptions())
成績上位者を一覧化
Arguments:
- m (MessageParserProtocol): メッセージデータ