303 lines
12 KiB
C#

using System;
using System.Linq;
using System.Text;
namespace ET.Server
{
[FriendOf(typeof(PlayerComponent))]
public static class HeroConsoleHelper
{
public static Player ResolvePlayer(Scene root, string account)
{
PlayerComponent playerComp = root.GetComponent<PlayerComponent>();
if (playerComp == null || playerComp.dictionary.Count == 0)
{
return null;
}
if (!string.IsNullOrWhiteSpace(account))
{
return playerComp.GetByAccount(account);
}
foreach (EntityRef<Player> value in playerComp.dictionary.Values)
{
return value;
}
return null;
}
public static string[] SplitArgs(string content)
{
string[] raw = content.Split(' ', StringSplitOptions.RemoveEmptyEntries);
return raw.Select(x => x.Trim()).Where(x => x.Length > 0).ToArray();
}
}
[ConsoleHandler("AddHero")]
public class C_AddHero : IConsoleHandler
{
// AddHero [account] configId
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
string account = args.Length > 1 ? args[0] : string.Empty;
string cfgText = args.Length > 1 ? args[1] : (args.Length == 1 ? args[0] : string.Empty);
if (!int.TryParse(cfgText, out int configId))
{
Log.Console("[GM] AddHero 参数错误: AddHero [account] configId");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
if (player == null)
{
Log.Console("[GM] AddHero 失败: 无在线玩家");
return;
}
Hero hero = HeroService.SummonHero(player, configId);
LogExtensions.AuditGM("AddHero", $"{{\"account\":\"{player.Account}\",\"heroId\":{hero.Id},\"configId\":{configId}}}");
Log.Console($"[GM] AddHero success account={player.Account} heroId={hero.Id} configId={configId}");
await ETTask.CompletedTask;
}
}
[ConsoleHandler("SetHeroLevel")]
[FriendOf(typeof(Hero))]
public class C_SetHeroLevel : IConsoleHandler
{
// SetHeroLevel [account] heroId level
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
if (args.Length < 2 || args.Length > 3)
{
Log.Console("[GM] SetHeroLevel 参数错误: SetHeroLevel [account] heroId level");
return;
}
string account = args.Length == 3 ? args[0] : string.Empty;
int shift = args.Length == 3 ? 1 : 0;
if (!long.TryParse(args[0 + shift], out long heroId) || !int.TryParse(args[1 + shift], out int level))
{
Log.Console("[GM] SetHeroLevel 参数错误: heroId/level 非法");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
Hero hero = player?.GetComponent<HeroComponent>()?.GetHero(heroId);
if (hero == null)
{
Log.Console("[GM] SetHeroLevel 失败: hero 不存在");
return;
}
int before = hero.Level;
hero.Level = Math.Clamp(level, 1, 60);
player.SetDirty();
LogExtensions.AuditGM("SetHeroLevel", $"{{\"account\":\"{player.Account}\",\"heroId\":{heroId},\"before\":{before},\"after\":{hero.Level}}}");
Log.Console($"[GM] SetHeroLevel success account={player.Account} heroId={heroId} level={hero.Level}");
await ETTask.CompletedTask;
}
}
[ConsoleHandler("SetHeroSkill")]
[FriendOf(typeof(Hero))]
public class C_SetHeroSkill : IConsoleHandler
{
// SetHeroSkill [account] heroId skillIndex level
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
if (args.Length < 3 || args.Length > 4)
{
Log.Console("[GM] SetHeroSkill 参数错误: SetHeroSkill [account] heroId skillIndex level");
return;
}
string account = args.Length == 4 ? args[0] : string.Empty;
int shift = args.Length == 4 ? 1 : 0;
if (!long.TryParse(args[0 + shift], out long heroId) ||
!int.TryParse(args[1 + shift], out int skillIndex) ||
!int.TryParse(args[2 + shift], out int level))
{
Log.Console("[GM] SetHeroSkill 参数错误: heroId/skillIndex/level 非法");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
Hero hero = player?.GetComponent<HeroComponent>()?.GetHero(heroId);
if (hero == null || skillIndex < 0 || skillIndex >= hero.Skills.Count)
{
Log.Console("[GM] SetHeroSkill 失败: hero 或 skillIndex 不存在");
return;
}
int before = hero.Skills[skillIndex];
hero.Skills[skillIndex] = Math.Clamp(level, 0, 10);
player.SetDirty();
LogExtensions.AuditGM("SetHeroSkill", $"{{\"account\":\"{player.Account}\",\"heroId\":{heroId},\"skillIndex\":{skillIndex},\"before\":{before},\"after\":{hero.Skills[skillIndex]}}}");
Log.Console($"[GM] SetHeroSkill success account={player.Account} heroId={heroId} skillIndex={skillIndex} level={hero.Skills[skillIndex]}");
await ETTask.CompletedTask;
}
}
[ConsoleHandler("ShowHeroes")]
[FriendOf(typeof(Hero))]
public class C_ShowHeroes : IConsoleHandler
{
// ShowHeroes [account]
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, content.Trim());
HeroComponent heroComp = player?.GetComponent<HeroComponent>();
if (heroComp == null)
{
Log.Console("[GM] ShowHeroes: 无英雄数据");
return;
}
var heroes = heroComp.GetAllHeroes();
StringBuilder sb = new();
sb.Append($"[GM] ShowHeroes account={player.Account} count={heroes.Count}");
foreach (Hero hero in heroes)
{
sb.Append($" | id={hero.Id},cfg={hero.ConfigId},lv={hero.Level},star={hero.Star},skills=[{string.Join(",", hero.Skills)}]");
}
Log.Console(sb.ToString());
await ETTask.CompletedTask;
}
}
[ConsoleHandler("SummonHero")]
public class C_SummonHero : IConsoleHandler
{
// SummonHero [account] configId
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
await new C_AddHero().Run(fiber, contex, content);
}
}
[ConsoleHandler("HeroAddExp")]
[FriendOf(typeof(Hero))]
public class C_HeroAddExp : IConsoleHandler
{
// HeroAddExp [account] heroId itemId count
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
if (args.Length < 3 || args.Length > 4)
{
Log.Console("[GM] HeroAddExp 参数错误: HeroAddExp [account] heroId itemId count");
return;
}
string account = args.Length == 4 ? args[0] : string.Empty;
int shift = args.Length == 4 ? 1 : 0;
if (!long.TryParse(args[0 + shift], out long heroId) ||
!int.TryParse(args[1 + shift], out int itemId) ||
!int.TryParse(args[2 + shift], out int count))
{
Log.Console("[GM] HeroAddExp 参数错误: heroId/itemId/count 非法");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
Hero hero = player?.GetComponent<HeroComponent>()?.GetHero(heroId);
if (hero == null)
{
Log.Console("[GM] HeroAddExp 失败: hero 不存在");
return;
}
string reason = string.Empty;
if (!HeroService.TryAddExp(player, hero, itemId, count, out reason))
{
Log.Console($"[GM] HeroAddExp 失败: {reason}");
return;
}
LogExtensions.AuditGM("HeroAddExp", $"{{\"account\":\"{player.Account}\",\"heroId\":{heroId},\"itemId\":{itemId},\"count\":{count},\"level\":{hero.Level}}}");
Log.Console($"[GM] HeroAddExp success account={player.Account} heroId={heroId} level={hero.Level} exp={hero.Exp}");
await ETTask.CompletedTask;
}
}
[ConsoleHandler("HeroWearEquip")]
public class C_HeroWearEquip : IConsoleHandler
{
// HeroWearEquip [account] heroId itemIndex slotIndex
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
if (args.Length < 3 || args.Length > 4)
{
Log.Console("[GM] HeroWearEquip 参数错误: HeroWearEquip [account] heroId itemIndex slotIndex");
return;
}
string account = args.Length == 4 ? args[0] : string.Empty;
int shift = args.Length == 4 ? 1 : 0;
if (!long.TryParse(args[0 + shift], out long heroId) ||
!long.TryParse(args[1 + shift], out long itemIndex) ||
!int.TryParse(args[2 + shift], out int slotIndex))
{
Log.Console("[GM] HeroWearEquip 参数错误");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
string reason = string.Empty;
if (player == null || !HeroEquipService.TryWearEquip(player, heroId, itemIndex, slotIndex, out reason))
{
Log.Console($"[GM] HeroWearEquip 失败: {reason}");
return;
}
LogExtensions.AuditGM("HeroWearEquip", $"{{\"account\":\"{player.Account}\",\"heroId\":{heroId},\"itemIndex\":{itemIndex},\"slotIndex\":{slotIndex}}}");
Log.Console($"[GM] HeroWearEquip success account={player.Account} heroId={heroId} itemIndex={itemIndex}");
await ETTask.CompletedTask;
}
}
[ConsoleHandler("HeroTakeOffEquip")]
public class C_HeroTakeOffEquip : IConsoleHandler
{
// HeroTakeOffEquip [account] heroId slotIndex
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
{
string[] args = HeroConsoleHelper.SplitArgs(content);
if (args.Length < 2 || args.Length > 3)
{
Log.Console("[GM] HeroTakeOffEquip 参数错误: HeroTakeOffEquip [account] heroId slotIndex");
return;
}
string account = args.Length == 3 ? args[0] : string.Empty;
int shift = args.Length == 3 ? 1 : 0;
if (!long.TryParse(args[0 + shift], out long heroId) || !int.TryParse(args[1 + shift], out int slotIndex))
{
Log.Console("[GM] HeroTakeOffEquip 参数错误");
return;
}
Player player = HeroConsoleHelper.ResolvePlayer(fiber.Root, account);
long itemIndex = 0;
string reason = string.Empty;
if (player == null || !HeroEquipService.TryTakeOffEquip(player, heroId, slotIndex, out itemIndex, out reason))
{
Log.Console($"[GM] HeroTakeOffEquip 失败: {reason}");
return;
}
LogExtensions.AuditGM("HeroTakeOffEquip", $"{{\"account\":\"{player.Account}\",\"heroId\":{heroId},\"slotIndex\":{slotIndex},\"itemIndex\":{itemIndex}}}");
Log.Console($"[GM] HeroTakeOffEquip success account={player.Account} heroId={heroId} itemIndex={itemIndex}");
await ETTask.CompletedTask;
}
}
}