├── package.json ├── README.md ├── LICENSE ├── index.js └── index.html /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatty-chatty", 3 | "version": "0.0.3", 4 | "description": "Go Chatty Chatty and go Nuts!", 5 | "dependencies": { 6 | "socket.io": "~1.0.4" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | click-race 2 | ========== 3 | 4 | Multiplayer cookie clicker! It doesn't have the cookies, but it has the clicks! 5 | 6 | ##changelog 7 | 8 | ###Version 0.0.1 9 | 10 | Global counter that gets incremented and pushed to everyone whenever a user presses a button 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mihai Radu Popescu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var app = require('http').createServer(httpHandler); 2 | var io = require('socket.io')(app) 3 | var fs = require('fs'); 4 | 5 | function httpHandler (req, res) { 6 | fs.readFile(__dirname + '/index.html', function (err, data) { 7 | if (err) { 8 | res.writeHead(500); 9 | console.error('Error loading index.html'); 10 | res.end('Error loading index.html'); 11 | return; 12 | } 13 | 14 | res.writeHead(200); 15 | res.end(data); 16 | }); 17 | } 18 | 19 | ////////////////////////////// 20 | // USERS AND CONNECTIONS 21 | 22 | var sockets = []; 23 | var counter = 0; 24 | 25 | var lowestAvailableSocket = function() { 26 | for (var i = 0, len = sockets.length; i < len; i++) { 27 | if (sockets[i] === null) { 28 | return i; 29 | } 30 | } 31 | return len; 32 | } 33 | 34 | io.on('connection', function(socket) { 35 | var currentId = lowestAvailableSocket(); 36 | sockets[currentId] = {nick: 'Anonymous', counter: 0}; // todo: actually use nick, counter 37 | //console.log('new connection: ' + currentId); 38 | socket.broadcast.emit('join', currentId, sockets[currentId]); // tell others a new user joined 39 | socket.emit('welcome', sockets); // give new user list of people and scores 40 | 41 | socket.on('click', function() { 42 | counter++; 43 | sockets[currentId].counter++; 44 | //console.log('new counter by ' + currentId + ': ' +counter); 45 | socket.emit('new personal counter', sockets[currentId].counter); 46 | io.emit('new total counter', counter); 47 | io.emit('new counter', currentId, sockets[currentId].counter); 48 | }); 49 | 50 | socket.on('change nick', function(nick) { 51 | console.log('user ' + currentId + ' (' + sockets[currentId].nick + ') changed nick to ' + nick); 52 | sockets[currentId].nick = nick; 53 | io.emit('nick changed', currentId, nick); 54 | }); 55 | 56 | socket.on('disconnect', function() { 57 | //console.log('user ' + currentId + ' disconnected'); 58 | sockets[currentId] = null; 59 | io.emit('leave', currentId); 60 | }); 61 | }); 62 | 63 | app.listen(1234); 64 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Click Race! 5 | 6 | 7 |

Our total clicks: press the button and then I'll tell you!

8 |

My counter: 0.

9 |

10 | Nickname: 11 |

People:

12 | 13 | 14 | 15 | 16 | 72 | 73 | 74 | --------------------------------------------------------------------------------