2018-08-26 21:16:56 +00:00
|
|
|
import * as loki from 'lokijs';
|
2018-08-12 14:03:00 +00:00
|
|
|
import 藍 from '../../ai';
|
|
|
|
import IModule from '../../module';
|
|
|
|
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
|
|
|
|
|
|
|
export default class GuessingGameModule implements IModule {
|
2018-08-30 23:20:49 +00:00
|
|
|
public readonly name = 'guessingGame';
|
2018-08-12 14:03:00 +00:00
|
|
|
private ai: 藍;
|
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
|
|
|
|
|
|
|
public install = (ai: 藍) => {
|
|
|
|
this.ai = ai;
|
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
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public onMention = (msg: MessageLike) => {
|
2018-08-23 08:27:39 +00:00
|
|
|
if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) {
|
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
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:25:47 +00:00
|
|
|
if (!msg.isMessage) {
|
|
|
|
if (exist != null) {
|
2018-08-28 00:12:59 +00:00
|
|
|
msg.reply(serifs.guessingGame.arleadyStarted);
|
2018-08-12 14:25:47 +00:00
|
|
|
} else {
|
2018-08-28 00:12:59 +00:00
|
|
|
msg.reply(serifs.guessingGame.plzDm);
|
2018-08-12 14:25:47 +00:00
|
|
|
}
|
2018-08-12 14:03:00 +00:00
|
|
|
|
2018-08-12 14:25:47 +00:00
|
|
|
return true;
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
2018-08-12 14:25:47 +00:00
|
|
|
|
|
|
|
const secret = Math.floor(Math.random() * 100);
|
|
|
|
|
2018-08-26 21:16:56 +00:00
|
|
|
this.guesses.insertOne({
|
2018-08-12 14:25:47 +00:00
|
|
|
userId: msg.userId,
|
|
|
|
secret: secret,
|
|
|
|
tries: [],
|
|
|
|
isEnded: false,
|
|
|
|
startedAt: Date.now(),
|
|
|
|
endedAt: null
|
|
|
|
});
|
|
|
|
|
2018-08-28 00:12:59 +00:00
|
|
|
msg.reply(serifs.guessingGame.started).then(reply => {
|
2018-08-12 14:25:47 +00:00
|
|
|
this.ai.subscribeReply(this, msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id);
|
|
|
|
});
|
|
|
|
|
2018-08-12 14:03:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public onReplyThisModule = (msg: MessageLike) => {
|
|
|
|
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);
|
2018-08-12 14:03:00 +00:00
|
|
|
this.ai.unsubscribeReply(this, msg.userId);
|
|
|
|
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 => {
|
2018-08-12 14:03:00 +00:00
|
|
|
this.ai.subscribeReply(this, msg.userId, msg.isMessage, reply.id);
|
|
|
|
});
|
|
|
|
} else {
|
2018-08-12 14:27:29 +00:00
|
|
|
if (guess.length > 3) return;
|
|
|
|
|
2018-08-29 07:19:09 +00:00
|
|
|
const g = parseInt(guess[0], 10);
|
2018-08-12 14:03:00 +00:00
|
|
|
|
|
|
|
const firsttime = exist.tries.indexOf(g) === -1;
|
|
|
|
|
2018-08-12 17:17:26 +00:00
|
|
|
exist.tries.push(g);
|
|
|
|
|
2018-08-12 14:03:00 +00:00
|
|
|
let text: string;
|
|
|
|
let end = false;
|
|
|
|
|
|
|
|
if (exist.secret < g) {
|
|
|
|
text = firsttime
|
2018-08-29 07:47:07 +00:00
|
|
|
? serifs.guessingGame.less(g.toString())
|
|
|
|
: serifs.guessingGame.lessAgain(g.toString());
|
2018-08-12 14:03:00 +00:00
|
|
|
} else if (exist.secret > g) {
|
|
|
|
text = firsttime
|
2018-08-29 07:47:07 +00:00
|
|
|
? serifs.guessingGame.grater(g.toString())
|
|
|
|
: serifs.guessingGame.graterAgain(g.toString());
|
2018-08-12 14:03:00 +00:00
|
|
|
} else {
|
|
|
|
end = true;
|
2018-08-29 07:47:07 +00:00
|
|
|
text = serifs.guessingGame.congrats(exist.tries.length.toString());
|
2018-08-12 14:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end) {
|
|
|
|
exist.isEnded = true;
|
|
|
|
exist.endedAt = Date.now();
|
|
|
|
this.ai.unsubscribeReply(this, msg.userId);
|
|
|
|
}
|
|
|
|
|
2018-08-26 21:16:56 +00:00
|
|
|
this.guesses.update(exist);
|
2018-08-12 17:17:26 +00:00
|
|
|
|
2018-08-12 14:03:00 +00:00
|
|
|
msg.reply(text).then(reply => {
|
|
|
|
if (!end) {
|
|
|
|
this.ai.subscribeReply(this, msg.userId, msg.isMessage, reply.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|