libs.registry.team

libs/registry/team.py

  1"""
  2libs/registry/team.py
  3"""
  4
  5import logging
  6
  7import libs.global_value as g
  8from libs.data import initialization, lookup, modify
  9from libs.utils import dbutil, formatter, textutil, validator
 10
 11
 12def create(argument: list) -> str:
 13    """チーム作成
 14
 15    Args:
 16        argument (list): 作成するチーム名
 17
 18    Returns:
 19        str: 処理結果
 20    """
 21
 22    ret = False
 23    msg = "使い方が間違っています。"
 24
 25    if len(argument) == 1:  # 新規追加
 26        team_name = textutil.str_conv(argument[0], "h2z")
 27        if len(g.team_list) > g.cfg.team.registration_limit:
 28            msg = "登録上限を超えています。"
 29        else:  # 登録処理
 30            ret, msg = validator.check_namepattern(team_name, "team")
 31            if ret:
 32                resultdb = dbutil.connection(g.cfg.setting.database_file)
 33                resultdb.execute(
 34                    "insert into team(name) values (?)",
 35                    (team_name,)
 36                )
 37                resultdb.commit()
 38                resultdb.close()
 39                g.team_list = lookup.db.get_team_list()
 40                msg = f"チーム「{team_name}」を登録しました。"
 41                logging.info("add new team: %s", team_name)
 42
 43    return msg
 44
 45
 46def delete(argument: list) -> str:
 47    """チーム削除
 48
 49    Args:
 50        argument (list): 削除するチーム名
 51
 52    Returns:
 53        str: 処理結果
 54    """
 55
 56    msg = "使い方が間違っています。"
 57
 58    if len(argument) == 1:  # 新規追加
 59        team_name = textutil.str_conv(argument[0], "h2z")
 60        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
 61            msg = f"チーム「{team_name}」は登録されていません。"
 62        else:
 63            msg = modify.db_backup()
 64            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
 65            resultdb = dbutil.connection(g.cfg.setting.database_file)
 66            resultdb.execute(
 67                "delete from team where id = ?",
 68                (team_id,)
 69            )
 70            resultdb.execute(
 71                "update member set team_id = null where team_id = ?",
 72                (team_id,)
 73            )
 74            resultdb.commit()
 75            resultdb.close()
 76            g.team_list = lookup.db.get_team_list()
 77            msg += f"\nチーム「{team_name}」を削除しました。"
 78            logging.info("team delete: %s", team_name)
 79
 80    return msg
 81
 82
 83def append(argument: list) -> str:
 84    """チーム所属
 85
 86    Args:
 87        argument (list): 登録情報
 88            - argument[0]: 所属させるチーム名
 89            - argument[1]: 所属するメンバー名
 90
 91    Returns:
 92        str: 処理結果
 93    """
 94
 95    msg = "使い方が間違っています。"
 96
 97    if len(argument) == 1:  # 新規作成
 98        msg = create(argument)
 99
100    if len(argument) == 2:  # チーム所属
101        g.params.update(unregistered_replace=False)
102
103        team_name = textutil.str_conv(argument[0], "h2z")
104        player_name = formatter.name_replace(argument[1])
105        registration_flg = True
106        team_id = None
107
108        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
109            msg = f"チーム「{team_name}」はまだ登録されていません。"
110            registration_flg = False
111        else:
112            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
113
114        if player_name not in g.member_list:  # 未登録プレイヤーチェック
115            msg = f"「{player_name}」はレギュラーメンバーではありません。"
116            registration_flg = False
117
118        # 登録上限を超えていないか?
119        # select count() from member where team_id=? group by team_id;
120        # rows = resultdb.execute("select count() from team where name=?", (team_name,))
121        # count = rows.fetchone()[0]
122        # if count > g.cfg.team.member_limit:
123        #    msg = f"登録上限を超えています。"
124        #    registration_flg = False
125
126        if registration_flg and team_id:  # 登録処理
127            resultdb = dbutil.connection(g.cfg.setting.database_file)
128            resultdb.execute(
129                "update member set team_id = ? where name = ?",
130                (team_id, player_name)
131            )
132            resultdb.commit()
133            resultdb.close()
134            g.team_list = lookup.db.get_team_list()
135            msg = f"チーム「{team_name}」に「{player_name}」を所属させました。"
136            logging.info("team participation: %s -> %s", team_name, player_name)
137
138    return msg
139
140
141def remove(argument: list) -> str:
142    """チームから除名
143
144    Args:
145        argument (list): 登録情報
146            - argument[0]: 対象チーム名
147            - argument[1]: チームから離脱するメンバー名
148
149    Returns:
150        str: 処理結果
151    """
152
153    msg = "使い方が間違っています。"
154
155    resultdb = dbutil.connection(g.cfg.setting.database_file)
156
157    if len(argument) == 1:
158        msg = delete(argument)
159
160    if len(argument) == 2:  # チーム名指
161        g.params.update(unregistered_replace=False)
162        team_name = textutil.str_conv(argument[0], "h2z")
163        player_name = formatter.name_replace(argument[1])
164
165        registration_flg = True
166        team_id = None
167
168        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
169            msg = f"チーム「{team_name}」は登録されていません。"
170            registration_flg = False
171        else:
172            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
173
174        if player_name not in g.member_list:  # 未登録プレイヤーチェック
175            msg = f"「{player_name}」はレギュラーメンバーではありません。"
176            registration_flg = False
177
178        if registration_flg and team_id:  # 登録処理
179            resultdb = dbutil.connection(g.cfg.setting.database_file)
180            resultdb.execute(
181                "update member set team_id = null where name = ?",
182                (player_name,)
183            )
184            resultdb.commit()
185            resultdb.close()
186            g.team_list = lookup.db.get_team_list()
187            msg = f"チーム「{team_name}」から「{player_name}」を離脱させました。"
188            logging.info("team breakaway: %s -> %s", team_name, player_name)
189
190    return msg
191
192
193def clear() -> str:
194    """全チーム削除
195
196    Returns:
197        str: 処理結果
198    """
199
200    msg = modify.db_backup()
201
202    resultdb = dbutil.connection(g.cfg.setting.database_file)
203    resultdb.execute("update member set team_id = null;")
204    resultdb.execute("drop table team;")
205    resultdb.execute("delete from sqlite_sequence where name = 'team';")
206    resultdb.commit()
207    resultdb.close()
208
209    initialization.initialization_resultdb()
210    g.member_list = lookup.db.get_member_list()
211    g.team_list = lookup.db.get_team_list()
212
213    return msg
def create(argument: list) -> str:
13def create(argument: list) -> str:
14    """チーム作成
15
16    Args:
17        argument (list): 作成するチーム名
18
19    Returns:
20        str: 処理結果
21    """
22
23    ret = False
24    msg = "使い方が間違っています。"
25
26    if len(argument) == 1:  # 新規追加
27        team_name = textutil.str_conv(argument[0], "h2z")
28        if len(g.team_list) > g.cfg.team.registration_limit:
29            msg = "登録上限を超えています。"
30        else:  # 登録処理
31            ret, msg = validator.check_namepattern(team_name, "team")
32            if ret:
33                resultdb = dbutil.connection(g.cfg.setting.database_file)
34                resultdb.execute(
35                    "insert into team(name) values (?)",
36                    (team_name,)
37                )
38                resultdb.commit()
39                resultdb.close()
40                g.team_list = lookup.db.get_team_list()
41                msg = f"チーム「{team_name}」を登録しました。"
42                logging.info("add new team: %s", team_name)
43
44    return msg

チーム作成

Arguments:
  • argument (list): 作成するチーム名
Returns:

str: 処理結果

def delete(argument: list) -> str:
47def delete(argument: list) -> str:
48    """チーム削除
49
50    Args:
51        argument (list): 削除するチーム名
52
53    Returns:
54        str: 処理結果
55    """
56
57    msg = "使い方が間違っています。"
58
59    if len(argument) == 1:  # 新規追加
60        team_name = textutil.str_conv(argument[0], "h2z")
61        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
62            msg = f"チーム「{team_name}」は登録されていません。"
63        else:
64            msg = modify.db_backup()
65            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
66            resultdb = dbutil.connection(g.cfg.setting.database_file)
67            resultdb.execute(
68                "delete from team where id = ?",
69                (team_id,)
70            )
71            resultdb.execute(
72                "update member set team_id = null where team_id = ?",
73                (team_id,)
74            )
75            resultdb.commit()
76            resultdb.close()
77            g.team_list = lookup.db.get_team_list()
78            msg += f"\nチーム「{team_name}」を削除しました。"
79            logging.info("team delete: %s", team_name)
80
81    return msg

チーム削除

Arguments:
  • argument (list): 削除するチーム名
Returns:

str: 処理結果

def append(argument: list) -> str:
 84def append(argument: list) -> str:
 85    """チーム所属
 86
 87    Args:
 88        argument (list): 登録情報
 89            - argument[0]: 所属させるチーム名
 90            - argument[1]: 所属するメンバー名
 91
 92    Returns:
 93        str: 処理結果
 94    """
 95
 96    msg = "使い方が間違っています。"
 97
 98    if len(argument) == 1:  # 新規作成
 99        msg = create(argument)
100
101    if len(argument) == 2:  # チーム所属
102        g.params.update(unregistered_replace=False)
103
104        team_name = textutil.str_conv(argument[0], "h2z")
105        player_name = formatter.name_replace(argument[1])
106        registration_flg = True
107        team_id = None
108
109        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
110            msg = f"チーム「{team_name}」はまだ登録されていません。"
111            registration_flg = False
112        else:
113            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
114
115        if player_name not in g.member_list:  # 未登録プレイヤーチェック
116            msg = f"「{player_name}」はレギュラーメンバーではありません。"
117            registration_flg = False
118
119        # 登録上限を超えていないか?
120        # select count() from member where team_id=? group by team_id;
121        # rows = resultdb.execute("select count() from team where name=?", (team_name,))
122        # count = rows.fetchone()[0]
123        # if count > g.cfg.team.member_limit:
124        #    msg = f"登録上限を超えています。"
125        #    registration_flg = False
126
127        if registration_flg and team_id:  # 登録処理
128            resultdb = dbutil.connection(g.cfg.setting.database_file)
129            resultdb.execute(
130                "update member set team_id = ? where name = ?",
131                (team_id, player_name)
132            )
133            resultdb.commit()
134            resultdb.close()
135            g.team_list = lookup.db.get_team_list()
136            msg = f"チーム「{team_name}」に「{player_name}」を所属させました。"
137            logging.info("team participation: %s -> %s", team_name, player_name)
138
139    return msg

チーム所属

Arguments:
  • argument (list): 登録情報
    • argument[0]: 所属させるチーム名
    • argument[1]: 所属するメンバー名
Returns:

str: 処理結果

def remove(argument: list) -> str:
142def remove(argument: list) -> str:
143    """チームから除名
144
145    Args:
146        argument (list): 登録情報
147            - argument[0]: 対象チーム名
148            - argument[1]: チームから離脱するメンバー名
149
150    Returns:
151        str: 処理結果
152    """
153
154    msg = "使い方が間違っています。"
155
156    resultdb = dbutil.connection(g.cfg.setting.database_file)
157
158    if len(argument) == 1:
159        msg = delete(argument)
160
161    if len(argument) == 2:  # チーム名指
162        g.params.update(unregistered_replace=False)
163        team_name = textutil.str_conv(argument[0], "h2z")
164        player_name = formatter.name_replace(argument[1])
165
166        registration_flg = True
167        team_id = None
168
169        if team_name not in [x["team"] for x in g.team_list]:  # 未登録チームチェック
170            msg = f"チーム「{team_name}」は登録されていません。"
171            registration_flg = False
172        else:
173            team_id = [x["id"] for x in g.team_list if x["team"] == team_name][0]
174
175        if player_name not in g.member_list:  # 未登録プレイヤーチェック
176            msg = f"「{player_name}」はレギュラーメンバーではありません。"
177            registration_flg = False
178
179        if registration_flg and team_id:  # 登録処理
180            resultdb = dbutil.connection(g.cfg.setting.database_file)
181            resultdb.execute(
182                "update member set team_id = null where name = ?",
183                (player_name,)
184            )
185            resultdb.commit()
186            resultdb.close()
187            g.team_list = lookup.db.get_team_list()
188            msg = f"チーム「{team_name}」から「{player_name}」を離脱させました。"
189            logging.info("team breakaway: %s -> %s", team_name, player_name)
190
191    return msg

チームから除名

Arguments:
  • argument (list): 登録情報
    • argument[0]: 対象チーム名
    • argument[1]: チームから離脱するメンバー名
Returns:

str: 処理結果

def clear() -> str:
194def clear() -> str:
195    """全チーム削除
196
197    Returns:
198        str: 処理結果
199    """
200
201    msg = modify.db_backup()
202
203    resultdb = dbutil.connection(g.cfg.setting.database_file)
204    resultdb.execute("update member set team_id = null;")
205    resultdb.execute("drop table team;")
206    resultdb.execute("delete from sqlite_sequence where name = 'team';")
207    resultdb.commit()
208    resultdb.close()
209
210    initialization.initialization_resultdb()
211    g.member_list = lookup.db.get_member_list()
212    g.team_list = lookup.db.get_team_list()
213
214    return msg

全チーム削除

Returns:

str: 処理結果