From 8c1912481711299ab3ec8cd723d4f0fdff3a32cb Mon Sep 17 00:00:00 2001 From: Charlie Kobayashi Date: Sun, 19 Feb 2017 22:35:12 -0500 Subject: [PATCH] Fix display style / mechanics Display changes: - done to Zarel's request/suggestions! - hides the UNO button automatically the turn after Drawing: - refactored regular drawing of a card to be an "event" of its own (onDraw) - prevents users from drawing a card after playing a colour card (drawing can only happen in during the "play" state now) - only drawing on your own turn will show you your hand display (you can still view your hand any time using ``/uno`` or ``/uno hand``) UNO handling: - handles uno parsing if the next player draws, refactored into it's own function as well (since it occurs at both the draw and the playing part) - for 2p games, if the user does not say uno before playing their last card (Skip => [last card]), they can draw/play without having to say UNO and be penalized 2 cards (in official rules, iirc you have to beat the next player's actions, and not your own) --- chat-plugins/uno.js | 61 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/chat-plugins/uno.js b/chat-plugins/uno.js index 6809e9b999..07a435d3bf 100644 --- a/chat-plugins/uno.js +++ b/chat-plugins/uno.js @@ -144,7 +144,7 @@ class UNOgame extends Rooms.RoomGame { if (this.awaitUno) { this.unoId = Math.floor(Math.random() * 100).toString(); - this.players[this.awaitUno].sendRoom(`|raw|
`); + this.players[this.awaitUno].sendRoom(`|uhtml|uno-hand|
`); } clearTimeout(this.timer); @@ -180,6 +180,20 @@ class UNOgame extends Rooms.RoomGame { return player; } + onDraw(user) { + if (this.currentPlayer !== user.userid || this.state !== "play") return false; + if (this.players[user.userid].cardLock) return true; + + this.onCheckUno(); + + this.sendToRoom(`${user.name} has drawn a card.`); + let player = this.players[user.userid]; + + let card = this.onDrawCard(user, 1, true); + player.sendDisplay(); + player.cardLock = card[0].name; + } + onPlay(user, cardName) { if (this.currentPlayer !== user.userid || this.state !== "play") return false; let player = this.players[user.userid]; @@ -194,13 +208,7 @@ class UNOgame extends Rooms.RoomGame { clearTimeout(this.timer); // reset the autodq timer. - if (this.awaitUno) { - // if the previous player hasn't hit UNO before the next player plays something, they are forced to draw 2 cards. - this.sendToRoom(`${this.players[this.awaitUno].name} forgot to say UNO! and is forced to draw 2 cards.`); - this.onDrawCard({userid: this.awaitUno}, 2); - delete this.awaitUno; - delete this.unoId; - } + this.onCheckUno(); // update the game information. this.topCard = card; @@ -278,15 +286,14 @@ class UNOgame extends Rooms.RoomGame { this.nextTurn(); } - onDrawCard(user, count, selfDraw) { + onDrawCard(user, count) { if (!(user.userid in this.players)) return false; let drawnCards = this.drawCard(count); let player = this.players[user.userid]; player.hand.push(...drawnCards); - player.sendDisplay(); player.sendRoom(`|raw|You have drawn the following card${drawnCards.length > 1 ? "s" : ""}: ${drawnCards.map(card => `${card.name}`).join(", ")}.`); - if (selfDraw) player.cardLock = drawnCards[0].name; + return drawnCards; } drawCard(count) { @@ -311,6 +318,18 @@ class UNOgame extends Rooms.RoomGame { delete this.unoId; } + onCheckUno() { + if (this.awaitUno) { + // if the previous player hasn't hit UNO before the next player plays something, they are forced to draw 2 cards; + if (this.awaitUno !== this.currentPlayer) { + this.sendToRoom(`${this.players[this.awaitUno].name} forgot to say UNO! and is forced to draw 2 cards.`); + this.onDrawCard({userid: this.awaitUno}, 2); + } + delete this.awaitUno; + delete this.unoId; + } + } + onSendHand(user) { if (!(user.userid in this.players) || this.state === "signups") return false; @@ -368,18 +387,17 @@ class UNOgamePlayer extends Rooms.RoomGamePlayer { sendDisplay() { let hand = this.buildHand().join(""); let players = `

Players (${this.game.playerCount}):

` + this.game.getPlayers(true).join("
"); - let draw = ""; - let pass = ""; + let draw = ""; + let pass = ""; - let top = `Top Card: ${this.game.topCard.name}`; + let top = `Top Card: ${this.game.topCard.name}`; // clear previous display and show new display this.sendRoom("|uhtmlchange|uno-hand|"); this.sendRoom( - `|uhtml|uno-hand|${this.game.currentPlayer === this.userid ? `` : ""}` + - `` + - `` + - `${this.game.currentPlayer === this.userid ? `` : ""}
${top}
${hand}
${players}
${draw}${pass}
` + `|uhtml|uno-hand|${this.game.currentPlayer === this.userid ? `` : ""}` + + `` + + `${this.game.currentPlayer === this.userid ? `` : ""}
${hand}
${top}
${players}
${draw}${pass}
` ); } } @@ -489,11 +507,8 @@ exports.commands = { draw: function (target, room, user) { if (!room.game || room.game.gameid !== "uno") return false; - if (room.game.currentPlayer !== user.userid) return false; - if (room.game.players[user.userid].cardLock) return this.errorReply("You have already drawn a card this turn."); - room.add(`${user.name} has drawn a card.`).update(); - - room.game.onDrawCard(user, 1, true); + let error = room.game.onDraw(user); + if (error) return this.errorReply("You have already drawn a card this turn."); }, pass: function (target, room, user) {