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

132 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-08-26 21:16:56 +00:00
import * as loki from 'lokijs';
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';
export default class GuessingGameModule implements IModule {
public name = 'guessingGame';
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;
}>;
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
}
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({
userId: msg.userId,
isEnded: false
});
if (!msg.isMessage) {
if (exist != null) {
2018-08-28 00:12:59 +00:00
msg.reply(serifs.guessingGame.arleadyStarted);
} else {
2018-08-28 00:12:59 +00:00
msg.reply(serifs.guessingGame.plzDm);
}
return true;
}
const secret = Math.floor(Math.random() * 100);
2018-08-26 21:16:56 +00:00
this.guesses.insertOne({
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 => {
this.ai.subscribeReply(this, msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id);
});
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({
userId: msg.userId,
isEnded: false
});
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);
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 => {
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);
const firsttime = exist.tries.indexOf(g) === -1;
2018-08-12 17:17:26 +00:00
exist.tries.push(g);
let text: string;
let end = false;
if (exist.secret < g) {
text = firsttime
2018-08-28 00:12:59 +00:00
? serifs.guessingGame.less.replace('$', g.toString())
: serifs.guessingGame.lessAgain.replace('$', g.toString());
} else if (exist.secret > g) {
text = firsttime
2018-08-28 00:12:59 +00:00
? serifs.guessingGame.grater.replace('$', g.toString())
: serifs.guessingGame.graterAgain.replace('$', g.toString());
} else {
end = true;
2018-08-28 00:12:59 +00:00
text = serifs.guessingGame.congrats.replace('{tries}', exist.tries.length.toString());
}
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
msg.reply(text).then(reply => {
if (!end) {
this.ai.subscribeReply(this, msg.userId, msg.isMessage, reply.id);
}
});
}
}
}