Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let Auth = require("./Auth.js");
- let WebSocketServer = require("websocket").server;
- let http = require("http");
- let nodeMailer = require("nodemailer");
- let server = http.createServer(function(request, response){});
- server.listen(800, function(){});
- wsServer = new WebSocketServer({httpServer: server});
- let clients = {};
- let sessionInfo = {};
- function validateProperties(data, properties) {
- for (i in properties) {
- if (!(properties[i] in data)) {
- return false;
- }
- }
- return true;
- }
- wsServer.on("request", function(request) {
- let connection = request.accept(null, request.origin);
- console.log("Connected to " + request.remoteAddress);
- // Find an index to assign it to
- let index = 0;
- while (index in clients)
- index++;
- clients[index] = connection;
- sessionInfo[index] = {};
- connection.on("message", function(message) {
- if (message.type === "utf8") {
- let data = "";
- try {
- data = JSON.parse(message.utf8Data);
- } catch (err) {
- return;
- }
- let curClient = clients[index];
- let session = sessionInfo[index];
- if (!("what" in data)) {
- return;
- } else if (data.what == "begin-login") {
- if (!validateProperties(data, ["username"]))
- return;
- Auth.getSaltForUser(data.username, function(salt) {
- let token = Auth.randomHex(64);
- session.loginToken = token;
- session.salt = salt;
- curClient.send(JSON.stringify({
- what: "login-token",
- token: token,
- salt: salt
- }));
- }, function() {
- curClient.send(JSON.stringify({
- what: "bad-login"
- }));
- });
- } else if (data.what == "login") {
- if (!validateProperties(data, ["username", "value"]))
- return;
- Auth.checkLogin(data.username, session.loginToken, data.value, function() {
- // Check if this user is already logged in, and don't log in if so
- for (i in sessionInfo) {
- if (sessionInfo[i].username == data.username) {
- curClient.send(JSON.stringify({
- what: "already-logged-in"
- }));
- return;
- }
- }
- delete session.loginToken;
- session.loggedIn = true;
- session.username = data.username;
- curClient.send(JSON.stringify({
- what: "logged-in",
- user: session.username
- }));
- }, function() {
- curClient.send(JSON.stringify({
- what: "bad-login"
- }));
- });
- } else if (data.what == "begin-create-account") {
- let salt = Auth.randomHex(64);
- session.newSalt = salt;
- curClient.send(JSON.stringify({
- what: "new-account-salt",
- salt: salt
- }));
- } else if (data.what == "create-account") {
- if (!validateProperties(data, ["username", "storedPW", "email"]))
- return;
- if (!session.newSalt)
- return;
- Auth.getUserInfo(data.username, function() {
- // Username is already taken!
- curClient.send(JSON.stringify({
- what: "username-taken"
- }));
- }, function() {
- curClient.send(JSON.stringify({
- what: "activation-email-sent"
- }));
- let activationCode = Auth.randomHex(10);
- Auth.addUnverifiedUser(data.username, session.newSalt, data.storedPW, data.email, activationCode);
- // Send verification email
- let transporter = nodeMailer.createTransport({
- service: "gmail",
- auth: {
- // Replace this with the correct email details!
- user: "email@gmail.com",
- pass: "emailpassword"
- }
- });
- let mailOptions = {
- from: "email@gmail.com",
- to: data.email,
- subject: "MomoRTS Account Verification",
- // Replace this with the correct url!!!!
- html: `<a href="file:///home/glaba/Desktop/Intense TicTacToe/client/index.html?user=${data.username}&code=${activationCode}">Activation URL</a>`
- };
- transporter.sendMail(mailOptions, function(error, info){
- if (error) {
- console.log(error);
- } else {
- console.log(`Verification email sent to ${data.email}: ` + info.response);
- }
- });
- });
- } else if (data.what == "verify-account") {
- if (!validateProperties(data, ["username", "activationCode"]))
- return;
- Auth.verifyUser(data.username, data.activationCode, function() {
- curClient.send(JSON.stringify({
- what: "account-activated"
- }));
- });
- } else if (data.what == "log-out") {
- sessionInfo[index] = {};
- // Sample of setting account details (obviously not accurate)
- } else if (data.what == "game-move") {
- // Code to process game move
- // ... lots of code ...
- // end result of lots of code:
- let gameOver = true;
- let didIWin = true;
- let opponent = "glaba";
- if (gameOver) {
- Auth.getDetails(session.username, function(details) {
- Auth.getDetails(opponent, function(opponentDetails) {
- // Effective MMR update function that takes into account the skill of both players
- function calculateMMRChange(didIWin, myMMR, opponentMMR) {
- return Math.random() * 100 - 50;
- }
- let mmrChange = calculateMMRChange(didIWin, details.MMR, opponentDetails.MMR);
- details.MMR += mmrChange;
- Auth.setDetails(session.username, details);
- curClient.send(JSON.stringify({
- what: "game-over",
- mmrChange: mmrChange,
- victory: didIWin
- }))
- })
- });
- }
- // Sample of getting account details (maybe accurate)
- } else if (data.what == "get-gold") {
- Auth.getDetails(session.username, function(details) {
- curClient.send(JSON.stringify({
- goldCount: details.gold
- }));
- })
- }
- }
- });
- connection.on("close", function(connection) {
- delete clients[index];
- delete sessionInfo[index];
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement