From c95c2c76eff04ffaaabaf8e7ed42a8e372f196fe Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 15 Jan 2019 12:03:31 +0900 Subject: [PATCH] Improve readability --- src/modules/guessing-game/index.ts | 133 ++++++++++++++--------------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/src/modules/guessing-game/index.ts b/src/modules/guessing-game/index.ts index 006c77f..4fb68d6 100644 --- a/src/modules/guessing-game/index.ts +++ b/src/modules/guessing-game/index.ts @@ -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); + } + }); } }