206 lines
7.5 KiB
C#
206 lines
7.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ET.Server
|
|
{
|
|
[FriendOf(typeof(PlayerComponent))]
|
|
public static class BagConsoleHelper
|
|
{
|
|
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("AddItem")]
|
|
public class C_AddItem : IConsoleHandler
|
|
{
|
|
// AddItem [account] itemId count
|
|
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
|
|
{
|
|
string[] args = BagConsoleHelper.SplitArgs(content);
|
|
if (args.Length < 2 || args.Length > 3)
|
|
{
|
|
Log.Console("[GM] AddItem 参数错误: AddItem [account] itemId count");
|
|
return;
|
|
}
|
|
|
|
string account = args.Length == 3 ? args[0] : string.Empty;
|
|
int shift = args.Length == 3 ? 1 : 0;
|
|
if (!int.TryParse(args[0 + shift], out int itemId) || !long.TryParse(args[1 + shift], out long count))
|
|
{
|
|
Log.Console("[GM] AddItem 参数错误: itemId/count 非法");
|
|
return;
|
|
}
|
|
|
|
Player player = BagConsoleHelper.ResolvePlayer(fiber.Root, account);
|
|
BagComponent bag = player?.GetComponent<BagComponent>() ?? player?.AddComponent<BagComponent>();
|
|
if (player == null || bag == null)
|
|
{
|
|
Log.Console("[GM] AddItem 失败: 无在线玩家");
|
|
return;
|
|
}
|
|
|
|
ItemUnit item = bag.AddItem(itemId, count);
|
|
player.SetDirty();
|
|
LogExtensions.AuditGM("AddItem", $"{{\"account\":\"{player.Account}\",\"itemId\":{itemId},\"count\":{count},\"itemIndex\":{item?.ItemIndex ?? 0}}}");
|
|
Log.Console($"[GM] AddItem success account={player.Account} itemId={itemId} count={count}");
|
|
await ETTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[ConsoleHandler("RemoveItem")]
|
|
public class C_RemoveItem : IConsoleHandler
|
|
{
|
|
// RemoveItem [account] itemId count
|
|
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
|
|
{
|
|
string[] args = BagConsoleHelper.SplitArgs(content);
|
|
if (args.Length < 2 || args.Length > 3)
|
|
{
|
|
Log.Console("[GM] RemoveItem 参数错误: RemoveItem [account] itemId count");
|
|
return;
|
|
}
|
|
|
|
string account = args.Length == 3 ? args[0] : string.Empty;
|
|
int shift = args.Length == 3 ? 1 : 0;
|
|
if (!int.TryParse(args[0 + shift], out int itemId) || !long.TryParse(args[1 + shift], out long count))
|
|
{
|
|
Log.Console("[GM] RemoveItem 参数错误: itemId/count 非法");
|
|
return;
|
|
}
|
|
|
|
Player player = BagConsoleHelper.ResolvePlayer(fiber.Root, account);
|
|
BagComponent bag = player?.GetComponent<BagComponent>();
|
|
if (player == null || bag == null || !bag.RemoveItem(itemId, count))
|
|
{
|
|
Log.Console("[GM] RemoveItem 失败: 数量不足或玩家不存在");
|
|
return;
|
|
}
|
|
|
|
player.SetDirty();
|
|
LogExtensions.AuditGM("RemoveItem", $"{{\"account\":\"{player.Account}\",\"itemId\":{itemId},\"count\":{count}}}");
|
|
Log.Console($"[GM] RemoveItem success account={player.Account} itemId={itemId} count={count}");
|
|
await ETTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[ConsoleHandler("ShowBag")]
|
|
public class C_ShowBag : IConsoleHandler
|
|
{
|
|
// ShowBag [account]
|
|
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
|
|
{
|
|
Player player = BagConsoleHelper.ResolvePlayer(fiber.Root, content.Trim());
|
|
BagComponent bag = player?.GetComponent<BagComponent>();
|
|
if (bag == null)
|
|
{
|
|
Log.Console("[GM] ShowBag: 背包不存在");
|
|
return;
|
|
}
|
|
|
|
StringBuilder sb = new();
|
|
var items = bag.GetAllItems();
|
|
sb.Append($"[GM] ShowBag account={player.Account} count={items.Count}");
|
|
foreach (ItemUnit item in items)
|
|
{
|
|
sb.Append($" | idx={item.ItemIndex},itemId={item.ItemId},count={item.Count},heroId={item.HeroId}");
|
|
}
|
|
Log.Console(sb.ToString());
|
|
await ETTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[ConsoleHandler("BagClear")]
|
|
public class C_BagClear : IConsoleHandler
|
|
{
|
|
// BagClear [account]
|
|
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
|
|
{
|
|
Player player = BagConsoleHelper.ResolvePlayer(fiber.Root, content.Trim());
|
|
BagComponent bag = player?.GetComponent<BagComponent>();
|
|
if (bag == null)
|
|
{
|
|
Log.Console("[GM] BagClear: 背包不存在");
|
|
return;
|
|
}
|
|
|
|
bag.ClearBagKeepEquipped();
|
|
LogExtensions.AuditGM("BagClear", $"{{\"account\":\"{player.Account}\"}}");
|
|
Log.Console($"[GM] BagClear success account={player.Account}");
|
|
await ETTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
[ConsoleHandler("UseItem")]
|
|
public class C_UseItem : IConsoleHandler
|
|
{
|
|
// UseItem [account] itemIndex [count]
|
|
public async ETTask Run(Fiber fiber, ModeContex contex, string content)
|
|
{
|
|
string[] args = BagConsoleHelper.SplitArgs(content);
|
|
if (args.Length < 1 || args.Length > 3)
|
|
{
|
|
Log.Console("[GM] UseItem 参数错误: UseItem [account] itemIndex [count]");
|
|
return;
|
|
}
|
|
|
|
int shift = 0;
|
|
string account = string.Empty;
|
|
if (args.Length >= 2 && !long.TryParse(args[0], out _))
|
|
{
|
|
account = args[0];
|
|
shift = 1;
|
|
}
|
|
|
|
if (!long.TryParse(args[shift], out long itemIndex))
|
|
{
|
|
Log.Console("[GM] UseItem 参数错误: itemIndex 非法");
|
|
return;
|
|
}
|
|
|
|
int count = 1;
|
|
if (shift + 1 < args.Length)
|
|
{
|
|
int.TryParse(args[shift + 1], out count);
|
|
}
|
|
|
|
Player player = BagConsoleHelper.ResolvePlayer(fiber.Root, account);
|
|
string reason = string.Empty;
|
|
ItemUnit item = null;
|
|
if (player == null || !BagService.TryUseItem(player, itemIndex, count, out reason, out item))
|
|
{
|
|
Log.Console($"[GM] UseItem 失败: {reason}");
|
|
return;
|
|
}
|
|
|
|
LogExtensions.AuditGM("UseItem", $"{{\"account\":\"{player.Account}\",\"itemIndex\":{itemIndex},\"count\":{count},\"left\":{item?.Count ?? 0}}}");
|
|
Log.Console($"[GM] UseItem success account={player.Account} itemIndex={itemIndex} left={item?.Count ?? 0}");
|
|
await ETTask.CompletedTask;
|
|
}
|
|
}
|
|
}
|