ai/src/modules/guessing-game/index.ts

139 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-01-14 15:14:22 +00:00
import autobind from 'autobind-decorator';
2018-08-26 21:16:56 +00:00
import * as loki from 'lokijs';
2019-01-14 15:14:22 +00:00
import Module from '../../module';
2019-01-15 09:47:22 +00:00
import Message from '../../message';
import serifs from '../../serifs';
2019-01-23 11:10:58 +00:00
export default class extends Module {
2018-08-30 23:20:49 +00:00
public readonly name = 'guessingGame';
2019-01-15 03:03:31 +00:00
2018-08-26 21:16:56 +00:00
private guesses: loki.Collection<{
userId: string;
secret: number;
tries: number[];
isEnded: boolean;
startedAt: number;
2020-09-02 12:54:01 +00:00
endedAt: number | null;
2018-08-26 21:16:56 +00:00
}>;
2019-01-14 15:14:22 +00:00
@autobind
public install() {
2019-01-24 00:34:03 +00:00
this.guesses = this.ai.getCollection('guessingGame', {
2018-08-29 07:26:33 +00:00
indices: ['userId']
});
2019-01-14 15:14:22 +00:00
return {
2019-01-15 03:01:58 +00:00
mentionHook: this.mentionHook,
contextHook: this.contextHook
2019-01-14 15:14:22 +00:00
};
}
2019-01-14 15:14:22 +00:00
@autobind
2019-01-23 12:49:10 +00:00
private async mentionHook(msg: Message) {
2019-01-15 03:03:31 +00:00
if (!msg.includes(['数当て', '数あて'])) return false;
2019-01-15 03:03:31 +00:00
const exist = this.guesses.findOne({
userId: msg.userId,
isEnded: false
});
2019-01-15 09:47:22 +00:00
if (!msg.isDm) {
2019-01-15 03:03:31 +00:00
if (exist != null) {
2019-01-19 15:27:41 +00:00
msg.reply(serifs.guessingGame.alreadyStarted);
2019-01-15 03:03:31 +00:00
} else {
msg.reply(serifs.guessingGame.plzDm);
}
2019-01-15 03:03:31 +00:00
return true;
}
const secret = Math.floor(Math.random() * 100);
2019-01-15 03:03:31 +00:00
this.guesses.insertOne({
userId: msg.userId,
secret: secret,
tries: [],
isEnded: false,
startedAt: Date.now(),
endedAt: null
});
2019-01-15 03:03:31 +00:00
msg.reply(serifs.guessingGame.started).then(reply => {
2019-01-15 09:47:22 +00:00
this.subscribeReply(msg.userId, msg.isDm, msg.isDm ? msg.userId : reply.id);
2019-01-15 03:03:31 +00:00
});
2019-01-15 03:03:31 +00:00
return true;
}
2019-01-14 15:14:22 +00:00
@autobind
2019-01-23 12:49:10 +00:00
private async contextHook(msg: Message) {
if (msg.text == null) return;
2018-08-26 21:16:56 +00:00
const exist = this.guesses.findOne({
userId: msg.userId,
isEnded: false
});
2020-09-02 12:54:01 +00:00
// 処理の流れ上、実際にnullになることは無さそうだけど一応
if (exist == null) {
this.unsubscribeReply(msg.userId);
return;
}
if (msg.text.includes('やめ')) {
2018-08-28 00:12:59 +00:00
msg.reply(serifs.guessingGame.cancel);
exist.isEnded = true;
exist.endedAt = Date.now();
2018-08-26 21:16:56 +00:00
this.guesses.update(exist);
2019-01-14 15:14:22 +00:00
this.unsubscribeReply(msg.userId);
return;
}
2019-01-23 16:21:40 +00:00
const guess = msg.extractedText.match(/[0-9]+/);
if (guess == null) {
2018-08-28 00:12:59 +00:00
msg.reply(serifs.guessingGame.nan).then(reply => {
2019-01-15 09:47:22 +00:00
this.subscribeReply(msg.userId, msg.isDm, reply.id);
});
2019-01-15 03:03:31 +00:00
return;
}
2018-08-12 14:27:29 +00:00
2019-01-15 03:03:31 +00:00
if (guess.length > 3) return;
2019-01-15 03:03:31 +00:00
const g = parseInt(guess[0], 10);
const firsttime = exist.tries.indexOf(g) === -1;
2019-01-15 03:03:31 +00:00
exist.tries.push(g);
2018-08-12 17:17:26 +00:00
2019-01-15 03:03:31 +00:00
let text: string;
let end = false;
2019-01-15 03:03:31 +00:00
if (exist.secret < g) {
text = firsttime
? serifs.guessingGame.less(g.toString())
: serifs.guessingGame.lessAgain(g.toString());
} else if (exist.secret > g) {
text = firsttime
? serifs.guessingGame.grater(g.toString())
: serifs.guessingGame.graterAgain(g.toString());
} else {
end = true;
text = serifs.guessingGame.congrats(exist.tries.length.toString());
}
2019-01-15 03:03:31 +00:00
if (end) {
exist.isEnded = true;
exist.endedAt = Date.now();
this.unsubscribeReply(msg.userId);
}
2019-01-15 03:03:31 +00:00
this.guesses.update(exist);
2018-08-12 17:17:26 +00:00
2019-01-15 03:03:31 +00:00
msg.reply(text).then(reply => {
if (!end) {
2019-01-15 09:47:22 +00:00
this.subscribeReply(msg.userId, msg.isDm, reply.id);
2019-01-15 03:03:31 +00:00
}
});
}
}