Commit ea5a9738b3dde183695a61356edb8d1c1a7b2a3c

Authored by Geoffrey PREUD'HOMME
1 parent cd85f6db

Scoreboard : coté serveur

.gitignore 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +scores.json
  2 +node_modules
  3 +__pycache__
  4 +*.pyc
  5 +*.swp
... ...
package.json 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +{
  2 + "name": "trosh-scoreboard",
  3 + "version": "0.0.1",
  4 + "description": "Trosh scoreboard web app for Club Info",
  5 + "main": "server.js",
  6 + "scripts": {
  7 + "test": "echo \"Error: no test specified\" && exit 1",
  8 + "start": "node server.js"
  9 + },
  10 + "repository": {
  11 + "type": "git",
  12 + "url": "git+https://github.com/clubinfopolytechlille/trosh.git"
  13 + },
  14 + "author": "ClubInfoPolytechLille",
  15 + "license": "WTFPL",
  16 + "bugs": {
  17 + "url": "https://github.com/clubinfopolytechlille/trosh/issues"
  18 + },
  19 + "homepage": "https://github.com/clubinfopolytechlille/trosh#readme",
  20 + "dependencies": {
  21 + "express": "^4.13.4"
  22 + }
  23 +}
... ...
public/index.html 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +<!doctype html>
  2 +<html lang="fr">
  3 +<head>
  4 + <meta charset="UTF-8">
  5 + <title>Trosh Scoreboard</title>
  6 +</head>
  7 +<body>
  8 + <h1>Trosh scores</h1>
  9 + <ul id="list">
  10 + </ul>
  11 + <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
  12 + <script src="/socket.io/socket.io.js"></script>
  13 + <script src="/main.js"></script>
  14 +</body>
  15 +</html>
... ...
public/main.js 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +$(function() {
  2 + var socket = io();
  3 + var scores = [];
  4 + var list = $('#list');
  5 +
  6 + socket.emit('getScores');
  7 +
  8 + socket.on('scores', function(newScores) {
  9 + scores = newScores;
  10 + redrawScores();
  11 + });
  12 +
  13 + socket.on('newScore', addScore);
  14 +
  15 + function redrawScores() {
  16 + list.empty();
  17 + for (s in scores) {
  18 + addScore(scores[s]);
  19 + }
  20 + }
  21 +
  22 + function addScore(obj) {
  23 + list.append($('<li>').text(obj.score + ' by ' + obj.name));
  24 + }
  25 +
  26 +});
... ...
server.js 0 → 100755
... ... @@ -0,0 +1,51 @@
  1 +#!/usr/bin/env node
  2 +var express = require('express');
  3 +var app = express();
  4 +var server = require('http').createServer(app);
  5 +var io = require('socket.io')(server);
  6 +var port = process.env.PORT || 3000;
  7 +var url = require('url');
  8 +var fs = require('fs');
  9 +
  10 +var scores = [];
  11 +loadScores();
  12 +
  13 +server.listen(port, function() {
  14 + console.log('Server listening at port %d', port);
  15 +});
  16 +
  17 +app.use(express.static(__dirname + '/public'));
  18 +
  19 +app.get('/', function(req, res) {
  20 + res.sendStatus(200);
  21 +});
  22 +
  23 +app.get('/addScore', function(req, res) {
  24 + var query = url.parse(req.url, true).query;
  25 + addScore(parseInt(query.score), query.name);
  26 + res.sendStatus(200);
  27 +});
  28 +
  29 +io.on('connection', function(socket) {
  30 + socket.on('getScores', function() {
  31 + socket.emit('scores', scores);
  32 + });
  33 +
  34 +});
  35 +
  36 +function loadScores() {
  37 + scores = JSON.parse(fs.readFileSync('scores.json', 'utf8'));
  38 +}
  39 +
  40 +function saveScores() {
  41 + fs.writeFileSync('scores.json', JSON.stringify(scores), 'utf8');
  42 +}
  43 +
  44 +function addScore(score, name) {
  45 + console.log("Score of %d by %s", score, name);
  46 + var obj = {score: score, name: name};
  47 + scores.push(obj);
  48 + io.emit('newScore', obj);
  49 + saveScores();
  50 +}
  51 +
... ...