Skip to content

Commit

Permalink
Add /dungeon command(also GMTalk)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sycamore0 committed Oct 5, 2024
1 parent 9614053 commit 845271b
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 10 deletions.
57 changes: 57 additions & 0 deletions src/cli/commands/worldCommands/dungeonCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import SceneData from '$/gameData/data/SceneData'
import DungeonData from '$/gameData/data/DungeonData'
import Vector from '$/utils/vector'
import translate from '@/translate'
import { SceneEnterReasonEnum, SceneEnterTypeEnum } from '@/types/proto/enum'
import { CommandDefinition } from '..'

const dungeonCommand: CommandDefinition = {
name: 'dungeon',
usage: 2,
args: [
{ name: 'id', type: 'int' },
{ name: 'uidInput', type: 'str', optional: true }
],
allowPlayer: true,
exec: async (cmdInfo) => {
const { args, sender, cli, kcpServer } = cmdInfo
const { print, printError } = cli
const [id, uidInput] = args

let uid;
if (uidInput === '@s' || uidInput === undefined) {
uid = sender?.uid;
} else if (!isNaN(parseInt(uidInput))) {
uid = parseInt(uidInput);
} else {
return printError(translate('generic.invalidTarget'));
}

const player = kcpServer.game.getPlayerByUid(uid || sender?.uid)
if (!player) return printError(translate('generic.playerNotFound'))

const { currentWorld, currentScene, context } = player
if (!currentWorld) return printError(translate('generic.notInWorld'))

const sceneId = await DungeonData.getSceneByDungeon(id)

const scene = await currentWorld.getScene(sceneId)
const sceneData = await SceneData.getScene(sceneId)
if (!scene || !sceneData) return printError(translate('cli.commands.dungeon.error.sceneNotFound'))
if (currentScene === scene) return printError(translate('cli.commands.dungeon.error.sameScene'))

print(translate('cli.commands.dungeon.info.changeScene', id, scene.id))

const { BornPos, BornRot } = sceneData

const pos = new Vector()
const rot = new Vector()

pos.setData(BornPos)
rot.setData(BornRot)

scene.join(context, pos, rot, SceneEnterTypeEnum.ENTER_DUNGEON, SceneEnterReasonEnum.DUNGEON_ENTER)
}
}

export default dungeonCommand
4 changes: 3 additions & 1 deletion src/cli/commands/worldCommands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandDefinition } from '..'
import coopCommand from './coopCommand'
import dungeonCommand from './dungeonCommand'
import posCommand from './posCommand'
import sceneCommand from './sceneCommand'
import tpCommand from './tpCommand'
Expand All @@ -8,7 +9,8 @@ const worldCommands: CommandDefinition[] = [
posCommand,
sceneCommand,
tpCommand,
coopCommand
coopCommand,
dungeonCommand
]

export default worldCommands
4 changes: 4 additions & 0 deletions src/kcpServer/game/gameData/data/DungeonData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class DungeonDataLoader extends Loader {
return (await this.getDungeonList()).find(data => data.SceneId === sceneId)
}

async getSceneByDungeon(id: number): Promise<number> {
return (await this.getDungeon(id))?.SceneId
}

async getDungeonList(): Promise<DungeonData[]> {
return (await this.getData())?.Dungeon || []
}
Expand Down
30 changes: 25 additions & 5 deletions src/kcpServer/packets/GmTalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ChangeHpReasonEnum, PlayerDieTypeEnum, ProtEntityTypeEnum, RetcodeEnum,
import Logger from '@/logger'
import Gadget from '$/entity/gadget'
import SceneData from '$/gameData/data/SceneData'
import DungeonData from '$/gameData/data/DungeonData'
const logger = new Logger('GMTALK')

export interface GmTalkReq {
Expand Down Expand Up @@ -108,21 +109,37 @@ class GmTalkPacket extends Packet implements PacketInterface {
await player.currentScene.entityManager.add(gadget)
}

private async gmtJump(context: PacketContext, id: number) {
const { player } = context

const scene = await player.currentWorld.getScene(id)
const sceneData = await SceneData.getScene(id)
private async getSceneData(sceneId: number) {
const sceneData = await SceneData.getScene(sceneId)

const { BornPos, BornRot } = sceneData
const pos = new Vector()
const rot = new Vector()
pos.setData(BornPos)
rot.setData(BornRot)

return { pos, rot }
}

private async gmtJump(context: PacketContext, id: number) {
const { player } = context

const scene = await player.currentWorld.getScene(id)
const { pos, rot } = await this.getSceneData(id)

scene.join(context, pos, rot, SceneEnterTypeEnum.ENTER_JUMP, SceneEnterReasonEnum.TRANS_POINT)
}

private async gmtDungeon(context: PacketContext, id: number) {
const { player } = context
const sceneId = await DungeonData.getSceneByDungeon(id)

const scene = await player.currentWorld.getScene(sceneId)
const { pos, rot } = await this.getSceneData(sceneId)

scene.join(context, pos, rot, SceneEnterTypeEnum.ENTER_DUNGEON, SceneEnterReasonEnum.DUNGEON_ENTER)
}

async request(context: PacketContext, data: GmTalkReq): Promise<void> {
const { msg } = data
const cmd = msg?.split(' ')?.[0]?.toLowerCase()
Expand Down Expand Up @@ -155,6 +172,9 @@ class GmTalkPacket extends Packet implements PacketInterface {
case 'jump':
await this.gmtJump(context, Number(args[0]))
break
case 'dungeon':
await this.gmtDungeon(context, Number(args[0]))
break
default:
logger.warn(`Unsupported GM command: ${msg}`)
await this.response(context, { retcode: RetcodeEnum.RET_UNKNOWN_ERROR })
Expand Down
14 changes: 14 additions & 0 deletions src/translate/data/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@
"sameScene": "Same scene."
}
},
"dungeon": {
"desc": "Enter dungeon",
"usage": {
"0": "dungeon <id> <uidInput> - Enter dungeon for player",
"1": "dungeon <id> - (In game) Enter dungeon for yourself"
},
"info": {
"changeScene": "Enter dungeon: %0(Scene: %1)"
},
"error": {
"sceneNotFound": "Scene not found.",
"sameScene": "Same scene."
}
},
"tp": {
"desc": "Teleport to location",
"usage": {
Expand Down
16 changes: 15 additions & 1 deletion src/translate/data/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
}
},
"scene": {
"desc": "改变场景",
"desc": "进入场景",
"usage": {
"0": "scene <id> <uidInput> - 将该玩家传送到该场景",
"1": "scene <id> - (游戏中)改变自己所在的场景"
Expand All @@ -585,6 +585,20 @@
"sameScene": "处于同一场景中."
}
},
"dungeon": {
"desc": "进入秘境",
"usage": {
"0": "dungeon <id> <uidInput> - 将该玩家传送到该秘境",
"1": "dungeon <id> - (游戏中)改变自己所在的秘境"
},
"info": {
"changeScene": "传送到秘境: %0(场景: %1)"
},
"error": {
"sceneNotFound": "该场景未找到.",
"sameScene": "处于同一场景中."
}
},
"tp": {
"desc": "传送到某位置",
"usage": {
Expand Down
20 changes: 17 additions & 3 deletions src/translate/data/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -572,17 +572,31 @@
}
},
"scene": {
"desc": "場景改變",
"desc": "進入場景",
"usage": {
"0": "scene <id> <uidInput> - 將該玩家傳送到該場景",
"1": "scene <id> - (遊戲中)為自己改變場景"
"1": "scene <id> - (遊戲中)改變自己所在的場景"
},
"info": {
"changeScene": "修改場景到: %0"
},
"error": {
"sceneNotFound": "該場景未找到.",
"sameScene": "相同的場景."
"sameScene": "處於同一場景中."
}
},
"dungeon": {
"desc": "進入秘境",
"usage": {
"0": "dungeon <id> <uidInput> - 將該玩家傳送到該秘境",
"1": "dungeon <id> - (遊戲中)改變自己所在的秘境"
},
"info": {
"changeScene": "傳送到秘境: %0(場景: %1)"
},
"error": {
"sceneNotFound": "該場景未找到.",
"sameScene": "處於同一場景中."
}
},
"tp": {
Expand Down

0 comments on commit 845271b

Please sign in to comment.