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

Commit

Permalink
Add Account State & Player State to Vue Instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk committed Apr 4, 2023
1 parent 9e819c0 commit a06179b
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 48 deletions.
14 changes: 12 additions & 2 deletions src-webviews/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@
:is="page.component"
:id="'page-' + page.name"
:state="state"
:accountState="accountState"
/>
<component
v-for="(page, index) in overlays"
:key="index"
:is="page.component"
:id="'page-' + page.name"
:state="state"
:accountState="accountState"
/>
<component
v-for="(page, index) in persistent"
:key="index"
:is="page.component"
:id="'page-' + page.name"
:state="state"
:accountState="accountState"
/>
</div>
</template>
Expand All @@ -45,6 +48,7 @@ import * as state from '@utility/state';
// Interfaces
import IPageData from './interfaces/IPageData';
import WebViewEvents from '@utility/webViewEvents';
import { Account } from '@AthenaShared/interfaces/iAccount';
const ALL_THE_COMPONENTS = {
...CORE_IMPORTS,
Expand Down Expand Up @@ -74,7 +78,8 @@ export default defineComponent({
pages: [] as Array<IPageData>,
pageBindings: componentsToArray(),
devMode: false,
state: {} as { [key: string]: any },
state: {} as Character,
accountState: {} as Account,
};
},
computed: {
Expand Down Expand Up @@ -147,12 +152,17 @@ export default defineComponent({
localStorage.setItem('pages', JSON.stringify(currentPages));
},
setCharacterState(characterData: Character) {
state.set('data', characterData);
state.set('characterState', characterData);
this.state = characterData;
},
setAccountState(accountData: Account) {
state.set('accountState', accountData);
this.accountState = accountData;
},
},
mounted() {
WebViewEvents.on(SYSTEM_EVENTS.PLAYER_EMIT_STATE, this.setCharacterState);
WebViewEvents.on(SYSTEM_EVENTS.PLAYER_EMIT_ACCOUNT_STATE, this.setAccountState);
// What to show when 'alt' is not present.
// Basically if alt:V isn't running with this page present inside of it.
Expand Down
30 changes: 6 additions & 24 deletions src-webviews/src/pages/stateTest/StateTest.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<template>
<div class="stack" style="color: black">
<div class="stack" style="color: white; text-shadow: 1px 1px black">
<div class="stack">
<div>Health: {{ state.hp ? state.hp : 'Not Defined' }}</div>
<div>Armour: {{ state.armour ? state.armour : 'Not Defined' }}</div>
<div>Random: {{ state.random ? state.random : 'Not Defined' }}</div>
</div>
<div class="stack">
<h2>Delayed Changes...</h2>
<div>Health: {{ hp ? hp : 'Not Defined' }}</div>
<p>{{ state }}</p>
<p>{{ accountState }}</p>
</div>
</div>
</template>
Expand All @@ -29,22 +24,9 @@ export default defineComponent({
type: Object,
required: true,
},
},
watch: {
'state.hp': {
handler(newValue) {
if (Date.now() < this.hpDebounce) {
return;
}
this.hpDebounce = Date.now() + 500;
this.hp = newValue;
},
},
},
methods: {
test() {
this.hpDebounce;
accountState: {
type: Object,
required: true,
},
},
});
Expand Down
16 changes: 14 additions & 2 deletions src-webviews/src/utility/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Character } from '@AthenaShared/interfaces/character';
import { Account } from '@AthenaShared/interfaces/iAccount';

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

Expand Down Expand Up @@ -32,8 +33,19 @@ export function get<T>(key: string): T | undefined {
*
* @export
* @template T
* @return {*} {(T | undefined)}
* @return {(T | undefined)}
*/
export function getCharacterData<T = Character>(): T | undefined {
return state['data'];
return state['characterState'];
}

/**
* Returns account state when synchronized from server.
*
* @export
* @template T
* @return {(T | undefined)}
*/
export function getAccountData<T = Partial<Account>>(): T | undefined {
return state['accountState'];
}
2 changes: 1 addition & 1 deletion src/core/plugins/discord-auth/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import Database from '@stuyk/ezmongodb';
import axios from 'axios';
import { Account } from '@AthenaServer/interface/iAccount';
import { Account } from '@AthenaShared/interfaces/iAccount';
import { DiscordAuthConfig } from './config';
import { DiscordAuthEvents } from '../shared/events';

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/controllers/admin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as alt from 'alt-server';
import Database from '@stuyk/ezmongodb';
import { Account } from '../interface/iAccount';
import { Account } from '../../shared/interfaces/iAccount';
import { Collections } from '../database/collections';
import { LocaleController } from '../../shared/locale/locale';
import { LOCALE_KEYS } from '../../shared/locale/languages/keys';
Expand Down
36 changes: 34 additions & 2 deletions src/core/server/document/accountData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';

import { KnownKeys } from '@AthenaShared/utility/knownKeys';
import { Account } from '@AthenaServer/interface/iAccount';
import Database from '@stuyk/ezmongodb';
import { KnownKeys } from '@AthenaShared/utility/knownKeys';
import { Account } from '@AthenaShared/interfaces/iAccount';
import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';
import { deepCloneObject } from '@AthenaShared/utility/deepCopy';

export type KeyChangeCallback = (player: alt.Player, newValue: any, oldValue: any) => void;

const callbacks: { [key: string]: Array<KeyChangeCallback> } = {};
const cache: { [id: string]: Account } = {};
const DEBUG_MODE = false; // Use this to see what state is being set.

const restrictedFields = ['username', 'password', 'salt', 'hardware', 'ips', 'banned'];

/**
* Removes restricted fields to be passed to the player.
*
* @param {Account} data
* @return {Partial<Account>}
*/
function removeRestrictedFields(data: Account): Partial<Account> {
for (let fieldName of restrictedFields) {
delete data[fieldName];
}

return data;
}

/**
* Binds a player identifier to a Account document.
* This document is cleared on disconnected automatically.
Expand All @@ -34,6 +52,10 @@ export function bind(player: alt.Player, document: Account) {
}

cache[player.id] = document;
try {
const dataCopy = deepCloneObject<Account>(cache[player.id]);
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_ACCOUNT_STATE, removeRestrictedFields(dataCopy));
} catch (err) {}
}

/**
Expand Down Expand Up @@ -129,6 +151,11 @@ export async function set<T = {}, Keys = keyof KnownKeys<Account & T>>(
);
}

try {
const dataCopy = deepCloneObject<Account>(cache[player.id]);
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_ACCOUNT_STATE, removeRestrictedFields(dataCopy));
} catch (err) {}

if (typeof callbacks[typeSafeFieldName] === 'undefined') {
return;
}
Expand Down Expand Up @@ -161,6 +188,11 @@ export async function setBulk<T = {}, Keys = Partial<Account & T>>(player: alt.P
cache[player.id] = Object.assign(cache[player.id], fields);
await Database.updatePartialData(cache[player.id]._id, fields, Athena.database.collections.Accounts);

try {
const dataCopy = deepCloneObject<Account>(cache[player.id]);
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_ACCOUNT_STATE, removeRestrictedFields(dataCopy));
} catch (err) {}

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

cache[player.id] = document;
Athena.webview.emit(player, SYSTEM_EVENTS.PLAYER_EMIT_STATE, cache[player.id]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/getters/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Athena from '@AthenaServer/api';

import Database from '@stuyk/ezmongodb';
import { Character } from '@AthenaShared/interfaces/character';
import { Account } from '@AthenaServer/interface/iAccount';
import { Account } from '@AthenaShared/interfaces/iAccount';
import { OwnedVehicle } from '@AthenaShared/interfaces/vehicleOwned';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/player/setter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as PlayerEvents from '@AthenaServer/player/events';

import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';
import { ActionMenu } from '@AthenaShared/interfaces/actions';
import { Account } from '../interface/iAccount';
import { Account } from '../../shared/interfaces/iAccount';
import { Collections } from '../database/collections';
import { PLAYER_SYNCED_META } from '@AthenaShared/enums/playerSynced';
import Database from '@stuyk/ezmongodb';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/systems/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import Database from '@stuyk/ezmongodb';
import { Account } from '../interface/iAccount';
import { Account } from '../../shared/interfaces/iAccount';
import { Collections } from '../database/collections';

const globalKey = 'accountId';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/systems/jwt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import { Account } from '../interface/iAccount';
import { Account } from '../../shared/interfaces/iAccount';
import nJWT from 'njwt';
import { SYSTEM_EVENTS } from '@AthenaShared/enums/system';

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/systems/permission.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as alt from 'alt-server';
import * as Athena from '@AthenaServer/api';
import { Account } from '@AthenaServer/interface/iAccount';
import { Account } from '@AthenaShared/interfaces/iAccount';
import { Character } from '@AthenaShared/interfaces/character';
import Database from '@stuyk/ezmongodb';

Expand Down
3 changes: 2 additions & 1 deletion src/core/shared/enums/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ 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_EMIT_STATE = 'playerEmit:state:character',
PLAYER_EMIT_ACCOUNT_STATE = 'playerEmit:state:account',
//
PLAYER_SET_FREEZE = 'freeze:Set',
PLAYER_SET_DEATH = 'death:Toggle',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PERMISSIONS } from '../../shared/flags/permissionFlags';

/**
* Used to store Discord Information, IPs, and User Data
*
Expand Down Expand Up @@ -55,14 +53,6 @@ export interface Account {
*/
lastLogin: number;

/**
* What administrative permissions does this account have.
* Default: 0
* @type {PERMISSIONS}
*
*/
permissionLevel: PERMISSIONS;

/**
* A list of permissions assigned to this account.
*
Expand Down

0 comments on commit a06179b

Please sign in to comment.