Skip to content

Commit

Permalink
fix: World send bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Jan 15, 2024
1 parent e4d3b15 commit 2301a4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sync-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sync-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-room",
"version": "3.1.1",
"version": "3.1.2",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion sync-server/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class WorldClass {
for (let [_, room] of this.rooms) {
const obj = room.$currentState()
if (Object.keys(obj).length == 0) {
return
continue
}
Transmitter.addPacket(room, obj)
for (let id in room.users) {
Expand Down
51 changes: 49 additions & 2 deletions sync-server/tests/room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { World } from '../src/world'
import { Transmitter } from '../src/transmitter'
import MockSocketIo from '../src/testing/mock-socket'
import { testSend } from './fixture'
import { beforeEach, test, expect, afterEach } from 'vitest'
import { beforeEach, test, expect, afterEach, vi } from 'vitest'

let auth = vi.fn()

beforeEach(() => {
World.transport(MockSocketIo.serverIo)
World.transport(MockSocketIo.serverIo, {
auth
})
Transmitter.encode = false
})

Expand Down Expand Up @@ -206,6 +210,49 @@ test('change current state', () => {
})
})

test('World send: All Rooms', async () => {
class Room {
$schema = {
test: String
}
test = 'aa'
}

const firstPlayerId = 'first'
const secondPlayerId = 'second'

auth.mockReturnValue(firstPlayerId)
const socket1 = new MockSocketIo.ClientIo(firstPlayerId)
await socket1.connection()

auth.mockReturnValue(secondPlayerId)
const socket2 = new MockSocketIo.ClientIo(secondPlayerId)
await socket2.connection()

const room1 = World.addRoom('room1', new Room())
const room2 = World.addRoom('room2', new Room())

const watch = vi.fn()
socket2.on('w', watch)

//await World.joinRoom('room1', firstPlayerId)
await World.joinRoom('room2', secondPlayerId)

await World.send()

room2.test = 'bb'

await World.send()

expect(watch).toHaveBeenCalledTimes(3)
expect(watch).toHaveBeenLastCalledWith([
'room2',
expect.any(Number),
{ test: 'bb' },
], undefined)

})

afterEach(() => {
World.clear()
})
Expand Down

0 comments on commit 2301a4e

Please sign in to comment.