Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //index.js
- var mongojs = require('mongojs');
- var db = mongojs('mongodb://chatapp:chatapp1@ds225902.mlab.com:25902/danielhartydb', ['account']);
- db.account.insert({username:'a', password:'b'});
- var app = require('express')();
- var http = require('http').Server(app);
- var io = require('socket.io')(http);
- var port = process.env.PORT || 3000;
- app.get('/', function(req, res){
- res.sendFile(__dirname + '/index.html');
- });
- //callback is true if the data {username:'user', password:'pw'} is in the database
- function isValidPassword(data, cb) {
- //try to find the data in the userbase
- db.account.find({username:data.username, password:data.password}, function(err, res) {
- //the res of .find is an array of all matching documents, so if the array has something in it we callback true
- if (res.length > 0) {
- cb(true);
- } else {
- cb(false);
- }
- });
- }
- //cb is true if the username is taken
- function isUsernameTaken(data, cb) {
- db.account.find({username:data.username}, function(err, res) {
- if (res.length > 0) {
- cb(true);
- } else {
- cb(false);
- }
- });
- }
- //adds a user to the db, data should be formatted as {username:'user', password:'pw'}
- function addUser(data, cb) {
- db.account.insert({username:data.username, password:data.password}, function(err) {
- cb();
- });
- }
- io.on('connection', function(socket){
- console.log('A socket connected');
- socket.join('global');
- socket.on('chat message', function(data) {
- console.log('emmitting chat message: ' + data.msg + " to room " + data.room);
- io.in(data.room).emit('chat message', {msg:data.msg, username:data.username});
- });
- socket.on('game list update', function() {
- socket.emit('game list update', GAME_LIST);
- });
- socket.on('command', function(data) {
- var commandArray = data.command.split(' ');
- switch (commandArray[0]) {
- case 'newGame':
- if (data.room != 'global') {
- socket.emit('server message', "You're already in a game.");
- break;
- }
- if (commandArray.length > 1) {
- var nameTaken = false;
- for (var i = 0; i < GAME_LIST.length; i++) {
- if (GAME_LIST[i].name === commandArray[1]) {
- socket.emit('server message', "There's already a game named " + commandArray[1] + " .");
- nameTaken = true;
- break;
- }
- }
- if (!nameTaken) {
- GAME_LIST.push(new Game(commandArray[1]));
- socket.leave('global');
- socket.join(commandArray[1]);
- socket.emit('set room', commandArray[1]);
- socket.emit('server message', "Game " + commandArray[1] + " created.");
- }
- } else {
- socket.emit('server message', "/newGame game_name");
- }
- break;
- case 'listGames':
- if (gameListHasOpenGames(GAME_LIST)) {
- socket.emit('server message', "Open games: " + gameListToString(GAME_LIST) + ".");
- } else {
- socket.emit('server message', "There are no open games.");
- }
- break;
- case 'joinGame':
- var game = gameListFindName(GAME_LIST, commandArray[1]);
- if (game === null) {
- socket.emit('server message', "There's no game by that name.");
- break;
- }
- if (!game.open) {
- socket.emit('server message', "That game is closed to new players.");
- break;
- }
- if (data.room != 'global') {
- socket.emit('server message', "You're already in a game.");
- break;
- }
- socket.leave('global');
- socket.join(commandArray[1]);
- socket.emit('set room', commandArray[1]);
- socket.emit('server message', "Joined game " + commandArray[1] + ".");
- break;
- default:
- socket.emit('server message', "Command " + commandArray[0] + " not found.");
- }
- });
- socket.on('signIn', function(data) {
- isValidPassword(data, function(res) {
- if (res) {
- socket.emit('signInResponse', {success:true, username: data.username});
- io.emit('signed in', {username: data.username})
- } else {
- socket.emit('signInResponse', {success:false});
- }
- });
- });
- socket.on('signUp', function(data) {
- isUsernameTaken(data, function(res) {
- if (res) {
- socket.emit('signUpResponse', {success:false});
- } else {
- addUser(data, function() {
- socket.emit('signUpResponse', {success:true});
- });
- }
- });
- });
- });
- http.listen(port, function(){
- console.log('listening on *:' + port);
- });
- var GAME_LIST = [];
- class Game {
- constructor(name, isPrivate, password) {
- this.name = name;
- this.open = true;
- this.isPrivate = isPrivate;
- this.password = password;
- }
- }
- function gameListToString(array) {
- res = '';
- for (var i = 0; i < array.length; i++) {
- if (array[i].open) {
- res += array[i].name;
- if (i != array.length-1) {
- res+= ", "
- }
- }
- }
- return res;
- }
- function gameListHasOpenGames(array) {
- for (var i = 0; i < array.length; i++) {
- if (array[i].open) {
- return true;
- }
- }
- return false;
- }
- function gameListFindName(array, name) {
- for (var i = 0; i < array.length; i++) {
- if (array[i].name === name) {
- return array[i];
- }
- }
- return null;
- }
- //index.html
- <!doctype html>
- <html>
- <head>
- <title>Socket.IO chat</title>
- <style>
- * { margin: 0; padding: 0; box-sizing: border-box; }
- body { font: 13px Helvetica, Arial; }
- table, th, td { border: 1px solid black; border-collapse: collapse; font: 24px Helvetica, Arial; }
- #chatDiv {border-style: solid; border-width: 1px; display: none; width: 100%; position: fixed; top:75%; height:21%}
- #chat-form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
- #chat-input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
- #chat-button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
- #messages { list-style-type: none; margin: 0; padding: 0; position: fixed; height:20%; width:100%; overflow-y:scroll}
- #messages li { padding: 5px 10px; }
- #messages li:nth-child(odd) { background: #eee; }
- #messages { margin-bottom: 40px }
- #signDiv { font: 24px Helvetica, Arial;
- padding: 6px;
- position: absolute;
- width:250px;
- height:130px;
- top:50%;
- left:50%;
- margin: -65px 0 0 -125px;
- background: #000;
- text-align: center;
- color: white;
- }
- </style>
- </head>
- <body>
- <div id = 'gameDiv' style="display:none;">
- <div id = 'canvasDiv' style="position:absolute;width:500px;height:500px"> </div>
- <div id = 'domUiDiv' style="position:absolute;width:500px;height:500px">
- <button onclick="changeMap()" style="position:absolute;bottom:0px;left:0px">
- Change Map
- </button>
- </div>
- </div>
- <div id = 'gameListDiv' style="display:none; position:absolute; width:100%; height:75%; top:0px; left:0px">
- <div id = 'abovestuff' style="position:absolute; top:0px; left:0px">
- <button onclick="refreshGamesList()" style="position:absolute;top:0px;left:0px">Refresh</button>
- </div>
- <div id = 'gamesTableDiv' style="position:absolute;top:30px;left:0px; width:100%">
- <table id = 'gamesTable' style = 'width:100%;'>
- <tr>
- <th>Name</th>
- <th>Players</th>
- <th>Private</th>
- </tr>
- </table>
- </div>
- </div>
- <div id = 'chatDiv'>
- <ul id="messages"></ul>
- <form id='chat-form' action="">
- <input id="chat-input" autocomplete="off" /><button id = 'chat-button'>Send</button>
- </form>
- </div>
- <div id="signDiv">
- <form id = sign-form>
- Welcome<br>
- <input id="signDiv-username" type="text" placeholder="Username"></input><br>
- <input id="signDiv-password" type="password" placeholder="Password"></input><br>
- <button id="signDiv-signIn">Sign In</button>
- <button id="signDiv-signUp" type = "button">Sign Up</button>
- </form>
- </div>
- <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.dom.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.sound.min.js"></script>
- <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
- <script>
- var socket = io();
- //signIn
- var signDiv = document.getElementById('signDiv');
- var signForm = document.getElementById('sign-form');
- var signDivUsername = document.getElementById('signDiv-username');
- var signDivSignIn = document.getElementById('signDiv-signIn');
- var signDivSignUp = document.getElementById('signDiv-signUp');
- var signDivPassword = document.getElementById('signDiv-password');
- var username = 'defaultusername';
- signDivSignIn.onclick = function(){
- socket.emit('signIn',{username:signDivUsername.value,password:signDivPassword.value});
- }
- signDivSignUp.onclick = function(){
- socket.emit('signUp',{username:signDivUsername.value,password:signDivPassword.value});
- }
- signForm.onsubmit = function(e) {
- e.preventDefault();
- signDivSignIn.click();
- }
- socket.on('signInResponse',function(data){
- if(data.success){
- username = data.username;
- signDiv.style.display = 'none';
- chatDiv.style.display = 'inline-block';
- gameListDiv.style.display = 'inline-block';
- } else
- alert("Sign in unsuccessul.");
- });
- socket.on('signUpResponse',function(data){
- if(data.success){
- alert("Sign up successful.");
- } else
- alert("Sign up unsuccessful.");
- });
- socket.on('signed in', function(data) {
- console.log(data.username + " signed in.");
- });
- //chat
- var chatForm = document.getElementById('chat-form');
- var chatInput = document.getElementById('chat-input');
- var messages = document.getElementById('messages');
- var room = 'global';
- socket.on('chat message', function(data) {
- var li = document.createElement('li');
- li.innerHTML = '<b>' + data.username + '</b>: ' + data.msg;
- messages.appendChild(li);
- messages.scrollTop = messages.scrollHeight;
- });
- socket.on('server message', function(msg) {
- var li = document.createElement('li');
- li.innerHTML = msg;
- messages.appendChild(li);
- messages.scrollTop = messages.scrollHeight;
- });
- socket.on('set room', function(data) {
- room = data;
- });
- chatForm.onsubmit = function(e) {
- e.preventDefault();
- if(chatInput.value[0] === '/') {
- socket.emit('command', {command:chatInput.value.slice(1), room:room});
- } else {
- socket.emit('chat message', {msg:chatInput.value, room:room, username:username});
- }
- chatInput.value = '';
- }
- //gameList
- var clientList = [];
- var gameListDiv = document.getElementById('gameListDiv');
- var gamesTable = document.getElementById('gamesTable')
- function refreshGamesList() {
- console.log('getting game list from server');
- socket.emit('game list update');
- }
- socket.on('game list update', function(data) {
- console.log('retrieved game list')
- clientList = data;
- for (var i = 1; i < gamesTable.rows.length; i++) {
- gamesTable.deleteRow(i);
- }
- for (var i = 0; i < clientList.length; i++) {
- var gameName = clientList[i].name
- var newRow = gamesTable.insertRow();
- newRow.onmouseover = function() {
- newRow.style.backgroundColor = 'yellow';
- }
- newRow.onmouseout = function() {
- newRow.style.backgroundColor = 'white';
- }
- var cell1 = newRow.insertCell(0);
- cell1.innerHTML = clientList[i].name
- var cell2 = newRow.insertCell(1);
- cell2.innerHTML = '0/6';
- var cell3 = newRow.insertCell('2');
- cell3.innerHTML = 'No';
- newRow.onclick = function() {
- socket.emit('command', {command: 'joinGame ' + gameName, room:room});
- gameListDiv.style.display = 'none';
- gameDiv.style = 'absolute';
- }
- }
- });
- //DOM UI
- //game
- var gameDiv = document.getElementById('gameDiv');
- var canvasDiv = document.getElementById('canvasDiv');
- function setup() {
- var cnv = createCanvas(500, 500);
- cnv.parent('canvasDiv');
- background('grey');
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement