From 9989d60ee40596281d85950e2883aa9f3769a132 Mon Sep 17 00:00:00 2001 From: janing Date: Sun, 14 Dec 2025 22:38:09 +0800 Subject: [PATCH] =?UTF-8?q?Shared=E5=90=8C=E6=AD=A5=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/assets/scripts/Shared.meta | 9 + client/assets/scripts/Shared/protocols.meta | 9 + .../scripts/Shared/protocols/MsgChat.ts | 7 + .../scripts/Shared/protocols/MsgChat.ts.meta | 9 + .../scripts/Shared/protocols/MsgPlayerJoin.ts | 22 + .../Shared/protocols/MsgPlayerJoin.ts.meta | 9 + .../scripts/Shared/protocols/MsgPlayerMove.ts | 19 + .../Shared/protocols/MsgPlayerMove.ts.meta | 9 + .../scripts/Shared/protocols/PtlLogin.ts | 61 +++ .../scripts/Shared/protocols/PtlLogin.ts.meta | 9 + .../scripts/Shared/protocols/PtlMove.ts | 26 ++ .../scripts/Shared/protocols/PtlMove.ts.meta | 9 + .../scripts/Shared/protocols/PtlSend.ts | 10 + .../scripts/Shared/protocols/PtlSend.ts.meta | 9 + .../assets/scripts/Shared/protocols/base.ts | 23 ++ .../scripts/Shared/protocols/base.ts.meta | 9 + .../scripts/Shared/protocols/serviceProto.ts | 378 ++++++++++++++++++ .../Shared/protocols/serviceProto.ts.meta | 9 + client/sync-shared.js | 102 +++++ 19 files changed, 738 insertions(+) create mode 100644 client/assets/scripts/Shared.meta create mode 100644 client/assets/scripts/Shared/protocols.meta create mode 100644 client/assets/scripts/Shared/protocols/MsgChat.ts create mode 100644 client/assets/scripts/Shared/protocols/MsgChat.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts create mode 100644 client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/MsgPlayerMove.ts create mode 100644 client/assets/scripts/Shared/protocols/MsgPlayerMove.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/PtlLogin.ts create mode 100644 client/assets/scripts/Shared/protocols/PtlLogin.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/PtlMove.ts create mode 100644 client/assets/scripts/Shared/protocols/PtlMove.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/PtlSend.ts create mode 100644 client/assets/scripts/Shared/protocols/PtlSend.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/base.ts create mode 100644 client/assets/scripts/Shared/protocols/base.ts.meta create mode 100644 client/assets/scripts/Shared/protocols/serviceProto.ts create mode 100644 client/assets/scripts/Shared/protocols/serviceProto.ts.meta create mode 100644 client/sync-shared.js diff --git a/client/assets/scripts/Shared.meta b/client/assets/scripts/Shared.meta new file mode 100644 index 0000000..0c84ca3 --- /dev/null +++ b/client/assets/scripts/Shared.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "d880422b-7474-4694-9f01-138e7df9414d", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols.meta b/client/assets/scripts/Shared/protocols.meta new file mode 100644 index 0000000..76004a7 --- /dev/null +++ b/client/assets/scripts/Shared/protocols.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "bfd5a75c-43bc-41e7-a05b-a4e75d7ff56b", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/MsgChat.ts b/client/assets/scripts/Shared/protocols/MsgChat.ts new file mode 100644 index 0000000..b18ed7b --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgChat.ts @@ -0,0 +1,7 @@ +// This is a demo code file +// Feel free to delete it + +export interface MsgChat { + content: string, + time: Date +} \ No newline at end of file diff --git a/client/assets/scripts/Shared/protocols/MsgChat.ts.meta b/client/assets/scripts/Shared/protocols/MsgChat.ts.meta new file mode 100644 index 0000000..f05d21d --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgChat.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "b478ded6-3b0f-4383-8369-02afbd456031", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts b/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts new file mode 100644 index 0000000..28c3d40 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts @@ -0,0 +1,22 @@ +import { Position } from './base'; + +/** + * 玩家加入游戏广播消息 + * 当有新玩家登录或加入游戏时,广播给所有在线玩家 + */ +export interface MsgPlayerJoin { + /** 加入的玩家ID */ + playerId: string; + + /** 玩家昵称 */ + playerName: string; + + /** 玩家位置 */ + position: Position; + + /** 是否新玩家 */ + isNewPlayer: boolean; + + /** 加入时间戳 */ + timestamp: number; +} diff --git a/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts.meta b/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts.meta new file mode 100644 index 0000000..c9bb325 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgPlayerJoin.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "bb38d10f-d355-4311-a606-6981836fe8bf", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts b/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts new file mode 100644 index 0000000..0ea3694 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts @@ -0,0 +1,19 @@ +import { Position } from './base'; + +/** + * 玩家移动广播消息 + * 当有玩家移动时,广播给所有在线玩家 + */ +export interface MsgPlayerMove { + /** 移动的玩家ID */ + playerId: string; + + /** 玩家昵称 */ + playerName: string; + + /** 移动后的位置 */ + position: Position; + + /** 移动时间戳 */ + timestamp: number; +} diff --git a/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts.meta b/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts.meta new file mode 100644 index 0000000..9eaa9ca --- /dev/null +++ b/client/assets/scripts/Shared/protocols/MsgPlayerMove.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "c1357c6f-8bdc-4760-964a-1796582de8db", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/PtlLogin.ts b/client/assets/scripts/Shared/protocols/PtlLogin.ts new file mode 100644 index 0000000..f8a3ab2 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlLogin.ts @@ -0,0 +1,61 @@ +import { Position } from './base'; + +/** + * 登录请求 + */ +export interface ReqLogin { + /** 玩家ID(用于识别玩家) */ + playerId: string; + + /** 玩家昵称(可选,新玩家时使用) */ + playerName?: string; +} + +/** + * 玩家角色信息 + */ +export interface PlayerInfo { + /** 玩家ID */ + id: string; + + /** 玩家昵称 */ + name: string; + + /** 当前位置 */ + position: Position; + + /** 出生点 */ + spawnPoint: Position; + + /** 当前生命值 */ + hp: number; + + /** 最大生命值 */ + maxHp: number; + + /** 是否存活 */ + isAlive: boolean; + + /** 创建时间 */ + createdAt: number; + + /** 最后登录时间 */ + lastLoginAt: number; +} + +/** + * 登录响应 + */ +export interface ResLogin { + /** 是否成功 */ + success: boolean; + + /** 消息 */ + message: string; + + /** 玩家信息 */ + player?: PlayerInfo; + + /** 是否新玩家 */ + isNewPlayer?: boolean; +} diff --git a/client/assets/scripts/Shared/protocols/PtlLogin.ts.meta b/client/assets/scripts/Shared/protocols/PtlLogin.ts.meta new file mode 100644 index 0000000..8bd8f29 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlLogin.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "db7f0511-7e71-4088-9822-186f68082db6", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/PtlMove.ts b/client/assets/scripts/Shared/protocols/PtlMove.ts new file mode 100644 index 0000000..7fe831c --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlMove.ts @@ -0,0 +1,26 @@ +import { Position } from './base'; + +/** + * 移动请求 + */ +export interface ReqMove { + /** 目标位置 X 坐标 */ + x: number; + + /** 目标位置 Y 坐标 */ + y: number; +} + +/** + * 移动响应 + */ +export interface ResMove { + /** 是否成功 */ + success: boolean; + + /** 消息 */ + message?: string; + + /** 实际移动后的位置 */ + position?: Position; +} diff --git a/client/assets/scripts/Shared/protocols/PtlMove.ts.meta b/client/assets/scripts/Shared/protocols/PtlMove.ts.meta new file mode 100644 index 0000000..e30337e --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlMove.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "9fdf30ed-1223-4112-81f6-75339229fd1b", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/PtlSend.ts b/client/assets/scripts/Shared/protocols/PtlSend.ts new file mode 100644 index 0000000..a732699 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlSend.ts @@ -0,0 +1,10 @@ +// This is a demo code file +// Feel free to delete it + +export interface ReqSend { + content: string +} + +export interface ResSend { + time: Date +} \ No newline at end of file diff --git a/client/assets/scripts/Shared/protocols/PtlSend.ts.meta b/client/assets/scripts/Shared/protocols/PtlSend.ts.meta new file mode 100644 index 0000000..ce2e4d2 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/PtlSend.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "4414c606-7995-4f83-b83c-30c9fe840a6b", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/base.ts b/client/assets/scripts/Shared/protocols/base.ts new file mode 100644 index 0000000..cdf5c5d --- /dev/null +++ b/client/assets/scripts/Shared/protocols/base.ts @@ -0,0 +1,23 @@ +export interface BaseRequest { + +} + +export interface BaseResponse { + +} + +export interface BaseConf { + +} + +export interface BaseMessage { + +} + +/** + * 位置坐标 + */ +export interface Position { + x: number; + y: number; +} \ No newline at end of file diff --git a/client/assets/scripts/Shared/protocols/base.ts.meta b/client/assets/scripts/Shared/protocols/base.ts.meta new file mode 100644 index 0000000..b02d8fa --- /dev/null +++ b/client/assets/scripts/Shared/protocols/base.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "2024ee2b-052f-472d-81c4-8aa59cd65029", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/assets/scripts/Shared/protocols/serviceProto.ts b/client/assets/scripts/Shared/protocols/serviceProto.ts new file mode 100644 index 0000000..53be2c6 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/serviceProto.ts @@ -0,0 +1,378 @@ +import { ServiceProto } from 'tsrpc-proto'; +import { MsgChat } from './MsgChat'; +import { MsgPlayerJoin } from './MsgPlayerJoin'; +import { MsgPlayerMove } from './MsgPlayerMove'; +import { ReqLogin, ResLogin } from './PtlLogin'; +import { ReqMove, ResMove } from './PtlMove'; +import { ReqSend, ResSend } from './PtlSend'; + +export interface ServiceType { + api: { + "Login": { + req: ReqLogin, + res: ResLogin + }, + "Move": { + req: ReqMove, + res: ResMove + }, + "Send": { + req: ReqSend, + res: ResSend + } + }, + msg: { + "Chat": MsgChat, + "PlayerJoin": MsgPlayerJoin, + "PlayerMove": MsgPlayerMove + } +} + +export const serviceProto: ServiceProto = { + "version": 3, + "services": [ + { + "id": 0, + "name": "Chat", + "type": "msg" + }, + { + "id": 3, + "name": "PlayerJoin", + "type": "msg" + }, + { + "id": 4, + "name": "PlayerMove", + "type": "msg" + }, + { + "id": 2, + "name": "Login", + "type": "api" + }, + { + "id": 5, + "name": "Move", + "type": "api" + }, + { + "id": 1, + "name": "Send", + "type": "api" + } + ], + "types": { + "MsgChat/MsgChat": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "time", + "type": { + "type": "Date" + } + } + ] + }, + "MsgPlayerJoin/MsgPlayerJoin": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "playerId", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "playerName", + "type": { + "type": "String" + } + }, + { + "id": 2, + "name": "position", + "type": { + "type": "Reference", + "target": "base/Position" + } + }, + { + "id": 3, + "name": "isNewPlayer", + "type": { + "type": "Boolean" + } + }, + { + "id": 4, + "name": "timestamp", + "type": { + "type": "Number" + } + } + ] + }, + "base/Position": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "x", + "type": { + "type": "Number" + } + }, + { + "id": 1, + "name": "y", + "type": { + "type": "Number" + } + } + ] + }, + "MsgPlayerMove/MsgPlayerMove": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "playerId", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "playerName", + "type": { + "type": "String" + } + }, + { + "id": 2, + "name": "position", + "type": { + "type": "Reference", + "target": "base/Position" + } + }, + { + "id": 3, + "name": "timestamp", + "type": { + "type": "Number" + } + } + ] + }, + "PtlLogin/ReqLogin": { + "type": "Interface", + "properties": [ + { + "id": 1, + "name": "playerId", + "type": { + "type": "String" + } + }, + { + "id": 2, + "name": "playerName", + "type": { + "type": "String" + }, + "optional": true + } + ] + }, + "PtlLogin/ResLogin": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "success", + "type": { + "type": "Boolean" + } + }, + { + "id": 1, + "name": "message", + "type": { + "type": "String" + } + }, + { + "id": 4, + "name": "player", + "type": { + "type": "Reference", + "target": "PtlLogin/PlayerInfo" + }, + "optional": true + }, + { + "id": 5, + "name": "isNewPlayer", + "type": { + "type": "Boolean" + }, + "optional": true + } + ] + }, + "PtlLogin/PlayerInfo": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "id", + "type": { + "type": "String" + } + }, + { + "id": 1, + "name": "name", + "type": { + "type": "String" + } + }, + { + "id": 2, + "name": "position", + "type": { + "type": "Reference", + "target": "base/Position" + } + }, + { + "id": 3, + "name": "spawnPoint", + "type": { + "type": "Reference", + "target": "base/Position" + } + }, + { + "id": 4, + "name": "hp", + "type": { + "type": "Number" + } + }, + { + "id": 5, + "name": "maxHp", + "type": { + "type": "Number" + } + }, + { + "id": 6, + "name": "isAlive", + "type": { + "type": "Boolean" + } + }, + { + "id": 7, + "name": "createdAt", + "type": { + "type": "Number" + } + }, + { + "id": 8, + "name": "lastLoginAt", + "type": { + "type": "Number" + } + } + ] + }, + "PtlMove/ReqMove": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "x", + "type": { + "type": "Number" + } + }, + { + "id": 1, + "name": "y", + "type": { + "type": "Number" + } + } + ] + }, + "PtlMove/ResMove": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "success", + "type": { + "type": "Boolean" + } + }, + { + "id": 1, + "name": "message", + "type": { + "type": "String" + }, + "optional": true + }, + { + "id": 2, + "name": "position", + "type": { + "type": "Reference", + "target": "base/Position" + }, + "optional": true + } + ] + }, + "PtlSend/ReqSend": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "content", + "type": { + "type": "String" + } + } + ] + }, + "PtlSend/ResSend": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "time", + "type": { + "type": "Date" + } + } + ] + } + } +}; \ No newline at end of file diff --git a/client/assets/scripts/Shared/protocols/serviceProto.ts.meta b/client/assets/scripts/Shared/protocols/serviceProto.ts.meta new file mode 100644 index 0000000..57e1882 --- /dev/null +++ b/client/assets/scripts/Shared/protocols/serviceProto.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "a67c105c-09d0-4f7e-acc7-4e2c94dc1360", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/client/sync-shared.js b/client/sync-shared.js new file mode 100644 index 0000000..32f3723 --- /dev/null +++ b/client/sync-shared.js @@ -0,0 +1,102 @@ +#!/usr/bin/env node + +/** + * 同步服务端 shared 目录到客户端 + * 用法: node sync-shared.js + */ + +const fs = require('fs-extra'); +const path = require('path'); + +// 路径配置 +const SERVER_SHARED_PATH = path.resolve(__dirname, '../server/src/shared'); +const CLIENT_SHARED_PATH = path.resolve(__dirname, 'assets/scripts/Shared'); + +/** + * 同步目录 + */ +async function syncShared() { + console.log('=========================================='); + console.log('开始同步 Shared 协议目录'); + console.log('=========================================='); + console.log('源目录:', SERVER_SHARED_PATH); + console.log('目标目录:', CLIENT_SHARED_PATH); + console.log(''); + + try { + // 检查源目录是否存在 + if (!fs.existsSync(SERVER_SHARED_PATH)) { + console.error('❌ 错误: 服务端 shared 目录不存在!'); + console.error(' 路径:', SERVER_SHARED_PATH); + console.error(' 请确保服务端项目在正确的位置'); + process.exit(1); + } + + // 确保目标目录存在 + await fs.ensureDir(CLIENT_SHARED_PATH); + + // 复制目录 + console.log('📦 正在复制文件...'); + await fs.copy(SERVER_SHARED_PATH, CLIENT_SHARED_PATH, { + overwrite: true, + errorOnExist: false, + filter: (src) => { + // 过滤掉不需要的文件 + const basename = path.basename(src); + if (basename === 'node_modules' || basename === '.git') { + return false; + } + return true; + } + }); + + console.log('✅ 同步完成!'); + console.log(''); + + // 列出同步的文件 + const files = await getFileList(CLIENT_SHARED_PATH); + console.log(`共同步 ${files.length} 个文件:`); + files.forEach(file => { + const relativePath = path.relative(CLIENT_SHARED_PATH, file); + console.log(' -', relativePath); + }); + + console.log(''); + console.log('=========================================='); + + } catch (error) { + console.error('❌ 同步失败:', error.message); + process.exit(1); + } +} + +/** + * 递归获取目录下所有文件 + */ +async function getFileList(dir) { + const files = []; + + async function walk(currentPath) { + const items = await fs.readdir(currentPath); + + for (const item of items) { + const itemPath = path.join(currentPath, item); + const stat = await fs.stat(itemPath); + + if (stat.isDirectory()) { + await walk(itemPath); + } else { + files.push(itemPath); + } + } + } + + await walk(dir); + return files; +} + +// 执行同步 +syncShared().catch(error => { + console.error('未知错误:', error); + process.exit(1); +});