Skip to content

Commit

Permalink
Update package and room versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Dec 8, 2023
1 parent 8aa29da commit e4d3b15
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 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.0",
"version": "3.1.1",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
18 changes: 10 additions & 8 deletions sync-server/src/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Room {
}
}

constructor(private options: RoomOptions) {
constructor(private options: RoomOptions) {
if (options.propagateOldRoom) {
this.propagateOldRoom = options.propagateOldRoom
}
Expand All @@ -121,15 +121,12 @@ export class Room {
let firstJoin = !room.users[user.id]

room.users[user.id] = user

const userProxy = World.users[user.id]['proxy']
userProxy.$state = UserState.Connected

if (firstJoin) {
if (!userProxy._rooms) userProxy._rooms = []
userProxy._rooms.push(room.id)
if (!userProxy.id) userProxy.id = Utils.generateId()
if (room['onJoin']) room['onJoin'](userProxy)
if (room['onJoin']) await Utils.resolveValue(room['onJoin'](userProxy))
}

if (this.getUsersLength(room) == 1) {
Expand All @@ -140,7 +137,7 @@ export class Room {
...this.memoryTotalObject,
join: firstJoin
}, <string>room.id)

await Transmitter.emit(userProxy, packet, room)

return true
Expand Down Expand Up @@ -221,6 +218,11 @@ export class Room {
const valProxy = deepProxy(val, p, genericPath)
proxifiedObjects.add(valProxy);
if (path == 'users') {
if (!room.users[key]) {
if (!valProxy._rooms) valProxy._rooms = []
valProxy._rooms.push(room.id)
if (!valProxy.id) valProxy.id = Utils.generateId()
}
World.users[key]['proxy'] = valProxy
}
Reflect.set(target, key, val, receiver)
Expand Down Expand Up @@ -308,7 +310,7 @@ export class Room {
deleteProperty(target, key) {
const { fullPath: p, infoDict } = getInfoDict(path, key, dictPath)
//target[key]._isDeleted = true

Reflect.deleteProperty(target, key)
if (infoDict) self.detectChanges(room, null, p)
return true
Expand Down
51 changes: 42 additions & 9 deletions sync-server/tests/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { beforeEach, test, expect, afterEach, vi } from 'vitest'
let event, socket

const CLIENT_ID = 'mock'
let auth = vi.fn()

beforeEach(() => {
auth.mockReturnValue(CLIENT_ID)
World.transport(MockSocketIo.serverIo, {
auth() {
return CLIENT_ID
}
auth
})
socket = new MockSocketIo.ClientIo(CLIENT_ID)
socket.connection()
Expand Down Expand Up @@ -83,7 +83,7 @@ test('Change Schema', () => {
case 1:
expect(value.count).toBe(0)
break;
case 2:
case 3:
expect(value.countAsync).toEqual(1)
resolve()
break;
Expand All @@ -102,7 +102,6 @@ test('Change Schema', () => {
})
})


test('Change Room', () => {
return new Promise(async (resolve: any) => {
let send = 0
Expand Down Expand Up @@ -189,23 +188,22 @@ test('Old Proxy, after change Room, propagate values', () => {
})

let user

await World.joinRoom(room1.id, CLIENT_ID)
user = World.getUser(CLIENT_ID)
user.position = { x: 10, y: 10 }
await World.send()

await World.leaveRoom(room1.id, CLIENT_ID)


await World.joinRoom(room2.id, CLIENT_ID)
user.position.x = 20
user.position.y = 20
await World.send()
})
})


test('Disconnect', async () => {
class Room {
$schema = {
Expand All @@ -228,7 +226,7 @@ test('Disconnect', async () => {
})

let user

await World.joinRoom(room1.id, CLIENT_ID)
user = World.getUser(CLIENT_ID)
user.position = { x: 10, y: 10 }
Expand All @@ -243,7 +241,42 @@ test('Disconnect', async () => {

expect(watch).toHaveBeenCalledTimes(2)
expect(changes).toHaveBeenLastCalledWith({})

})

test('Multi User: view client side', async () => {
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 watch = vi.fn()
socket2.on('w', watch)

class Room {}
const room = World.addRoom('room', Room, {
propagateOldRoom: false
})
await World.joinRoom('room', firstPlayerId)
await World.joinRoom('room', secondPlayerId)

expect(room.users[firstPlayerId]).toBeDefined()
expect(room.users[secondPlayerId]).toBeDefined()

await World.send()

expect(watch).toHaveBeenCalledTimes(2)
expect(watch).toHaveBeenLastCalledWith([
'room',
expect.any(Number),
{ users: { [firstPlayerId]: expect.any(Object), [secondPlayerId]: expect.any(Object) } },
], undefined)
})

afterEach(() => {
Expand Down

0 comments on commit e4d3b15

Please sign in to comment.