Skip to content

Commit

Permalink
feat: add mechanism to automatically load items while executing commands
Browse files Browse the repository at this point in the history
  • Loading branch information
messi-yang committed Jul 7, 2024
1 parent 1542dd8 commit 37f4e6a
Show file tree
Hide file tree
Showing 29 changed files with 184 additions and 215 deletions.
1 change: 0 additions & 1 deletion src/adapters/apis/world-journey-api/command-dtos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ export const toCommandDto = (sourceCommand: Command) => {
playerId: command.getPlayerId(),
itemId: command.getItemId(),
}),
[CommandNameEnum.AddItem]: () => null,
[CommandNameEnum.AddPlayer]: () => null,
[CommandNameEnum.RemovePlayer]: () => null,
});
Expand Down
5 changes: 2 additions & 3 deletions src/contexts/world-journey-service-context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { WorldJourneyService } from '@/services/world-journey-service';
import { PositionVo } from '@/models/world/common/position-vo';
import { PortalUnitModel } from '@/models/world/unit/portal-unit-model';
import { PlayerActionVo } from '@/models/world/player/player-action-vo';
import { AddItemCommand } from '@/services/world-journey-service/managers/command-manager/commands/add-item-command';
import { SendPlayerIntoPortalCommand } from '@/services/world-journey-service/managers/command-manager/commands/send-player-into-portal-command';
import { ChangePlayerActionCommand } from '@/services/world-journey-service/managers/command-manager/commands/change-player-action-command';
import { CreateStaticUnitCommand } from '@/services/world-journey-service/managers/command-manager/commands/create-static-unit-command';
Expand Down Expand Up @@ -107,7 +106,7 @@ export function Provider({ children }: Props) {
return worldJourneyService.subscribe('PLACEHOLDER_ITEM_IDS_ADDED', (placeholderItemIds) => {
itemApi.getItemsOfIds(placeholderItemIds).then((_items) => {
_items.forEach((item) => {
worldJourneyService.executeLocalCommand(AddItemCommand.create(item));
worldJourneyService.loadItem(item);
});
});
});
Expand All @@ -124,7 +123,7 @@ export function Provider({ children }: Props) {
useEffect(() => {
if (!worldJourneyService || !items) return;
items.forEach((item) => {
worldJourneyService.executeLocalCommand(AddItemCommand.create(item));
worldJourneyService.loadItem(item);
});
}, [worldJourneyService, items]);

Expand Down
45 changes: 22 additions & 23 deletions src/models/global/maze-vo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export class MazeVo {
private grid: boolean[][];

constructor(width: number, depth: number) {
if (width < 0 || width % 2 !== 1 || depth < 0 || depth % 2 !== 1)
throw Error(`Width ${width} or depth ${depth} is not valid`);
if (width < 0 || width % 2 !== 1 || depth < 0 || depth % 2 !== 1) throw Error(`Width ${width} or depth ${depth} is not valid`);
this.width = width;
this.depth = depth;
this.grid = this.initializeGrid();
Expand All @@ -22,9 +21,9 @@ export class MazeVo {

private initializeGrid(): boolean[][] {
const grid = [];
for (let y = 0; y < this.depth; y += 1) {
for (let x = 0; x < this.width; x += 1) {
const row = [];
for (let x = 0; x < this.width; x += 1) {
for (let z = 0; z < this.depth; z += 1) {
row.push(true);
}
grid.push(row);
Expand All @@ -34,49 +33,49 @@ export class MazeVo {

private initializeVisited(): boolean[][] {
const visited = [];
for (let y = 0; y < this.depth; y += 1) {
for (let x = 0; x < this.width; x += 1) {
const row = [];
for (let x = 0; x < this.width; x += 1) {
for (let z = 0; z < this.depth; z += 1) {
row.push(false);
}
visited.push(row);
}
return visited;
}

private generateMaze(x: number, y: number): void {
private generateMaze(x: number, z: number): void {
const directions = [
{ dx: 0, dy: -1 },
{ dx: 1, dy: 0 },
{ dx: 0, dy: 1 },
{ dx: -1, dy: 0 },
{ dx: 0, dz: -1 },
{ dx: 1, dz: 0 },
{ dx: 0, dz: 1 },
{ dx: -1, dz: 0 },
];

const visited = this.initializeVisited();
const stack: { x: number; y: number }[] = [];
this.grid[y][x] = false;
stack.push({ x, y });
const stack: { x: number; z: number }[] = [];
this.grid[z][x] = false;
stack.push({ x, z });

while (stack.length > 0) {
const current = stack[stack.length - 1];
const availableDirections = directions.filter((d) => {
const nx = current.x + d.dx * 2;
const ny = current.y + d.dy * 2;
return nx >= 0 && ny >= 0 && nx < this.width && ny < this.depth && !visited[ny][nx];
const nz = current.z + d.dz * 2;
return nx >= 0 && nz >= 0 && nx < this.width && nz < this.depth && !visited[nx][nz];
});

if (availableDirections.length > 0) {
const { dx, dy } = availableDirections[Math.floor(Math.random() * availableDirections.length)];
const { dx, dz } = availableDirections[Math.floor(Math.random() * availableDirections.length)];
const nx = current.x + dx;
const ny = current.y + dy;
const nz = current.z + dz;
const nx2 = current.x + dx * 2;
const ny2 = current.y + dy * 2;
const nz2 = current.z + dz * 2;

this.grid[ny][nx] = false;
this.grid[ny2][nx2] = false;
visited[ny2][nx2] = true;
this.grid[nx][nz] = false;
this.grid[nx2][nz2] = false;
visited[nx2][nz2] = true;

stack.push({ x: nx2, y: ny2 });
stack.push({ x: nx2, z: nz2 });
} else {
stack.pop();
}
Expand Down
54 changes: 13 additions & 41 deletions src/services/world-journey-service/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { uniq } from 'lodash';
import { BoundVo } from '@/models/world/common/bound-vo';
import { ItemModel } from '@/models/world/item/item-model';
import { PlayerModel } from '@/models/world/player/player-model';
Expand Down Expand Up @@ -44,25 +43,15 @@ export class WorldJourneyService {

this.perspectiveManager = PerspectiveManager.create(30, this.playerManager.getMyPlayer().getPrecisePosition());

const appearingItemIdsInUnitManager = this.unitManager.getAppearingItemIds();
const appearingItemIdsInPlayerManager = this.playerManager.getAppearingItemIds();
const appearingItemIds = uniq([...appearingItemIdsInUnitManager, ...appearingItemIdsInPlayerManager]);
this.itemManager = ItemManager.create(appearingItemIds);
this.itemManager = ItemManager.create();

this.commandManager = CommandManager.create();
this.commandManager = CommandManager.create(world, this.playerManager, this.unitManager, this.itemManager, this.perspectiveManager);

this.calculatePlayerPositionTickFps = 24;
this.calculatePlayerPositionTickCount = 0;
this.calculatePlayerPositionTicker();

this.subscribe('UNITS_CHANGED', ([itemId]) => {
this.itemManager.addPlaceholderItemId(itemId);
});

this.subscribe('PLAYER_UPDATED', ([, newPlayer]) => {
const playerHeldItemId = newPlayer.getHeldItemId();
if (playerHeldItemId) this.itemManager.addPlaceholderItemId(playerHeldItemId);

if (this.isMyPlayer(newPlayer)) {
this.perspectiveManager.updateTargetPrecisePosition(newPlayer.getPrecisePosition());
}
Expand All @@ -80,13 +69,7 @@ export class WorldJourneyService {
}

public removeFailedCommand(commandId: string) {
this.commandManager.removeFailedCommand(commandId, {
world: this.world,
playerManager: this.playerManager,
unitManager: this.unitManager,
itemManager: this.itemManager,
perspectiveManager: this.perspectiveManager,
});
this.commandManager.removeFailedCommand(commandId);
}

/**
Expand All @@ -95,33 +78,15 @@ export class WorldJourneyService {
* @param speed 1 is normal speed
*/
public replayCommands(duration: number, speed: number) {
this.commandManager.replayCommands(duration, speed, {
world: this.world,
playerManager: this.playerManager,
unitManager: this.unitManager,
itemManager: this.itemManager,
perspectiveManager: this.perspectiveManager,
});
this.commandManager.replayCommands(duration, speed);
}

public executeRemoteCommand(command: Command) {
this.commandManager.executeRemoteCommand(command, {
world: this.world,
playerManager: this.playerManager,
unitManager: this.unitManager,
itemManager: this.itemManager,
perspectiveManager: this.perspectiveManager,
});
this.commandManager.executeRemoteCommand(command);
}

public executeLocalCommand(command: Command) {
this.commandManager.executeLocalCommand(command, {
world: this.world,
playerManager: this.playerManager,
unitManager: this.unitManager,
itemManager: this.itemManager,
perspectiveManager: this.perspectiveManager,
});
this.commandManager.executeLocalCommand(command);
}

public addPerspectiveDepth() {
Expand Down Expand Up @@ -182,6 +147,13 @@ export class WorldJourneyService {
return this.unitManager.getAllUnitsByItemId();
}

/**
* Load item
*/
public loadItem(item: ItemModel) {
this.itemManager.loadItem(item);
}

public getItem(itemId: string): ItemModel | null {
return this.itemManager.getItem(itemId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export enum CommandNameEnum {
AddItem = 'ADD_ITEM',
AddPlayer = 'ADD_PLAYER',
RemovePlayer = 'REMOVE_PLAYER',
MovePlayer = 'MOVE_PLAYER',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@ export abstract class Command {
return this.createdAt.isBetween(dateA, dateB);
}

/**
* If the command is initially executed in local?
*/
public getIsLocal() {
return !this.isRemote;
}

/**
* If the command is initially executed from remote?
*/
public getIsRemote() {
return this.isRemote;
}

public abstract getIsClientOnly(): boolean;

/**
* Get required item id for executing this command
*/
public abstract getRequiredItemId(): string | null;

public abstract execute(params: CommandParams): void;

protected setUndoAction(newUndoAction: () => void) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class AddPlayerCommand extends Command {

public getIsClientOnly = () => true;

public getRequiredItemId = () => null;

public execute({ playerManager }: CommandParams): void {
const isPlayerAdded = playerManager.addPlayer(this.player);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class ChangePlayerActionCommand extends Command {

public getIsClientOnly = () => true;

public getRequiredItemId = () => null;

public execute({ playerManager }: CommandParams): void {
const player = playerManager.getPlayer(this.playerId);
if (!player) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class ChangePlayerHeldItemCommand extends Command {

public getIsClientOnly = () => true;

public getRequiredItemId = () => this.itemId;

public execute({ playerManager }: CommandParams): void {
const player = playerManager.getPlayer(this.playerId);
if (!player) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class ChangePlayerPrecisePositionCommand extends Command {

public getIsClientOnly = () => true;

public getRequiredItemId = () => null;

public execute({ playerManager }: CommandParams): void {
const player = playerManager.getPlayer(this.playerId);
if (!player) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class CreateEmbedUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => this.itemId;

public execute({ unitManager, playerManager, itemManager }: CommandParams): void {
const item = itemManager.getItem(this.itemId);
if (!item) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class CreateFenceUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => this.itemId;

public execute({ unitManager, playerManager, itemManager }: CommandParams): void {
const item = itemManager.getItem(this.itemId);
if (!item) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class CreateLinkUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => this.itemId;

public execute({ unitManager, playerManager, itemManager }: CommandParams): void {
const item = itemManager.getItem(this.itemId);
if (!item) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class CreatePortalUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => this.itemId;

public execute({ unitManager, playerManager, itemManager }: CommandParams): void {
const item = itemManager.getItem(this.itemId);
if (!item) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class CreateStaticUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => this.itemId;

public execute({ unitManager, playerManager, itemManager }: CommandParams): void {
const item = itemManager.getItem(this.itemId);
if (!item) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class RemoveEmbedUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => null;

public execute({ unitManager }: CommandParams): void {
const currentUnit = unitManager.getUnit(this.unitId);
if (!currentUnit) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class RemoveFenceUnitCommand extends Command {

public getIsClientOnly = () => false;

public getRequiredItemId = () => null;

public execute({ unitManager }: CommandParams): void {
const currentUnit = unitManager.getUnit(this.unitId);
if (!currentUnit) return;
Expand Down
Loading

0 comments on commit 37f4e6a

Please sign in to comment.