Skip to content

Commit

Permalink
leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
nalbam committed Aug 30, 2019
1 parent f1ac25d commit dc52863
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
22 changes: 18 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express'),
const request = require('request'),
express = require('express'),
gpio = require('rpi-gpio');

const app = express();
Expand All @@ -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'));
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions static/league.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* leaderboard.js
* league.js
*/

let lb_title = document.querySelector('.lb-title');
Expand All @@ -14,30 +14,32 @@ 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')

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);
});
}

Expand Down Expand Up @@ -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();
});
1 change: 1 addition & 0 deletions static/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion views/league.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

</html>

<script src="/jquery-3.3.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/jquery-3.3.1.min.js"></script>

<script>
let league = '<%= league %>';
Expand Down

0 comments on commit dc52863

Please sign in to comment.