diff --git a/resources/js/game/game.tsx b/resources/js/game/game.tsx index 37c2fceda..ed824621a 100755 --- a/resources/js/game/game.tsx +++ b/resources/js/game/game.tsx @@ -31,48 +31,14 @@ import MapState from "./sections/map/types/map-state"; import MapData from "./sections/map/lib/request-types/MapData"; import MapStateManager from "./sections/map/lib/state/map-state-manager"; import MapTabs from "./sections/map/map-tabs"; -import {CoreContainer, serviceContainer} from "./lib/containers/core-container"; +import {serviceContainer} from "./lib/containers/core-container"; import GameEventListeners from "./lib/game/event-listeners/game-event-listeners"; export default class Game extends React.Component { private gameEventListener?: GameEventListeners; - private characterTopBar: any; - - private characterAttacks: any; - - private characterStatus: any; - - private characterRevive: any; - - private characterAttackData: any; - - private unlockAlchemySkill: any; - - private updateCraftingTypes: any; - - private kingdomUpdates: any; - - private kingdomLogsUpdate: any; - - private updateSpecialShopsAccess: any; - - private updateSpecialEventGoals: any; - - private globalTimeOut: any; - - private updateCharacterBasePosition: any; - - private characterCurrencies: any; - - private traverseUpdate: any; - - private monsterUpdate: any; - - private raidMonsterUpdate: any; - - constructor(props: GameProps, context: CoreContainer | undefined) { + constructor(props: GameProps) { super(props); this.gameEventListener = serviceContainer().fetch(GameEventListeners); @@ -127,86 +93,6 @@ export default class Game extends React.Component { }, ], }; - - // @ts-ignore - this.monsterUpdate = Echo.private( - "update-monsters-list-" + this.props.userId - ); - - // @ts-ignore - this.raidMonsterUpdate = Echo.private( - "update-raid-monsters-list-" + this.props.userId - ); - - // @ts-ignore - this.characterTopBar = Echo.private( - "update-top-bar-" + this.props.userId - ); - - // @ts-ignore - this.characterCurrencies = Echo.private( - "update-currencies-" + this.props.userId - ); - - // @ts-ignore - this.characterAttacks = Echo.private( - "update-character-attacks-" + this.props.userId - ); - - // @ts-ignore - this.characterStatus = Echo.private( - "update-character-status-" + this.props.userId - ); - - // @ts-ignore - this.characterRevive = Echo.private( - "character-revive-" + this.props.userId - ); - - // @ts-ignore - this.characterAttackData = Echo.private( - "update-character-attack-" + this.props.userId - ); - - // @ts-ignore - this.unlockAlchemySkill = Echo.private( - "unlock-skill-" + this.props.userId - ); - - // @ts-ignore - this.updateCraftingTypes = Echo.private( - "update-location-base-crafting-options-" + this.props.userId - ); - - // @ts-ignore - this.updateSpecialShopsAccess = Echo.private( - "update-location-base-shops-" + this.props.userId - ); - - // @ts-ignore - this.updateSpecialEventGoals = Echo.private( - "update-location-base-event-goals-" + this.props.userId - ); - - // @ts-ignore - this.kingdomUpdates = Echo.private( - "update-kingdom-" + this.props.userId - ); - - // @ts-ignore - this.kingdomLogsUpdate = Echo.private( - "update-new-kingdom-logs-" + this.props.userId - ); - - // @ts-ignore - this.globalTimeOut = Echo.private( - "global-timeout-" + this.props.userId - ); - - // @ts-ignore - this.updateCharacterBasePosition = Echo.private( - "update-character-position-" + this.props.userId - ); } componentDidMount() { @@ -247,262 +133,6 @@ export default class Game extends React.Component { if (this.gameEventListener) { this.gameEventListener.listenToEvents(); } - - // @ts-ignore - this.characterTopBar.listen( - "Game.Core.Events.UpdateTopBarBroadcastEvent", - (event: any) => { - this.setState( - { - character: { - ...this.state.character, - ...event.characterSheet, - }, - }, - () => { - if (event.characterSheet.is_banned) { - location.reload(); - } - } - ); - } - ); - - // @ts-ignore - this.characterCurrencies.listen( - "Game.Core.Events.UpdateCharacterCurrenciesBroadcastEvent", - (event: any) => { - this.setState({ - character: { ...this.state.character, ...event.currencies }, - }); - } - ); - - // @ts-ignore - this.monsterUpdate.listen( - "Game.Maps.Events.UpdateMonsterList", - (event: any) => { - if (this.state.action_data === null) { - return; - } - - this.setState({ - action_data: { - ...this.state.action_data, - monsters: event.monsters, - }, - }); - } - ); - - this.raidMonsterUpdate.listen( - "Game.Maps.Events.UpdateRaidMonsters", - (event: any) => { - if (this.state.action_data === null) { - return; - } - - this.setState({ - action_data: { - ...this.state.action_data, - raid_monsters: event.raidMonsters, - }, - }); - } - ); - - // @ts-ignore - this.characterAttacks.listen( - "Game.Core.Events.UpdateCharacterAttacks", - (event: any) => { - this.setState({ - character: { - ...this.state.character, - ...event.characterAttacks, - }, - }); - } - ); - - // @ts-ignore - this.characterStatus.listen( - "Game.Battle.Events.UpdateCharacterStatus", - (event: any) => { - this.setState({ - character_status: event.characterStatuses, - character: { - ...this.state.character, - ...event.characterStatuses, - }, - }); - } - ); - - // @ts-ignore - this.characterAttackData.listen( - "Flare.Events.UpdateCharacterAttackBroadcastEvent", - (event: any) => { - this.setState({ - character: { ...this.state.character, ...event.attack }, - }); - } - ); - - // @ts-ignore - this.kingdomLogsUpdate.listen( - "Game.Kingdoms.Events.UpdateKingdomLogs", - (event: { logs: KingdomLogDetails[] | [] }) => { - this.setState( - { - kingdom_logs: event.logs, - }, - () => { - this.updateLogIcon(); - } - ); - } - ); - - // @ts-ignore - this.unlockAlchemySkill.listen( - "Game.Quests.Events.UnlockSkillEvent", - () => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.is_alchemy_locked = false; - - this.setState({ - character: character, - }); - } - ); - - // @ts-ignore - this.updateCraftingTypes.listen( - "Game.Maps.Events.UpdateLocationBasedCraftingOptions", - (event: any) => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.can_use_work_bench = event.canUseWorkBench; - character.can_access_queen = event.canUseQueenOfHearts; - - this.setState({ - character: character, - }); - } - ); - - //@ts-ignore - this.updateSpecialShopsAccess.listen( - "Game.Maps.Events.UpdateLocationBasedSpecialShops", - (event: any) => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.can_access_hell_forged = - event.canAccessHellForgedShop; - character.can_access_purgatory_chains = - event.canAccessPurgatoryChainsShop; - - this.setState({ - character: character, - }); - } - ); - - //@ts-ignore - this.updateSpecialEventGoals.listen( - "Game.Maps.Events.UpdateLocationBasedEventGoals", - (event: any) => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.can_use_event_goals_button = event.canSeeEventGoals; - - this.setState({ - character: character, - }); - } - ); - - // @ts-ignore - this.characterRevive.listen( - "Game.Battle.Events.CharacterRevive", - (event: { health: number }) => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.health = event.health; - - this.setState({ - character: character, - }); - } - ); - - // @ts-ignore - this.kingdomUpdates.listen( - "Game.Kingdoms.Events.UpdateKingdom", - (event: { kingdom: KingdomDetails }) => { - const eventKingdom = event.kingdom; - - if (Array.isArray(eventKingdom)) { - this.setState({ - kingdoms: eventKingdom, - }); - - return; - } - - let currentKingdoms = JSON.parse( - JSON.stringify(this.state.kingdoms) - ); - - const index = currentKingdoms.findIndex( - (kingdom: KingdomDetails) => kingdom.id === eventKingdom.id - ); - - if (index > -1) { - currentKingdoms[index] = eventKingdom; - } - - this.setState({ - kingdoms: currentKingdoms, - }); - } - ); - - // @ts-ignore - this.globalTimeOut.listen( - "Game.Core.Events.GlobalTimeOut", - (event: { showTimeOut: boolean }) => { - this.setState({ - show_global_timeout: event.showTimeOut, - }); - } - ); - - this.updateCharacterBasePosition.listen( - "Game.Maps.Events.UpdateCharacterBasePosition", - (event: any) => { - const character = JSON.parse( - JSON.stringify(this.state.character) - ); - - character.base_position = event.basePosition; - - this.setState({ - character: character, - }); - } - ); } setStateFromData(data: MapData) { diff --git a/resources/js/game/lib/containers/registrations/game-event-container.ts b/resources/js/game/lib/containers/registrations/game-event-container.ts index 281799d18..a111ad12d 100644 --- a/resources/js/game/lib/containers/registrations/game-event-container.ts +++ b/resources/js/game/lib/containers/registrations/game-event-container.ts @@ -1,6 +1,10 @@ import {CoreContainer} from "../core-container"; import GameEventListeners from "../../game/event-listeners/game-event-listeners"; import MapListeners from "../../game/event-listeners/game/map-listeners"; +import CharacterListeners from "../../game/event-listeners/game/character-listeners"; +import MonsterListeners from "../../game/event-listeners/game/monster-listeners"; +import KingdomListeners from "../../game/event-listeners/game/kingdom-listeners"; +import ActionListeners from "../../game/event-listeners/game/action-listeners"; /** * Register game event listeners here. @@ -12,6 +16,10 @@ function gameEventContainer(container: CoreContainer) { // Game Event Listeners: // Classes are registered with their interface as their key. container.register('GameListeners', {useClass: MapListeners}); + container.register('GameListeners', {useClass: CharacterListeners}); + container.register('GameListeners', {useClass: MonsterListeners}); + container.register('GameListeners', {useClass: KingdomListeners}); + container.register('GameListeners', {useClass: ActionListeners}); // The Core Listener Class container.register('game-event-listeners', GameEventListeners); diff --git a/resources/js/game/lib/game/event-listeners/game-event-listeners.ts b/resources/js/game/lib/game/event-listeners/game-event-listeners.ts index 409f685a6..f052a50bb 100644 --- a/resources/js/game/lib/game/event-listeners/game-event-listeners.ts +++ b/resources/js/game/lib/game/event-listeners/game-event-listeners.ts @@ -1,10 +1,13 @@ import CoreEventListener from "./core-event-listener"; import {singleton, inject} from "tsyringe"; -import {Channel} from "laravel-echo"; import Game from "../../../game"; import {serviceContainer} from "../../containers/core-container"; import GameListener from "./game-listener"; import MapListeners from "./game/map-listeners"; +import CharacterListeners from "./game/character-listeners"; +import MonsterListeners from "./game/monster-listeners"; +import KingdomListeners from "./game/kingdom-listeners"; +import ActionListeners from "./game/action-listeners"; @singleton() export default class GameEventListeners { @@ -13,7 +16,15 @@ export default class GameEventListeners { private userId?: number; - private traverseUpdate?: GameListener; + private mapListeners?: GameListener; + + private characterListeners?: GameListener; + + private monsterListeners?: GameListener; + + private kingdomListener?: GameListener; + + private actionListeners?: GameListener; constructor(@inject(CoreEventListener) private coreEventListener: CoreEventListener) {} @@ -21,7 +32,11 @@ export default class GameEventListeners { this.component = component; this.userId = userId; - this.traverseUpdate = serviceContainer().fetch(MapListeners); + this.mapListeners = serviceContainer().fetch(MapListeners); + this.characterListeners = serviceContainer().fetch(CharacterListeners); + this.monsterListeners = serviceContainer().fetch(MonsterListeners); + this.kingdomListener = serviceContainer().fetch(KingdomListeners); + this.actionListeners = serviceContainer().fetch(ActionListeners); } public registerEvents(): void { @@ -30,17 +45,52 @@ export default class GameEventListeners { throw new Error('Need to call initialize on GameEventListeners first.'); } + if (this.mapListeners) { + this.mapListeners.initialize(this.component, this.userId); + this.mapListeners.register(); + } + + if (this.characterListeners) { + this.characterListeners.initialize(this.component, this.userId); + this.characterListeners.register(); + } - if (this.traverseUpdate) { - this.traverseUpdate.initialize(this.component, this.userId); - this.traverseUpdate.register(); + if (this.monsterListeners) { + this.monsterListeners.initialize(this.component, this.userId); + this.monsterListeners.register(); + } + + if (this.kingdomListener) { + this.kingdomListener.initialize(this.component, this.userId); + this.kingdomListener.register(); + } + + if (this.actionListeners) { + this.actionListeners.initialize(this.component, this.userId); + this.actionListeners.register(); } } public listenToEvents(): void { - if (this.traverseUpdate) { - this.traverseUpdate.listen(); + if (this.mapListeners) { + this.mapListeners.listen(); + } + + if (this.characterListeners) { + this.characterListeners.listen() + } + + if (this.monsterListeners) { + this.monsterListeners.listen() + } + + if (this.kingdomListener) { + this.kingdomListener.listen() + } + + if (this.actionListeners) { + this.actionListeners.listen() } } } diff --git a/resources/js/game/lib/game/event-listeners/game/map-listeners.ts b/resources/js/game/lib/game/event-listeners/game/map-listeners.ts index 222ee01db..cd987224d 100644 --- a/resources/js/game/lib/game/event-listeners/game/map-listeners.ts +++ b/resources/js/game/lib/game/event-listeners/game/map-listeners.ts @@ -13,6 +13,14 @@ export default class MapListeners implements GameListener { private traverseUpdate?: Channel; + private updateCharacterBasePosition?: Channel; + + private updateCraftingTypes?: Channel; + + private updateSpecialShopsAccess?: Channel; + + private updateSpecialEventGoals?: Channel; + constructor(@inject(CoreEventListener) private coreEventListener: CoreEventListener) {} public initialize(component: Game, userId: number): void { @@ -27,27 +35,175 @@ export default class MapListeners implements GameListener { const echo = this.coreEventListener.getEcho(); this.traverseUpdate = echo.private("update-plane-" + this.userId); + this.updateCharacterBasePosition = echo.private("update-character-position-" + this.userId); + this.updateCraftingTypes = echo.private("update-location-base-crafting-options-" + this.userId); + this.updateSpecialShopsAccess = echo.private("update-location-base-shops-" + this.userId); + this.updateSpecialEventGoals = echo.private("update-location-base-event-goals-" + this.userId); } catch (e: any|unknown) { throw new Error(e); } } public listen(): void { - if (this.traverseUpdate) { - this.traverseUpdate.listen( - "Game.Maps.Events.UpdateMap", - (event: any) => { - if (!this.component) { - return; - } - - this.component.setStateFromData(event.mapDetails); - - this.component.updateQuestPlane( - event.mapDetails.character_map.game_map.name - ); + this.listenToTraverse(); + this.listenToBasePositionUpdate(); + this.listForLocationBasedCraftingTypes(); + this.listForUpdatesToSpecialShopsAccess(); + this.listenForEventGoalUpdates(); + } + + /** + * Listen to traverse updates. + * + * @protected + */ + protected listenToTraverse() { + if (!this.traverseUpdate) { + return + } + + this.traverseUpdate.listen( + "Game.Maps.Events.UpdateMap", + (event: any) => { + if (!this.component) { + return; + } + + this.component.setStateFromData(event.mapDetails); + + this.component.updateQuestPlane( + event.mapDetails.character_map.game_map.name + ); + } + ); + } + + /** + * Listen to base position update. + * + * @protected + */ + protected listenToBasePositionUpdate() { + if (!this.updateCharacterBasePosition) { + return + } + + this.updateCharacterBasePosition.listen( + "Game.Maps.Events.UpdateCharacterBasePosition", + (event: any) => { + if (!this.component) { + return; + } + + const character = JSON.parse( + JSON.stringify(this.component.state.character) + ); + + character.base_position = event.basePosition; + + this.component.setState({ + character: character, + }); + } + ); + } + + /** + * Listen for specific location based or plane based crafting types that unlock. + * + * @protected + */ + protected listForLocationBasedCraftingTypes() { + if (!this.updateCraftingTypes) { + return + } + + this.updateCraftingTypes.listen( + "Game.Maps.Events.UpdateLocationBasedCraftingOptions", + (event: any) => { + + if (!this.component) { + return; } - ); + + const character = JSON.parse( + JSON.stringify(this.component.state.character) + ); + + character.can_use_work_bench = event.canUseWorkBench; + character.can_access_queen = event.canUseQueenOfHearts; + + this.component.setState({ + character: character, + }); + } + ); + } + + /** + * Listen for when players should see specific special shop buttons. + * + * @protected + */ + protected listForUpdatesToSpecialShopsAccess() { + + if (!this.updateSpecialShopsAccess) { + return; } + + this.updateSpecialShopsAccess.listen( + "Game.Maps.Events.UpdateLocationBasedSpecialShops", + (event: any) => { + + if (!this.component) { + return; + } + + const character = JSON.parse( + JSON.stringify(this.component.state.character) + ); + + character.can_access_hell_forged = + event.canAccessHellForgedShop; + character.can_access_purgatory_chains = + event.canAccessPurgatoryChainsShop; + + this.component.setState({ + character: character, + }); + } + ); + } + + /** + * Listen for global event goal updates. + * + * @protected + */ + protected listenForEventGoalUpdates() { + + if (!this.updateSpecialEventGoals) { + return; + } + + this.updateSpecialEventGoals.listen( + "Game.Maps.Events.UpdateLocationBasedEventGoals", + (event: any) => { + + if (!this.component) { + return; + } + + const character = JSON.parse( + JSON.stringify(this.component.state.character) + ); + + character.can_use_event_goals_button = event.canSeeEventGoals; + + this.component.setState({ + character: character, + }); + } + ); } }