forked from codergautam/spicywar.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (129 loc) · 3.75 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const express = require("express");
var http = require("http");
require("dotenv").config();
const app = express();
var process = require("process");
app.use(express.json());
var server = http.createServer(app);
const io = require("./helpers/io").init(server);
const Player = require("./classes/Player");
const Room = require("./classes/Room");
const roomlist = require("./helpers/roomlist");
const scan = require("./helpers/scan");
app.use("/", express.static(__dirname + "/dist"));
app.use("/", express.static(__dirname+"/public"));
const axios = require("axios");
const recaptcha = true;
// require("dotenv").config()
// app.get("/assets/videos/intro.mp4", (req, res, next) => {
// setTimeout(() => {
// next();
// }, 1000);
// })
app.use("/assets", express.static(__dirname+"/assets"));
roomlist.setRoom(new Room())
app.get("/teams", (req, res) => {
// console.log("GET /teams");
// setTimeout(() => {
var room = roomlist.getAllRooms()[0];
var redPlayers = 0;
var bluePlayers = 0;
[...room.players.values()].forEach(player => {
if(player.team == "red") redPlayers++;
else if(player.team == "blue") bluePlayers++;
});
res.send({
red: {playerCount: redPlayers},
blue: {playerCount: bluePlayers}
});
// }, 1000)
})
io.on("connection", async (socket) => {
console.log("fghfggh")
socket.on("go", (name, team, mouseMove, thetoken) => {
if(!name || typeof name != "string") return;
name = name.trim();
if(scan(name).contains) {
name = "*".repeat(name.length);
}
if(!thetoken) return socket.disconnect();
const joinThemIn = () => {
if(name.length == 0) return socket.disconnect();
if(!team || typeof team != "string") team = "red";
if(team != "red" && team != "blue") team = "red";
name = name.substring(0,16);
var player = new Player(name, socket.id, socket, mouseMove);
player.team = team;
roomlist.getAllRooms()[0].addPlayer(player);
console.log(socket.id +" joined!")
}
if(thetoken == process.env.bot) return joinThemIn();
else {
var send = {
secret: process.env.captchaserver,
response: thetoken,
};
axios
.post(
"https://www.google.com/recaptcha/api/siteverify?" +
new URLSearchParams(send)
)
.then(async (f) => {
f = f.data;
if (!f.success) {
console.log("Captcha failed " + f["error-codes"].toString());
if(!recaptcha) joinThemIn();
return;
}
if (f.score < 0.3) {
console.log("Captcha score too low");
return;
}
joinThemIn();
});
}
});
socket.on("controller", (controller) => {
var room = roomlist.getRoomByPlayerId(socket.id);
if(!room) return;
room.playerControllerUpdate(socket.id, controller);
});
socket.on("mouse", (mouseAngle, distance, needsFlip) => {
var room = roomlist.getRoomByPlayerId(socket.id);
if(!room) return;
room.playerMouseUpdate(socket.id, mouseAngle, distance, needsFlip);
});
socket.on("down", (down) => {
var room = roomlist.getRoomByPlayerId(socket.id);
if(!room) return;
room.playerDown(socket.id, down);
})
socket.on("ping", (fn) => {
fn(); // Simply execute the callback on the client
})
socket.on("disconnect", async (why) => {
var room = roomlist.getRoomByPlayerId(socket.id);
if(room) {
room.removePlayer(socket.id);
}
console.log(socket.id +" disconnected because "+why);
});
});
//tick rooms
var tps = 0;
var actps = 0;
var secondStart = Date.now();
setInterval(() => {
roomlist.tickAll();
tps++;
if(Date.now() - secondStart > 1000) {
// console.log("tps: " + tps);
actps = tps;
// console.log("TPS", actps)
tps = 0;
secondStart = Date.now();
}
}, 1000/30);
server.listen(process.env.PORT || 3000, () => {
console.log("server started");
});