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
6 changes: 5 additions & 1 deletion src/core/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ import {
useServer,
useState,
useWO,
useQuestion
useQuestion,
useAlternative
} from "@utils";
import type { Entity, GameObject } from "@world";
import { ConflictResolverPlugin } from "@plugins";
Expand Down Expand Up @@ -424,6 +425,9 @@ export class Game implements IGame {
useState.prototype.game = this
useWO.prototype.game = this
useQuestion.prototype.game = this

if (!useAlternative.prototype) useAlternative.prototype = { game: this }
else useAlternative.prototype.game = this
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from "./use-async-state.hook.js"
export * from "./use-server.hook.js"
export * from "./use-state.hook.js"
export * from "./use-wo.hook.js"
export * from "./use-question.hook.js"
export * from "./use-question.hook.js"
export * from "./use-alternative.hook.js"
21 changes: 21 additions & 0 deletions src/utils/hooks/use-alternative.hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Game } from "@core";
import type { ICommand } from "@interfaces";
import { AsyncPlugin } from "@plugins";

/**
* This hook allows you to client-side prediction command
* Command will be executed in alternative game
* @param command - Command to execute in alternative world
* @param core - Game reference if hydration disabled
* @returns { Promise<Game> } - Alternative isolated game (not affect real)
*/
export async function useAlternative(command: ICommand, core?: Game): Promise<Game> {
const game = useAlternative.prototype.game as Game || core

const asyncPlugin = new AsyncPlugin()
const alternative = Game.fromSnapshot(game.save(), alternative => alternative.registerPlugin(asyncPlugin))

await asyncPlugin.asyncExecute(command)

return alternative
}
12 changes: 11 additions & 1 deletion test/gametest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BASE_SEARCH_RADIUS, USE_VALIDATION_EVENT_PREFIX, USE_VISIBILITY_EVENT }
import { BluePrintsFactory, EffectFactory, IteractionsFactory, QuestsFactory, SoundsFactory } from "@factories";
import { loggerMiddleware } from "@middlewares";
import { RegenerationPlugin, NetworkPlguin, AsyncPlugin, GraphicPlugin } from "@plugins";
import { useVisibility, checkTwoPositions, useValidation, useLink, useAsyncState, useServer, useState, useWO, useQuestion } from "@utils";
import { useVisibility, checkTwoPositions, useValidation, useLink, useAsyncState, useServer, useState, useWO, useQuestion, useAlternative } from "@utils";

const [game, manager, map] = createGame({
usingEntityMiddlewares: true,
Expand Down Expand Up @@ -417,6 +417,16 @@ async function checkAsyncPlugin() {
console.log(game.options.store.get('async'), 'ASYNC RESULT')
}

console.log((await useAlternative({
type: CommandType.SET_STATE,
tick: game.currentTick,
data: {
key: 'alternative',
value: true
}
})).options.store.get('alternative'), 'ALTERNATIVE')
console.log(game.options.store.get('alternative'))

checkAsyncPlugin()

game.options.store.set('isNight', false)
Expand Down
Loading