Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/core/game.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { CommandType, FactoryKeys, GameEvent } from "@enums";
import type { IGame, IGameOptions, IEventInfo, ISnapshot, ICommand, IInitGameOptions, IPlugin, IGlobalStateChangedData, IObjectDeletedOrCreatedData, IEntityTagsChangedData, ICommandBlocked } from "@interfaces";
import type {
IGame,
IGameOptions,
IEventInfo,
ISnapshot,
ICommand,
IInitGameOptions,
IPlugin,
IGlobalStateChangedData,
IObjectDeletedOrCreatedData,
IEntityTagsChangedData,
ICommandBlocked
} from "@interfaces";
import { EntityManager, UndoManager } from "@core";
import type {
EventCallback,
Expand All @@ -22,8 +34,29 @@ import type {
import { BASE_FPS, BASE_MAX_COMMAND_EXECUTING_ON_TICK_LIMIT, isServer } from "@const";
import { BluePrintsFactory, EffectFactory, IteractionsFactory, QuestsFactory, SoundsFactory } from "@factories";
import { GlobalStore } from "@store";
import { baseChecksMiddleware, DropItemGuard, EntityInteractGuard, EquipItemGuard, MovementGuard, OpenChestGuard, PickUpGuard, ShootGuard, UseItemGuard } from "@middlewares";
import { createPluginProto, extractMethodFromPlugin, extractPropertyFromPlugin, useLink, useValidation, useVisibility, useAttack, useAsyncState, useServer, useState } from "@utils";
import {
baseChecksMiddleware,
DropItemGuard,
EntityInteractGuard,
EquipItemGuard,
MovementGuard,
OpenChestGuard,
PickUpGuard,
ShootGuard,
UseItemGuard
} from "@middlewares";
import {
createPluginProto,
extractMethodFromPlugin,
extractPropertyFromPlugin,
useLink,
useValidation,
useVisibility,
useAttack,
useAsyncState,
useServer,
useState
} from "@utils";
import type { Entity, GameObject } from "@world";
import { ConflictResolverPlugin } from "@plugins";

Expand Down Expand Up @@ -467,7 +500,7 @@ export class Game implements IGame {
const snapshot = {
entities: Array.from(this.options.manager.entities.values()).map((e) => e.toDTO()),
objects: Array.from(this.options.map.objects.values()).map((o) => o.toDTO()),
state: Object.fromEntries(this.options.store.state)
state: this.options.store.getAll()
}

if (cb) cb(snapshot)
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/store/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export interface IGlobalStore {
* @returns { T | undefined } - Data if founded, else undefined
*/
readonly get: <T>(key: string) => T | undefined;

/**
* Get all global state key-value
* @returns { CommandContext } - Key-value state
*/
readonly getAll: () => CommandContext;
}

export interface IGlobalStoreOptions {
Expand Down
7 changes: 5 additions & 2 deletions src/store/global-store.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { IGlobalStateChangedData, IGlobalStore, IGlobalStoreOptions } from "@interfaces";

export class GlobalStore implements IGlobalStore {
public state = new Map<string, any>()

private readonly state = new Map<string, any>()
private readonly options: IGlobalStoreOptions;

public constructor(options: IGlobalStoreOptions) {
Expand Down Expand Up @@ -36,4 +35,8 @@ export class GlobalStore implements IGlobalStore {
public get<T = any>(key: string) {
return this.state.get(key) as T
}

public getAll() {
return Object.fromEntries(this.state.entries())
}
}
14 changes: 14 additions & 0 deletions src/types/hooks/state-callback.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/**
* Type defines callback with preview state, returns new state
*/
export type StateCallback<T = any> = (state: T) => T

/**
* State setter callback type, provide prev state into callback,
* accept new state
*/
export type StateSetterCallback<T = any> = (cb: StateCallback<T>) => T

/**
* State type, returns array of object with current value state,
* function to update state,
* function to unSub from state
*/
export type State<T = any> = [{ readonly value: T }, ((value: T) => T) & StateSetterCallback<T>, VoidFunction]
Loading