Skip to content

Commit

Permalink
feat: implement command failed handler
Browse files Browse the repository at this point in the history
  • Loading branch information
messi-yang committed Apr 28, 2024
1 parent 548a5b9 commit da06def
Show file tree
Hide file tree
Showing 27 changed files with 304 additions and 52 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@typescript-eslint/default-param-last": "off",
"arrow-body-style": "off",
"no-param-reassign": "off",
"noUncheckedIndexedAccess": "off",
"no-else-return": "off",
"class-methods-use-this": "off"
}
Expand Down
4 changes: 3 additions & 1 deletion src/adapters/apis/world-journey-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class WorldJourneyApi {
events: {
onWorldEntered: (worldJourneyService: WorldJourneyService) => void;
onCommandSucceeded: (command: Command) => void;
onCommandFailed: (commandId: string, errorMessage: string) => void;
onErrored: (message: string) => void;
onDisconnect: () => void;
onOpen: () => void;
Expand Down Expand Up @@ -59,7 +60,7 @@ export class WorldJourneyApi {
if (!command) return;
events.onCommandSucceeded(command);
} else if (event.name === ServerEventNameEnum.CommandFailed) {
events.onErrored(event.errorMessage);
events.onCommandFailed(event.commandId, event.errorMessage);
} else if (event.name === ServerEventNameEnum.Errored) {
events.onErrored(event.message);
}
Expand Down Expand Up @@ -92,6 +93,7 @@ export class WorldJourneyApi {
events: {
onWorldEntered: (worldJourneyService: WorldJourneyService) => void;
onCommandSucceeded: (command: Command) => void;
onCommandFailed: (commandId: string, errorMessage: string) => void;
onErrored: (message: string) => void;
onDisconnect: () => void;
onOpen: () => void;
Expand Down
26 changes: 20 additions & 6 deletions src/contexts/world-journey-service-context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export function Provider({ children }: Props) {
const itemApi = useMemo<ItemApi>(() => ItemApi.new(), []);
const [items, setItems] = useState<ItemModel[] | null>([]);
const [worldJourneyService, setWorldJourneyService] = useState<WorldJourneyService | null>(null);
useEffect(() => {
// @ts-expect-error
window.worldJourneyService = worldJourneyService;
}, [worldJourneyService]);

const notificationEventDispatcher = useMemo(() => NotificationEventDispatcher.new(), []);

const [embedCode, setEmbedCode] = useState<string | null>(null);
Expand Down Expand Up @@ -152,6 +157,11 @@ export function Provider({ children }: Props) {
if (!newWorldJourneyService) return;
newWorldJourneyService.executeCommand(command);
},
onCommandFailed: (commandId, errorMessage) => {
if (!newWorldJourneyService) return;
newWorldJourneyService.handleFailedCommand(commandId);
notificationEventDispatcher.publishErrorTriggeredEvent(errorMessage);
},
onErrored: (message) => {
notificationEventDispatcher.publishErrorTriggeredEvent(message);
},
Expand All @@ -175,16 +185,20 @@ export function Provider({ children }: Props) {
if (!worldJourneyApi.current) return;

const oldPlayerPos = oldPlayer.getPosition();
const playerPos = player.getPosition();
if (oldPlayerPos.isEqual(playerPos)) {
const newPlayerPos = player.getPosition();
if (oldPlayerPos.isEqual(newPlayerPos)) {
return;
}

const unitAtPlayerPos = worldJourneyService.getUnit(playerPos);
if (!unitAtPlayerPos) return;
const unitAtNewPlayerPos = worldJourneyService.getUnit(newPlayerPos);
if (!unitAtNewPlayerPos) return;

if (unitAtNewPlayerPos instanceof PortalUnitModel) {
// To prevent infinite teleporting loop, we need to check if we just came from another portal
const unitAtOldPlayerPos = worldJourneyService.getUnit(oldPlayerPos);
if (unitAtOldPlayerPos instanceof PortalUnitModel) return;

if (unitAtPlayerPos instanceof PortalUnitModel) {
const command = SendPlayerIntoPortalCommand.new(player.getId(), unitAtPlayerPos.getId());
const command = SendPlayerIntoPortalCommand.new(player.getId(), unitAtNewPlayerPos.getId());
worldJourneyService.executeCommand(command);
worldJourneyApi.current.sendCommand(command);
}
Expand Down
10 changes: 10 additions & 0 deletions src/services/world-journey-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export class WorldJourneyService {
return new WorldJourneyService(world, players, myPlayerId, units);
}

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

public executeCommand(command: Command) {
this.commandManager.executeCommand(command, {
world: this.world,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export class AddItemCommand extends BaseCommand {
}

public execute({ itemManager }: CommandParams): void {
itemManager.addItem(this.item);
const isItemAdded = itemManager.addItem(this.item);

this.setUndoAction(() => {
if (isItemAdded) {
itemManager.removeItem(this.item.getId());
}
});
}

public getItem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export class AddPerspectiveDepthCommand extends BaseCommand {
}

public execute({ perspectiveManager }: CommandParams): void {
perspectiveManager.addPerspectiveDepth();
const isDepthChanged = perspectiveManager.addPerspectiveDepth();

this.setUndoAction(() => {
if (isDepthChanged) {
perspectiveManager.subtractPerspectiveDepth();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ export class AddPlayerCommand extends BaseCommand {
}

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

const playerHeldItemId = this.player.getHeldItemId();
let isPlaceholderItemIdAdded = false;
if (playerHeldItemId) {
itemManager.addPlaceholderItemId(playerHeldItemId);
isPlaceholderItemIdAdded = itemManager.addPlaceholderItemId(playerHeldItemId);
}

this.setUndoAction(() => {
if (isPlayerAdded) {
playerManager.removePlayer(this.player.getId());
}
if (playerHeldItemId && isPlaceholderItemIdAdded) {
itemManager.removePlaceholderItemId(playerHeldItemId);
}
});
}

public getPlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export class ChangePlayerActionCommand extends BaseCommand {
const clonedPlayer = player.clone();
clonedPlayer.updateAction(this.action);
clonedPlayer.updatePrecisePosition(this.action.getPrecisePosition());
playerManager.updatePlayer(clonedPlayer);
const isPlayerUpdated = playerManager.updatePlayer(clonedPlayer);

this.setUndoAction(() => {
if (isPlayerUpdated) {
playerManager.updatePlayer(player);
}
});
}

public getPlayerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ export class ChangePlayerHeldItemCommand extends BaseCommand {
itemManager.addPlaceholderItemId(this.itemId);
}

playerManager.updatePlayer(clonedPlayer);
const isPlayerUpdated = playerManager.updatePlayer(clonedPlayer);

this.setUndoAction(() => {
if (isPlayerUpdated) {
playerManager.updatePlayer(player);
}
});
}

public getPlayerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ export class CreateEmbedUnitCommand extends BaseCommand {
if (playersAtPos) return;
}

unitManager.addUnit(newUnit);
const newUnitAdded = unitManager.addUnit(newUnit);

this.setUndoAction(() => {
if (newUnitAdded) {
unitManager.removeUnit(newUnit.getId());
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ export class CreateFenceUnitCommand extends BaseCommand {
if (playersAtPos) return;
}

unitManager.addUnit(newUnit);
const newUnitAdded = unitManager.addUnit(newUnit);

this.setUndoAction(() => {
if (newUnitAdded) {
unitManager.removeUnit(newUnit.getId());
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ export class CreateLinkUnitCommand extends BaseCommand {
if (playersAtPos) return;
}

unitManager.addUnit(newUnit);
const newUnitAdded = unitManager.addUnit(newUnit);

this.setUndoAction(() => {
if (newUnitAdded) {
unitManager.removeUnit(newUnit.getId());
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ export class CreatePortalUnitCommand extends BaseCommand {
}

const portalsWithoutTarget = unitManager.getPortalUnits().filter((unit) => !unit.getTargetUnitId());
if (portalsWithoutTarget.length === 0) {
unitManager.addUnit(newUnit);
} else {
const topLeftMostPortalWithoutTarget = portalsWithoutTarget.sort((unitA, unitB) => {
const topLeftMostPortalWithoutTarget = portalsWithoutTarget
.sort((unitA, unitB) => {
const unitPosA = unitA.getPosition();
const unitPosB = unitB.getPosition();

Expand All @@ -79,14 +77,31 @@ export class CreatePortalUnitCommand extends BaseCommand {
} else {
return unitPosA.getZ() - unitPosB.getZ();
}
})[0];

topLeftMostPortalWithoutTarget.updateTargetUnitId(newUnit.getId());
unitManager.updateUnit(topLeftMostPortalWithoutTarget);
})
.at(0);

let isUnitAdded = false;
let isTopLeftMostPortalWithoutTargetUpdated = false;
if (topLeftMostPortalWithoutTarget) {
newUnit.updateTargetUnitId(topLeftMostPortalWithoutTarget.getId());
unitManager.addUnit(newUnit);
isUnitAdded = unitManager.addUnit(newUnit);

const clonedTopLeftMostPortalWithoutTarget = topLeftMostPortalWithoutTarget.clone();
clonedTopLeftMostPortalWithoutTarget.updateTargetUnitId(newUnit.getId());
isTopLeftMostPortalWithoutTargetUpdated = unitManager.updateUnit(clonedTopLeftMostPortalWithoutTarget);
} else {
isUnitAdded = unitManager.addUnit(newUnit);
}

this.setUndoAction(() => {
if (isUnitAdded) {
unitManager.removeUnit(newUnit.getId());
}

if (topLeftMostPortalWithoutTarget && isTopLeftMostPortalWithoutTargetUpdated) {
unitManager.updateUnit(topLeftMostPortalWithoutTarget);
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ export class CreateStaticUnitCommand extends BaseCommand {
if (playersAtPos) return;
}

unitManager.addUnit(newUnit);
const newUnitAdded = unitManager.addUnit(newUnit);

this.setUndoAction(() => {
if (newUnitAdded) {
unitManager.removeUnit(newUnit.getId());
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ export class RemoveEmbedUnitCommand extends BaseCommand {
}

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

const hasRemovedUnit = unitManager.removeUnit(this.unitId);

this.setUndoAction(() => {
if (hasRemovedUnit) {
unitManager.addUnit(currentUnit);
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ export class RemoveFenceUnitCommand extends BaseCommand {
}

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

const hasRemovedUnit = unitManager.removeUnit(this.unitId);

this.setUndoAction(() => {
if (hasRemovedUnit) {
unitManager.addUnit(currentUnit);
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ export class RemoveLinkUnitCommand extends BaseCommand {
}

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

const hasRemovedUnit = unitManager.removeUnit(this.unitId);

this.setUndoAction(() => {
if (hasRemovedUnit) {
unitManager.addUnit(currentUnit);
}
});
}

public getUnitId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ export class RemovePlayerCommand extends BaseCommand {
}

public execute({ playerManager }: CommandParams): void {
playerManager.removePlayer(this.playerId);
const currentPlayer = playerManager.getPlayer(this.playerId);
if (!currentPlayer) return;

const isPlayerRemoved = playerManager.removePlayer(this.playerId);

this.setUndoAction(() => {
if (isPlayerRemoved) {
playerManager.addPlayer(currentPlayer);
}
});
}

public getPlayerId() {
Expand Down
Loading

0 comments on commit da06def

Please sign in to comment.