forked from TomOnderwater/RPG-IO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
162 lines (137 loc) · 4.14 KB
/
app.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
158
const express = require("express")
const app = express()
let path = require("path")
const port = 3000
const expressWS = require('express-ws')(app)
const bodyParser = require("body-parser")
var dogsArr = []
app.use(bodyParser.json())
const GameMaster = require("./gamemaster.js")
const AccountManager = require("./accounts.js")
const gameMaster = new GameMaster()
//const dungeon = new Dungeon(1) // floor count
runGames()
checkConnections()
let accountmanager = new AccountManager()
app.use(express.static("public"))
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/public/index.html"))
})
// MONITOR INTEGRATION
app.get("/monitor", (req, res) => {
res.sendFile(path.join(__dirname + "/public/monitor.html"))
})
app.get("/controller", (req, res) => {
res.sendFile(path.join(__dirname + "/public/controller.html"))
})
// TESTING
app.get("/testing", (req, res) => {
res.sendFile(path.join(__dirname + "/public/testing.html"))
})
app.post("/getLevel", (req, res) => {
let connection = req.body
let level = gameMaster.getLevelData(connection)
res.send({level})
})
app.post("/continue", (req, res) => {
let data = req.body
let id = 'none'
let player = gameMaster.getPlayer(data)
if (player) id = player.id
res.send({id})
})
app.post("/start", (req, res) => {
//console.log(req)
// contains a key
let connection = req.body
connection.key = connection.key.toLowerCase()
console.log('start', connection.name, 'in dungeon: ', connection.key)
let game = gameMaster.addPlayer(connection)
accountmanager.updateName(connection.session, connection.name) //update latest name
accountmanager.updateKey(connection.session, connection.key)
res.send(game)
})
app.post("/sess_id", (req, res) => {
let connection = req.body
console.log('session: ', connection.session)
//check if the account is alive
let player = gameMaster.getPlayerBySession(connection)
console.log('active player:', player)
let id = 'none'
if (player) id = player.id
// get account details
let account = accountmanager.getAccount(connection.session)
console.log('account: ', account)
res.send({id, account})
})
const server = app.listen(port, () => {
console.log(`rpg-io listening at http://localhost:${port}`)
})
function checkConnections()
{
gameMaster.checkConnections()
setTimeout(checkConnections, 1000)
}
function runGames()
{
gameMaster.update()
//console.log('update')
setTimeout(runGames, 34)
}
// identification is done via game id
app.ws('/gamestream', (ws, req) => {
//console.log('new client connected', ws)
// get player
//console.log('data: ', req.body)
//let player = dungeon.getPlayer()
//clients.push(new Connection(ws, ))
ws.onmessage = (body) =>
{
let msg = JSON.parse(body.data)
//get the key of the message
let key = msg.key
let id = msg.id
let type = msg.type
//console.log(msg)
let connection = {socket: ws, key, id, type}
switch(msg.code)
{
case 'input':
gameMaster.updateInput(msg)
// console.log(viewport)
//ws.send(viewport)
break
case 'ready':
if (type === 'spectator')
{
let game = gameMaster.addDungeon()
connection.key = game.key
connection.id = 'spectator'
if (gameMaster.manageConnections(connection))
{
//console.log('connection:', connection)
//console.log(gameMaster.getLevelData(connection).width)
ws.send(JSON.stringify({type: 'spectator',
key: connection.key,
level: gameMaster.getLevelData(connection)}))
}
break
}
gameMaster.startPlayer(connection)
// check if the player is still alive
if (gameMaster.manageConnections(connection))
ws.send(JSON.stringify({
type: 'start',
id: connection.id, key: connection.key,
level: gameMaster.getLevelData(connection)
}))
break
case 'level':
ws.send(JSON.stringify({type: 'view',
level: gameMaster.getLevelData(connection)}))
default:
//do nothing
break
}
}
})