@zhin.js/plugin-rps 1.1.18 → 1.1.20
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/commands/rps/[[action]]/index.js +5 -4
- package/commands/rps/[[action]]/index.ts +5 -4
- package/lib/game-flow.d.ts +4 -4
- package/lib/game-flow.js +65 -58
- package/lib/game-flow.js.map +1 -1
- package/lib/rps-command.d.ts +3 -3
- package/lib/rps-command.js +27 -25
- package/lib/rps-command.js.map +1 -1
- package/lib/session-service.d.ts +2 -2
- package/lib/session-service.js +4 -9
- package/lib/session-service.js.map +1 -1
- package/middlewares/rps-alias/index.js +12 -8
- package/middlewares/rps-alias/index.ts +10 -12
- package/middlewares/rps-choice/index.js +11 -9
- package/middlewares/rps-choice/index.ts +11 -9
- package/package.json +7 -7
- package/src/game-flow.ts +63 -57
- package/src/rps-command.ts +26 -25
- package/src/session-service.ts +6 -12
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
+
import { replyGameOperation, gamePlayerFromInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
2
3
|
import { defineCommand } from 'zhin.js/command';
|
|
3
|
-
import { messageFromCommandInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
4
4
|
import { getRpsHelp, runRpsCommand } from "../../../lib/rps-command.js";
|
|
5
5
|
import { resolveGameServices } from "../../../lib/runtime-store.js";
|
|
6
6
|
export default defineCommand({
|
|
7
7
|
description: 'RPS',
|
|
8
8
|
params: { action: { type: 'string', default: '' } },
|
|
9
|
-
async execute({ params, input, use, locale, formatCommand }) {
|
|
9
|
+
async execute({ params, input, use, locale, formatCommand, session }) {
|
|
10
10
|
const action = normalizeRpsAction(String(params.action ?? ''));
|
|
11
11
|
if (!action || action === 'help')
|
|
12
12
|
return getRpsHelp({ locale, formatCommand });
|
|
13
13
|
const services = resolveGameServices({ use });
|
|
14
|
-
const message =
|
|
15
|
-
|
|
14
|
+
const message = gamePlayerFromInput(input, { locale, formatCommand });
|
|
15
|
+
await replyGameOperation({ session: services, player: message, reply: session?.reply,
|
|
16
|
+
run: () => runRpsCommand(services, message, action) });
|
|
16
17
|
},
|
|
17
18
|
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { replyGameOperation, gamePlayerFromInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
1
2
|
import { defineCommand } from 'zhin.js/command';
|
|
2
|
-
import { messageFromCommandInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
3
3
|
import { getRpsHelp, runRpsCommand } from '../../../src/rps-command.js';
|
|
4
4
|
import { resolveGameServices } from '../../../src/runtime-store.js';
|
|
5
5
|
|
|
6
6
|
export default defineCommand({
|
|
7
7
|
description: 'RPS',
|
|
8
8
|
params: { action: { type: 'string', default: '' } },
|
|
9
|
-
async execute({ params, input, use, locale, formatCommand }) {
|
|
9
|
+
async execute({ params, input, use, locale, formatCommand, session }) {
|
|
10
10
|
const action = normalizeRpsAction(String(params.action ?? ''));
|
|
11
11
|
if (!action || action === 'help') return getRpsHelp({ locale, formatCommand });
|
|
12
12
|
const services = resolveGameServices({ use });
|
|
13
|
-
const message =
|
|
14
|
-
|
|
13
|
+
const message = gamePlayerFromInput(input, { locale, formatCommand });
|
|
14
|
+
await replyGameOperation({ session: services, player: message, reply: session?.reply,
|
|
15
|
+
run: () => runRpsCommand(services, message, action) });
|
|
15
16
|
},
|
|
16
17
|
});
|
package/lib/game-flow.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type GamePlayer, type GameReply } from '@zhin.js/game-kit';
|
|
2
2
|
import { RPS_PREFIX } from './engine.js';
|
|
3
3
|
import type { SessionService } from './session-service.js';
|
|
4
|
-
export declare function startGame(services: SessionService, message:
|
|
5
|
-
export declare function handleChoice(services: SessionService, message:
|
|
6
|
-
export declare function continueGame(services: SessionService, message:
|
|
4
|
+
export declare function startGame(services: SessionService, message: GamePlayer): Promise<GameReply | undefined>;
|
|
5
|
+
export declare function handleChoice(services: SessionService, message: GamePlayer, sessionId: string, choiceId: string): Promise<GameReply | null>;
|
|
6
|
+
export declare function continueGame(services: SessionService, message: GamePlayer): Promise<GameReply>;
|
|
7
7
|
export { RPS_PREFIX };
|
package/lib/game-flow.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createMessages, resolveLocale } from 'zhin.js/i18n';
|
|
2
|
+
import { channelKey } from '@zhin.js/game-kit';
|
|
2
3
|
import { RPS_PREFIX, WIN_TARGET, randomBotMove, resolveRound, } from './engine.js';
|
|
3
4
|
import { buildRpsView } from './view.js';
|
|
4
5
|
/**
|
|
@@ -7,70 +8,76 @@ import { buildRpsView } from './view.js';
|
|
|
7
8
|
* and the choice middleware return fresh text each turn.
|
|
8
9
|
*/
|
|
9
10
|
function renderView(session, message, lastRound) {
|
|
10
|
-
return buildRpsView(session, lastRound, message
|
|
11
|
+
return buildRpsView(session, lastRound, message.target.kind);
|
|
11
12
|
}
|
|
12
13
|
export async function startGame(services, message) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
15
|
+
const ch = channelKey(message);
|
|
16
|
+
const active = await services.getActiveByChannel(ch);
|
|
17
|
+
const locale = resolveLocale(active ? active.locale : message.locale);
|
|
18
|
+
if (active) {
|
|
19
|
+
if (active.player_id === message.sender.id) {
|
|
20
|
+
return messages(locale, 'text1');
|
|
21
|
+
}
|
|
22
|
+
return messages(locale, 'text2', { p0: active.player_name });
|
|
23
|
+
}
|
|
24
|
+
const session = await services.createSession(message);
|
|
25
|
+
return renderView(session, message);
|
|
26
|
+
} });
|
|
24
27
|
}
|
|
25
28
|
export async function handleChoice(services, message, sessionId, choiceId) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
29
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
30
|
+
const session = await services.getById(sessionId);
|
|
31
|
+
const locale = resolveLocale(session ? session.locale : message.locale);
|
|
32
|
+
if (!session)
|
|
33
|
+
return messages(locale, 'text3');
|
|
34
|
+
if (session.status !== 'active' && choiceId !== 'restart') {
|
|
35
|
+
return messages(locale, 'text4');
|
|
36
|
+
}
|
|
37
|
+
if (session.player_id !== message.sender.id)
|
|
38
|
+
return messages(locale, 'text5');
|
|
39
|
+
if (choiceId === 'restart') {
|
|
40
|
+
await services.updateSession(session.id, { status: 'aborted' });
|
|
41
|
+
return (await startGame(services, message)) ?? null;
|
|
42
|
+
}
|
|
43
|
+
if (choiceId === 'quit') {
|
|
44
|
+
await services.updateSession(session.id, { status: 'aborted' });
|
|
45
|
+
return renderView((await services.getById(session.id)), message);
|
|
46
|
+
}
|
|
47
|
+
const player = choiceId;
|
|
48
|
+
if (!['rock', 'paper', 'scissors'].includes(player))
|
|
49
|
+
return messages(locale, 'text6');
|
|
50
|
+
const bot = randomBotMove();
|
|
51
|
+
const result = resolveRound(player, bot);
|
|
52
|
+
let playerWins = session.player_wins;
|
|
53
|
+
let botWins = session.bot_wins;
|
|
54
|
+
if (result === 1)
|
|
55
|
+
playerWins++;
|
|
56
|
+
if (result === 2)
|
|
57
|
+
botWins++;
|
|
58
|
+
let status = 'active';
|
|
59
|
+
if (playerWins >= WIN_TARGET)
|
|
60
|
+
status = 'won';
|
|
61
|
+
else if (botWins >= WIN_TARGET)
|
|
62
|
+
status = 'lost';
|
|
63
|
+
await services.updateSession(session.id, {
|
|
64
|
+
player_wins: playerWins,
|
|
65
|
+
bot_wins: botWins,
|
|
66
|
+
round: session.round + 1,
|
|
67
|
+
status,
|
|
68
|
+
});
|
|
69
|
+
const updated = (await services.getById(session.id));
|
|
70
|
+
return renderView(updated, message, { player, bot, result });
|
|
71
|
+
} });
|
|
67
72
|
}
|
|
68
73
|
export async function continueGame(services, message) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
75
|
+
const session = await services.getActiveForUser(channelKey(message), message.sender.id);
|
|
76
|
+
const locale = resolveLocale(session ? session.locale : message.locale);
|
|
77
|
+
if (!session)
|
|
78
|
+
return messages(locale, 'text7');
|
|
79
|
+
return renderView(session, message);
|
|
80
|
+
} });
|
|
74
81
|
}
|
|
75
82
|
export { RPS_PREFIX };
|
|
76
83
|
const messages = createMessages({
|
package/lib/game-flow.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"game-flow.js","sourceRoot":"","sources":["../src/game-flow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"game-flow.js","sourceRoot":"","sources":["../src/game-flow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAmC,MAAM,mBAAmB,CAAC;AAEhF,OAAO,EACL,UAAU,EACV,UAAU,EACV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{ SessionService } from './session-service.js';
|
|
3
|
-
export declare function getRpsHelp(message?: Pick<
|
|
3
|
+
export declare function getRpsHelp(message?: Pick<GamePlayer, 'locale' | 'formatCommand'>): string;
|
|
4
4
|
export declare const RPS_HELP: string;
|
|
5
|
-
export declare function runRpsCommand(services: SessionService, message:
|
|
5
|
+
export declare function runRpsCommand(services: SessionService, message: GamePlayer, action: string): Promise<GameReply>;
|
package/lib/rps-command.js
CHANGED
|
@@ -2,34 +2,36 @@ import { formatGameHelp, gameCommandFeedback, gameCommandHint, channelKey } from
|
|
|
2
2
|
import { createMessages } from 'zhin.js/i18n';
|
|
3
3
|
import { continueGame, handleChoice, startGame } from './game-flow.js';
|
|
4
4
|
export function getRpsHelp(message = {}) {
|
|
5
|
-
return formatGameHelp(message, { 'zh-CN': "✊✋✌️ 猜拳对决(三局两胜)", en: "✊✋✌️ Rock Paper Scissors (best of three)" }, 'rps', ['start', 'continue', 'quit'], { 'zh-CN': "点击按钮或回复选项数字出拳。", en: "Choose a button or reply with its option number." });
|
|
5
|
+
return formatGameHelp({ presentation: message, title: { 'zh-CN': "✊✋✌️ 猜拳对决(三局两胜)", en: "✊✋✌️ Rock Paper Scissors (best of three)" }, commandPath: 'rps', actions: ['start', 'continue', 'quit'], footer: { 'zh-CN': "点击按钮或回复选项数字出拳。", en: "Choose a button or reply with its option number." } });
|
|
6
6
|
}
|
|
7
7
|
export const RPS_HELP = getRpsHelp();
|
|
8
8
|
export async function runRpsCommand(services, message, action) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
9
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
10
|
+
const ch = channelKey(message);
|
|
11
|
+
const userId = message.sender.id;
|
|
12
|
+
if (!action || action === 'help') {
|
|
13
|
+
const active = await services.getActiveByChannel(ch);
|
|
14
|
+
const lines = [getRpsHelp(message), ''];
|
|
15
|
+
if (active) {
|
|
16
|
+
lines.push(messages(active.locale ?? undefined, 'text1', { p0: active.player_name, p1: active.player_wins, p2: active.bot_wins }));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
lines.push(gameCommandFeedback(message, 'noGame'), gameCommandHint(message, 'rps'));
|
|
20
|
+
}
|
|
21
|
+
return lines.join('\n');
|
|
22
|
+
}
|
|
23
|
+
if (action === 'start')
|
|
24
|
+
return (await startGame(services, message)) ?? '';
|
|
25
|
+
if (action === 'continue')
|
|
26
|
+
return continueGame(services, message);
|
|
27
|
+
if (action === 'quit') {
|
|
28
|
+
const row = await services.getActiveForUser(ch, userId);
|
|
29
|
+
if (!row)
|
|
30
|
+
return gameCommandFeedback(message, 'noGame');
|
|
31
|
+
return (await handleChoice(services, message, row.id, 'quit')) ?? '';
|
|
32
|
+
}
|
|
33
|
+
return gameCommandFeedback(message, 'unknown', action) + '\n\n' + getRpsHelp(message);
|
|
34
|
+
} });
|
|
33
35
|
}
|
|
34
36
|
const messages = createMessages({
|
|
35
37
|
text1: { 'zh-CN': "进行中:{p0} · {p1}:{p2}", en: "Playing: {p0} · {p1}:{p2}" },
|
package/lib/rps-command.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rps-command.js","sourceRoot":"","sources":["../src/rps-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"rps-command.js","sourceRoot":"","sources":["../src/rps-command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAmC,MAAM,m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
import type { Database, Models } from '@zhin.js/core';
|
|
2
|
-
import { BaseSessionService, type
|
|
2
|
+
import { BaseSessionService, type GamePlayer } from '@zhin.js/game-kit';
|
|
3
3
|
import type { RpsSessionRow } from './models.js';
|
|
4
4
|
export type RpsDatabase = Database<unknown, Models, string>;
|
|
5
5
|
export declare class SessionService extends BaseSessionService<RpsSessionRow> {
|
|
6
6
|
constructor(db: RpsDatabase);
|
|
7
|
-
createSession(message:
|
|
7
|
+
createSession(message: GamePlayer): Promise<RpsSessionRow>;
|
|
8
8
|
}
|
|
9
9
|
export declare function createServices(db: RpsDatabase): SessionService;
|
package/lib/session-service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseSessionService,
|
|
1
|
+
import { BaseSessionService, generateSessionId, } from '@zhin.js/game-kit';
|
|
2
2
|
export class SessionService extends BaseSessionService {
|
|
3
3
|
constructor(db) {
|
|
4
4
|
super(db, {
|
|
@@ -26,13 +26,8 @@ export class SessionService extends BaseSessionService {
|
|
|
26
26
|
const row = {
|
|
27
27
|
id: generateSessionId(),
|
|
28
28
|
locale: message.locale ?? 'zh-CN',
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
channel_type: message.$channel.type,
|
|
32
|
-
channel_id: message.$channel.id,
|
|
33
|
-
channel_key: channelKey(message),
|
|
34
|
-
player_id: message.$sender.id,
|
|
35
|
-
player_name: message.$sender.name?.trim() || message.$sender.id,
|
|
29
|
+
player_id: message.sender.id,
|
|
30
|
+
player_name: message.sender.name?.trim() || message.sender.id,
|
|
36
31
|
player_wins: 0,
|
|
37
32
|
bot_wins: 0,
|
|
38
33
|
round: 0,
|
|
@@ -40,7 +35,7 @@ export class SessionService extends BaseSessionService {
|
|
|
40
35
|
updated_at: now,
|
|
41
36
|
created_at: now,
|
|
42
37
|
};
|
|
43
|
-
return this.createRow(row);
|
|
38
|
+
return this.createRow({ target: message.target, fields: row });
|
|
44
39
|
}
|
|
45
40
|
}
|
|
46
41
|
export function createServices(db) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-service.js","sourceRoot":"","sources":["../src/session-service.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,
|
|
1
|
+
{"version":3,"file":"session-service.js","sourceRoot":"","sources":["../src/session-service.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAClB,iBAAiB,GAGlB,MAAM,mBAAmB,CAAC;AAK3B,MAAM,OAAO,cAAe,SAAQ,kBAAiC;IACnE,YAAY,EAAe;QACzB,KAAK,CAAC,EAAmD,EAAE;YACzD,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,cAAc;YACrB,UAAU,EAAE,CAAC,WAAW,CAAC;YACzB,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,KAAK,KAAK;gBACpD,CAAC,CAAC,CAAC;wBACC,MAAM,EAAE,OAAO,CAAC,SAAS;wBACzB,QAAQ,EAAE,OAAO,CAAC,WAAW;wBAC7B,MAAM,EAAE,KAAK;wBACb,KAAK,EAAE,OAAO,CAAC,WAAW,GAAG,EAAE;qBAChC,CAAC;gBACJ,CAAC,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM;oBACzB,CAAC,CAAC,CAAC;4BACC,MAAM,EAAE,OAAO,CAAC,SAAS;4BACzB,QAAQ,EAAE,OAAO,CAAC,WAAW;4BAC7B,MAAM,EAAE,MAAM;yBACf,CAAC;oBACJ,CAAC,CAAC,EAAE;SACT,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,OAAmB;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,GAAG,GAAgG;YACvG,EAAE,EAAE,iBAAiB,EAAE;YACvB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,OAAO;YACjC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,EAAE;YAC5B,WAAW,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE;YAC7D,WAAW,EAAE,CAAC;YACd,QAAQ,EAAE,CAAC;YACX,KAAK,EAAE,CAAC;YACR,MAAM,EAAE,QAAQ;YAChB,UAAU,EAAE,GAAG;YACf,UAAU,EAAE,GAAG;SAChB,CAAC;QACF,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;IACjE,CAAC;CACF;AAED,MAAM,UAAU,cAAc,CAAC,EAAe;IAC5C,OAAO,IAAI,cAAc,CAAC,EAAE,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
-
import { defineGameCommandAliasMiddleware, isRpsAction,
|
|
2
|
+
import { replyGameOperation, defineGameCommandAliasMiddleware, isRpsAction, gamePlayerFromInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
3
3
|
import { getRpsHelp, runRpsCommand } from "../../lib/rps-command.js";
|
|
4
4
|
import { resolveGameServices } from "../../lib/runtime-store.js";
|
|
5
5
|
export default defineGameCommandAliasMiddleware({
|
|
6
6
|
aliases: ['猜拳', 'rps'],
|
|
7
7
|
async run(action, input, context) {
|
|
8
|
-
const
|
|
9
|
-
formatCommand: (path) => context.input.formatCommand?.(path, context.owner.id)
|
|
10
|
-
});
|
|
8
|
+
const presentation = { locale: context.input.locale,
|
|
9
|
+
formatCommand: (path) => context.input.formatCommand?.(path, context.owner.id) };
|
|
11
10
|
const normalized = normalizeRpsAction(String(action ?? ''));
|
|
12
|
-
if (normalized === 'help')
|
|
13
|
-
|
|
11
|
+
if (normalized === 'help') {
|
|
12
|
+
await context.input.$reply(getRpsHelp(presentation));
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
14
15
|
// action 无法识别:放行给后续中间件,避免劫持普通聊天
|
|
15
16
|
if (!isRpsAction(normalized))
|
|
16
|
-
return
|
|
17
|
+
return false;
|
|
18
|
+
const message = gamePlayerFromInput(input, presentation);
|
|
17
19
|
const services = resolveGameServices(context);
|
|
18
|
-
|
|
20
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
21
|
+
run: () => runRpsCommand(services, message, normalized) });
|
|
22
|
+
return true;
|
|
19
23
|
},
|
|
20
24
|
});
|
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defineGameCommandAliasMiddleware,
|
|
3
|
-
isRpsAction,
|
|
4
|
-
messageFromCommandInput,
|
|
5
|
-
normalizeRpsAction,
|
|
6
|
-
} from '@zhin.js/game-kit';
|
|
1
|
+
import { replyGameOperation, defineGameCommandAliasMiddleware, isRpsAction, gamePlayerFromInput, normalizeRpsAction } from '@zhin.js/game-kit';
|
|
7
2
|
import { getRpsHelp, runRpsCommand } from '../../src/rps-command.js';
|
|
8
3
|
import { resolveGameServices } from '../../src/runtime-store.js';
|
|
9
4
|
|
|
10
5
|
export default defineGameCommandAliasMiddleware({
|
|
11
6
|
aliases: ['猜拳', 'rps'],
|
|
12
7
|
async run(action, input, context) {
|
|
13
|
-
const
|
|
14
|
-
formatCommand: (path) => context.input.formatCommand?.(path, context.owner.id)
|
|
15
|
-
|
|
8
|
+
const presentation = { locale: context.input.locale,
|
|
9
|
+
formatCommand: (path: string) => context.input.formatCommand?.(path, context.owner.id) };
|
|
10
|
+
|
|
16
11
|
const normalized = normalizeRpsAction(String(action ?? ''));
|
|
17
|
-
if (normalized === 'help')
|
|
12
|
+
if (normalized === 'help') { await context.input.$reply(getRpsHelp(presentation)); return true; }
|
|
18
13
|
// action 无法识别:放行给后续中间件,避免劫持普通聊天
|
|
19
|
-
if (!isRpsAction(normalized)) return
|
|
14
|
+
if (!isRpsAction(normalized)) return false;
|
|
15
|
+
const message = gamePlayerFromInput(input, presentation);
|
|
20
16
|
const services = resolveGameServices(context);
|
|
21
|
-
|
|
17
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
18
|
+
run: () => runRpsCommand(services, message, normalized) });
|
|
19
|
+
return true;
|
|
22
20
|
},
|
|
23
21
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Generated by build-plugin-runtime-entries.mjs. Do not edit.
|
|
2
|
+
import { replyGameOperation, buildChoiceFallbackMap, channelKey, gamePlayerFromInput, parseChoicePayload, resolveGameTextPayload, } from '@zhin.js/game-kit';
|
|
2
3
|
import { defineMiddleware } from 'zhin.js/middleware';
|
|
3
|
-
import { buildChoiceFallbackMap, channelKey, messageFromCommandInput, parseChoicePayload, resolveGameTextPayload, } from '@zhin.js/game-kit';
|
|
4
4
|
import { resolveGameServices } from "../../lib/runtime-store.js";
|
|
5
5
|
import { handleChoice, RPS_PREFIX } from "../../lib/game-flow.js";
|
|
6
6
|
/**
|
|
@@ -10,13 +10,17 @@ import { handleChoice, RPS_PREFIX } from "../../lib/game-flow.js";
|
|
|
10
10
|
export default defineMiddleware({
|
|
11
11
|
target: 'inbound',
|
|
12
12
|
async handle(context, next) {
|
|
13
|
+
if (!context.input.sender?.id) {
|
|
14
|
+
await next();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
13
17
|
const raw = context.input.content?.trim() ?? '';
|
|
14
18
|
if (!raw) {
|
|
15
19
|
await next();
|
|
16
20
|
return;
|
|
17
21
|
}
|
|
18
22
|
const services = resolveGameServices(context);
|
|
19
|
-
const message =
|
|
23
|
+
const message = gamePlayerFromInput(context.input);
|
|
20
24
|
const ch = channelKey(message);
|
|
21
25
|
// 直接 payload(QQ 指令预填 / Sandbox action→text)
|
|
22
26
|
const payloadFromText = resolveGameTextPayload(raw);
|
|
@@ -25,16 +29,15 @@ export default defineMiddleware({
|
|
|
25
29
|
if (parsed) {
|
|
26
30
|
const session = await services.getById(parsed.sessionId);
|
|
27
31
|
if (session?.channel_key === ch) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
await context.input.$reply(reply);
|
|
32
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
33
|
+
run: () => handleChoice(services, message, parsed.sessionId, parsed.choiceId) });
|
|
31
34
|
return;
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
await next();
|
|
35
38
|
return;
|
|
36
39
|
}
|
|
37
|
-
const session = await services.getActiveForUser(ch, message
|
|
40
|
+
const session = await services.getActiveForUser(ch, message.sender.id);
|
|
38
41
|
if (!session || session.status !== 'active') {
|
|
39
42
|
await next();
|
|
40
43
|
return;
|
|
@@ -51,8 +54,7 @@ export default defineMiddleware({
|
|
|
51
54
|
await next();
|
|
52
55
|
return;
|
|
53
56
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
await context.input.$reply(reply);
|
|
57
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
58
|
+
run: () => handleChoice(services, message, session.id, parsed.choiceId) });
|
|
57
59
|
},
|
|
58
60
|
});
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { defineMiddleware } from 'zhin.js/middleware';
|
|
2
|
-
import type { Message } from '@zhin.js/core/runtime';
|
|
3
1
|
import {
|
|
2
|
+
replyGameOperation,
|
|
4
3
|
buildChoiceFallbackMap,
|
|
5
4
|
channelKey,
|
|
6
|
-
|
|
5
|
+
gamePlayerFromInput,
|
|
7
6
|
parseChoicePayload,
|
|
8
7
|
resolveGameTextPayload,
|
|
9
8
|
} from '@zhin.js/game-kit';
|
|
9
|
+
import { defineMiddleware } from 'zhin.js/middleware';
|
|
10
|
+
import type { Message } from '@zhin.js/core/runtime';
|
|
10
11
|
import { resolveGameServices } from '../../src/runtime-store.js';
|
|
11
12
|
import { handleChoice, RPS_PREFIX } from '../../src/game-flow.js';
|
|
12
13
|
|
|
@@ -17,13 +18,14 @@ import { handleChoice, RPS_PREFIX } from '../../src/game-flow.js';
|
|
|
17
18
|
export default defineMiddleware<Message>({
|
|
18
19
|
target: 'inbound',
|
|
19
20
|
async handle(context, next) {
|
|
21
|
+
if (!context.input.sender?.id) { await next(); return; }
|
|
20
22
|
const raw = context.input.content?.trim() ?? '';
|
|
21
23
|
if (!raw) {
|
|
22
24
|
await next();
|
|
23
25
|
return;
|
|
24
26
|
}
|
|
25
27
|
const services = resolveGameServices(context);
|
|
26
|
-
const message =
|
|
28
|
+
const message = gamePlayerFromInput(context.input);
|
|
27
29
|
const ch = channelKey(message);
|
|
28
30
|
|
|
29
31
|
// 直接 payload(QQ 指令预填 / Sandbox action→text)
|
|
@@ -33,8 +35,8 @@ export default defineMiddleware<Message>({
|
|
|
33
35
|
if (parsed) {
|
|
34
36
|
const session = await services.getById(parsed.sessionId);
|
|
35
37
|
if (session?.channel_key === ch) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
39
|
+
run: () => handleChoice(services, message, parsed.sessionId, parsed.choiceId) });
|
|
38
40
|
return;
|
|
39
41
|
}
|
|
40
42
|
}
|
|
@@ -42,7 +44,7 @@ export default defineMiddleware<Message>({
|
|
|
42
44
|
return;
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
const session = await services.getActiveForUser(ch, message
|
|
47
|
+
const session = await services.getActiveForUser(ch, message.sender.id);
|
|
46
48
|
if (!session || session.status !== 'active') {
|
|
47
49
|
await next();
|
|
48
50
|
return;
|
|
@@ -61,7 +63,7 @@ export default defineMiddleware<Message>({
|
|
|
61
63
|
return;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
await replyGameOperation({ session: services, player: message, reply: content => context.input.$reply(content),
|
|
67
|
+
run: () => handleChoice(services, message, session.id, parsed.choiceId) });
|
|
66
68
|
},
|
|
67
69
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/plugin-rps",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.20",
|
|
4
4
|
"description": "Zhin.js 猜拳游戏 — 石头剪刀布三局两胜 (Plugin Runtime)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@zhin.js/
|
|
27
|
-
"@zhin.js/
|
|
26
|
+
"@zhin.js/game-kit": "1.1.20",
|
|
27
|
+
"@zhin.js/core": "1.1.55"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"typescript": "^6.0.3",
|
|
31
31
|
"vitest": "^4.1.10",
|
|
32
|
-
"zhin.js": "1.1.
|
|
32
|
+
"zhin.js": "1.1.20"
|
|
33
33
|
},
|
|
34
34
|
"repository": {
|
|
35
35
|
"type": "git",
|
|
@@ -66,9 +66,9 @@
|
|
|
66
66
|
"plugins": []
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@zhin.js/command": "^1.1.
|
|
70
|
-
"@zhin.js/middleware": "^1.1.
|
|
71
|
-
"zhin.js": "^1.1.
|
|
69
|
+
"@zhin.js/command": "^1.1.13",
|
|
70
|
+
"@zhin.js/middleware": "^1.1.12",
|
|
71
|
+
"zhin.js": "^1.1.20"
|
|
72
72
|
},
|
|
73
73
|
"peerDependenciesMeta": {
|
|
74
74
|
"@zhin.js/command": {
|
package/src/game-flow.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createMessages, resolveLocale } from 'zhin.js/i18n';
|
|
2
|
-
import
|
|
2
|
+
import { channelKey, type GamePlayer, type GameReply } from '@zhin.js/game-kit';
|
|
3
3
|
import type { RpsSessionRow } from './models.js';
|
|
4
4
|
import {
|
|
5
5
|
RPS_PREFIX,
|
|
@@ -18,88 +18,94 @@ import { buildRpsView } from './view.js';
|
|
|
18
18
|
*/
|
|
19
19
|
function renderView(
|
|
20
20
|
session: RpsSessionRow,
|
|
21
|
-
message:
|
|
21
|
+
message: GamePlayer,
|
|
22
22
|
lastRound?: { player: RpsMove; bot: RpsMove; result: 0 | 1 | 2 },
|
|
23
23
|
): GameReply {
|
|
24
|
-
return buildRpsView(session, lastRound, message
|
|
24
|
+
return buildRpsView(session, lastRound, message.target.kind);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export async function startGame(
|
|
28
28
|
services: SessionService,
|
|
29
|
-
message:
|
|
29
|
+
message: GamePlayer,
|
|
30
30
|
): Promise<GameReply | undefined> {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (active
|
|
36
|
-
|
|
31
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
32
|
+
const ch = channelKey(message);
|
|
33
|
+
const active = await services.getActiveByChannel(ch);
|
|
34
|
+
const locale = resolveLocale(active ? active.locale : message.locale);
|
|
35
|
+
if (active) {
|
|
36
|
+
if (active.player_id === message.sender.id) {
|
|
37
|
+
return messages(locale, 'text1');
|
|
38
|
+
}
|
|
39
|
+
return messages(locale, 'text2', { p0: active.player_name });
|
|
37
40
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return renderView(session, message);
|
|
41
|
+
const session = await services.createSession(message);
|
|
42
|
+
return renderView(session, message);
|
|
43
|
+
} });
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
export async function handleChoice(
|
|
45
47
|
services: SessionService,
|
|
46
|
-
message:
|
|
48
|
+
message: GamePlayer,
|
|
47
49
|
sessionId: string,
|
|
48
50
|
choiceId: string,
|
|
49
51
|
): Promise<GameReply | null> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
53
|
+
const session = await services.getById(sessionId);
|
|
54
|
+
const locale = resolveLocale(session ? session.locale : message.locale);
|
|
55
|
+
if (!session) return messages(locale, 'text3');
|
|
56
|
+
if (session.status !== 'active' && choiceId !== 'restart') {
|
|
57
|
+
return messages(locale, 'text4');
|
|
58
|
+
}
|
|
59
|
+
if (session.player_id !== message.sender.id) return messages(locale, 'text5');
|
|
57
60
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
if (choiceId === 'restart') {
|
|
62
|
+
await services.updateSession(session.id, { status: 'aborted' });
|
|
63
|
+
return (await startGame(services, message)) ?? null;
|
|
64
|
+
}
|
|
65
|
+
if (choiceId === 'quit') {
|
|
66
|
+
await services.updateSession(session.id, { status: 'aborted' });
|
|
67
|
+
return renderView((await services.getById(session.id))!, message);
|
|
68
|
+
}
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
const player = choiceId as RpsMove;
|
|
71
|
+
if (!['rock', 'paper', 'scissors'].includes(player)) return messages(locale, 'text6');
|
|
69
72
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const bot = randomBotMove();
|
|
74
|
+
const result = resolveRound(player, bot);
|
|
75
|
+
let playerWins = session.player_wins;
|
|
76
|
+
let botWins = session.bot_wins;
|
|
77
|
+
if (result === 1) playerWins++;
|
|
78
|
+
if (result === 2) botWins++;
|
|
76
79
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
let status: RpsSessionRow['status'] = 'active';
|
|
81
|
+
if (playerWins >= WIN_TARGET) status = 'won';
|
|
82
|
+
else if (botWins >= WIN_TARGET) status = 'lost';
|
|
80
83
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
await services.updateSession(session.id, {
|
|
85
|
+
player_wins: playerWins,
|
|
86
|
+
bot_wins: botWins,
|
|
87
|
+
round: session.round + 1,
|
|
88
|
+
status,
|
|
89
|
+
});
|
|
87
90
|
|
|
88
|
-
|
|
89
|
-
|
|
91
|
+
const updated = (await services.getById(session.id))!;
|
|
92
|
+
return renderView(updated, message, { player, bot, result });
|
|
93
|
+
} });
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
export async function continueGame(
|
|
93
97
|
services: SessionService,
|
|
94
|
-
message:
|
|
98
|
+
message: GamePlayer,
|
|
95
99
|
): Promise<GameReply> {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
101
|
+
const session = await services.getActiveForUser(
|
|
102
|
+
channelKey(message),
|
|
103
|
+
message.sender.id,
|
|
104
|
+
);
|
|
105
|
+
const locale = resolveLocale(session ? session.locale : message.locale);
|
|
106
|
+
if (!session) return messages(locale, 'text7');
|
|
107
|
+
return renderView(session, message);
|
|
108
|
+
} });
|
|
103
109
|
}
|
|
104
110
|
|
|
105
111
|
export { RPS_PREFIX };
|
package/src/rps-command.ts
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
|
-
import { formatGameHelp, gameCommandFeedback, gameCommandHint, channelKey, type
|
|
1
|
+
import { formatGameHelp, gameCommandFeedback, gameCommandHint, channelKey, type GamePlayer, type GameReply } from '@zhin.js/game-kit';
|
|
2
2
|
import { createMessages } from 'zhin.js/i18n';
|
|
3
3
|
import { continueGame, handleChoice, startGame } from './game-flow.js';
|
|
4
4
|
import type { SessionService } from './session-service.js';
|
|
5
5
|
|
|
6
|
-
export function getRpsHelp(message: Pick<
|
|
7
|
-
return formatGameHelp(message, { 'zh-CN': "✊✋✌️ 猜拳对决(三局两胜)", en: "✊✋✌️ Rock Paper Scissors (best of three)" }, 'rps', ['start', 'continue', 'quit'],
|
|
8
|
-
{ 'zh-CN': "点击按钮或回复选项数字出拳。", en: "Choose a button or reply with its option number." });
|
|
6
|
+
export function getRpsHelp(message: Pick<GamePlayer, 'locale' | 'formatCommand'> = {}): string {
|
|
7
|
+
return formatGameHelp({ presentation: message, title: { 'zh-CN': "✊✋✌️ 猜拳对决(三局两胜)", en: "✊✋✌️ Rock Paper Scissors (best of three)" }, commandPath: 'rps', actions: ['start', 'continue', 'quit'], footer: { 'zh-CN': "点击按钮或回复选项数字出拳。", en: "Choose a button or reply with its option number." } });
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
export const RPS_HELP = getRpsHelp();
|
|
12
11
|
|
|
13
12
|
export async function runRpsCommand(
|
|
14
13
|
services: SessionService,
|
|
15
|
-
message:
|
|
14
|
+
message: GamePlayer,
|
|
16
15
|
action: string,
|
|
17
16
|
): Promise<GameReply> {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
return services.runInScope({ scope: channelKey(message), run: async () => {
|
|
18
|
+
const ch = channelKey(message);
|
|
19
|
+
const userId = message.sender.id;
|
|
20
|
+
|
|
21
|
+
if (!action || action === 'help') {
|
|
22
|
+
const active = await services.getActiveByChannel(ch);
|
|
23
|
+
const lines = [getRpsHelp(message), ''];
|
|
24
|
+
if (active) {
|
|
25
|
+
lines.push(messages(active.locale ?? undefined, 'text1', { p0: active.player_name, p1: active.player_wins, p2: active.bot_wins }));
|
|
26
|
+
} else {
|
|
27
|
+
lines.push(gameCommandFeedback(message, 'noGame'), gameCommandHint(message, 'rps'));
|
|
28
|
+
}
|
|
29
|
+
return lines.join('\n');
|
|
28
30
|
}
|
|
29
|
-
return lines.join('\n');
|
|
30
|
-
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
if (action === 'start') return (await startGame(services, message)) ?? '';
|
|
33
|
+
if (action === 'continue') return continueGame(services, message);
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
if (action === 'quit') {
|
|
36
|
+
const row = await services.getActiveForUser(ch, userId);
|
|
37
|
+
if (!row) return gameCommandFeedback(message, 'noGame');
|
|
38
|
+
return (await handleChoice(services, message, row.id, 'quit')) ?? '';
|
|
39
|
+
}
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
return gameCommandFeedback(message, 'unknown', action) + '\n\n' + getRpsHelp(message);
|
|
42
|
+
} });
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
const messages = createMessages({
|
package/src/session-service.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { Database, Models } from '@zhin.js/core';
|
|
2
2
|
import {
|
|
3
3
|
BaseSessionService,
|
|
4
|
-
channelKey,
|
|
5
4
|
generateSessionId,
|
|
6
|
-
type
|
|
5
|
+
type GamePlayer,
|
|
7
6
|
type GameSessionDatabase,
|
|
8
7
|
} from '@zhin.js/game-kit';
|
|
9
8
|
import type { RpsSessionRow } from './models.js';
|
|
@@ -33,18 +32,13 @@ export class SessionService extends BaseSessionService<RpsSessionRow> {
|
|
|
33
32
|
});
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
async createSession(message:
|
|
35
|
+
async createSession(message: GamePlayer): Promise<RpsSessionRow> {
|
|
37
36
|
const now = Date.now();
|
|
38
|
-
const row: RpsSessionRow = {
|
|
37
|
+
const row: Omit<RpsSessionRow, 'channel_key' | 'adapter' | 'endpoint' | 'channel_type' | 'channel_id'> = {
|
|
39
38
|
id: generateSessionId(),
|
|
40
39
|
locale: message.locale ?? 'zh-CN',
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
channel_type: message.$channel.type,
|
|
44
|
-
channel_id: message.$channel.id,
|
|
45
|
-
channel_key: channelKey(message),
|
|
46
|
-
player_id: message.$sender.id,
|
|
47
|
-
player_name: message.$sender.name?.trim() || message.$sender.id,
|
|
40
|
+
player_id: message.sender.id,
|
|
41
|
+
player_name: message.sender.name?.trim() || message.sender.id,
|
|
48
42
|
player_wins: 0,
|
|
49
43
|
bot_wins: 0,
|
|
50
44
|
round: 0,
|
|
@@ -52,7 +46,7 @@ export class SessionService extends BaseSessionService<RpsSessionRow> {
|
|
|
52
46
|
updated_at: now,
|
|
53
47
|
created_at: now,
|
|
54
48
|
};
|
|
55
|
-
return this.createRow(row);
|
|
49
|
+
return this.createRow({ target: message.target, fields: row });
|
|
56
50
|
}
|
|
57
51
|
}
|
|
58
52
|
|