Advertisement
Guest User

second paste

a guest
Aug 26th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.15 KB | None | 0 0
  1. //index.js
  2. var mongojs = require('mongojs');
  3. var db = mongojs('mongodb://chatapp:chatapp1@ds225902.mlab.com:25902/danielhartydb', ['account']);
  4.  
  5. db.account.insert({username:'a', password:'b'});
  6.  
  7. var app = require('express')();
  8. var http = require('http').Server(app);
  9. var io = require('socket.io')(http);
  10. var port = process.env.PORT || 3000;
  11.  
  12. app.get('/', function(req, res){
  13. res.sendFile(__dirname + '/index.html');
  14. });
  15.  
  16. //callback is true if the data {username:'user', password:'pw'} is in the database
  17. function isValidPassword(data, cb) {
  18. //try to find the data in the userbase
  19. db.account.find({username:data.username, password:data.password}, function(err, res) {
  20. //the res of .find is an array of all matching documents, so if the array has something in it we callback true
  21. if (res.length > 0) {
  22. cb(true);
  23. } else {
  24. cb(false);
  25. }
  26. });
  27. }
  28.  
  29. //cb is true if the username is taken
  30. function isUsernameTaken(data, cb) {
  31. db.account.find({username:data.username}, function(err, res) {
  32. if (res.length > 0) {
  33. cb(true);
  34. } else {
  35. cb(false);
  36. }
  37. });
  38. }
  39.  
  40. //adds a user to the db, data should be formatted as {username:'user', password:'pw'}
  41. function addUser(data, cb) {
  42. db.account.insert({username:data.username, password:data.password}, function(err) {
  43. cb();
  44. });
  45. }
  46.  
  47. io.on('connection', function(socket){
  48. console.log('A socket connected');
  49. socket.join('global');
  50.  
  51. socket.on('chat message', function(data) {
  52. console.log('emmitting chat message: ' + data.msg + " to room " + data.room);
  53. io.in(data.room).emit('chat message', {msg:data.msg, username:data.username});
  54. });
  55.  
  56. socket.on('game list update', function() {
  57. socket.emit('game list update', GAME_LIST);
  58. });
  59.  
  60. socket.on('command', function(data) {
  61. var commandArray = data.command.split(' ');
  62.  
  63. switch (commandArray[0]) {
  64. case 'newGame':
  65. if (data.room != 'global') {
  66. socket.emit('server message', "You're already in a game.");
  67. break;
  68. }
  69. if (commandArray.length > 1) {
  70. var nameTaken = false;
  71. for (var i = 0; i < GAME_LIST.length; i++) {
  72. if (GAME_LIST[i].name === commandArray[1]) {
  73. socket.emit('server message', "There's already a game named " + commandArray[1] + " .");
  74. nameTaken = true;
  75. break;
  76. }
  77. }
  78. if (!nameTaken) {
  79. GAME_LIST.push(new Game(commandArray[1]));
  80. socket.leave('global');
  81. socket.join(commandArray[1]);
  82. socket.emit('set room', commandArray[1]);
  83. socket.emit('server message', "Game " + commandArray[1] + " created.");
  84. }
  85. } else {
  86. socket.emit('server message', "/newGame game_name");
  87. }
  88. break;
  89.  
  90. case 'listGames':
  91. if (gameListHasOpenGames(GAME_LIST)) {
  92. socket.emit('server message', "Open games: " + gameListToString(GAME_LIST) + ".");
  93. } else {
  94. socket.emit('server message', "There are no open games.");
  95. }
  96. break;
  97.  
  98. case 'joinGame':
  99. var game = gameListFindName(GAME_LIST, commandArray[1]);
  100. if (game === null) {
  101. socket.emit('server message', "There's no game by that name.");
  102. break;
  103. }
  104. if (!game.open) {
  105. socket.emit('server message', "That game is closed to new players.");
  106. break;
  107. }
  108. if (data.room != 'global') {
  109. socket.emit('server message', "You're already in a game.");
  110. break;
  111. }
  112. socket.leave('global');
  113. socket.join(commandArray[1]);
  114. socket.emit('set room', commandArray[1]);
  115. socket.emit('server message', "Joined game " + commandArray[1] + ".");
  116. break;
  117.  
  118. default:
  119. socket.emit('server message', "Command " + commandArray[0] + " not found.");
  120. }
  121. });
  122.  
  123. socket.on('signIn', function(data) {
  124. isValidPassword(data, function(res) {
  125. if (res) {
  126. socket.emit('signInResponse', {success:true, username: data.username});
  127. io.emit('signed in', {username: data.username})
  128. } else {
  129. socket.emit('signInResponse', {success:false});
  130. }
  131. });
  132. });
  133.  
  134. socket.on('signUp', function(data) {
  135. isUsernameTaken(data, function(res) {
  136. if (res) {
  137. socket.emit('signUpResponse', {success:false});
  138. } else {
  139. addUser(data, function() {
  140. socket.emit('signUpResponse', {success:true});
  141. });
  142. }
  143. });
  144. });
  145. });
  146.  
  147. http.listen(port, function(){
  148. console.log('listening on *:' + port);
  149. });
  150.  
  151. var GAME_LIST = [];
  152. class Game {
  153. constructor(name, isPrivate, password) {
  154. this.name = name;
  155. this.open = true;
  156. this.isPrivate = isPrivate;
  157. this.password = password;
  158. }
  159. }
  160.  
  161. function gameListToString(array) {
  162. res = '';
  163. for (var i = 0; i < array.length; i++) {
  164. if (array[i].open) {
  165. res += array[i].name;
  166. if (i != array.length-1) {
  167. res+= ", "
  168. }
  169. }
  170. }
  171. return res;
  172. }
  173.  
  174. function gameListHasOpenGames(array) {
  175. for (var i = 0; i < array.length; i++) {
  176. if (array[i].open) {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182.  
  183. function gameListFindName(array, name) {
  184. for (var i = 0; i < array.length; i++) {
  185. if (array[i].name === name) {
  186. return array[i];
  187. }
  188. }
  189. return null;
  190. }
  191.  
  192.  
  193.  
  194.  
  195. //index.html
  196. <!doctype html>
  197. <html>
  198. <head>
  199. <title>Socket.IO chat</title>
  200. <style>
  201. * { margin: 0; padding: 0; box-sizing: border-box; }
  202. body { font: 13px Helvetica, Arial; }
  203. table, th, td { border: 1px solid black; border-collapse: collapse; font: 24px Helvetica, Arial; }
  204. #chatDiv {border-style: solid; border-width: 1px; display: none; width: 100%; position: fixed; top:75%; height:21%}
  205. #chat-form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
  206. #chat-input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
  207. #chat-button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
  208. #messages { list-style-type: none; margin: 0; padding: 0; position: fixed; height:20%; width:100%; overflow-y:scroll}
  209. #messages li { padding: 5px 10px; }
  210. #messages li:nth-child(odd) { background: #eee; }
  211. #messages { margin-bottom: 40px }
  212. #signDiv { font: 24px Helvetica, Arial;
  213. padding: 6px;
  214. position: absolute;
  215. width:250px;
  216. height:130px;
  217. top:50%;
  218. left:50%;
  219. margin: -65px 0 0 -125px;
  220. background: #000;
  221. text-align: center;
  222. color: white;
  223. }
  224. </style>
  225. </head>
  226. <body>
  227.  
  228. <div id = 'gameDiv' style="display:none;">
  229. <div id = 'canvasDiv' style="position:absolute;width:500px;height:500px"> </div>
  230. <div id = 'domUiDiv' style="position:absolute;width:500px;height:500px">
  231. <button onclick="changeMap()" style="position:absolute;bottom:0px;left:0px">
  232. Change Map
  233. </button>
  234. </div>
  235. </div>
  236.  
  237. <div id = 'gameListDiv' style="display:none; position:absolute; width:100%; height:75%; top:0px; left:0px">
  238. <div id = 'abovestuff' style="position:absolute; top:0px; left:0px">
  239. <button onclick="refreshGamesList()" style="position:absolute;top:0px;left:0px">Refresh</button>
  240. </div>
  241. <div id = 'gamesTableDiv' style="position:absolute;top:30px;left:0px; width:100%">
  242. <table id = 'gamesTable' style = 'width:100%;'>
  243. <tr>
  244. <th>Name</th>
  245. <th>Players</th>
  246. <th>Private</th>
  247. </tr>
  248. </table>
  249. </div>
  250. </div>
  251.  
  252. <div id = 'chatDiv'>
  253. <ul id="messages"></ul>
  254. <form id='chat-form' action="">
  255. <input id="chat-input" autocomplete="off" /><button id = 'chat-button'>Send</button>
  256. </form>
  257. </div>
  258. <div id="signDiv">
  259. <form id = sign-form>
  260. Welcome<br>
  261. <input id="signDiv-username" type="text" placeholder="Username"></input><br>
  262. <input id="signDiv-password" type="password" placeholder="Password"></input><br>
  263. <button id="signDiv-signIn">Sign In</button>
  264. <button id="signDiv-signUp" type = "button">Sign Up</button>
  265. </form>
  266. </div>
  267.  
  268. <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
  269. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.js"></script>
  270. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.dom.js"></script>
  271. <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.sound.min.js"></script>
  272. <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
  273. <script>
  274. var socket = io();
  275.  
  276. //signIn
  277. var signDiv = document.getElementById('signDiv');
  278. var signForm = document.getElementById('sign-form');
  279. var signDivUsername = document.getElementById('signDiv-username');
  280. var signDivSignIn = document.getElementById('signDiv-signIn');
  281. var signDivSignUp = document.getElementById('signDiv-signUp');
  282. var signDivPassword = document.getElementById('signDiv-password');
  283. var username = 'defaultusername';
  284.  
  285. signDivSignIn.onclick = function(){
  286. socket.emit('signIn',{username:signDivUsername.value,password:signDivPassword.value});
  287. }
  288. signDivSignUp.onclick = function(){
  289. socket.emit('signUp',{username:signDivUsername.value,password:signDivPassword.value});
  290. }
  291. signForm.onsubmit = function(e) {
  292. e.preventDefault();
  293. signDivSignIn.click();
  294. }
  295.  
  296. socket.on('signInResponse',function(data){
  297. if(data.success){
  298. username = data.username;
  299. signDiv.style.display = 'none';
  300. chatDiv.style.display = 'inline-block';
  301. gameListDiv.style.display = 'inline-block';
  302. } else
  303. alert("Sign in unsuccessul.");
  304. });
  305.  
  306. socket.on('signUpResponse',function(data){
  307. if(data.success){
  308. alert("Sign up successful.");
  309. } else
  310. alert("Sign up unsuccessful.");
  311. });
  312.  
  313. socket.on('signed in', function(data) {
  314. console.log(data.username + " signed in.");
  315. });
  316.  
  317. //chat
  318. var chatForm = document.getElementById('chat-form');
  319. var chatInput = document.getElementById('chat-input');
  320. var messages = document.getElementById('messages');
  321. var room = 'global';
  322.  
  323.  
  324. socket.on('chat message', function(data) {
  325. var li = document.createElement('li');
  326. li.innerHTML = '<b>' + data.username + '</b>: ' + data.msg;
  327. messages.appendChild(li);
  328. messages.scrollTop = messages.scrollHeight;
  329. });
  330.  
  331. socket.on('server message', function(msg) {
  332. var li = document.createElement('li');
  333. li.innerHTML = msg;
  334. messages.appendChild(li);
  335. messages.scrollTop = messages.scrollHeight;
  336. });
  337.  
  338. socket.on('set room', function(data) {
  339. room = data;
  340. });
  341.  
  342. chatForm.onsubmit = function(e) {
  343. e.preventDefault();
  344. if(chatInput.value[0] === '/') {
  345. socket.emit('command', {command:chatInput.value.slice(1), room:room});
  346. } else {
  347. socket.emit('chat message', {msg:chatInput.value, room:room, username:username});
  348. }
  349. chatInput.value = '';
  350. }
  351.  
  352. //gameList
  353. var clientList = [];
  354. var gameListDiv = document.getElementById('gameListDiv');
  355. var gamesTable = document.getElementById('gamesTable')
  356.  
  357. function refreshGamesList() {
  358. console.log('getting game list from server');
  359. socket.emit('game list update');
  360. }
  361. socket.on('game list update', function(data) {
  362. console.log('retrieved game list')
  363. clientList = data;
  364. for (var i = 1; i < gamesTable.rows.length; i++) {
  365. gamesTable.deleteRow(i);
  366. }
  367. for (var i = 0; i < clientList.length; i++) {
  368. var gameName = clientList[i].name
  369. var newRow = gamesTable.insertRow();
  370. newRow.onmouseover = function() {
  371. newRow.style.backgroundColor = 'yellow';
  372. }
  373. newRow.onmouseout = function() {
  374. newRow.style.backgroundColor = 'white';
  375. }
  376. var cell1 = newRow.insertCell(0);
  377. cell1.innerHTML = clientList[i].name
  378. var cell2 = newRow.insertCell(1);
  379. cell2.innerHTML = '0/6';
  380. var cell3 = newRow.insertCell('2');
  381. cell3.innerHTML = 'No';
  382. newRow.onclick = function() {
  383. socket.emit('command', {command: 'joinGame ' + gameName, room:room});
  384. gameListDiv.style.display = 'none';
  385. gameDiv.style = 'absolute';
  386. }
  387. }
  388. });
  389.  
  390.  
  391. //DOM UI
  392.  
  393.  
  394. //game
  395. var gameDiv = document.getElementById('gameDiv');
  396. var canvasDiv = document.getElementById('canvasDiv');
  397. function setup() {
  398. var cnv = createCanvas(500, 500);
  399. cnv.parent('canvasDiv');
  400. background('grey');
  401. }
  402. </script>
  403. </body>
  404. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement