Skip to content

Commit

Permalink
new part or refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Jun 17, 2024
1 parent cca09cf commit ee65134
Show file tree
Hide file tree
Showing 65 changed files with 1,810 additions and 1,799 deletions.
16 changes: 0 additions & 16 deletions src/lib/game/actions/baseAction.ts

This file was deleted.

35 changes: 15 additions & 20 deletions src/lib/game/actions/donateWoodToVillageAction.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import { Village } from '../chunks'
import type { Warehouse } from '../objects/buildings/warehouse'
import { BaseAction } from './baseAction'
import { ANSWER } from '$lib/game/scenes/services/actionService'
import type { GameScene } from '$lib/game/types'

interface IDonateWoodToVillageActionOptions {
scene: GameScene
}
import { ANSWER } from '$lib/game/services/actionService'
import type { Game, GameObjectPlayer } from '$lib/game/types'
import type { GameAction } from '$lib/game/actions/interface'

export class DonateWoodToVillageAction extends BaseAction {
scene: GameScene
interface DonateWoodToVillageActionOptions {
game: Game
}

constructor({ scene }: IDonateWoodToVillageActionOptions) {
super({ command: 'donate', commandDescription: '!donate [quantity]' })
export class DonateWoodToVillageAction implements GameAction {
command = 'donate'
commandDescription = '!donate [quantity]'
game: Game

this.scene = scene
constructor({ game }: DonateWoodToVillageActionOptions) {
this.game = game
}

async live(player, params) {
const amount = this.scene.actionService.getAmountFromChatCommand(params[0])
async live(player: GameObjectPlayer, params: string[]) {
const amount = this.game.actionService.getAmountFromChatCommand(params[0])
if (!amount) {
return ANSWER.WRONG_AMOUNT_ERROR
}

let warehouse: Warehouse | undefined
if (this.scene.chunkNow instanceof Village) {
warehouse = this.scene.chunkNow.getWarehouse()
}
const warehouse = this.game.chunkService.chunk?.warehouse

const isSuccess = await player.inventory.reduceOrDestroyItem('WOOD', amount)
if (!isSuccess) {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/game/actions/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { GameObjectPlayer, IGameActionResponse } from '$lib/game/types'

export interface GameAction {
command: string
commandDescription: string
live: (
player: GameObjectPlayer,
params: string[],
) => Promise<IGameActionResponse>
}
32 changes: 16 additions & 16 deletions src/lib/game/actions/plantTreeAction.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { Village } from '../chunks'
import { PlantNewTreeScript } from '../scripts/plantNewTreeScript'
import { BaseAction } from './baseAction'
import { ANSWER } from '$lib/game/scenes/services/actionService'
import type { GameScene } from '$lib/game/types'
import { ANSWER } from '$lib/game/services/actionService'
import type { Game, GameObjectPlayer } from '$lib/game/types'
import { VillageChunk } from '$lib/game/services/chunk/villageChunk'
import type { GameAction } from '$lib/game/actions/interface'

interface IPlantTreeActionOptions {
scene: GameScene
game: Game
}

export class PlantTreeAction extends BaseAction {
scene: GameScene
export class PlantTreeAction implements GameAction {
command = 'plant'
commandDescription = '!plant'
game: Game

constructor({ scene }: IPlantTreeActionOptions) {
super({ command: 'plant', commandDescription: '!plant' })

this.scene = scene
constructor({ game }: IPlantTreeActionOptions) {
this.game = game
}

async live(player) {
async live(player: GameObjectPlayer) {
if (player.script && !player.script.isInterruptible) {
return ANSWER.BUSY_ERROR
}

if (this.scene.chunkNow instanceof Village) {
const target = this.scene.chunkNow.checkIfNeedToPlantTree()
if (this.game.chunkService.chunk instanceof VillageChunk) {
const target = this.game.chunkService.chunk.checkIfNeedToPlantTree()
if (!target) {
return ANSWER.NO_SPACE_AVAILABLE_ERROR
}

const plantNewTreeFunc = () => {
if (this.scene.chunkNow instanceof Village) {
this.scene.chunkNow.plantNewTree(target)
if (this.game.chunkService.chunk instanceof VillageChunk) {
this.game.chunkService.chunk.plantNewTree(target)
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/lib/game/actions/voteAction.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { Poll } from '../common'
import { BaseAction } from './baseAction'
import { ANSWER } from '$lib/game/scenes/services/actionService'
import { ANSWER } from '$lib/game/services/actionService'
import type { Poll } from '$lib/game/common/poll'
import type { GameObjectPlayer } from '$lib/game/types'
import type { GameAction } from '$lib/game/actions/interface'

interface IVoteActionOptions {
poll: Poll
}

export class VoteAction extends BaseAction {
export class VoteAction implements GameAction {
command: GameAction['command']
commandDescription: GameAction['commandDescription']
#poll: Poll
readonly #id: string

constructor({ poll }: IVoteActionOptions) {
super({ command: 'go', commandDescription: '!go' })

this.#id = poll.generatePollId()

this.command = `go ${this.#id}`
Expand All @@ -21,8 +22,8 @@ export class VoteAction extends BaseAction {
this.#poll = poll
}

async live(player) {
const isSuccess = this.poll.vote(player)
async live(player: GameObjectPlayer) {
const isSuccess = this.#poll.vote(player)
if (!isSuccess) {
return ANSWER.ALREADY_VOTED_ERROR
}
Expand Down
Loading

0 comments on commit ee65134

Please sign in to comment.