Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
State Synchronization to WebView
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 3, 2023
1 parent 75aefbb commit 1620176
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Athena.player.appearance.setHeadBlendData
- Athena.player.appearance.setSkin
- Athena.player.appearance.clearSkin
- Added automatic character document synchronization to WebViews
--- Available as a prop, or from the "import * as state from '@utility/state';" export
--------------------------------------
--- Everything Below is Before April 2
Expand Down
11 changes: 10 additions & 1 deletion src-webviews/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ import { CORE_IMPORTS } from './pages/components';
import { PLUGIN_IMPORTS } from './plugins/imports';
import { WebViewEventNames } from '../../src/core/shared/enums/webViewEvents';
import VueDevMenu from './components/VueDevMenu.vue';
import './utility/state';
import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';
import { Character } from '@AthenaShared/interfaces/character';
import * as state from '@utility/state';
// Interfaces
import IPageData from './interfaces/IPageData';
import WebViewEvents from '@utility/webViewEvents';
const ALL_THE_COMPONENTS = {
...CORE_IMPORTS,
Expand Down Expand Up @@ -143,8 +146,14 @@ export default defineComponent({
this.handleSetPages(currentPages, 'pages');
localStorage.setItem('pages', JSON.stringify(currentPages));
},
setCharacterState(characterData: Character) {
state.set('data', characterData);
this.state = characterData;
},
},
mounted() {
WebViewEvents.on(SYSTEM_EVENTS.PLAYER_EMIT_STATE, this.setCharacterState);
// What to show when 'alt' is not present.
// Basically if alt:V isn't running with this page present inside of it.
if (!('alt' in window)) {
Expand Down
13 changes: 13 additions & 0 deletions src-webviews/src/utility/state.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Character } from '@AthenaShared/interfaces/character';

const state: { [key: string]: any } = {};

/**
Expand All @@ -24,3 +26,14 @@ export function set(key: string, value: any) {
export function get<T>(key: string): T | undefined {
return state[key];
}

/**
* Returns character data when synchronized from server.
*
* @export
* @template T
* @return {*} {(T | undefined)}
*/
export function getCharacterData<T = Character>(): T | undefined {
return state['data'];
}
3 changes: 3 additions & 0 deletions src/core/server/document/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import { Character } from '@AthenaShared/interfaces/character';
import { KnownKeys } from '@AthenaShared/utility/knownKeys';
import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';
import Database from '@stuyk/ezmongodb';

export type KeyChangeCallback = (player: alt.Player, newValue: any, oldValue: any) => void;
Expand Down Expand Up @@ -206,6 +207,7 @@ export async function set<T = {}, Keys = keyof KnownKeys<Character & T>>(
);
}

Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_STATE, cache[player.id]);
if (typeof callbacks[typeSafeFieldName] === 'undefined') {
return;
}
Expand Down Expand Up @@ -248,6 +250,7 @@ export async function setBulk<T = {}, Keys = Partial<Character & T>>(player: alt

cache[player.id] = Object.assign(cache[player.id], fields);
await Database.updatePartialData(cache[player.id]._id, fields, Athena.database.collections.Characters);
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_STATE, cache[player.id]);

Object.keys(fields).forEach((key) => {
if (typeof callbacks[key] === 'undefined') {
Expand Down
1 change: 1 addition & 0 deletions src/core/server/systems/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export async function select(player: alt.Player, character: Character) {

Athena.player.emit.fadeScreenFromBlack(player, 2000);
Athena.player.events.trigger('selected-character', player);
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_STATE, data);
}, 500);
}

Expand Down
1 change: 1 addition & 0 deletions src/core/shared/enums/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export enum SYSTEM_EVENTS {
PLAYER_EMIT_TEMP_OBJECT_LERP = 'temp:Object:Lerp',
PLAYER_EMIT_WHEEL_MENU = 'wheelMenu:Dynamic',
PLAYER_EMIT_MISSION_TEXT = 'missionText:Create',
PLAYER_EMIT_STATE = 'playerEmit:state',
//
PLAYER_SET_FREEZE = 'freeze:Set',
PLAYER_SET_DEATH = 'death:Toggle',
Expand Down

0 comments on commit 1620176

Please sign in to comment.