diff --git a/.gitignore b/.gitignore index 3ba18693..f940fc70 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ docs/others/* !docs/api/readme.md !docs/others/readme.md cache -packages/**/browser \ No newline at end of file +packages/**/browser +vitest.config.ts.timestamp* \ No newline at end of file diff --git a/packages/sample2/main/gui/test.tsx b/packages/sample2/main/gui/test.tsx index 5cafe48f..32d09ce0 100644 --- a/packages/sample2/main/gui/test.tsx +++ b/packages/sample2/main/gui/test.tsx @@ -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 } \ No newline at end of file diff --git a/packages/sample2/main/player.ts b/packages/sample2/main/player.ts index a1ed2397..fa3c7a45 100644 --- a/packages/sample2/main/player.ts +++ b/packages/sample2/main/player.ts @@ -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) { @@ -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); } } diff --git a/packages/server/package-lock.json b/packages/server/package-lock.json index 6e2d0df0..25f53b29 100644 --- a/packages/server/package-lock.json +++ b/packages/server/package-lock.json @@ -20,7 +20,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" }, "devDependencies": { @@ -6881,9 +6881,9 @@ } }, "node_modules/simple-room": { - "version": "3.0.6", - "resolved": "https://registry.npmjs.org/simple-room/-/simple-room-3.0.6.tgz", - "integrity": "sha512-2Gz7QY3ZDFCW0Zef2UAdcCGv639yJhnjgdIMz28NAMMTBjVyHXFDLBfB2qkVIXtRwKqTS1ezBK+mbvaLTOiLGA==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/simple-room/-/simple-room-3.1.0.tgz", + "integrity": "sha512-cjxNHEs2q9FmWNssrgPI8W92oaDOO+f4wUwF4gNDcPcIc7r6kMD6zPgfjzx7G2dIKQRvcKLHEAjm51xPYlqY9w==", "dependencies": { "@google-cloud/agones-sdk": "^1.35.0", "msgpack-lite": "0.1.26", @@ -14067,9 +14067,9 @@ "dev": true }, "simple-room": { - "version": "3.0.6", - "resolved": "https://registry.npmjs.org/simple-room/-/simple-room-3.0.6.tgz", - "integrity": "sha512-2Gz7QY3ZDFCW0Zef2UAdcCGv639yJhnjgdIMz28NAMMTBjVyHXFDLBfB2qkVIXtRwKqTS1ezBK+mbvaLTOiLGA==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/simple-room/-/simple-room-3.1.0.tgz", + "integrity": "sha512-cjxNHEs2q9FmWNssrgPI8W92oaDOO+f4wUwF4gNDcPcIc7r6kMD6zPgfjzx7G2dIKQRvcKLHEAjm51xPYlqY9w==", "requires": { "@google-cloud/agones-sdk": "^1.35.0", "msgpack-lite": "0.1.26", diff --git a/packages/server/package.json b/packages/server/package.json index 7cf8200f..de4d1109 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -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", diff --git a/tests/unit-tests/specs/item.spec.ts b/tests/unit-tests/specs/item.spec.ts index 31ac489c..1f4ab8b2 100644 --- a/tests/unit-tests/specs/item.spec.ts +++ b/tests/unit-tests/specs/item.spec.ts @@ -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 diff --git a/tests/unit-tests/specs/sync.spec.ts b/tests/unit-tests/specs/sync.spec.ts new file mode 100644 index 00000000..4bbde3d9 --- /dev/null +++ b/tests/unit-tests/specs/sync.spec.ts @@ -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({ + 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() +}) \ No newline at end of file