From 6dad9be22d99f0ff8f713c404c913674ccbb7b4a Mon Sep 17 00:00:00 2001 From: Marcelo Kopmann Date: Wed, 7 Jun 2023 19:12:19 -0300 Subject: [PATCH 1/2] feat: update boss system to coop with locations --- .../playerMoves/triggerSystems.ts | 6 +- src/systems/boss.ts | 97 ---------------- src/systems/coop.ts | 108 ++++++++++++++++++ 3 files changed, 111 insertions(+), 100 deletions(-) delete mode 100644 src/systems/boss.ts create mode 100644 src/systems/coop.ts diff --git a/src/interactions/playerMoves/triggerSystems.ts b/src/interactions/playerMoves/triggerSystems.ts index eebd8ac..5b58077 100644 --- a/src/interactions/playerMoves/triggerSystems.ts +++ b/src/interactions/playerMoves/triggerSystems.ts @@ -1,6 +1,6 @@ import { Game } from "@gathertown/gather-game-client"; import { ServerClientEventContext } from "@gathertown/gather-game-client/dist/src/public/utils"; -import BossSystem from "../../systems/boss"; +import CoopSystem from "../../systems/coop"; import { PlayerMovesEventData } from "../../types"; import { getPosition } from "../../utils/movement"; @@ -10,8 +10,8 @@ export async function triggerSystems(data: PlayerMovesEventData, context: Server if (!mapId) return const playerNewPosition = getPosition(data) - const bossSystem = BossSystem.getInstance() - bossSystem.triggerBoss(playerNewPosition, mapId, game) + const coopSystem = CoopSystem.getInstance() + coopSystem.triggerChest(playerNewPosition, mapId, game) } catch (error) { console.log(error) diff --git a/src/systems/boss.ts b/src/systems/boss.ts deleted file mode 100644 index 7755722..0000000 --- a/src/systems/boss.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { Game } from "@gathertown/gather-game-client"; -import { Position } from "../types"; -import { hasMatchingCoordinates } from "../utils/movement"; -import { getMapObjectById } from "../utils/objects"; - -type TogglesKeys = 'isPlate1Active' | 'isPlate2Active' | 'isBossActive' - -class BossSystem { - private static instance: BossSystem; - private toggles = { - isPlate1Active: false, - isPlate2Active: false, - isBossActive: false - } - // TODO: Check spaceId too - private plate1Location = { x: 18, y: 3 } - private plate2Location = { x: 21, y: 3 } - private bossObjectId = 'BarrelLitMetal - Nc5jVuh8lyMUbpZlCZVTa_05ccfa05-eac0-4b60-b0b9-924ad5076f3d' - private blankImage = 'https://cdn.gather.town/v0/b/gather-town-dev.appspot.com/o/objects%2Fblank.png?alt=media&token=6564fd34-433a-4e08-843a-5c4b50d6f9e5'; - private bossImage = 'https://cdn.gather.town/storage.googleapis.com/gather-town.appspot.com/internal-dashboard/images/hZSWr066RzaCkkv8uIR-l' - - - constructor() { - } - - public static getInstance(): BossSystem { - if (!BossSystem.instance) { - BossSystem.instance = new BossSystem() - } - - return BossSystem.instance - } - - - setBossObject(mapId: string, active: boolean, game: Game) { - const { key } = getMapObjectById(game, this.bossObjectId, mapId) - game.engine.sendAction({ - $case: "mapSetObjects", - mapSetObjects: { - mapId, - objects: { - [key as number]: { - type: active ? 6 : 0, - previewMessage: 'Press X to challenge the boss', - highlighted: active ? this.bossImage : this.blankImage, - normal: active ? this.bossImage : this.blankImage, - propertiesJson: JSON.stringify({ - message: 'Grr' - }), - _tags: [] - } - } - }, - }); - } - - activateBossObject(mapId: string, game: Game) { - this.toggles.isBossActive = true - this.setBossObject(mapId, true, game) - setTimeout(() => { - this.deactivateBossObject(mapId, game) - }, 5000) - } - - deactivateBossObject(mapId: string, game: Game) { - this.setBossObject(mapId, false, game) - this.toggles.isPlate1Active = false - this.toggles.isPlate2Active = false - this.toggles.isBossActive = false - } - - setBossActivation(key: TogglesKeys, active: boolean, mapId: string, game: Game) { - this.toggles[key] = active - - if (this.toggles.isPlate1Active && this.toggles.isPlate2Active && !this.toggles.isBossActive) { - this.activateBossObject(mapId, game) - return - } - - if (!this.toggles.isBossActive) { - setTimeout(() => this.toggles[key] = false, 1000) - return - } - } - - triggerBoss(playerNewPosition: Position, mapId: string, game: Game) { - if (hasMatchingCoordinates(playerNewPosition, this.plate1Location)) { - this.setBossActivation('isPlate1Active', true, mapId, game) - } - - if (hasMatchingCoordinates(playerNewPosition, this.plate2Location)) { - this.setBossActivation('isPlate2Active', true, mapId, game) - } - } -} - -export default BossSystem \ No newline at end of file diff --git a/src/systems/coop.ts b/src/systems/coop.ts new file mode 100644 index 0000000..94ba07c --- /dev/null +++ b/src/systems/coop.ts @@ -0,0 +1,108 @@ +import { Game } from "@gathertown/gather-game-client"; +import { Position } from "../types"; +import { hasMatchingCoordinates } from "../utils/movement"; +import { getMapObjectById } from "../utils/objects"; + +type TogglesKeys = 'isPlate1Active' | 'isPlate2Active' | 'isChestActive' + +class CoopSystem { + private static instance: CoopSystem; + private toggles = { + isPlate1Active: false, + isPlate2Active: false, + isChestActive: false + } + // TODO: Check spaceId too + private mapId = 'UMRC-_nAY2kcoptLFmdTD' + private plate1Location = { x: 41, y: 20 } + private plate2Location = { x: 41, y: 24 } + private chestObjectId = 'TreasureChestClosed - rh35enYGoIjzE5wEfWAFL_2c1f045e-c237-4a56-b83c-60ab1fa5f1c7' + private blankImage = 'https://cdn.gather.town/v0/b/gather-town-dev.appspot.com/o/objects%2Fblank.png?alt=media&token=6564fd34-433a-4e08-843a-5c4b50d6f9e5'; + private chestImage = 'https://cdn.gather.town/storage.googleapis.com/gather-town.appspot.com/internal-dashboard/images/onUu0ikT0JkdF5W8_T3mQ' + + + constructor() { + } + + public static getInstance(): CoopSystem { + if (!CoopSystem.instance) { + CoopSystem.instance = new CoopSystem() + } + + return CoopSystem.instance + } + + + setChestObject(mapId: string, active: boolean, game: Game) { + const { key } = getMapObjectById(game, this.chestObjectId, mapId) + game.engine.sendAction({ + $case: "mapSetObjects", + mapSetObjects: { + mapId, + objects: { + [key as number]: { + type: active ? 6 : 0, + previewMessage: 'Press X to challenge the chest', + highlighted: active ? this.chestImage : this.blankImage, + normal: active ? this.chestImage : this.blankImage, + propertiesJson: JSON.stringify({ + message: 'Grr' + }), + _tags: [] + } + } + }, + }); + } + + activateChestObject(mapId: string, game: Game) { + if (mapId !== this.mapId) return + this.toggles.isChestActive = true + this.setChestObject(mapId, true, game) + setTimeout(() => { + this.deactivateChestObject(mapId, game) + }, 5000) + } + + deactivateChestObject(mapId: string, game: Game) { + if (mapId !== this.mapId) return + this.setChestObject(mapId, false, game) + this.toggles.isPlate1Active = false + this.toggles.isPlate2Active = false + this.toggles.isChestActive = false + } + + setChestActivation(key: TogglesKeys, active: boolean, mapId: string, game: Game) { + if (mapId !== this.mapId) return + this.toggles[key] = active + + if (this.toggles.isPlate1Active && this.toggles.isPlate2Active && !this.toggles.isChestActive) { + console.log(`[COOP SYSTEM] Activating chest`) + this.activateChestObject(mapId, game) + return + } + + if (!this.toggles.isChestActive) { + setTimeout(() => { + console.log(`[COOP SYSTEM] Deactivating chest`) + this.toggles[key] = false + }, 5000) + return + } + } + + triggerChest(playerNewPosition: Position, mapId: string, game: Game) { + if (mapId !== this.mapId) return + if (hasMatchingCoordinates(playerNewPosition, this.plate1Location)) { + console.log(`[COOP SYSTEM] Plate 1 activated`) + this.setChestActivation('isPlate1Active', true, mapId, game) + } + + if (hasMatchingCoordinates(playerNewPosition, this.plate2Location)) { + console.log(`[COOP SYSTEM] Plate 2 activated`) + this.setChestActivation('isPlate2Active', true, mapId, game) + } + } +} + +export default CoopSystem \ No newline at end of file From 65a02c1624a15e3c14c33e8968460eebc6ce6319 Mon Sep 17 00:00:00 2001 From: Marcelo Kopmann Date: Wed, 7 Jun 2023 19:58:45 -0300 Subject: [PATCH 2/2] feat: change coops system to use interval instead of cooldown --- src/index.ts | 2 + .../playerMoves/triggerSystems.ts | 6 +- src/systems/coop.ts | 73 +++++++------------ 3 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3cb9182..fa76898 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { getSpacesIdsFromEnv } from "./utils/spaces"; import { GatherManagers } from './types' import AIManager from "./services/ai"; import ChatBotSystem from "./systems/chatbot"; +import CoopSystem from "./systems/coop"; require('dotenv').config() // Global object with GatherManager instances for each SpaceId @@ -27,6 +28,7 @@ async function start() { gatherManagers[spaceId] = gatherManager })) + new CoopSystem() new BugsSystem() new AIManager() new ChatBotSystem() diff --git a/src/interactions/playerMoves/triggerSystems.ts b/src/interactions/playerMoves/triggerSystems.ts index 5b58077..0e8ebc2 100644 --- a/src/interactions/playerMoves/triggerSystems.ts +++ b/src/interactions/playerMoves/triggerSystems.ts @@ -1,17 +1,13 @@ import { Game } from "@gathertown/gather-game-client"; import { ServerClientEventContext } from "@gathertown/gather-game-client/dist/src/public/utils"; -import CoopSystem from "../../systems/coop"; import { PlayerMovesEventData } from "../../types"; -import { getPosition } from "../../utils/movement"; export async function triggerSystems(data: PlayerMovesEventData, context: ServerClientEventContext, game: Game) { try { const mapId = context.player?.map if (!mapId) return - const playerNewPosition = getPosition(data) - const coopSystem = CoopSystem.getInstance() - coopSystem.triggerChest(playerNewPosition, mapId, game) + // Currently, no system is triggered by player moves } catch (error) { console.log(error) diff --git a/src/systems/coop.ts b/src/systems/coop.ts index 94ba07c..ca6cfef 100644 --- a/src/systems/coop.ts +++ b/src/systems/coop.ts @@ -1,9 +1,7 @@ -import { Game } from "@gathertown/gather-game-client"; -import { Position } from "../types"; +import { Game, Player } from "@gathertown/gather-game-client"; import { hasMatchingCoordinates } from "../utils/movement"; import { getMapObjectById } from "../utils/objects"; - -type TogglesKeys = 'isPlate1Active' | 'isPlate2Active' | 'isChestActive' +import { gatherManagers } from ".."; class CoopSystem { private static instance: CoopSystem; @@ -12,7 +10,7 @@ class CoopSystem { isPlate2Active: false, isChestActive: false } - // TODO: Check spaceId too + private spaceId = 'cfTnBsETAY2ODiMS/Codecon 2023 - Atividades' private mapId = 'UMRC-_nAY2kcoptLFmdTD' private plate1Location = { x: 41, y: 20 } private plate2Location = { x: 41, y: 24 } @@ -22,6 +20,31 @@ class CoopSystem { constructor() { + console.log(`[COOP SYSTEM] Starting coop system`) + const gatherManager = gatherManagers[this.spaceId] + if (!gatherManager) return + + const game = gatherManager.getGame() + setInterval(() => { + const players = Object.values(game.players) + const playerOnPlate1 = players.find((player: Player) => { + return hasMatchingCoordinates(player, this.plate1Location) + }) + const playerOnPlate2 = players.find((player: Player) => { + return hasMatchingCoordinates(player, this.plate2Location) + }) + if (playerOnPlate1 && playerOnPlate2 && !this.toggles.isChestActive) { + console.log(`[COOP SYSTEM] All players are on the plates`) + this.activateChestObject(this.mapId, game) + return + } + + if ((!playerOnPlate1 || !playerOnPlate2) && this.toggles.isChestActive) { + console.log(`[COOP SYSTEM] At least one of the plates is now empty`) + this.deactivateChestObject(this.mapId, game) + return + } + }, 500) } public static getInstance(): CoopSystem { @@ -32,7 +55,6 @@ class CoopSystem { return CoopSystem.instance } - setChestObject(mapId: string, active: boolean, game: Game) { const { key } = getMapObjectById(game, this.chestObjectId, mapId) game.engine.sendAction({ @@ -56,53 +78,14 @@ class CoopSystem { } activateChestObject(mapId: string, game: Game) { - if (mapId !== this.mapId) return this.toggles.isChestActive = true this.setChestObject(mapId, true, game) - setTimeout(() => { - this.deactivateChestObject(mapId, game) - }, 5000) } deactivateChestObject(mapId: string, game: Game) { - if (mapId !== this.mapId) return this.setChestObject(mapId, false, game) - this.toggles.isPlate1Active = false - this.toggles.isPlate2Active = false this.toggles.isChestActive = false } - - setChestActivation(key: TogglesKeys, active: boolean, mapId: string, game: Game) { - if (mapId !== this.mapId) return - this.toggles[key] = active - - if (this.toggles.isPlate1Active && this.toggles.isPlate2Active && !this.toggles.isChestActive) { - console.log(`[COOP SYSTEM] Activating chest`) - this.activateChestObject(mapId, game) - return - } - - if (!this.toggles.isChestActive) { - setTimeout(() => { - console.log(`[COOP SYSTEM] Deactivating chest`) - this.toggles[key] = false - }, 5000) - return - } - } - - triggerChest(playerNewPosition: Position, mapId: string, game: Game) { - if (mapId !== this.mapId) return - if (hasMatchingCoordinates(playerNewPosition, this.plate1Location)) { - console.log(`[COOP SYSTEM] Plate 1 activated`) - this.setChestActivation('isPlate1Active', true, mapId, game) - } - - if (hasMatchingCoordinates(playerNewPosition, this.plate2Location)) { - console.log(`[COOP SYSTEM] Plate 2 activated`) - this.setChestActivation('isPlate2Active', true, mapId, game) - } - } } export default CoopSystem \ No newline at end of file