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';
|
2018-08-12 14:03:00 +00:00
|
|
|
import MessageLike from '../../message-like';
|
|
|
|
import serifs from '../../serifs';
|
2018-08-29 07:26:33 +00:00
|
|
|
import getCollection from '../../utils/get-collection';
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-14 15:14:22 +00:00
|
|
|
export default class GuessingGameModule 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;
|
|
|
|
endedAt: number;
|
|
|
|
}>;
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-14 15:14:22 +00:00
|
|
|
@autobind
|
|
|
|
public install() {
|
2018-08-26 21:16:56 +00:00
|
|
|
//#region Init DB
|
2018-08-29 07:26:33 +00:00
|
|
|
this.guesses = getCollection(this.ai.db, 'guessingGame', {
|
|
|
|
indices: ['userId']
|
|
|
|
});
|
2018-08-26 21:16:56 +00:00
|
|
|
//#endregion
|
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
|
|
|
};
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:14:22 +00:00
|
|
|
@autobind
|
2019-01-15 03:01:58 +00:00
|
|
|
private mentionHook(msg: MessageLike) {
|
2019-01-15 03:03:31 +00:00
|
|
|
if (!msg.includes(['数当て', '数あて'])) return false;
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
const exist = this.guesses.findOne({
|
|
|
|
userId: msg.userId,
|
|
|
|
isEnded: false
|
|
|
|
});
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
if (!msg.isMessage) {
|
|
|
|
if (exist != null) {
|
|
|
|
msg.reply(serifs.guessingGame.arleadyStarted);
|
|
|
|
} else {
|
|
|
|
msg.reply(serifs.guessingGame.plzDm);
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
2018-08-12 14:25:47 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const secret = Math.floor(Math.random() * 100);
|
2018-08-12 14:25:47 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
this.guesses.insertOne({
|
|
|
|
userId: msg.userId,
|
|
|
|
secret: secret,
|
|
|
|
tries: [],
|
|
|
|
isEnded: false,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
endedAt: null
|
|
|
|
});
|
2018-08-12 14:25:47 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
msg.reply(serifs.guessingGame.started).then(reply => {
|
|
|
|
this.subscribeReply(msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id);
|
|
|
|
});
|
2018-08-12 14:25:47 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
return true;
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:14:22 +00:00
|
|
|
@autobind
|
2019-01-15 03:01:58 +00:00
|
|
|
private contextHook(msg: MessageLike) {
|
2018-08-12 14:03:00 +00:00
|
|
|
if (msg.text == null) return;
|
|
|
|
|
2018-08-26 21:16:56 +00:00
|
|
|
const exist = this.guesses.findOne({
|
2018-08-12 14:03:00 +00:00
|
|
|
userId: msg.userId,
|
|
|
|
isEnded: false
|
|
|
|
});
|
|
|
|
|
|
|
|
if (msg.text.includes('やめ')) {
|
2018-08-28 00:12:59 +00:00
|
|
|
msg.reply(serifs.guessingGame.cancel);
|
2018-08-12 14:03:00 +00:00
|
|
|
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);
|
2018-08-12 14:03:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const guess = msg.text.toLowerCase().replace(this.ai.account.username.toLowerCase(), '').match(/[0-9]+/);
|
|
|
|
|
|
|
|
if (guess == null) {
|
2018-08-28 00:12:59 +00:00
|
|
|
msg.reply(serifs.guessingGame.nan).then(reply => {
|
2019-01-14 15:14:22 +00:00
|
|
|
this.subscribeReply(msg.userId, msg.isMessage, reply.id);
|
2018-08-12 14:03:00 +00:00
|
|
|
});
|
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;
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
const g = parseInt(guess[0], 10);
|
|
|
|
const firsttime = exist.tries.indexOf(g) === -1;
|
2018-08-12 14:03:00 +00:00
|
|
|
|
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;
|
2018-08-12 14:03:00 +00:00
|
|
|
|
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());
|
|
|
|
}
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2019-01-15 03:03:31 +00:00
|
|
|
if (end) {
|
|
|
|
exist.isEnded = true;
|
|
|
|
exist.endedAt = Date.now();
|
|
|
|
this.unsubscribeReply(msg.userId);
|
|
|
|
}
|
2018-08-12 14:03:00 +00:00
|
|
|
|
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) {
|
|
|
|
this.subscribeReply(msg.userId, msg.isMessage, reply.id);
|
|
|
|
}
|
|
|
|
});
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
}
|