diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc42588 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +scores.json +node_modules +__pycache__ +*.pyc +*.swp diff --git a/package.json b/package.json new file mode 100644 index 0000000..edc513f --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "trosh-scoreboard", + "version": "0.0.1", + "description": "Trosh scoreboard web app for Club Info", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/clubinfopolytechlille/trosh.git" + }, + "author": "ClubInfoPolytechLille", + "license": "WTFPL", + "bugs": { + "url": "https://github.com/clubinfopolytechlille/trosh/issues" + }, + "homepage": "https://github.com/clubinfopolytechlille/trosh#readme", + "dependencies": { + "express": "^4.13.4" + } +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..6a0bd00 --- /dev/null +++ b/public/index.html @@ -0,0 +1,15 @@ + + + + + Trosh Scoreboard + + +

Trosh scores

+ + + + + + diff --git a/public/main.js b/public/main.js new file mode 100644 index 0000000..336684e --- /dev/null +++ b/public/main.js @@ -0,0 +1,26 @@ +$(function() { + var socket = io(); + var scores = []; + var list = $('#list'); + + socket.emit('getScores'); + + socket.on('scores', function(newScores) { + scores = newScores; + redrawScores(); + }); + + socket.on('newScore', addScore); + + function redrawScores() { + list.empty(); + for (s in scores) { + addScore(scores[s]); + } + } + + function addScore(obj) { + list.append($('
  • ').text(obj.score + ' by ' + obj.name)); + } + +}); diff --git a/server.js b/server.js new file mode 100755 index 0000000..d7513e8 --- /dev/null +++ b/server.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node +var express = require('express'); +var app = express(); +var server = require('http').createServer(app); +var io = require('socket.io')(server); +var port = process.env.PORT || 3000; +var url = require('url'); +var fs = require('fs'); + +var scores = []; +loadScores(); + +server.listen(port, function() { + console.log('Server listening at port %d', port); +}); + +app.use(express.static(__dirname + '/public')); + +app.get('/', function(req, res) { + res.sendStatus(200); +}); + +app.get('/addScore', function(req, res) { + var query = url.parse(req.url, true).query; + addScore(parseInt(query.score), query.name); + res.sendStatus(200); +}); + +io.on('connection', function(socket) { + socket.on('getScores', function() { + socket.emit('scores', scores); + }); + +}); + +function loadScores() { + scores = JSON.parse(fs.readFileSync('scores.json', 'utf8')); +} + +function saveScores() { + fs.writeFileSync('scores.json', JSON.stringify(scores), 'utf8'); +} + +function addScore(score, name) { + console.log("Score of %d by %s", score, name); + var obj = {score: score, name: name}; + scores.push(obj); + io.emit('newScore', obj); + saveScores(); +} + -- libgit2 0.21.2