-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameLogic.js
48 lines (38 loc) · 1.2 KB
/
gameLogic.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
import mongoose from 'mongoose'
import { Server } from 'socket.io'
import Entity from './models/Entity.js'
const setupGameLogic = (server) => {
const io = new Server(server)
io.on('connection', (socket) => {
console.log('New player connected')
socket.on('playerAction', async (action) => {
console.log(`Received action: ${JSON.stringify(action)}`)
if (!mongoose.Types.ObjectId.isValid(action.entityId)) {
console.log(`Invalid entity ID: ${action.entityId}`)
return
}
const entity = await Entity.findById(action.entityId)
if (!entity) {
console.log(`Entity not found for ID: ${action.entityId}`)
return
}
console.log(`Entity found: ${entity}`)
entity.position = action.newPosition
await entity.save()
io.emit('gameState', { entities: await Entity.find() })
})
socket.on('disconnect', () => {
console.log('Player disconnected')
})
})
simulateWorld()
}
const simulateWorld = async () => {
const entities = await Entity.find()
for (const entity of entities) {
entity.attributes.energy -= 1
await entity.save()
}
setTimeout(simulateWorld, 1000)
}
export default setupGameLogic