重构Api接口到Msg

This commit is contained in:
janing
2025-12-18 12:03:03 +08:00
parent 7301adbb43
commit c12e439add
15 changed files with 450 additions and 356 deletions

View File

@@ -0,0 +1,83 @@
import { MsgCall } from "tsrpc";
import { playerManager } from "../managers/PlayerManager";
import { MsgReqLogin } from "../shared/protocols/MsgReqLogin";
import { MsgResLogin, PlayerInfo } from "../shared/protocols/MsgResLogin";
/**
* 登录消息处理器
* 处理玩家登录流程:
* 1. 查找已有角色
* 2. 若无角色,自动注册并创建角色
* 3. 分配出生点
* 4. 返回角色信息
*/
export default async function (call: MsgCall<MsgReqLogin>) {
const { playerId, playerName } = call.msg;
// 验证玩家ID
if (!playerId || playerId.trim().length === 0) {
call.conn.sendMsg('ResLogin', {
success: false,
message: '玩家ID不能为空'
});
return;
}
try {
// 检查玩家是否已存在
const isNewPlayer = !playerManager.hasPlayer(playerId);
// 获取或创建玩家(自动处理注册和创角)
const player = playerManager.getOrCreatePlayer(playerId, playerName);
// 设置玩家在线状态
playerManager.setPlayerOnline(playerId, call.conn.id);
// 保存玩家ID到连接对象供其他消息处理器使用
(call.conn as any).playerId = playerId;
// 转换为协议格式
const playerInfo: PlayerInfo = {
id: player.id,
name: player.name,
position: { ...player.position },
spawnPoint: { ...player.spawnPoint },
hp: player.hp,
maxHp: player.maxHp,
isAlive: player.isAlive,
createdAt: player.createdAt,
lastLoginAt: player.lastLoginAt
};
// 广播玩家加入消息给其他在线玩家
const { broadcastToAll } = await import('../utils/broadcast');
broadcastToAll('PlayerJoin', {
playerId: player.id,
playerName: player.name,
position: { ...player.position },
isNewPlayer,
timestamp: Date.now()
}, call.conn.id);
// 返回成功结果
const response: MsgResLogin = {
success: true,
message: isNewPlayer ? '创建角色成功,欢迎来到游戏世界!' : '登录成功,欢迎回来!',
player: playerInfo,
isNewPlayer
};
call.conn.sendMsg('ResLogin', response);
console.log(`玩家 ${player.name} (${playerId}) ${isNewPlayer ? '首次登录' : '登录成功'}`);
console.log(` 在线玩家数: ${playerManager.getOnlinePlayerCount()}`);
} catch (error) {
console.error('登录失败:', error);
call.conn.sendMsg('ResLogin', {
success: false,
message: `登录失败: ${error instanceof Error ? error.message : '未知错误'}`
});
}
}

View File

@@ -0,0 +1,104 @@
import { MsgCall } from "tsrpc";
import { playerManager } from "../managers/PlayerManager";
import { MsgPlayerMove } from "../shared/protocols/MsgPlayerMove";
import { MsgReqMove } from "../shared/protocols/MsgReqMove";
import { broadcastToAll } from "../utils/broadcast";
/**
* 移动消息处理器
* 处理玩家移动请求,验证位置合法性,更新玩家位置并广播给其他玩家
*/
export default async function (call: MsgCall<MsgReqMove>) {
const { x, y } = call.msg;
// 从连接中获取玩家ID需要在登录时保存
const playerId = (call.conn as any).playerId;
if (!playerId) {
call.conn.sendMsg('ResMove', {
success: false,
message: '未登录,请先登录'
});
return;
}
// 获取玩家信息
const player = playerManager.getPlayer(playerId);
if (!player) {
call.conn.sendMsg('ResMove', {
success: false,
message: '玩家不存在'
});
return;
}
// 检查玩家是否存活
if (!player.isAlive) {
call.conn.sendMsg('ResMove', {
success: false,
message: '玩家已死亡,无法移动'
});
return;
}
// 验证坐标是否为数字
if (typeof x !== 'number' || typeof y !== 'number') {
call.conn.sendMsg('ResMove', {
success: false,
message: '坐标格式错误'
});
return;
}
// 验证坐标是否为整数
if (!Number.isInteger(x) || !Number.isInteger(y)) {
call.conn.sendMsg('ResMove', {
success: false,
message: '坐标必须为整数'
});
return;
}
try {
// 尝试更新玩家位置
const success = playerManager.updatePlayerPosition(playerId, x, y);
if (!success) {
call.conn.sendMsg('ResMove', {
success: false,
message: '移动失败,位置无效或超出世界边界'
});
return;
}
// 获取更新后的位置
const newPosition = { x, y };
// 广播移动消息给所有其他玩家
const moveMsg: MsgPlayerMove = {
playerId: player.id,
playerName: player.name,
position: newPosition,
timestamp: Date.now()
};
broadcastToAll('MsgPlayerMove', moveMsg, call.conn.id);
// 返回成功结果
call.conn.sendMsg('ResMove', {
success: true,
message: '移动成功',
position: newPosition
});
console.log(`玩家 ${player.name} 移动到 (${x}, ${y})`);
} catch (error) {
console.error('移动失败:', error);
call.conn.sendMsg('ResMove', {
success: false,
message: `移动失败: ${error instanceof Error ? error.message : '未知错误'}`
});
}
}

View File

@@ -0,0 +1,28 @@
import { MsgCall } from "tsrpc";
import { server } from "..";
import { MsgReqSend } from "../shared/protocols/MsgReqSend";
/**
* 发送消息处理器
*/
export default async function (call: MsgCall<MsgReqSend>) {
// 验证消息内容
if (call.msg.content.length === 0) {
call.conn.sendMsg('ResSend', {
time: new Date()
});
return;
}
// 返回成功响应
let time = new Date();
call.conn.sendMsg('ResSend', {
time: time
});
// 广播消息
server.broadcastMsg('Chat', {
content: call.msg.content,
time: time
});
}