-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (136 loc) · 4.03 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
150
151
152
153
154
155
156
157
"use strict";
const gameChecksum = ""
const fs = require('fs')
let serverDir = './serverData'
if (!fs.existsSync(serverDir)) {
fs.mkdirSync(serverDir)
}
//set up CLI
const consoleManager = require("./modules/console.js")
global.CLI = new consoleManager.cli(require('./modules/commands.js').commands)
//set up player database
const playerDatabase = require("./modules/database.js")
global.PlayerDatabase = new playerDatabase()
PlayerDatabase.initializeTable(function() {
CLI.prompt()//start CLI
})
//serve web page
const express = require('express')
const app = express()
const PORT = process.env['PORT']
app.use(express.static('web'))
const webServer = app.listen(PORT, function() {
CLI.printLine("Started Rogues Server")
})
//listen to the webserver's connection
const socketio = require('socket.io')
const io = socketio(webServer)
//load in server functions
const { accountEvents, gameEvents, matchMaking } = require("./modules/serverFunctions.js")
global.GlobalServerInfo = {
username: {
min: 3,
max: 7
},
password: {
min: 6,
max: 24
},
motd: "",
roomCode: {
max: 6
}
}
//server globals
global.allowSignups = true
global.matchServers = true
global.concurrentUsers = 0
global.concurrentOnlineUsers = 0
global.connectionsLimit = 100
global.runningRooms = {}
//var lastRequest = null
//global.updateLastRequest = function() {lastRequest = Date.now()}
global.spamStop = false
//matchmaker
setInterval(() => {
let matchMakers = []
let currentSockets = Array.from(io.sockets.sockets)
for (let socket of currentSockets) {
let socketObject = socket[1]
if (socketObject.matchmake == true) {
matchMakers.push(socketObject)
}
}
for (let i = matchMakers.length - 1; i > 0; i--) {
if (matchMakers.length >= 2) {
let player = io.sockets.sockets.get(matchMakers[i].id)
player.matchmake = false
matchMakers.splice(i, 1)
let opponentIndex = Math.floor(Math.random(matchMakers.length))
let opponent = matchMakers[opponentIndex]
opponent.matchmake = false
matchMakers.splice(opponentIndex, 1)
i--
matchMaking["createRoom"](null, io, player)
let roomCode = player.id.substring(0, 6).toUpperCase()
matchMaking["joinRoom"]({room: roomCode}, io, opponent)
let room = io.sockets.adapter.rooms.get(roomCode)
room.competitive = true//the elo of the players will be affected
} else {
//not enough players to matchmake
return
}
}
}, 1000)
io.on('connection', function(socket) {
//limit connections to protect server
if (io.engine.clientsCount > connectionsLimit) {
socket.emit("blocked", { code: "maxOnlineUsers" })
socket.disconnect()
return
}
//they are not signed in
socket.authorised = null
socket.matchmake = false
//cooldowns
socket.accountCooldown = {
timer: setInterval(function() {
if (socket.accountCooldown.time > 0) {
socket.accountCooldown.time -= 100
}
}, 100),
time: 0
}
socket.valueChecker = setInterval(() => {
if (socket.authorised == null) {socket.matchmake = false}
}, 100)
CLI.printLine("connected to " + socket.id)
concurrentUsers++
//handshake
socket.emit("globalServerInfo", GlobalServerInfo)
//disconnect
socket.once('disconnecting', (reason) => {
CLI.printLine(socket.id + " disconnected: " + reason)
if (socket.authorised != null) {
accountEvents.signOut({ ID: socket.authorised }, io, socket)
}
concurrentUsers--
})
//when packets happen
let accountMethodNames = Object.keys(accountEvents)
for (let accountAction of accountMethodNames) {
socket.on(accountAction, (data) => {
accountEvents[accountAction](data, io, socket)
})
}
let matchmakingNames = Object.keys(matchMaking)
for (let matchAction of matchmakingNames) {
socket.on(matchAction, (data) => {
matchMaking[matchAction](data, io, socket)
})
}
let gameMethodNames = Object.keys(gameEvents)
for (let gameAction of gameMethodNames) {
socket.on(gameAction, (data) => { gameEvents[gameAction](data, io, socket) })
}
})