Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http');
- var url = require('url');
- var fs = require('fs');
- var crypto = require('crypto');
- var chance = require('chance');
- var salts = new chance();
- var waiting = [];
- var game_id = [];
- function isInteger(x) {
- return x % 1 === 0;
- }
- check_file();
- var headers = {
- plain: {
- 'Content-Type': 'application/javascript',
- 'Cache-Control': 'no-cache',
- 'Access-Control-Allow-Origin': '*'
- },
- sse: {
- 'Content-Type': 'text/event-stream',
- 'Cache-Control': 'no-cache',
- 'Access-Control-Allow-Origin': '*',
- 'Connection': 'keep-alive'
- }
- };
- function check_file()
- {
- fs.readFile('dados.json', function(err, data)
- {
- try
- {
- JSON.parse(data.toString());
- }
- catch (e)
- {
- fs.writeFile('dados.json', "[]", function(err){});
- }
- });
- fs.readFile('ranking.json', function(err, data)
- {
- try
- {
- JSON.parse(data.toString());
- }
- catch (e)
- {
- fs.writeFile('ranking.json', "[]", function(err){});
- }
- });
- fs.readFile('join.json', function(err, data)
- {
- try
- {
- JSON.parse(data.toString());
- }
- catch (e)
- {
- fs.writeFile('join.json', "[]", function(err){});
- }
- });
- }
- function sort_object(object){
- object.sort(function(a, b) {
- if(a.victories==b.victories)
- return parseInt(a.games) - parseInt(b.games);
- return -(parseInt(a.victories) - parseInt(b.victories));
- });
- }
- function oponent(group,size){
- console.log("entra waiting");
- if(waiting.length==0){
- console.log("entra vazio waiting");
- return null;
- }
- for(var i = 0; i < waiting.length; i++)
- {
- console.log("entra for waiting");
- if(waiting[i].group == group && waiting[i].size == size)
- {
- var temp = waiting[i];
- waiting.splice(i, 1);
- return temp;
- }
- }
- return null;
- }
- function newGame(user1,user2,gameID,hash,size){
- var rack=[size];
- for(var i=1;i<=size;i++){
- rack[i]=i;
- }
- var data ={
- board:{
- table: null,
- size: size,
- rack: rack
- },
- user1 :{
- nick : user1
- },
- user2:{
- nick: user2
- },
- game :
- {
- turn: user1,
- hash: hash
- }
- };
- game_id[gameID] = data;
- }
- var server = http.createServer(function (req, res){
- var preq = url.parse(req.url,true);
- var pathname = preq.pathname;
- var answer = {};
- switch(req.method){
- case "POST":
- doPost(pathname,req,res);
- break;
- /*case "GET":
- doGet(pathname,req,res);
- break;*/
- }
- // res.writeHead(200, headers.plain);
- });
- server.listen(8080);
- function doPost(pathname, req, res){
- var body = "";
- var resposta;
- console.log("do post " + pathname);
- res.setHeader('Access-Control-Allow-Origin','*');
- switch(pathname) {
- case "/join":
- req
- .on("data",function(data) {body +=data})
- .on("end",function(){
- try{
- console.log("create join");
- var file = fs.readFile('join.json', function(err, data) {
- if(!err)
- {
- var flag = 0;
- console.log("entra join if");
- var dados = JSON.parse(body);
- console.log("dados" + JSON.stringify(dados));
- var group = dados.group;
- var nick = dados.nick;
- var pass = dados.pass;
- var size = dados.size;
- var gameID;
- var opo = oponent(group,size);
- console.log(" opo" + opo);
- if(opo === null){
- waiting.splice(0,0,dados);
- console.log("adicionar a waiting");
- console.log(waiting);
- }
- else{
- if(opo.size === size){
- console.log("possivel jogo");
- console.log(waiting);
- var hash = crypto.createHash('md5').update(salts.string({length:8})).digest('hex');
- gameID++;
- newGame(nick,opo.nick,gameID,hash,size);
- var game_data = game_id[gameID].game.hash;
- //console.log(game_data);
- var object = {game: game_data};
- if(game_data != hash){
- flag=1;
- res.writeHead(401, {'Content-Type' : 'text/plain'});
- res.end(JSON.stringify({error:"Playing in other game!"}));
- }
- console.log("game data " +JSON.stringify(object))
- fs.writeFile('join.json', JSON.stringify(object), function(err) {
- if (err)
- return console.log(err);
- });
- res.writeHead(200, {'Content-Type' : 'text/plain'});
- res.end(JSON.stringify(object));
- }
- }
- res.end();
- }
- else{
- res.writeHead(404, {'Content-Type' : 'text/plain'});
- res.end();
- console.log("erro join");
- }
- })
- }
- catch(err) { console.log("erro json"); }
- })
- .on("error", function(err) { console.log(err.message); })
- break;
- case "/register":
- req
- .on("data",function(data) {body +=data})
- .on("end",function(){
- try{
- var data = "";
- console.log("createServer");
- var file = fs.readFile('dados.json', function(err, data) {
- if (!err) {
- console.log("entrou no if");
- console.log(body);
- var dados = JSON.parse(body);
- var user = dados.nick;
- var pass = dados.pass;
- console.log("user no register " + user + " pass " + pass);
- var flag = 1;
- var object = JSON.parse(data.toString('utf8'));
- for(var i=0;i<object.length;i++){
- var object_teste = JSON.stringify(object);
- // console.log("for2 user " + object_teste);
- // console.log("for2 pass " +
- // object_teste.body.pass[i]);
- if(object[i].nick === user){
- console.log("This user already exits");
- // var password =
- // crypto.createHash('md5').update(object[i].salt
- // + pass).digest('hex');
- if(pass === object[i].pass){
- console.log("etnra flag2");
- flag=2; // 200
- console.log("falg " + flag);
- break;
- }
- else {
- flag=3; // 401
- console.log("falg " + flag);
- break;
- }
- }
- else{
- flag=1;
- }
- }
- console.log("falg final " + flag);
- if(flag==1){
- var salt = salts.string({length: 4});
- var password = crypto.createHash('md5').update(salt + pass).digest('hex');
- // console.log(" data " + object[0]);
- var savefile = {};
- savefile.nick=user;
- savefile.pass=password;
- savefile.salt=salt;
- // console.log(" body " +
- // JSON.stringify(req.body));
- // console.log("pass " + pass);
- // console.log("pass cri " + password);
- object.push(savefile);
- console.log("aqui " + object);
- fs.writeFile('dados.json', JSON.stringify(object), function(err) {
- if (err)
- return console.log(err);
- });
- res.writeHead(200, {'Content-Type' : 'text/plain'});
- res.write("{}");
- }
- else if(flag==2){
- res.writeHead(200, {'Content-Type' : 'text/plain'});
- res.write("{}");
- console.log("flag2" + flag);
- }
- else{
- res.writeHead(401, {'Content-Type' : 'text/hml'});
- res.write(JSON.stringify({error: "User registered with a different password"}));
- console.log("flag3" + flag);
- }
- res.end();
- }
- else{
- res.writeHead(404, {'Content-Type' : 'text/hml'});
- res.write(JSON.stringify({error: "ERROR"}));
- }
- })}
- catch(err) { console.log("erro json"); /* erros de JSON */ }
- })
- .on("error", function(err) { console.log(err.message); })
- break;
- case "/ranking":
- req
- .on("data",function(data) {body +=data})
- .on("end",function(){
- try{
- console.log("create server ranking");
- var file = fs.readFile('ranking.json', function(err, data) {
- if (!err) {
- console.log(body);
- var dados = JSON.parse(body);
- var size = dados.size;
- console.log("size " + size +isInteger(size));
- if(isInteger(size) && (size>=2 && size<=10)){
- var object = JSON.parse(data.toString('utf8'));
- console.log("asd " +object);
- if(object[size] != undefined){
- sort_object(object);
- res.writeHead(200, {'Content-Type' : 'text/plain'});
- res.write(JSON.stringify(object[size]));
- console.log(object[size]);
- }
- else{
- res.writeHead(401, {'Content-Type' : 'text/hml'});
- res.write(JSON.stringify({error: "Doens't exits table with this size"}));
- console.log("nao tem tabela");
- }
- }
- else{
- res.writeHead(401, {'Content-Type' : 'text/hml'});
- res.write(JSON.stringify({error: "Size isn't a valid"}));
- console.log("deu erro com size not int");
- }
- res.end();
- }
- else{
- res.writeHead(404, {'Content-Type' : 'text/html'});
- res.end();
- console.log("deu erro");
- }
- });
- }
- catch(err) { console.log("erro json"); /* erros de JSON */ }
- })
- .on("error", function(err) { console.log(err.message); })
- break;
- return;
- }
- }
Add Comment
Please, Sign In to add comment