mirror of
https://github.com/syuilo/ai.git
synced 2024-11-12 17:08:00 +00:00
Improve readability
This commit is contained in:
parent
87b6ba55c5
commit
c95c2c76ef
|
@ -7,6 +7,7 @@ import getCollection from '../../utils/get-collection';
|
|||
|
||||
export default class GuessingGameModule extends Module {
|
||||
public readonly name = 'guessingGame';
|
||||
|
||||
private guesses: loki.Collection<{
|
||||
userId: string;
|
||||
secret: number;
|
||||
|
@ -32,41 +33,39 @@ export default class GuessingGameModule extends Module {
|
|||
|
||||
@autobind
|
||||
private mentionHook(msg: MessageLike) {
|
||||
if (msg.includes(['数当て', '数あて'])) {
|
||||
const exist = this.guesses.findOne({
|
||||
userId: msg.userId,
|
||||
isEnded: false
|
||||
});
|
||||
if (!msg.includes(['数当て', '数あて'])) return false;
|
||||
|
||||
if (!msg.isMessage) {
|
||||
if (exist != null) {
|
||||
msg.reply(serifs.guessingGame.arleadyStarted);
|
||||
} else {
|
||||
msg.reply(serifs.guessingGame.plzDm);
|
||||
}
|
||||
const exist = this.guesses.findOne({
|
||||
userId: msg.userId,
|
||||
isEnded: false
|
||||
});
|
||||
|
||||
return true;
|
||||
if (!msg.isMessage) {
|
||||
if (exist != null) {
|
||||
msg.reply(serifs.guessingGame.arleadyStarted);
|
||||
} else {
|
||||
msg.reply(serifs.guessingGame.plzDm);
|
||||
}
|
||||
|
||||
const secret = Math.floor(Math.random() * 100);
|
||||
|
||||
this.guesses.insertOne({
|
||||
userId: msg.userId,
|
||||
secret: secret,
|
||||
tries: [],
|
||||
isEnded: false,
|
||||
startedAt: Date.now(),
|
||||
endedAt: null
|
||||
});
|
||||
|
||||
msg.reply(serifs.guessingGame.started).then(reply => {
|
||||
this.subscribeReply(msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id);
|
||||
});
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
const secret = Math.floor(Math.random() * 100);
|
||||
|
||||
this.guesses.insertOne({
|
||||
userId: msg.userId,
|
||||
secret: secret,
|
||||
tries: [],
|
||||
isEnded: false,
|
||||
startedAt: Date.now(),
|
||||
endedAt: null
|
||||
});
|
||||
|
||||
msg.reply(serifs.guessingGame.started).then(reply => {
|
||||
this.subscribeReply(msg.userId, msg.isMessage, msg.isMessage ? msg.userId : reply.id);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@autobind
|
||||
|
@ -93,44 +92,44 @@ export default class GuessingGameModule extends Module {
|
|||
msg.reply(serifs.guessingGame.nan).then(reply => {
|
||||
this.subscribeReply(msg.userId, msg.isMessage, reply.id);
|
||||
});
|
||||
} else {
|
||||
if (guess.length > 3) return;
|
||||
|
||||
const g = parseInt(guess[0], 10);
|
||||
|
||||
const firsttime = exist.tries.indexOf(g) === -1;
|
||||
|
||||
exist.tries.push(g);
|
||||
|
||||
let text: string;
|
||||
let end = false;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
if (end) {
|
||||
exist.isEnded = true;
|
||||
exist.endedAt = Date.now();
|
||||
this.unsubscribeReply(msg.userId);
|
||||
}
|
||||
|
||||
this.guesses.update(exist);
|
||||
|
||||
msg.reply(text).then(reply => {
|
||||
if (!end) {
|
||||
this.subscribeReply(msg.userId, msg.isMessage, reply.id);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (guess.length > 3) return;
|
||||
|
||||
const g = parseInt(guess[0], 10);
|
||||
const firsttime = exist.tries.indexOf(g) === -1;
|
||||
|
||||
exist.tries.push(g);
|
||||
|
||||
let text: string;
|
||||
let end = false;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
if (end) {
|
||||
exist.isEnded = true;
|
||||
exist.endedAt = Date.now();
|
||||
this.unsubscribeReply(msg.userId);
|
||||
}
|
||||
|
||||
this.guesses.update(exist);
|
||||
|
||||
msg.reply(text).then(reply => {
|
||||
if (!end) {
|
||||
this.subscribeReply(msg.userId, msg.isMessage, reply.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue