From dc52863b1f90f8034625b97b4887643b39ce1c55 Mon Sep 17 00:00:00 2001 From: nalbam Date: Fri, 30 Aug 2019 23:30:06 +0900 Subject: [PATCH] leaderboard --- server.js | 22 ++++++++++++++++++---- static/league.js | 28 ++++++++++++++-------------- static/timer.js | 1 + views/league.ejs | 2 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/server.js b/server.js index 1edb9e1..cecbac5 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,5 @@ -const express = require('express'), +const request = require('request'), + express = require('express'), gpio = require('rpi-gpio'); const app = express(); @@ -9,6 +10,8 @@ const sockets = {}; const port = process.env.PORT || '3000'; +const apiurl = process.env.API_URL || 'https://dev-api-league.nalbam.com/league'; + // express app.set('view engine', 'ejs'); app.use(express.static('static')); @@ -30,9 +33,15 @@ app.get('/league/:league', function (req, res) { app.get('/leaderboard/:league', function (req, res) { const league = req.params.league; - res.render('leaderboard.ejs', { - league: league - }); + const options = { + uri: apiurl, + qs: { + league: league + } + }; + request(options, function (err, response, body) { + return res.status(200).json(JSON.parse(body)); + }) }); app.get('/timer/:name', function (req, res) { @@ -63,6 +72,11 @@ io.on('connection', function (socket) { console.log('timer : ', socket.id, name); io.sockets.emit('timer', `${name}`); }); + + socket.on('league', function (name) { + console.log('league : ', socket.id, name); + io.sockets.emit('league', `${name}`); + }); }); // http diff --git a/static/league.js b/static/league.js index 83ad123..5282a89 100644 --- a/static/league.js +++ b/static/league.js @@ -1,5 +1,5 @@ /** - * leaderboard.js + * league.js */ let lb_title = document.querySelector('.lb-title'); @@ -14,20 +14,22 @@ function clear() { } function reload() { - let url = '/leaderboard.json?league=' + league; + let url = '/leaderboard/' + league; + console.log(`reload ${url}`); $.ajax({ url: url, type: 'get', success: function (res, status) { if (res) { - items = res.items; - print(); + print(res); } } }); } -function print() { +function print(res) { + items = res; + clear(); addRow('lb-header', 'Position', 'Name', 'Time') @@ -35,9 +37,9 @@ function print() { items.sort(compare); let pos = 0; - items.forEach(function (e) { + items.forEach(function (item) { pos++; - addRow('lb-row', pos, e.name, e.time); + addRow('lb-row', pos, item.name, item.time); }); } @@ -72,14 +74,12 @@ function addText(row, text) { row.appendChild(item); } -$(function () { +let socket = io(); +socket.on('league', function (name) { + console.log(`socket league ${name}`); reload(); - setInterval(function () { - reload(); - }, 1000); }); -let socket = io(); -socket.on('leaderboard', function (name) { - +$(function () { + reload(); }); diff --git a/static/timer.js b/static/timer.js index f3900eb..0e975a0 100644 --- a/static/timer.js +++ b/static/timer.js @@ -177,6 +177,7 @@ let timer = new Timer( let socket = io(); socket.on('timer', function (name) { + console.log(`socket timer ${name}`); if (name === 'start') { timer.start(); } else if (name === 'pause') { diff --git a/views/league.ejs b/views/league.ejs index 42b5078..b9f4e06 100644 --- a/views/league.ejs +++ b/views/league.ejs @@ -32,8 +32,8 @@ - +