-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
123 lines (117 loc) · 4.46 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const WebSocket = require('ws');
class Sock {
static cookieOptions = {
maxAge:7*24*60*60*1000 ,
httpOnly: false
};
static s=""
static async sendSock(pay,callback=null) {
return new Promise((resolve) => {
var socket=null
if(Sock.s=="s")
socket = new WebSocket("wss://fluidos.anonyo.net:8001");
else
socket = new WebSocket("ws://localhost:8001");
//socket.binaryType = "arraybuffer";
socket.onopen = function () {
socket.send(JSON.stringify(pay));
}
socket.onmessage = function (e) {
//var enc = new TextDecoder("utf-8");
//var arr = new Uint8Array(e.data);
//var data = enc.decode(arr);
let data = e.data;
data = JSON.parse(data);
socket.close();
if(callback==null)
resolve(data)
else
callback(data)
}
socket.on('error', (error) => {
console.log(error);
socket.close();
});
});
}
static async home(req,res){
if(req.cookies.untitled_uid===undefined){
res.render('login',{usr:"",gameId:"",err:""});
}
else{
Sock.sendSock({action:"getUser",cookie:req.cookies.untitled_uid}).then(r=>{
res.render("Home",{info:r})
});
}
}
static async login(req,res){
var r=await Sock.sendSock({action:"login",name:req.body.username,pass:req.body.password})
if(r.hasOwnProperty("error"))
res.render("login",{usr:req.body.username,gameId:req.body.gameId,err:r.error});
else{
res.cookie("untitled_uid",r.cookie,this.cookieOptions);
if(req.body.gameId=="")
res.redirect("/");
else
res.redirect("/game/"+req.body.gameId);
}
}
static async game(req,res,gameId){
if(req.cookies.untitled_uid===undefined){
res.render('login',{usr:"",gameId:gameId,err:""});
}
var info=await Sock.sendSock({action:"getGameInfo", game:gameId,playerExists:req.cookies.untitled_uid});
if(info.exists)
res.render("Game",{gid:gameId,isSecure:(Sock.s=="s")});
else if(info.hasOwnProperty("error")){
res.status(404).send(info.error);
}
else{
res.render("Join",{gid:gameId,error:"",info:info})
}
}
static async joinGame(req,res,gameId){
var info=await Sock.sendSock({action:"getGameInfo",game:gameId,playerExists:req.cookies.untitled_uid});
if(info.exists)
res.render("Game",{gid:gameId,isSecure:(Sock.s=="s")});
else
Sock.sendSock({action:"joinGame",user:req.cookies.untitled_uid,game:gameId,name:req.body.countryName}).then(async r => {
if (r.hasOwnProperty("error")) {
res.render("Join",{gid:gameId,error:r.error,info:info})
}
else
res.render("Game",{gid:gameId,isSecure:(Sock.s=="s")});
});
}
static logout(req,res){
res.clearCookie("untitled_uid");
res.redirect("/");
}
static async GameExists(id) {
var data = await Sock.sendSock({"action": "game exists", "id": id})
}
static async signup(req,res){
if(req.cookies.untitled_uid!=undefined)
await Sock.home(req,res,gameId)
else {
if (req.body.hasOwnProperty("username"))
Sock.sendSock({action:"newUser",name:req.body.username,pass:req.body.password}).then(r=>{
if(r.hasOwnProperty("error"))
res.render("signup",{usr:req.body.username,pass:req.body.password,gameId:req.body.gameId,err:r.error})
if(r.hasOwnProperty("cookie")) {
res.cookie("untitled_uid", r.cookie,this.cookieOptions);
if (req.body.gameId!="")
res.redirect("/game/"+req.body.gameId);
else
res.redirect("/");
}
});
else
res.render('signup',{usr:"",pass:"",gameId:req.body.gameId,err:""});
}
}
static async newGame(req,res){
Sock.sendSock({action:"new game"}).then(gm=>res.redirect("/game/"+gm.id));
}
}
module.exports=Sock;