-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (102 loc) · 3.23 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
const { spawn } = require('child_process')
const fetch = require('node-fetch')
const config = require('./config.json')
var StreamSplitter = require('stream-splitter')
const WebSocket = require('ws')
const jwt = require('jsonwebtoken')
const quake = spawn('node', ['build/ioq3ded.js', '+set', 'fs_game', 'baseq3', '+set', 'dedicated', '1', '+exec', 'server.cfg'])
var playerList = {}
const ws = new WebSocket(`ws://${config.baseUrl}/server`)
function makeJWT () {
const payload = {
'source': 'ILDM'
}
const token = jwt.sign(payload, config.secret, {
algorithm: 'HS256',
expiresIn: 3600,
issuer: 'ILDeathmatch'
})
return token
}
ws.on('open', function open () {
ws.send('server ws connected.')
})
ws.on('message', (message) => {
console.log(message)
const parse = JSON.parse(message)
const msg = parse.msg || ''
if (msg.includes('--ILDM_CONNECT_CLIENT')) {
fetch(`http://${config.baseUrl}/game/spawn/` + parse.id, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ jwt: makeJWT() })
}).then(res => {
console.log(res.status)
if (res.status !== 200) {
console.log('kick player for having no balance/pointer')
const kickPlayer = 'kick ' + parse.index + '\n'
quake.stdin.write(kickPlayer)
} else {
if (playerList[parse.index] === 'await') {
playerList[parse.index] = parse.id
}
}
})
}
})
var quakeErr = quake.stderr.pipe(StreamSplitter('\n'))
quakeErr.encoding = 'utf-8'
quakeErr.on('token', token => {
console.log('TOKEN: ', token)
if (token.includes('ClientBegin:')) {
const client = token.split(':').map(e => e.trim())[1]
const telluser = 'tell ' + client + ' --ILDM_CONNECT ' + client + '\n'
playerList[client] = 'await'
quake.stdin.write(telluser)
}
if (token.includes('ClientDisconnect:')) {
const client = token.split(':').map(e => e.trim())[1]
console.log('Disconnected: ', playerList[client])
fetch(`http://${config.baseUrl}/game/disconnect/` + playerList[client], {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({jwt: makeJWT()})
})
delete playerList[client]
}
if (token.includes('Kill:')) {
const players = token.split(' ').map(e => e.trim())
const killer = players[1]
const killed = players[2]
console.log(`Player ${killer} killed Player ${killed}`)
if (killer !== '1022' && killer !== killed) {
fetch(`http://${config.baseUrl}/game/kill/` + playerList[killer], {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({jwt: makeJWT()})
}).then(res => {
console.log(res.status)
})
}
fetch(`http://${config.baseUrl}/game/killed/` + playerList[killed], {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({jwt: makeJWT()})
}).then(res => {
console.log(res.status)
if (res.status !== 200) {
console.log('kick player for having no balance/pointer')
const kickPlayer = 'kick ' + killed + '\n'
quake.stdin.write(kickPlayer)
}
})
}
})