@hieuzest/koishi-plugin-mahjongpub 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/api.d.ts +28 -4
- package/lib/api.js +43 -5
- package/lib/index.d.ts +8 -1
- package/lib/index.js +68 -15
- package/lib/locales/zh-CN.json +1 -1
- package/package.json +1 -1
package/lib/api.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { Context, Quester } from 'koishi';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Context, Dict, Quester } from 'koishi';
|
|
2
|
+
export interface Team {
|
|
3
|
+
pw?: string;
|
|
4
|
+
tid: string;
|
|
5
|
+
cid?: string;
|
|
6
|
+
name: string;
|
|
7
|
+
players: string[];
|
|
8
|
+
qq?: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare class TeamAdmin implements Team {
|
|
11
|
+
pw: string;
|
|
12
|
+
options: {
|
|
13
|
+
endpoint: string;
|
|
14
|
+
};
|
|
5
15
|
tid: string;
|
|
6
16
|
cid: string;
|
|
7
17
|
name: string;
|
|
@@ -16,3 +26,17 @@ export declare class Team {
|
|
|
16
26
|
read(): Promise<void>;
|
|
17
27
|
write(): Promise<any>;
|
|
18
28
|
}
|
|
29
|
+
export declare class ContestAdmin {
|
|
30
|
+
cid: string;
|
|
31
|
+
pw: string;
|
|
32
|
+
options: {
|
|
33
|
+
endpoint: string;
|
|
34
|
+
};
|
|
35
|
+
name: string;
|
|
36
|
+
teams: Dict<Team>;
|
|
37
|
+
http: Quester;
|
|
38
|
+
constructor(ctx: Context, cid: string, pw: string, options: {
|
|
39
|
+
endpoint: string;
|
|
40
|
+
});
|
|
41
|
+
read(): Promise<void>;
|
|
42
|
+
}
|
package/lib/api.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
3
|
+
exports.ContestAdmin = exports.TeamAdmin = void 0;
|
|
4
|
+
class TeamAdmin {
|
|
5
5
|
pw;
|
|
6
6
|
options;
|
|
7
7
|
tid;
|
|
@@ -52,9 +52,47 @@ class Team {
|
|
|
52
52
|
return (await this.http.post('/team_post.php', form, { responseType: 'json' }))?.msg;
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
exports.
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
exports.TeamAdmin = TeamAdmin;
|
|
56
|
+
class ContestAdmin {
|
|
57
|
+
cid;
|
|
58
|
+
pw;
|
|
59
|
+
options;
|
|
60
|
+
name;
|
|
61
|
+
teams;
|
|
62
|
+
http;
|
|
63
|
+
constructor(ctx, cid, pw, options) {
|
|
64
|
+
this.cid = cid;
|
|
65
|
+
this.pw = pw;
|
|
66
|
+
this.options = options;
|
|
67
|
+
this.http = ctx.http.extend({
|
|
68
|
+
endpoint: options.endpoint,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async read() {
|
|
72
|
+
const payload = {
|
|
73
|
+
posttype: 'login',
|
|
74
|
+
cid: this.cid,
|
|
75
|
+
c_pw: this.pw,
|
|
76
|
+
};
|
|
77
|
+
const form = new FormData();
|
|
78
|
+
Object.entries(payload).forEach(([key, value]) => form.append(key, value));
|
|
79
|
+
const data = await this.http.post('/api/admin_post.php', form, {
|
|
80
|
+
responseType: 'json',
|
|
81
|
+
});
|
|
82
|
+
if (!data?.c_admin?.cid)
|
|
83
|
+
throw new Error('failed to read contest info');
|
|
84
|
+
this.name = data.c_admin.c_name;
|
|
85
|
+
this.teams = Object.fromEntries(data.c_team.map(team => [team.tid, {
|
|
86
|
+
tid: team.tid,
|
|
87
|
+
cid: this.cid,
|
|
88
|
+
name: team.t_name,
|
|
89
|
+
qq: team.qq.split(/\s+/),
|
|
90
|
+
players: [...(team.t_player?.split(/\s+/) ?? []), ...(team.t_sub?.split(/\s+/) ?? [])],
|
|
91
|
+
pw: team.t_pw,
|
|
92
|
+
}]));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.ContestAdmin = ContestAdmin;
|
|
58
96
|
// namespace Contest {
|
|
59
97
|
// interface Payload {
|
|
60
98
|
// CS_EBD?: string
|
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { Context, Schema } from 'koishi';
|
|
1
|
+
import { Context, Dict, Schema } from 'koishi';
|
|
2
|
+
import { ContestAdmin, TeamAdmin } from './api';
|
|
2
3
|
declare module 'koishi' {
|
|
3
4
|
interface User {
|
|
4
5
|
'mahjongpub/bind-team': string;
|
|
6
|
+
'mahjongpub/bind-teams': Dict<string>;
|
|
5
7
|
}
|
|
6
8
|
interface Channel {
|
|
7
9
|
'mahjongpub/bind-team': string;
|
|
10
|
+
'mahjongpub/bind-contest': string;
|
|
11
|
+
'mahjongpub/bind-contestpw': string;
|
|
8
12
|
}
|
|
9
13
|
}
|
|
10
14
|
export declare class MahjongPub {
|
|
11
15
|
private ctx;
|
|
16
|
+
private config;
|
|
12
17
|
constructor(ctx: Context, config: MahjongPub.Config);
|
|
18
|
+
getTeam(pw: string): TeamAdmin;
|
|
19
|
+
getContest(cid: string, pw: string): ContestAdmin;
|
|
13
20
|
}
|
|
14
21
|
export declare namespace MahjongPub {
|
|
15
22
|
const inject: {
|
package/lib/index.js
CHANGED
|
@@ -11,14 +11,19 @@ function parsePlatform(target) {
|
|
|
11
11
|
}
|
|
12
12
|
class MahjongPub {
|
|
13
13
|
ctx;
|
|
14
|
+
config;
|
|
14
15
|
constructor(ctx, config) {
|
|
15
16
|
this.ctx = ctx;
|
|
17
|
+
this.config = config;
|
|
16
18
|
ctx.i18n.define('zh-CN', require('./locales/zh-CN'));
|
|
17
19
|
ctx.model.extend('user', {
|
|
18
20
|
'mahjongpub/bind-team': 'string',
|
|
21
|
+
'mahjongpub/bind-teams': 'json',
|
|
19
22
|
});
|
|
20
23
|
ctx.model.extend('channel', {
|
|
21
24
|
'mahjongpub/bind-team': 'string',
|
|
25
|
+
'mahjongpub/bind-contest': 'string',
|
|
26
|
+
'mahjongpub/bind-contestpw': 'string',
|
|
22
27
|
});
|
|
23
28
|
ctx.command('mahjongpub.team.bind <pw:string>')
|
|
24
29
|
.alias('!绑定', '!绑定')
|
|
@@ -29,7 +34,7 @@ class MahjongPub {
|
|
|
29
34
|
.action(async ({ session, options }, pw) => {
|
|
30
35
|
if (pw?.length !== 20)
|
|
31
36
|
return;
|
|
32
|
-
const team = new api_1.
|
|
37
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
33
38
|
try {
|
|
34
39
|
await team.read();
|
|
35
40
|
}
|
|
@@ -72,17 +77,29 @@ class MahjongPub {
|
|
|
72
77
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
73
78
|
return session.text('.output', [pw]);
|
|
74
79
|
});
|
|
75
|
-
ctx.command('mahjongpub.team.stats')
|
|
80
|
+
ctx.command('mahjongpub.team.stats [user:user]')
|
|
76
81
|
.alias('!信息', '!信息', '!概况', '!概况')
|
|
77
82
|
.option('channel', '-c <channel:channel>')
|
|
78
|
-
.
|
|
79
|
-
.
|
|
80
|
-
.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
.option('team', '-t <team:string>')
|
|
84
|
+
.userFields(['mahjongpub/bind-team', 'mahjongpub/bind-teams'])
|
|
85
|
+
.channelFields(['mahjongpub/bind-team', 'mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
|
|
86
|
+
.action(async ({ session, options }, user) => {
|
|
87
|
+
let pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
|
|
88
|
+
: session.isDirect ? session.user['mahjongpub/bind-team']
|
|
89
|
+
: session.channel['mahjongpub/bind-contest']
|
|
90
|
+
? (user ? session.user['mahjongpub/bind-teams'][session.channel['mahjongpub/bind-contest']] : session.user['mahjongpub/bind-team'])
|
|
91
|
+
: session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
92
|
+
if (options.team) {
|
|
93
|
+
const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
|
|
94
|
+
if (!cid || !cpw)
|
|
95
|
+
return session.text('.notbind');
|
|
96
|
+
const contest = new api_1.ContestAdmin(ctx, cid, cpw, config);
|
|
97
|
+
await contest.read();
|
|
98
|
+
pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
|
|
99
|
+
}
|
|
83
100
|
if (!pw)
|
|
84
101
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
85
|
-
const team = new api_1.
|
|
102
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
86
103
|
try {
|
|
87
104
|
await team.read();
|
|
88
105
|
return session.text('.output', team);
|
|
@@ -102,7 +119,7 @@ class MahjongPub {
|
|
|
102
119
|
: session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
103
120
|
if (!pw)
|
|
104
121
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
105
|
-
const team = new api_1.
|
|
122
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
106
123
|
try {
|
|
107
124
|
await team.read();
|
|
108
125
|
if (!desc)
|
|
@@ -125,7 +142,7 @@ class MahjongPub {
|
|
|
125
142
|
: session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
126
143
|
if (!pw)
|
|
127
144
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
128
|
-
const team = new api_1.
|
|
145
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
129
146
|
try {
|
|
130
147
|
await team.read();
|
|
131
148
|
if (!img)
|
|
@@ -149,7 +166,7 @@ class MahjongPub {
|
|
|
149
166
|
: session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
150
167
|
if (!pw)
|
|
151
168
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
152
|
-
const team = new api_1.
|
|
169
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
153
170
|
try {
|
|
154
171
|
await team.read();
|
|
155
172
|
team.players.push(...users);
|
|
@@ -171,7 +188,7 @@ class MahjongPub {
|
|
|
171
188
|
: session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
172
189
|
if (!pw)
|
|
173
190
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
174
|
-
const team = new api_1.
|
|
191
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
175
192
|
try {
|
|
176
193
|
await team.read();
|
|
177
194
|
team.players = team.players.filter((_, i) => !indices.includes(i + 1));
|
|
@@ -186,16 +203,25 @@ class MahjongPub {
|
|
|
186
203
|
ctx.command('mahjongpub.team.swap <...indices:natural>')
|
|
187
204
|
.alias('!交换', '!交换')
|
|
188
205
|
.option('channel', '-c <channel:channel>')
|
|
206
|
+
.option('team', '-t <team:string>')
|
|
189
207
|
.userFields(['mahjongpub/bind-team'])
|
|
190
|
-
.channelFields(['mahjongpub/bind-team'])
|
|
208
|
+
.channelFields(['mahjongpub/bind-team', 'mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
|
|
191
209
|
.action(async ({ session, options }, ...indices) => {
|
|
192
|
-
|
|
210
|
+
let pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
|
|
193
211
|
: session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
|
|
212
|
+
if (options.team) {
|
|
213
|
+
const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
|
|
214
|
+
if (!cid || !cpw)
|
|
215
|
+
return session.text('.notbind');
|
|
216
|
+
const contest = new api_1.ContestAdmin(ctx, cid, cpw, config);
|
|
217
|
+
await contest.read();
|
|
218
|
+
pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
|
|
219
|
+
}
|
|
194
220
|
if (!pw)
|
|
195
221
|
return config.informNotbind ? session.text('.notbind') : '';
|
|
196
222
|
if (indices.length % 2 !== 0)
|
|
197
223
|
return session.text('.invalid');
|
|
198
|
-
const team = new api_1.
|
|
224
|
+
const team = new api_1.TeamAdmin(ctx, pw, config);
|
|
199
225
|
try {
|
|
200
226
|
await team.read();
|
|
201
227
|
for (let i = 0; i < indices.length; i += 2) {
|
|
@@ -210,6 +236,33 @@ class MahjongPub {
|
|
|
210
236
|
return session.text('.failed');
|
|
211
237
|
}
|
|
212
238
|
});
|
|
239
|
+
ctx.command('mahjongpub.contest.bind <cid:string> <pw:string>')
|
|
240
|
+
// .alias('!绑定', '!绑定')
|
|
241
|
+
.option('channel', '-c <channel:channel>')
|
|
242
|
+
.channelFields(['mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
|
|
243
|
+
.action(async ({ session, options }, cid, pw) => {
|
|
244
|
+
const contest = new api_1.ContestAdmin(ctx, cid, pw, config);
|
|
245
|
+
try {
|
|
246
|
+
await contest.read();
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
ctx.logger.warn(e);
|
|
250
|
+
return session.text('.failed');
|
|
251
|
+
}
|
|
252
|
+
if (options.channel)
|
|
253
|
+
ctx.database.setChannel(...parsePlatform(options.channel), { 'mahjongpub/bind-contest': cid, 'mahjongpub/bind-contestpw': pw });
|
|
254
|
+
else {
|
|
255
|
+
session.channel['mahjongpub/bind-contest'] = cid;
|
|
256
|
+
session.channel['mahjongpub/bind-contestpw'] = pw;
|
|
257
|
+
}
|
|
258
|
+
return session.text('.success', contest);
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
getTeam(pw) {
|
|
262
|
+
return new api_1.TeamAdmin(this.ctx, pw, this.config);
|
|
263
|
+
}
|
|
264
|
+
getContest(cid, pw) {
|
|
265
|
+
return new api_1.ContestAdmin(this.ctx, cid, pw, this.config);
|
|
213
266
|
}
|
|
214
267
|
}
|
|
215
268
|
exports.MahjongPub = MahjongPub;
|
package/lib/locales/zh-CN.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commands":{"mahjongpub.team.bind":{"description":"绑定队伍","messages":{"failed":"绑定失败","success":"成功: [{cid}:{tid}] {name}"}},"mahjongpub.team.unbind":{"description":"解除绑定","messages":{"notbind":"未绑定","success":"成功"}},"mahjongpub.team.password":{"description":"查看密码","messages":{"notbind":"未绑定","output":"密码: {0}"}},"mahjongpub.team.stats":{"description":"查看队伍信息","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.desc":{"description":"队伍简介","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{description}\n"}},"mahjongpub.team.logo":{"description":"队伍头像","messages":{"notbind":"未绑定","failed":"失败"}},"mahjongpub.team.add":{"description":"添加队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.remove":{"description":"删除队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.swap":{"description":"交换队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}}}}
|
|
1
|
+
{"commands":{"mahjongpub.team.bind":{"description":"绑定队伍","messages":{"failed":"绑定失败","success":"成功: [{cid}:{tid}] {name}"}},"mahjongpub.team.unbind":{"description":"解除绑定","messages":{"notbind":"未绑定","success":"成功"}},"mahjongpub.team.password":{"description":"查看密码","messages":{"notbind":"未绑定","output":"密码: {0}"}},"mahjongpub.team.stats":{"description":"查看队伍信息","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.desc":{"description":"队伍简介","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{description}\n"}},"mahjongpub.team.logo":{"description":"队伍头像","messages":{"notbind":"未绑定","failed":"失败"}},"mahjongpub.team.add":{"description":"添加队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.remove":{"description":"删除队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.swap":{"description":"交换队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.contest.bind":{"description":"绑定比赛","messages":{"failed":"绑定失败","success":"成功: [{cid}] {name}"}}}}
|