Guest User

Untitled

a guest
Jan 2nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. var http = require('http');
  2. var url = require('url');
  3. var fs = require('fs');
  4. var crypto = require('crypto');
  5. var chance = require('chance');
  6.  
  7. var salts = new chance();
  8.  
  9. var waiting = [];
  10. var game_id = [];
  11. function isInteger(x) {
  12. return x % 1 === 0;
  13. }
  14. check_file();
  15. var headers = {
  16. plain: {
  17. 'Content-Type': 'application/javascript',
  18. 'Cache-Control': 'no-cache',
  19. 'Access-Control-Allow-Origin': '*'
  20. },
  21. sse: {
  22. 'Content-Type': 'text/event-stream',
  23. 'Cache-Control': 'no-cache',
  24. 'Access-Control-Allow-Origin': '*',
  25. 'Connection': 'keep-alive'
  26. }
  27. };
  28.  
  29. function check_file()
  30. {
  31. fs.readFile('dados.json', function(err, data)
  32. {
  33. try
  34. {
  35. JSON.parse(data.toString());
  36. }
  37. catch (e)
  38. {
  39. fs.writeFile('dados.json', "[]", function(err){});
  40. }
  41. });
  42. fs.readFile('ranking.json', function(err, data)
  43. {
  44. try
  45. {
  46. JSON.parse(data.toString());
  47. }
  48. catch (e)
  49. {
  50. fs.writeFile('ranking.json', "[]", function(err){});
  51. }
  52. });
  53. fs.readFile('join.json', function(err, data)
  54. {
  55. try
  56. {
  57. JSON.parse(data.toString());
  58. }
  59. catch (e)
  60. {
  61. fs.writeFile('join.json', "[]", function(err){});
  62. }
  63. });
  64. }
  65.  
  66. function sort_object(object){
  67. object.sort(function(a, b) {
  68. if(a.victories==b.victories)
  69. return parseInt(a.games) - parseInt(b.games);
  70.  
  71. return -(parseInt(a.victories) - parseInt(b.victories));
  72.  
  73. });
  74.  
  75. }
  76.  
  77. function oponent(group,size){
  78. console.log("entra waiting");
  79.  
  80. if(waiting.length==0){
  81. console.log("entra vazio waiting");
  82. return null;
  83. }
  84. for(var i = 0; i < waiting.length; i++)
  85. {
  86. console.log("entra for waiting");
  87.  
  88. if(waiting[i].group == group && waiting[i].size == size)
  89. {
  90. var temp = waiting[i];
  91. waiting.splice(i, 1);
  92. return temp;
  93. }
  94. }
  95.  
  96. return null;
  97.  
  98. }
  99. function newGame(user1,user2,gameID,hash,size){
  100. var rack=[size];
  101. for(var i=1;i<=size;i++){
  102. rack[i]=i;
  103. }
  104. var data ={
  105. board:{
  106. table: null,
  107. size: size,
  108. rack: rack
  109.  
  110. },
  111. user1 :{
  112. nick : user1
  113. },
  114. user2:{
  115. nick: user2
  116. },
  117. game :
  118. {
  119. turn: user1,
  120. hash: hash
  121. }
  122. };
  123. game_id[gameID] = data;
  124. }
  125.  
  126.  
  127. var server = http.createServer(function (req, res){
  128. var preq = url.parse(req.url,true);
  129. var pathname = preq.pathname;
  130. var answer = {};
  131. switch(req.method){
  132. case "POST":
  133. doPost(pathname,req,res);
  134. break;
  135.  
  136. /*case "GET":
  137. doGet(pathname,req,res);
  138. break;*/
  139.  
  140.  
  141. }
  142. // res.writeHead(200, headers.plain);
  143. });
  144.  
  145. server.listen(8080);
  146.  
  147. function doPost(pathname, req, res){
  148. var body = "";
  149. var resposta;
  150. console.log("do post " + pathname);
  151. res.setHeader('Access-Control-Allow-Origin','*');
  152. switch(pathname) {
  153.  
  154. case "/join":
  155. req
  156. .on("data",function(data) {body +=data})
  157. .on("end",function(){
  158. try{
  159. console.log("create join");
  160. var file = fs.readFile('join.json', function(err, data) {
  161. if(!err)
  162. {
  163. var flag = 0;
  164. console.log("entra join if");
  165. var dados = JSON.parse(body);
  166. console.log("dados" + JSON.stringify(dados));
  167. var group = dados.group;
  168. var nick = dados.nick;
  169. var pass = dados.pass;
  170. var size = dados.size;
  171. var gameID;
  172. var opo = oponent(group,size);
  173.  
  174. console.log(" opo" + opo);
  175. if(opo === null){
  176. waiting.splice(0,0,dados);
  177. console.log("adicionar a waiting");
  178. console.log(waiting);
  179.  
  180. }
  181. else{
  182.  
  183. if(opo.size === size){
  184. console.log("possivel jogo");
  185. console.log(waiting);
  186. var hash = crypto.createHash('md5').update(salts.string({length:8})).digest('hex');
  187.  
  188.  
  189. gameID++;
  190. newGame(nick,opo.nick,gameID,hash,size);
  191. var game_data = game_id[gameID].game.hash;
  192. //console.log(game_data);
  193. var object = {game: game_data};
  194. if(game_data != hash){
  195. flag=1;
  196. res.writeHead(401, {'Content-Type' : 'text/plain'});
  197. res.end(JSON.stringify({error:"Playing in other game!"}));
  198. }
  199. console.log("game data " +JSON.stringify(object))
  200. fs.writeFile('join.json', JSON.stringify(object), function(err) {
  201. if (err)
  202. return console.log(err);
  203.  
  204. });
  205. res.writeHead(200, {'Content-Type' : 'text/plain'});
  206. res.end(JSON.stringify(object));
  207. }
  208.  
  209.  
  210.  
  211. }
  212. res.end();
  213.  
  214.  
  215. }
  216. else{
  217. res.writeHead(404, {'Content-Type' : 'text/plain'});
  218. res.end();
  219. console.log("erro join");
  220. }
  221.  
  222. })
  223. }
  224. catch(err) { console.log("erro json"); }
  225. })
  226. .on("error", function(err) { console.log(err.message); })
  227. break;
  228.  
  229. case "/register":
  230. req
  231. .on("data",function(data) {body +=data})
  232. .on("end",function(){
  233. try{
  234. var data = "";
  235.  
  236. console.log("createServer");
  237. var file = fs.readFile('dados.json', function(err, data) {
  238.  
  239. if (!err) {
  240. console.log("entrou no if");
  241. console.log(body);
  242. var dados = JSON.parse(body);
  243. var user = dados.nick;
  244. var pass = dados.pass;
  245. console.log("user no register " + user + " pass " + pass);
  246. var flag = 1;
  247. var object = JSON.parse(data.toString('utf8'));
  248.  
  249. for(var i=0;i<object.length;i++){
  250. var object_teste = JSON.stringify(object);
  251. // console.log("for2 user " + object_teste);
  252. // console.log("for2 pass " +
  253. // object_teste.body.pass[i]);
  254. if(object[i].nick === user){
  255. console.log("This user already exits");
  256. // var password =
  257. // crypto.createHash('md5').update(object[i].salt
  258. // + pass).digest('hex');
  259.  
  260.  
  261.  
  262. if(pass === object[i].pass){
  263. console.log("etnra flag2");
  264. flag=2; // 200
  265. console.log("falg " + flag);
  266.  
  267. break;
  268. }
  269. else {
  270. flag=3; // 401
  271. console.log("falg " + flag);
  272.  
  273. break;
  274. }
  275.  
  276. }
  277. else{
  278. flag=1;
  279.  
  280. }
  281.  
  282. }
  283. console.log("falg final " + flag);
  284.  
  285. if(flag==1){
  286. var salt = salts.string({length: 4});
  287. var password = crypto.createHash('md5').update(salt + pass).digest('hex');
  288. // console.log(" data " + object[0]);
  289. var savefile = {};
  290. savefile.nick=user;
  291. savefile.pass=password;
  292. savefile.salt=salt;
  293. // console.log(" body " +
  294. // JSON.stringify(req.body));
  295. // console.log("pass " + pass);
  296. // console.log("pass cri " + password);
  297. object.push(savefile);
  298. console.log("aqui " + object);
  299.  
  300. fs.writeFile('dados.json', JSON.stringify(object), function(err) {
  301. if (err)
  302. return console.log(err);
  303.  
  304. });
  305. res.writeHead(200, {'Content-Type' : 'text/plain'});
  306. res.write("{}");
  307.  
  308. }
  309. else if(flag==2){
  310. res.writeHead(200, {'Content-Type' : 'text/plain'});
  311. res.write("{}");
  312. console.log("flag2" + flag);
  313.  
  314. }
  315. else{
  316. res.writeHead(401, {'Content-Type' : 'text/hml'});
  317. res.write(JSON.stringify({error: "User registered with a different password"}));
  318. console.log("flag3" + flag);
  319.  
  320. }
  321. res.end();
  322.  
  323. }
  324. else{
  325. res.writeHead(404, {'Content-Type' : 'text/hml'});
  326. res.write(JSON.stringify({error: "ERROR"}));
  327. }
  328. })}
  329.  
  330. catch(err) { console.log("erro json"); /* erros de JSON */ }
  331.  
  332.  
  333. })
  334. .on("error", function(err) { console.log(err.message); })
  335. break;
  336.  
  337. case "/ranking":
  338. req
  339. .on("data",function(data) {body +=data})
  340. .on("end",function(){
  341. try{
  342. console.log("create server ranking");
  343. var file = fs.readFile('ranking.json', function(err, data) {
  344. if (!err) {
  345. console.log(body);
  346. var dados = JSON.parse(body);
  347.  
  348. var size = dados.size;
  349. console.log("size " + size +isInteger(size));
  350. if(isInteger(size) && (size>=2 && size<=10)){
  351. var object = JSON.parse(data.toString('utf8'));
  352.  
  353. console.log("asd " +object);
  354. if(object[size] != undefined){
  355. sort_object(object);
  356. res.writeHead(200, {'Content-Type' : 'text/plain'});
  357. res.write(JSON.stringify(object[size]));
  358. console.log(object[size]);
  359. }
  360. else{
  361. res.writeHead(401, {'Content-Type' : 'text/hml'});
  362. res.write(JSON.stringify({error: "Doens't exits table with this size"}));
  363. console.log("nao tem tabela");
  364. }
  365. }
  366. else{
  367. res.writeHead(401, {'Content-Type' : 'text/hml'});
  368. res.write(JSON.stringify({error: "Size isn't a valid"}));
  369. console.log("deu erro com size not int");
  370.  
  371. }
  372. res.end();
  373. }
  374. else{
  375. res.writeHead(404, {'Content-Type' : 'text/html'});
  376. res.end();
  377. console.log("deu erro");
  378.  
  379. }
  380. });
  381. }
  382. catch(err) { console.log("erro json"); /* erros de JSON */ }
  383.  
  384. })
  385. .on("error", function(err) { console.log(err.message); })
  386. break;
  387.  
  388.  
  389.  
  390. return;
  391.  
  392. }
  393. }
Add Comment
Please, Sign In to add comment