Guest User

Untitled

a guest
Aug 1st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.96 KB | None | 0 0
  1. let Auth = require("./Auth.js");
  2. let WebSocketServer = require("websocket").server;
  3. let http = require("http");
  4. let nodeMailer = require("nodemailer");
  5. let server = http.createServer(function(request, response){});
  6.  
  7. server.listen(800, function(){});
  8. wsServer = new WebSocketServer({httpServer: server});
  9.  
  10. let clients = {};
  11. let sessionInfo = {};
  12.  
  13. function validateProperties(data, properties) {
  14. for (i in properties) {
  15. if (!(properties[i] in data)) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21.  
  22. wsServer.on("request", function(request) {
  23. let connection = request.accept(null, request.origin);
  24. console.log("Connected to " + request.remoteAddress);
  25.  
  26. // Find an index to assign it to
  27. let index = 0;
  28. while (index in clients)
  29. index++;
  30.  
  31. clients[index] = connection;
  32. sessionInfo[index] = {};
  33.  
  34. connection.on("message", function(message) {
  35. if (message.type === "utf8") {
  36. let data = "";
  37. try {
  38. data = JSON.parse(message.utf8Data);
  39. } catch (err) {
  40. return;
  41. }
  42.  
  43. let curClient = clients[index];
  44. let session = sessionInfo[index];
  45.  
  46. if (!("what" in data)) {
  47. return;
  48. } else if (data.what == "begin-login") {
  49. if (!validateProperties(data, ["username"]))
  50. return;
  51.  
  52. Auth.getSaltForUser(data.username, function(salt) {
  53. let token = Auth.randomHex(64);
  54. session.loginToken = token;
  55. session.salt = salt;
  56.  
  57. curClient.send(JSON.stringify({
  58. what: "login-token",
  59. token: token,
  60. salt: salt
  61. }));
  62. }, function() {
  63. curClient.send(JSON.stringify({
  64. what: "bad-login"
  65. }));
  66. });
  67. } else if (data.what == "login") {
  68. if (!validateProperties(data, ["username", "value"]))
  69. return;
  70.  
  71. Auth.checkLogin(data.username, session.loginToken, data.value, function() {
  72. // Check if this user is already logged in, and don't log in if so
  73. for (i in sessionInfo) {
  74. if (sessionInfo[i].username == data.username) {
  75. curClient.send(JSON.stringify({
  76. what: "already-logged-in"
  77. }));
  78.  
  79. return;
  80. }
  81. }
  82.  
  83. delete session.loginToken;
  84.  
  85. session.loggedIn = true;
  86. session.username = data.username;
  87. curClient.send(JSON.stringify({
  88. what: "logged-in",
  89. user: session.username
  90. }));
  91. }, function() {
  92. curClient.send(JSON.stringify({
  93. what: "bad-login"
  94. }));
  95. });
  96. } else if (data.what == "begin-create-account") {
  97. let salt = Auth.randomHex(64);
  98. session.newSalt = salt;
  99.  
  100. curClient.send(JSON.stringify({
  101. what: "new-account-salt",
  102. salt: salt
  103. }));
  104. } else if (data.what == "create-account") {
  105. if (!validateProperties(data, ["username", "storedPW", "email"]))
  106. return;
  107.  
  108. if (!session.newSalt)
  109. return;
  110.  
  111. Auth.getUserInfo(data.username, function() {
  112. // Username is already taken!
  113. curClient.send(JSON.stringify({
  114. what: "username-taken"
  115. }));
  116. }, function() {
  117. curClient.send(JSON.stringify({
  118. what: "activation-email-sent"
  119. }));
  120.  
  121. let activationCode = Auth.randomHex(10);
  122. Auth.addUnverifiedUser(data.username, session.newSalt, data.storedPW, data.email, activationCode);
  123.  
  124. // Send verification email
  125. let transporter = nodeMailer.createTransport({
  126. service: "gmail",
  127. auth: {
  128. // Replace this with the correct email details!
  129. pass: "emailpassword"
  130. }
  131. });
  132.  
  133. let mailOptions = {
  134. to: data.email,
  135. subject: "MomoRTS Account Verification",
  136. // Replace this with the correct url!!!!
  137. html: `<a href="file:///home/glaba/Desktop/Intense TicTacToe/client/index.html?user=${data.username}&code=${activationCode}">Activation URL</a>`
  138. };
  139.  
  140. transporter.sendMail(mailOptions, function(error, info){
  141. if (error) {
  142. console.log(error);
  143. } else {
  144. console.log(`Verification email sent to ${data.email}: ` + info.response);
  145. }
  146. });
  147. });
  148. } else if (data.what == "verify-account") {
  149. if (!validateProperties(data, ["username", "activationCode"]))
  150. return;
  151.  
  152. Auth.verifyUser(data.username, data.activationCode, function() {
  153. curClient.send(JSON.stringify({
  154. what: "account-activated"
  155. }));
  156. });
  157. } else if (data.what == "log-out") {
  158. sessionInfo[index] = {};
  159.  
  160. // Sample of setting account details (obviously not accurate)
  161. } else if (data.what == "game-move") {
  162. // Code to process game move
  163. // ... lots of code ...
  164. // end result of lots of code:
  165. let gameOver = true;
  166. let didIWin = true;
  167. let opponent = "glaba";
  168.  
  169. if (gameOver) {
  170. Auth.getDetails(session.username, function(details) {
  171. Auth.getDetails(opponent, function(opponentDetails) {
  172. // Effective MMR update function that takes into account the skill of both players
  173. function calculateMMRChange(didIWin, myMMR, opponentMMR) {
  174. return Math.random() * 100 - 50;
  175. }
  176.  
  177. let mmrChange = calculateMMRChange(didIWin, details.MMR, opponentDetails.MMR);
  178. details.MMR += mmrChange;
  179.  
  180. Auth.setDetails(session.username, details);
  181.  
  182. curClient.send(JSON.stringify({
  183. what: "game-over",
  184. mmrChange: mmrChange,
  185. victory: didIWin
  186. }))
  187. })
  188. });
  189. }
  190.  
  191. // Sample of getting account details (maybe accurate)
  192. } else if (data.what == "get-gold") {
  193. Auth.getDetails(session.username, function(details) {
  194. curClient.send(JSON.stringify({
  195. goldCount: details.gold
  196. }));
  197. })
  198. }
  199.  
  200.  
  201. }
  202. });
  203.  
  204. connection.on("close", function(connection) {
  205. delete clients[index];
  206. delete sessionInfo[index];
  207. });
  208. });
Advertisement
Add Comment
Please, Sign In to add comment