From 8f68db4a330876b3985c539699fe69e02c8bf6da Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Wed, 18 Dec 2024 12:32:38 +0000 Subject: [PATCH] feat: game navigator, player debouncer --- apps/telegram-game/components.d.ts | 1 + apps/telegram-game/public/wheel-1.png | Bin 0 -> 1147 bytes apps/telegram-game/src/components/Game.vue | 8 +++-- .../src/components/GameNavigator.vue | 30 ++++++++++++++++++ apps/website/server/api/websocket.ts | 6 ++++ packages/game/src/lib/baseGameAddon.ts | 4 ++- packages/game/src/lib/objects/treeObject.ts | 5 +++ .../game/src/lib/objects/unit/playerObject.ts | 16 ++++------ .../game/src/lib/objects/unit/unitObject.ts | 4 +-- .../src/lib/services/basePlayerService.ts | 9 +++++- packages/game/src/lib/types.ts | 4 ++- 11 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 apps/telegram-game/public/wheel-1.png create mode 100644 apps/telegram-game/src/components/GameNavigator.vue diff --git a/apps/telegram-game/components.d.ts b/apps/telegram-game/components.d.ts index 15bd9f2..dfb0235 100644 --- a/apps/telegram-game/components.d.ts +++ b/apps/telegram-game/components.d.ts @@ -9,6 +9,7 @@ declare module 'vue' { export interface GlobalComponents { ComingSoon: typeof import('./src/components/ComingSoon.vue')['default'] Game: typeof import('./src/components/Game.vue')['default'] + GameNavigator: typeof import('./src/components/GameNavigator.vue')['default'] Modal: typeof import('./src/components/Modal.vue')['default'] Navigation: typeof import('./src/components/Navigation.vue')['default'] PageContainer: typeof import('./src/components/PageContainer.vue')['default'] diff --git a/apps/telegram-game/public/wheel-1.png b/apps/telegram-game/public/wheel-1.png new file mode 100644 index 0000000000000000000000000000000000000000..a57a0496a18fe8976e8b1bedfa8892e1c32b6946 GIT binary patch literal 1147 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1qucH3FV4jv*CsZ|`i(n&K$J`k+@qLXg|>G)Gj< z@=6wI#RVMK%(gYLczxcy{#<_Z(|O(Wo#oHY z&--lZH}!6ByJh;>V_R=_?JRov>VA>W$B&=er~aBZSEHUwleJ+ElSAI8+GlUS+{NvoZt5@kOpb+V^o-a&wj1-zmuO` zEuU6YK)^U$z7vl*{ zu<=)Gh3@{m*SFmzpTQd_Q@0N;^X}h@w|`#rZQpW_VcLt{fZu;gnI{A@fV5pQXDF+C z&G&9EJvEo$a;H?zb2oEn)!K()&yCSMC0kqig7Ta~z-G487cjH#+1*OlU_sn*XcIaCI)wl9Hn?o48{ogOE=dhWd zkJDH#lV4Y(yZo?gx!onU2?1OTmaGkTZe6ju-y~a+R=ao(+l`C&=BFyW3AiKC|NWk6 z?cWt;!Y|lB?gKhQb~kT>Ty9)^)xYoW*G<37V11r@zx??)nJJeaJU>`x>Dz49utNh9 zj?xeQ&JTWig?*p?cC!O-qsvddELb16UuSvaR$wSa9p2BM%TO2y4!%|94OcC1uJJo} zkKx*L=1rCV?HO|!A|d_`&2PB(;WNY9da(uPnNMt726n~TJI@)T>eU47`7iu7o`e)e zS+8a@%$LvlyStHJZ@%90gZKV@FOqVpxBs%cQOFA%#(zhN z+Fyc2o$>FpyBP(_wEopI-JILM$(&);U6zaQe^1N zHtgw_|GQ8AB`jE%<}?Tqd>^J*1yThL^*s-1g78neku6{1- HoD!M
-
+
-
+ + +
avatar -

+

{{ profile?.energy }}

diff --git a/apps/telegram-game/src/components/GameNavigator.vue b/apps/telegram-game/src/components/GameNavigator.vue new file mode 100644 index 0000000..f932aa0 --- /dev/null +++ b/apps/telegram-game/src/components/GameNavigator.vue @@ -0,0 +1,30 @@ + + + diff --git a/apps/website/server/api/websocket.ts b/apps/website/server/api/websocket.ts index fe86972..cf0eff3 100644 --- a/apps/website/server/api/websocket.ts +++ b/apps/website/server/api/websocket.ts @@ -75,6 +75,12 @@ export default defineWebSocketHandler({ return } + // Check, if already exists by Telegram + const playerExist = activeRoom.objects.find((obj) => obj.type === 'PLAYER' && obj.telegramId === parsed.data.token) + if (playerExist) { + return + } + activeRoom.addPlayer({ id: peer.id, telegramId: parsed.data.token, x: wagon?.x ? wagon.x - 200 : 100, character }) peer.subscribe(activeRoom.id) diff --git a/packages/game/src/lib/baseGameAddon.ts b/packages/game/src/lib/baseGameAddon.ts index f9b43b0..d0ddbfc 100644 --- a/packages/game/src/lib/baseGameAddon.ts +++ b/packages/game/src/lib/baseGameAddon.ts @@ -95,7 +95,7 @@ export class BaseGameAddon extends Container implements GameAddon { this.cameraTarget = this.player this.app.stage.addEventListener('pointerdown', (e) => { - if (!this.player) { + if (!this.player || !this.player.canClick) { return } @@ -116,6 +116,8 @@ export class BaseGameAddon extends Container implements GameAddon { } this.player.target = flag + this.player.click() + this.websocketService.send({ type: 'NEW_PLAYER_TARGET', data: { diff --git a/packages/game/src/lib/objects/treeObject.ts b/packages/game/src/lib/objects/treeObject.ts index 38dbe4e..0791688 100644 --- a/packages/game/src/lib/objects/treeObject.ts +++ b/packages/game/src/lib/objects/treeObject.ts @@ -54,6 +54,11 @@ export class TreeObject extends BaseObject implements GameObjectTree { } click() { + if (!this.addon.player || !this.addon.player?.canClick) { + return + } + + this.addon.player.click() this.chop() } diff --git a/packages/game/src/lib/objects/unit/playerObject.ts b/packages/game/src/lib/objects/unit/playerObject.ts index e00b583..8ac620d 100644 --- a/packages/game/src/lib/objects/unit/playerObject.ts +++ b/packages/game/src/lib/objects/unit/playerObject.ts @@ -17,6 +17,8 @@ export class PlayerObject extends UnitObject implements GameObjectPlayer { refuellerPoints!: number raiderPoints!: number lastActionAt: GameObjectPlayer['lastActionAt'] + canClick: boolean + nextClick: number public inventoryId?: string @@ -26,21 +28,17 @@ export class PlayerObject extends UnitObject implements GameObjectPlayer { this.telegramId = telegramId this.speedPerSecond = 70 this.lastActionAt = new Date() + this.canClick = true + this.nextClick = 0 } async initChar(character?: CharacterEditionWithCharacter): Promise { super.initVisual(character?.character?.codename ?? 'telegramo') } - updateCoins(amount: number): void { - this.coins = this.coins + amount - - // return db.player.update({ - // where: { id: this.id }, - // data: { - // coins: this.coins, - // }, - // }) + click(): void { + this.canClick = false + this.nextClick = 15 } addReputation(amount: number) { diff --git a/packages/game/src/lib/objects/unit/unitObject.ts b/packages/game/src/lib/objects/unit/unitObject.ts index 9f6e2be..94eb56c 100644 --- a/packages/game/src/lib/objects/unit/unitObject.ts +++ b/packages/game/src/lib/objects/unit/unitObject.ts @@ -116,11 +116,11 @@ export class UnitObject extends BaseObject implements GameObjectUnit { this.animationIdle.animationSpeed = 0.05 this.animationIdle.visible = true - if (this.direction === 'LEFT') { + if (this.direction === 'RIGHT') { this.animationIdle.scale.x = 4 this.animationIdle.play() } - if (this.direction === 'RIGHT') { + if (this.direction === 'LEFT') { this.animationIdle.scale.x = -4 this.animationIdle.play() } diff --git a/packages/game/src/lib/services/basePlayerService.ts b/packages/game/src/lib/services/basePlayerService.ts index c89a7ac..b634d47 100644 --- a/packages/game/src/lib/services/basePlayerService.ts +++ b/packages/game/src/lib/services/basePlayerService.ts @@ -7,7 +7,14 @@ export class BasePlayerService implements PlayerService { constructor(readonly addon: GameAddon) {} update() { - // this.#removeInactivePlayers() + // Debounce + if (this.addon.player?.canClick === false) { + this.addon.player.nextClick -= 1 + + if (this.addon.player.nextClick <= 0) { + this.addon.player.canClick = true + } + } } get activePlayers() { diff --git a/packages/game/src/lib/types.ts b/packages/game/src/lib/types.ts index 726a088..56d1581 100644 --- a/packages/game/src/lib/types.ts +++ b/packages/game/src/lib/types.ts @@ -91,13 +91,15 @@ export interface GameObjectPlayer extends GameObjectUnit { refuellerPoints: number raiderPoints: number lastActionAt: Date + canClick: boolean + nextClick: number + click: () => void initChar: (character?: CharacterEditionWithCharacter) => Promise updateLastActionAt: () => void addReputation: (amount: number) => void addVillainPoints: (amount: number) => void addRefuellerPoints: (amount: number) => void addRaiderPoints: (amount: number) => void - updateCoins: (amount: number) => void } export interface ServerService {