Skip to content

Commit

Permalink
fix #273 #community-244
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Dec 8, 2023
1 parent 4ddccd3 commit a617bfd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ docs/others/*
!docs/api/readme.md
!docs/others/readme.md
cache
packages/**/browser
packages/**/browser
vitest.config.ts.timestamp*
13 changes: 10 additions & 3 deletions packages/sample2/main/gui/test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { room } from '@rpgjs/client'
import { RpgReactContext, useObjects, useCurrentPlayer } from '@rpgjs/client/react'
import { useContext } from 'react'
import { useContext, useEffect } from 'react'

export default function Test({ gold }) {
const player = useCurrentPlayer()
return <>{player?.position?.x}</>
const { rpgCurrentPlayer } = useContext(RpgReactContext)

useEffect(() => {
rpgCurrentPlayer.subscribe(({ object }) => {
console.log('frontend', object.items);
})
}, [])

return <>test</>
}
9 changes: 6 additions & 3 deletions packages/sample2/main/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { RpgMap } from '@rpgjs/server';
import { Speed } from '@rpgjs/server';
import { RpgPlayer, RpgPlayerHooks, Control, Components, RpgEvent, EventData } from '@rpgjs/server'
import Potion from './database/items/Potion';
import CharaEvent from './events/npc';

const player: RpgPlayerHooks = {
onConnected(player: RpgPlayer) {
Expand All @@ -26,8 +25,12 @@ const player: RpgPlayerHooks = {
player.callMainMenu()
}
},
async onJoinMap(player: RpgPlayer, map: RpgMap) {
player.save();
async onJoinMap(player: RpgPlayer) {
player.gui('test').open();

setTimeout(() => {
player.addItem(Potion, 1);
}, 5000);
}
}

Expand Down
14 changes: 7 additions & 7 deletions packages/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 packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"pretty-error": "^4.0.0",
"rxjs": "7.8.0",
"sat": "^0.9.0",
"simple-room": "^3.0.6",
"simple-room": "^3.1.0",
"socket.io": "^4.6.1"
},
"gitHead": "5abe6ca78be96524d74a052a230f2315c900ddee",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/specs/item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Potion, Key } from './fixtures/item'
import { _beforeEach } from './beforeEach'
import { clear } from '@rpgjs/testing'
import { Item } from '@rpgjs/database'
import { RpgPlayer } from '@rpgjs/server'
import { RpgPlayer, RpgMap, MapData } from '@rpgjs/server'
import { beforeEach, test, afterEach, expect, describe, vi } from 'vitest'

let client, fixture, playerId, server
Expand Down
67 changes: 67 additions & 0 deletions tests/unit-tests/specs/sync.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { _beforeEach } from './beforeEach'
import { clear } from '@rpgjs/testing'
import { RpgPlayer, RpgMap, MapData, RpgModule, RpgServer } from '@rpgjs/server'
import { beforeEach, test, afterEach, expect, describe, vi } from 'vitest'

let client, fixture, playerId, server
let player: RpgPlayer

beforeEach(async () => {
const ret = await _beforeEach()
client = ret.client
player = ret.player
fixture = ret.fixture
server = ret.server
playerId = ret.playerId
})

describe('Test props', () => {
test('sync props', async () => {
clear()

@RpgModule<RpgServer>({
player: {
props: {
bronze: Number
},
onJoinMap(player, map) {
player['bronze'] = 100
},
}
})
class RpgServerModule {}

const { server, client } = await _beforeEach([{
server: RpgServerModule
}])

await server.send()

expect(client.player.bronze).toEqual(100)
})
})

// https://community.rpgjs.dev/d/244-playeradditem-bug
test('change map before, sync player', async () => {
const sceneMap = server.sceneMap

@MapData({
id: 'myid',
file: require('./fixtures/maps/map.tmx')
})
class SampleMap extends RpgMap { }

sceneMap.createDynamicMap(SampleMap)

await player.changeMap('myid')

player.gold += 100

await server.send()

expect(client.player.gold).toEqual(player.gold)
})

afterEach(() => {
clear()
})

0 comments on commit a617bfd

Please sign in to comment.