CTT/Server/Hotfix/Game/Helper/GMTool.cs

801 lines
36 KiB
C#
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Cal.DataTable;
namespace ET
{
public static class GMTool
{
public static async ETTask<string> Execute(Unit unit, string chatContent)
{
if (chatContent.StartsWith("cmd", StringComparison.OrdinalIgnoreCase))
{
string[] arr = chatContent.Trim().Split('/');
int paramsCount = 0;
if (arr.Length == 3)
paramsCount = 1;
else if (arr.Length == 4)
paramsCount = 2;
if (paramsCount == 0)
return "参数至少1个";
string cmdType = arr[1];
string cmdValue = arr[2];
long count = 1;
if (paramsCount == 2)
{
long.TryParse(arr[3], out count);
}
if (cmdType.Equals("sys", StringComparison.OrdinalIgnoreCase))
{
Chat.Instance.SendSystemCaht(cmdValue);
return "成功";
}
else if (cmdType.Equals("updateDay", StringComparison.OrdinalIgnoreCase))
{
Game.EventSystem.Publish(new ET.EventType.UpdatePer1DayOfMonth { now = TimeHelper.ClientNow() }).Coroutine();
return "成功";
}
else if (cmdType.Equals("requireTrialMost", StringComparison.OrdinalIgnoreCase))
{
string gmTxt = "../Config/GMTxt.txt";
string[] gcTxtStrArr = await File.ReadAllLinesAsync(gmTxt);
foreach (string line in gcTxtStrArr)
{
try
{
if (string.IsNullOrWhiteSpace(line)) continue;
string[] recordArr = line.Split(' ');
long id = long.Parse(recordArr[0].Remove(0, 3));
Unit unit_ = MapUnitComponent.Instance.Get(id);
PlayerData data;
if (!unit_)
{
data = await DBComponent.Instance.Query<PlayerData>(id);
}
else
{
data = unit_.GetComponent<PlayerData>();
}
string trialMapIdStr = recordArr[2][..5];
string trialMapLayerStr = recordArr[2].Substring(5, 2);
Log.Info($"{trialMapIdStr} {trialMapLayerStr}");
int mapId = int.Parse(trialMapIdStr);
int layer = int.Parse(trialMapLayerStr);
switch (mapId)
{
case 10009:
IntFlagHelper.SetFlagValue(ref data.trialCopyMostLayer, 0, layer, 2, 3);
break;
case 10036:
IntFlagHelper.SetFlagValue(ref data.trialCopyMostLayer, 1, layer, 2, 3);
break;
}
UnitHelper.SaveComponenet(data);
}
catch (Exception e)
{
Log.Error($" {line} {e}");
}
}
return "成功";
}
else if (cmdType.Equals("clearPvpCount", StringComparison.OrdinalIgnoreCase))
{
List<PlayerData> list = await DBComponent.Instance.QueryJson<PlayerData>("{}");
foreach (PlayerData data in list)
{
try
{
data.personalPvpCount = 0;
Unit _unit = MapUnitComponent.Instance.Get(data.Id);
if (_unit)
{
_unit.RemoveComponent<PlayerData>();
_unit.AddComponent(data);
}
UnitHelper.SaveComponenet(data).Coroutine();
}
catch (Exception e)
{
Log.Error(e);
}
}
return "成功";
}
else if (cmdType.Equals("settlePvp", StringComparison.OrdinalIgnoreCase))
{
await PvpMap.inatance.SettlePvp();
return "成功";
}
else if (cmdType.Equals("requireStore", StringComparison.OrdinalIgnoreCase))
{
List<Store> list = await DBComponent.Instance.QueryJson<Store>("{}");
foreach (Store store in list)
{
try
{
bool isRequired = false;
List<Item> itemList = new();
int index = -1;
foreach (KeyValuePair<KeyValuePair<int, int>, Item> kv in store.StoreDic)
{
++index;
if (!isRequired)
isRequired = (kv.Key.Key != index);
itemList.Add(kv.Value);
}
store.StoreDic.Clear();
index = -1;
foreach (Item item in itemList)
{
++index;
if (item.IsEmpty)
{
store.StoreDic.Add(index, 0, item);
}
else
store.StoreDic.Add(index, item.ItemId, item);
}
if (isRequired)
{
Store _store = await StoreComponent.Instance.Query(store.Id);
if (_store != null)
{
_store = store;
}
await DBComponent.Instance.Save(store);
}
}
catch (Exception e)
{
Log.Error(e);
}
}
return "成功";
}
else if (cmdType.Equals("removeInvalidIdMail", StringComparison.OrdinalIgnoreCase))
{
List<MailLib> list = await DBComponent.Instance.QueryJson<MailLib>("{}");
foreach (MailLib mailLib in list)
{
try
{
using ListComponent<long> listComponent = ListComponent<long>.Create();
foreach (Mail mail in mailLib.Dic.Values)
{
try
{
foreach (MailItem mailItem in mail.RewordArr)
{
try
{
IConfig _item = BagHelper.GetItemBase(mailItem.ItemId);
}
catch (Exception e)
{
Log.Error(e);
}
finally
{
listComponent.List.Add(mail.Id);
User user = await UserComponent.Instance.Query(mailLib.Id);
Log.Info($"{user?.NickName}{mailLib.Id} 清理了邮件");
}
}
}
catch (Exception e)
{
Log.Error(e);
}
}
foreach (long item in listComponent.List)
{
mailLib.Dic.Remove(item);
}
DBComponent.Instance.Save(mailLib).Coroutine();
}
catch (Exception e)
{
Log.Error(e);
}
}
return "成功";
}
else if (cmdType.Equals("requireBag", StringComparison.OrdinalIgnoreCase))
{
List<Bag> list = await DBComponent.Instance.QueryJson<Bag>("{}");
foreach (Bag bag in list)
{
try
{
int itemCount = bag.ItemDic.Count(t => t.Key.Value != 0);
if (itemCount == bag.ItemCount) continue;
bag.ItemCount = 0;
for (int i = 0; i < bag.MaxItemCount; i++)
{
if (bag.ItemDic.TryGetValueByKey1(i, out Item item))
{
if (item.ItemId != 0)
bag.ItemCount++;
}
}
Unit unitInServer = MapUnitComponent.Instance.Get(bag.Id);
if (unitInServer != null)
{
unitInServer.RemoveComponent<Bag>();
unitInServer.AddComponent(bag);
}
await DBComponent.Instance.Save(bag);
}
catch (Exception e)
{
Log.Error(e);
}
}
return "成功";
}
else if (int.TryParse(cmdValue, out int value))
{
if (cmdType.Equals("addItem", StringComparison.OrdinalIgnoreCase))
{
await MailHelper.AddItem(unit.Id, value, count, true, getSource: "GM发放", "GM邮件", "GM发放的物品。", "GM");
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = count,
uid = unit.Id,
itemId = value
});
}
else if (cmdType.Equals("addStarSoulItem", StringComparison.OrdinalIgnoreCase))
{
if (!AppConfig.inst.isTest)
return "测试服专用";
foreach (Unit u in MapUnitComponent.Instance.GetAll())
{
try
{
if (u.UnitType == UnitType.Player)
{
for (int i = 0; i < 100; i++)
{
if (value == 0)
{
value = RandomHelper.RandomNumber(1, 5);
}
var item = ItemHelper.GenerateStarSoulItem(RandomHelper.RandomNumber(1001, 1011), (Quality) value);
StarSoulBag bag = u.GetComponent<StarSoulBag>();
bag.Add(item);
}
}
}
catch (Exception e)
{
Log.Error($"unit id is {u.Id}");
Log.Error(e);
}
}
return "成功";
}
else if (cmdType.Equals("addPetExp", StringComparison.OrdinalIgnoreCase))
{
Pet pet = unit.GetComponent<Pet>();
pet.AddExp(value);
return "成功";
}
else if (cmdType.Equals("exportUser", StringComparison.OrdinalIgnoreCase))
{
long uid = value;
try
{
User user = await DBComponent.Instance.Query<User>(uid);
if (user == null)
{
return $"user is null where id = {uid}";
}
Character character = await DBComponent.Instance.Query<Character>(uid);
Bag bag = await DBComponent.Instance.Query<Bag>(uid);
UnitSkillComponent skill = await DBComponent.Instance.Query<UnitSkillComponent>(uid);
UserSetting userSetting = await DBComponent.Instance.Query<UserSetting>(uid);
NumericComponent num = await DBComponent.Instance.Query<NumericComponent>(uid);
Pet pet = await DBComponent.Instance.Query<Pet>(uid);
string dirPath = "../Export";
if (!File.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
StringBuilder sb = new StringBuilder();
sb
.Append(MongoHelper.ToJson(user)).Append("#cal#")
.Append(MongoHelper.ToJson(character)).Append("#cal#")
.Append(MongoHelper.ToJson(bag)).Append("#cal#")
.Append(MongoHelper.ToJson(skill)).Append("#cal#")
.Append(MongoHelper.ToJson(userSetting)).Append("#cal#")
.Append(MongoHelper.ToJson(num)).Append("#cal#")
.Append(MongoHelper.ToJson(pet))
;
string path = dirPath + $"/{uid}.json";
if (File.Exists(path))
File.Delete(path);
await File.WriteAllTextAsync(path, sb.ToString());
path = dirPath;
DirectoryInfo info = new DirectoryInfo(path);
FileInfo[] fileInfos = info.GetFiles("*.json");
string __str = null;
foreach (FileInfo fileInfo in fileInfos)
{
long.TryParse(fileInfo.Name.Replace(fileInfo.Extension, string.Empty), out long existId);
if (existId == 0) continue;
User __user = await DBComponent.Instance.Query<User>(existId);
__str += $"{__user.NickName} {existId}\r\n";
}
path = dirPath + $"/name.json";
await File.WriteAllTextAsync(path, __str);
}
catch (Exception e)
{
Log.Error(e);
}
return "成功";
}
else if (cmdType.Equals("inportUser", StringComparison.OrdinalIgnoreCase))
{
if (!AppConfig.inst.isTest)
return "测试服专用";
long olsId = value;
long uid = count;
try
{
User user = await UserComponent.Instance.Query(uid);
if (user == null)
{
return $"user is null where id = {uid}";
}
string path = $"../Export/{olsId}.json";
string str = await File.ReadAllTextAsync(path);
string[] arrStr = str.Replace($"\"_id\" : NumberLong({olsId})", $"\"_id\" : NumberLong({uid})").Split("#cal#");
int index = 0;
User user_ = MongoHelper.FromJson<User>(arrStr[index++]);
user.JobId = user_.JobId;
user.Level = user_.Level;
Character character = MongoHelper.FromJson<Character>(arrStr[index++]);
Bag bag = MongoHelper.FromJson<Bag>(arrStr[index++]);
UnitSkillComponent skill = MongoHelper.FromJson<UnitSkillComponent>(arrStr[index++]);
UserSetting userSetting = MongoHelper.FromJson<UserSetting>(arrStr[index++]);
NumericComponent num = MongoHelper.FromJson<NumericComponent>(arrStr[index++]);
Pet pet = MongoHelper.FromJson<Pet>(arrStr[index++]);
Unit unit_ = MapUnitComponent.Instance.Get(uid);
if (unit_)
{
unit_.RemoveComponent<Character>();
unit_.AddComponent(character);
unit_.RemoveComponent<Bag>();
unit_.AddComponent(bag);
unit_.RemoveComponent<UnitSkillComponent>();
unit_.AddComponent(skill);
unit_.RemoveComponent<UserSetting>();
unit_.AddComponent(userSetting);
unit_.RemoveComponent<NumericComponent>();
unit_.AddComponent(num);
unit_.RemoveComponent<Pet>();
unit_.AddComponent(pet);
CharacterHelper.SyncNumeric(unit_);
}
else
{
DBComponent.Instance.Save(user).Coroutine();
UnitHelper.SaveComponenet(character).Coroutine();
UnitHelper.SaveComponenet(bag).Coroutine();
UnitHelper.SaveComponenet(skill).Coroutine();
UnitHelper.SaveComponenet(userSetting).Coroutine();
UnitHelper.SaveComponenet(num).Coroutine();
UnitHelper.SaveComponenet(pet).Coroutine();
}
}
catch (Exception e)
{
Log.Error(e);
}
return "成功";
}
else if (cmdType.Equals("addAllAITime", StringComparison.OrdinalIgnoreCase))
{
List<PlayerData> list = await DBComponent.Instance.QueryJson<PlayerData>("{}");
foreach (PlayerData data in list)
{
try
{
data.mainstoryAITime += value * 60 * 1000;
Unit _unit = MapUnitComponent.Instance.Get(data.Id);
if (_unit)
{
_unit.RemoveComponent<PlayerData>();
_unit.AddComponent(data);
}
await DBComponent.Instance.Save(data);
}
catch (Exception e)
{
Log.Error(e);
}
}
return "成功";
}
else if (cmdType.Equals("addLevel", StringComparison.OrdinalIgnoreCase))
{
CharacterHelper.AddLevel(unit, value);
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = value,
uid = unit.Id,
});
}
else if (cmdType.Equals("addAllItem", StringComparison.OrdinalIgnoreCase))
{
if (!AppConfig.inst.isTest)
return "测试服专用";
foreach (Unit u in MapUnitComponent.Instance.GetAll())
{
try
{
if (u.UnitType == UnitType.Player)
await MailHelper.AddItem(u.Id, value, count, true, getSource: "GM发放", "GM邮件", "GM发放的物品。", "GM");
}
catch (Exception e)
{
Log.Error($"unit id is {u.Id}");
Log.Error(e);
}
}
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = count,
uid = unit.Id,
itemId = value
});
}
return "成功";
}
}
else if (chatContent.StartsWith("othercmd", StringComparison.OrdinalIgnoreCase))
{
string[] arr = chatContent.Trim().Split('/');
int paramsCount = 0;
if (arr.Length == 4)
paramsCount = 1;
else if (arr.Length == 5)
paramsCount = 2;
if (paramsCount == 0)
return "参数至少1个";
string idStr = arr[1];
if (!long.TryParse(idStr, out long id))
{
return "请输入id";
}
Unit unitInLib = await MapUnitComponent.Instance.Query(id);
if (unitInLib == null)
{
return "玩家不存在";
}
string cmdType = arr[2];
string cmdValue = arr[3];
long count = 1;
if (paramsCount == 2)
{
long.TryParse(arr[4], out count);
}
if (cmdType.Equals("modifyPwd", StringComparison.OrdinalIgnoreCase))
{
if (paramsCount != 2)
{
return "旧密码/新密码";
}
string old = arr[3];
string newPwd = arr[4];
if (string.IsNullOrWhiteSpace(newPwd))
{
return "请输入新密码";
}
List<Account> list = await DBComponent.Instance.Query<Account>(t => t.UserId == id);
if (list == null || list.Count == 0)
{
return "用户没找到";
}
Account account = list[0];
if (!account.Pwd.Equals(old))
{
return "密码错误";
}
account.Pwd = newPwd;
await DBComponent.Instance.Save(account);
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = 0,
uid = id,
});
return "成功";
}
else if (cmdType.Equals("TransLevel", StringComparison.OrdinalIgnoreCase))
{
//othercmd/10001/TransLevel/XXX/5685
Log.Info($"{id} 旧等级:{count}");
string nickName = arr[3];
User user = await UserComponent.Instance.Query(unitInLib.Id);
if (user == null) return "用户不存在";
if (!user.IsOnline())
{
return "不在线";
}
if (user.NickName.Trim() != nickName.Trim())
{
return user.NickName;
}
if (count > user.Level)
{
return $"{user.Level}";
}
await CharacterHelper.ReSetCharacter(unitInLib);
CharacterHelper.AddLevel(unitInLib, 6000);
await TimerComponent.Instance.WaitAsync(1000);
string ret = await CharacterHelper.Transmigration(unitInLib);
if (ret != null)
{
return ret;
}
long exp = 0;
for (int i = 1; i < count; i++)
{
long exp_ = CharacterHelper.GetUpgradeLevelExp(0, i);
if (exp_ > 0)
{
exp += exp_;
}
}
Log.Info($"{id} 经验:{exp}");
await MailHelper.AddItem(id, BagHelper.ExpId, exp, false, "", "经验补偿", "", "");
return "成功";
}
else if (cmdType.Equals("winBattle", StringComparison.OrdinalIgnoreCase))
{
if (!unitInLib) return "不在线";
CopyBattle battle = BattleMgrCompnent.Instance.GetBattle(unitInLib);
battle.Victory().Coroutine();
return "成功";
}
else if (cmdType.Equals("queryAccount", StringComparison.OrdinalIgnoreCase))
{
User user = await UserComponent.Instance.Query(id);
if (user == null)
return "用户不存在";
if (!cmdValue.Equals(user.NickName, StringComparison.OrdinalIgnoreCase))
return $"昵称错误:目标昵称为{user.NickName}";
List<Account> list = await DBComponent.Instance.Query<Account>(t => t.UserId == id);
if (list.Count < 1) return "未找到";
return $"【{list[0].Username}】";
}
if (int.TryParse(cmdValue, out int value))
{
if (cmdType.Equals("addItem", StringComparison.OrdinalIgnoreCase))
{
//if (!AppConfig.inst.isTest)
// return "测试服专用";
try
{
IConfig item = BagHelper.GetItemBase(value);
}
catch (Exception e)
{
Log.Error(e);
return "你在写几把呢";
}
await MailHelper.AddItem(unitInLib.Id, value, count, true, getSource: "GM发放", "GM邮件", "GM发放的物品。", "GM");
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = count,
uid = id,
itemId = value
});
}
else if (cmdType.Equals("updateDayPerson", StringComparison.OrdinalIgnoreCase))
{
PlayerData data = unitInLib.GetComponent<PlayerData>();
data.InitData();
return "成功";
}
else if (cmdType.Equals("changePetId", StringComparison.OrdinalIgnoreCase))
{
Pet pet = unitInLib.GetComponent<Pet>();
var petConfig = PetConfigCategory.Instance.Get(value, false);
if (petConfig == null)
{
return "petId 错误";
}
pet.petId = value;
Game.EventSystem.Change(pet);
return "成功";
}
else if (cmdType.Equals("passLevel", StringComparison.OrdinalIgnoreCase))
{
PlayerData data = unitInLib.GetComponent<PlayerData>();
data.MainStoryRecordId = 10026;
Game.EventSystem.Publish(new ET.EventType.ChangeMap { unit = unitInLib, mapId = value }).Coroutine();
return "成功";
}
else if (cmdType.Equals("transJob", StringComparison.OrdinalIgnoreCase))
{
CharacterHelper.TransferJob(unitInLib, value, true).Coroutine();
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = count,
uid = id,
itemId = value
});
}
else if (cmdType.Equals("addLevel", StringComparison.OrdinalIgnoreCase))
{
CharacterHelper.AddLevel(unitInLib, value);
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = value,
uid = id,
});
}
else if (cmdType.Equals("requireStore", StringComparison.OrdinalIgnoreCase))
{
Store store = await StoreComponent.Instance.Query(unitInLib.Id);
try
{
bool isRequired = false;
List<Item> itemList = new();
int index = -1;
foreach (KeyValuePair<KeyValuePair<int, int>, Item> kv in store.StoreDic)
{
++index;
if (!isRequired)
isRequired = (kv.Key.Key != index);
itemList.Add(kv.Value);
}
store.StoreDic.Clear();
index = -1;
foreach (Item item in itemList)
{
++index;
if (item.IsEmpty)
{
store.StoreDic.Add(index, 0, item);
}
else
store.StoreDic.Add(index, item.ItemId, item);
}
if (isRequired)
{
await DBComponent.Instance.Save(store);
}
}
catch (Exception e)
{
Log.Error(e);
}
}
else if (cmdType.Equals("resetCharacter", StringComparison.OrdinalIgnoreCase))
{
await CharacterHelper.ReSetCharacter(unitInLib);
await DBComponent.Instance.Save(new AccountLog
{
Id = Game.IdGenerater.GenerateId(),
cmd = chatContent,
time = DateTime.Now,
count = value,
uid = id,
});
}
else if (cmdType.Equals("resetTeamState", StringComparison.OrdinalIgnoreCase))
{
Team team = TeamComponent.Instance.Get(unitInLib.TeamLeaderId);
team.ChangeState(TeamState.None);
}
else if (cmdType.Equals("resetBag", StringComparison.OrdinalIgnoreCase))
{
Bag bag = unitInLib.GetComponent<Bag>();
List<(KeyValuePair<int, int>, Item)> keyList = new List<(KeyValuePair<int, int>, Item)>();
foreach (KeyValuePair<KeyValuePair<int, int>, Item> kv in bag.ItemDic)
{
if (kv.Value?.Count == 0)
{
keyList.Add((kv.Key, kv.Value));
}
}
foreach ((KeyValuePair<int, int>, Item) item in keyList)
{
bag.ItemDic.Remove(item.Item1.Key, item.Item1.Value, item.Item2);
bag.ItemCount--;
}
}
UnitHelper.Save<Bag>(unitInLib).Coroutine();
UnitHelper.Save<NumericComponent>(unitInLib).Coroutine();
UnitHelper.Save<Character>(unitInLib).Coroutine();
return "成功";
}
}
return "cmd/othercmd";
}
}
}