From f398db4e42e6aaafd94fede1f011be4da7b12bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Sat, 14 Mar 2020 14:04:07 +0100 Subject: [PATCH] Splitting more, added chat, and fancier draw-animations --- api/chat.js | 25 +++ api/virtualLottery.js | 45 ++-- server.js | 4 + src/components/VirtualLotteryPage.vue | 211 +++++++----------- .../VirtualLotteryRegistrationPage.vue | 54 ++++- src/styles/global.scss | 40 +++- src/ui/Attendees.vue | 64 ++++++ src/ui/Chat.vue | 180 +++++++++++++++ src/ui/TotalBought.vue | 35 +-- src/ui/WinnerDraw.vue | 178 +++++++++++++++ src/ui/Winners.vue | 48 ++++ 11 files changed, 688 insertions(+), 196 deletions(-) create mode 100644 api/chat.js create mode 100644 src/ui/Attendees.vue create mode 100644 src/ui/Chat.vue create mode 100644 src/ui/WinnerDraw.vue create mode 100644 src/ui/Winners.vue diff --git a/api/chat.js b/api/chat.js new file mode 100644 index 0000000..e319d4a --- /dev/null +++ b/api/chat.js @@ -0,0 +1,25 @@ +module.exports = io => { + io.on("connection", socket => { + let username = null; + + socket.on("username", msg => { + if (msg.username == null) { + username = null; + socket.emit("accept_username", false); + return; + } + if (msg.username.length > 3 && msg.username.length < 30) { + username = msg.username; + socket.emit("accept_username", true); + return; + } + socket.emit("accept_username", false); + }); + + socket.on("chat", msg => { + msg.username = username; + msg.timestamp = new Date().getTime(); + io.emit("chat", msg); + }); + }); +}; diff --git a/api/virtualLottery.js b/api/virtualLottery.js index d5aa5fb..0bd3ba0 100644 --- a/api/virtualLottery.js +++ b/api/virtualLottery.js @@ -52,28 +52,33 @@ router.route("/winners/secure").get(mustBeAuthenticated, async (req, res) => { }); router.route("/winner").get(mustBeAuthenticated, async (req, res) => { - let colorWinner = Math.floor(Math.random() * 4); - let colorToChoseFrom; + let allContestants = await Attendee.find(); + if (allContestants.length == 0) { + res.json(false); + return; + } + let ballotColors = []; + for (let i = 0; i < allContestants.length; i++) { + let currentContestant = allContestants[i]; + for (let blue = 0; blue < currentContestant.blue; blue++) { + ballotColors.push("blue"); + } + for (let red = 0; red < currentContestant.red; red++) { + ballotColors.push("red"); + } + for (let green = 0; green < currentContestant.green; green++) { + ballotColors.push("green"); + } + for (let yellow = 0; yellow < currentContestant.yellow; yellow++) { + ballotColors.push("yellow"); + } + } + + let colorToChoseFrom = + ballotColors[Math.floor(Math.random() * ballotColors.length)]; let findObject = {}; - switch (colorWinner) { - case 0: - colorToChoseFrom = "red"; - break; - case 1: - colorToChoseFrom = "blue"; - break; - case 2: - colorToChoseFrom = "green"; - break; - case 3: - colorToChoseFrom = "yellow"; - break; - } - io.emit("color_winner", { color: colorToChoseFrom }); - findObject[colorToChoseFrom] = { $gt: 0 }; - let contestantsToChoseFrom = await Attendee.find(findObject); let attendeeListDemocratic = []; @@ -93,7 +98,7 @@ router.route("/winner").get(mustBeAuthenticated, async (req, res) => { Math.floor(Math.random() * attendeeListDemocratic.length) ]; - io.emit("winner", { name: winner.name }); + io.emit("winner", { color: colorToChoseFrom, name: winner.name }); let newWinnerElement = new VirtualWinner({ name: winner.name, diff --git a/server.js b/server.js index 3a9dc92..9aa428f 100644 --- a/server.js +++ b/server.js @@ -12,6 +12,10 @@ const subscriptionApi = require(path.join(__dirname + "/api/subscriptions")); const loginApi = require(path.join(__dirname + "/api/login")); const wineinfoApi = require(path.join(__dirname + "/api/wineinfo")); const virtualApi = require(path.join(__dirname + "/api/virtualLottery")); + +//This is required for the chat to work +const chat = require(path.join(__dirname + "/api/chat"))(io); + const bodyParser = require("body-parser"); const mongoose = require("mongoose"); diff --git a/src/components/VirtualLotteryPage.vue b/src/components/VirtualLotteryPage.vue index 0ee3798..17014d4 100644 --- a/src/components/VirtualLotteryPage.vue +++ b/src/components/VirtualLotteryPage.vue @@ -11,93 +11,86 @@ > Her var det lite.. Sikker på at det er en virtuell trekning nå? -
-

NY VINNER:

-
- {{ currentWinnerName }} - {{ secondsNameLeft }} -
-
-
-
-
-
-
-

Trekker ny om: {{ secondsLeft }}

-
-
+ -

Vinnere

-
-
-
- {{ winner.name }} -
-
-
-
-

Deltakere

-
- {{ attendee.name }} -
{{ attendee.red }}
-
{{ attendee.blue }}
-
- {{ attendee.green }} -
-
- {{ attendee.yellow }} -
-
+ +
+
+ +
+ + diff --git a/src/ui/Chat.vue b/src/ui/Chat.vue new file mode 100644 index 0000000..26ba956 --- /dev/null +++ b/src/ui/Chat.vue @@ -0,0 +1,180 @@ + + + + + diff --git a/src/ui/TotalBought.vue b/src/ui/TotalBought.vue index 4d43f6b..217cd84 100644 --- a/src/ui/TotalBought.vue +++ b/src/ui/TotalBought.vue @@ -4,14 +4,17 @@
- {{ - color.total - }} + {{ color.total }} kjøpte
@@ -35,7 +38,6 @@
diff --git a/src/ui/Winners.vue b/src/ui/Winners.vue new file mode 100644 index 0000000..f13b49e --- /dev/null +++ b/src/ui/Winners.vue @@ -0,0 +1,48 @@ + + + + +