libs.registry.team

libs/commands/team.py

  1"""
  2libs/commands/team.py
  3"""
  4
  5import logging
  6
  7import libs.global_value as g
  8from libs.data import initialization, modify
  9from libs.functions import configuration
 10from libs.utils import dbutil, formatter, textutil, validator
 11
 12
 13def create(argument: list) -> str:
 14    """チーム作成
 15
 16    Args:
 17        argument (list): 作成するチーム名
 18
 19    Returns:
 20        str: slackにpostする内容(処理結果)
 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.get_connection()
 34                resultdb.execute(
 35                    "insert into team(name) values (?)",
 36                    (team_name,)
 37                )
 38                resultdb.commit()
 39                resultdb.close()
 40                configuration.read_memberslist()
 41                msg = f"チーム「{team_name}」を登録しました。"
 42                logging.notice("add new team: %s", team_name)  # type: ignore
 43
 44    return msg
 45
 46
 47def delete(argument: list) -> str:
 48    """チーム削除
 49
 50    Args:
 51        argument (list): 削除するチーム名
 52
 53    Returns:
 54        str: slackにpostする内容(処理結果)
 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.get_connection()
 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            configuration.read_memberslist()
 78            msg += f"\nチーム「{team_name}」を削除しました。"
 79            logging.notice("team delete: %s", team_name)  # type: ignore
 80
 81    return msg
 82
 83
 84def append(argument: list) -> str:
 85    """チーム所属
 86
 87    Args:
 88        argument (list): 登録情報
 89            - argument[0]: 所属させるチーム名
 90            - argument[1]: 所属するメンバー名
 91
 92    Returns:
 93        str: slackにpostする内容(処理結果)
 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.get_connection()
129            resultdb.execute(
130                "update member set team_id = ? where name = ?",
131                (team_id, player_name)
132            )
133            resultdb.commit()
134            resultdb.close()
135            configuration.read_memberslist()
136            msg = f"チーム「{team_name}」に「{player_name}」を所属させました。"
137            logging.notice("team participation: %s -> %s", team_name, player_name)  # type: ignore
138
139    return msg
140
141
142def remove(argument: list) -> str:
143    """チームから除名
144
145    Args:
146        argument (list): 登録情報
147            - argument[0]: 対象チーム名
148            - argument[1]: チームから離脱するメンバー名
149
150    Returns:
151        str: slackにpostする内容(処理結果)
152    """
153
154    msg = "使い方が間違っています。"
155
156    resultdb = dbutil.get_connection()
157
158    if len(argument) == 1:
159        pass
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.get_connection()
181            resultdb.execute(
182                "update member set team_id = null where name = ?",
183                (player_name,)
184            )
185            resultdb.commit()
186            resultdb.close()
187            configuration.read_memberslist()
188            msg = f"チーム「{team_name}」から「{player_name}」を離脱させました。"
189            logging.notice("team breakaway: %s -> %s", team_name, player_name)  # type: ignore
190
191    return msg
192
193
194def clear() -> str:
195    """全チーム削除
196
197    Returns:
198        str: slackにpostする内容
199    """
200
201    msg = modify.db_backup()
202
203    resultdb = dbutil.get_connection()
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    configuration.read_memberslist()
212
213    return msg
def create(argument: list) -> str:
14def create(argument: list) -> str:
15    """チーム作成
16
17    Args:
18        argument (list): 作成するチーム名
19
20    Returns:
21        str: slackにpostする内容(処理結果)
22    """
23
24    ret = False
25    msg = "使い方が間違っています。"
26
27    if len(argument) == 1:  # 新規追加
28        team_name = textutil.str_conv(argument[0], "h2z")
29        if len(g.team_list) > g.cfg.team.registration_limit:
30            msg = "登録上限を超えています。"
31        else:  # 登録処理
32            ret, msg = validator.check_namepattern(team_name, "team")
33            if ret:
34                resultdb = dbutil.get_connection()
35                resultdb.execute(
36                    "insert into team(name) values (?)",
37                    (team_name,)
38                )
39                resultdb.commit()
40                resultdb.close()
41                configuration.read_memberslist()
42                msg = f"チーム「{team_name}」を登録しました。"
43                logging.notice("add new team: %s", team_name)  # type: ignore
44
45    return msg

チーム作成

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

str: slackにpostする内容(処理結果)

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

チーム削除

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

str: slackにpostする内容(処理結果)

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

チーム所属

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

str: slackにpostする内容(処理結果)

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

チームから除名

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

str: slackにpostする内容(処理結果)

def clear() -> str:
195def clear() -> str:
196    """全チーム削除
197
198    Returns:
199        str: slackにpostする内容
200    """
201
202    msg = modify.db_backup()
203
204    resultdb = dbutil.get_connection()
205    resultdb.execute("update member set team_id = null;")
206    resultdb.execute("drop table team;")
207    resultdb.execute("delete from sqlite_sequence where name = 'team';")
208    resultdb.commit()
209    resultdb.close()
210
211    initialization.initialization_resultdb()
212    configuration.read_memberslist()
213
214    return msg

全チーム削除

Returns:

str: slackにpostする内容