using System; using System.Collections.Generic; using System.IO; using Sog; using LitJson; using SimpleHttpServer; using System.Text; using ProtoCSStruct; namespace Operation { public class OpExchangeGroup { public int codeCount; public int maxExchangeNum; public string content; public int groupId; } [MenuMapping(MenuName.CDK_MANAGER, MenuIcon.CDK, 3, PermissionCode.CDK_MANAGER)] public class Exchange { public static Object m_locker = new Object(); private static StringBuilder GetExchangeJson(ExchangeConfigure d) { var time = OperationServerUtils.GetApp().GetTimeSecond(); var State = 1; if (!(d.BeginTime == 0 && d.EndTime == 0)) { //未开始 if (d.BeginTime != 0 && time < d.BeginTime) { State = 2; } //已过期 else if (d.EndTime != 0 && time > d.EndTime) { State = 3; } } StringBuilder Jsonstr = new StringBuilder(); Jsonstr.Append('{'); Jsonstr.Append($"\"Id\":{d.Id},\"ExchangeCode\":\"{d.ExchangeCode.GetString()}\",\"BeginTime\":{d.BeginTime},\"EndTime\":{d.EndTime},\"Mainline\":{d.Mainline},\"MaxExchangeNum\":{d.MaxExchangeNum},\"ExchangeNum\":{d.ExchangeNum},\"State\":{State},\"Status\":{d.Status},\"Type\":{d.Type},\"Content\":{{ \"AddGoods\":["); //d.Status 1 开启 2 关闭 for (int i = 0; i < d.Content.AddGoods.Count; i++) { var _goods = d.Content.AddGoods.Get(i); if (string.IsNullOrEmpty(_goods.Reward.Id.GetString()) && (_goods.Reward.Type == 0 || _goods.Reward.Type >= (int)CurrencyType.Max)) { continue; } Jsonstr.Append('{'); Jsonstr.Append($"\"Type\":{_goods.Reward.Type},\"ID\":\"{_goods.Reward.Id.GetString()}\",\"Count\":{_goods.Reward.Value},\"Param\":{_goods.Param},\"FromType\":\"{_goods.FromType}\""); Jsonstr.Append("},"); } Jsonstr = new StringBuilder(Jsonstr.ToString().TrimEnd(',')); Jsonstr.Append("],\"Groups\":["); for (int i = 0; i < d.Content.Groups.Count; i++) { var group = d.Content.Groups.Get(i); if (i != 0) { Jsonstr.Append(","); } Jsonstr.Append($"{{\"GroupId\":{group.GroupId},\"MaxExchangeNum\":{group.MaxExchangeNum},\"AddGoods\":["); for (int j = 0; j < group.AddGoods.Count; j++) { var _goods = group.AddGoods.Get(j); if(j != 0) { Jsonstr.Append(","); } if (string.IsNullOrEmpty(_goods.Reward.Id.GetString()) && (_goods.Reward.Type == 0 || _goods.Reward.Type >= (int)CurrencyType.Max)) { continue; } Jsonstr.Append('{'); Jsonstr.Append($"\"Type\":{_goods.Reward.Type},\"ID\":\"{_goods.Reward.Id.GetString()}\",\"Count\":{_goods.Reward.Value},\"Param\":{_goods.Param},\"FromType\":\"{_goods.FromType}\""); Jsonstr.Append("}"); } Jsonstr.Append("]}"); } Jsonstr.Append("]}"); Jsonstr.Append("},"); return Jsonstr; } [RequestMapping("兑换码查询",PermissionCode.CDK_SELECT,true)] public static int OnSelectExchangeHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { var page = query.GetValue("page").Toint32(1); var limit = query.GetValue("limit").Toint32(50); var codeId = query.GetValue("id").Toint32(0); var maxpage = page + limit; StringBuilder Jsonstr = new StringBuilder(); Jsonstr.Append('['); rsp.IsWaitFor = true; StructPacket dbPacket = new StructPacket(); dbPacket.Header.Type = (int)SSGameMsgID.ExchangeQueryDbReq; StructMessageParser parser = new StructMessageParser(); dbPacket.Parser = parser; ref SSExchangeCodeQueryDbReq req = ref dbPacket.GetMessage(); req.ReqId = httpContextId; if (codeId == 0) { req.BeginIdx = (page - 1) * limit; req.EndIdx = req.BeginIdx + limit; } else { req.CodeId = codeId; } OperationServerUtils.GetPacketSender().SendToPlayerOpServer((int)SSGameMsgID.ExchangeQueryDbReq, ref req, 0); return 0; } public static void OnMsgSelect(uint id, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, ref SSExchangeCodeQueryDbRes res) { var list = res.List; StringBuilder Jsonstr = new StringBuilder(); Jsonstr.Append('['); try { for (int i = 0; i < list.Count; i++) { ExchangeConfigure config = list[i]; var str = GetExchangeJson(config); Jsonstr.Append(str); } Jsonstr = new StringBuilder(Jsonstr.ToString().TrimEnd(',')); Jsonstr.Append(']'); Jsondata["data"] = Jsonstr.ToString(); } catch (Exception ex) { TraceLog.Error("Exchange.OnMsgSelect error: {0} ", ex.Message); Jsondata["ret"] = 1; Jsondata["msg"] = "数据错误,请联系管理员!"; return; } Jsondata["count"] = res.Count; Jsondata["ret"] = 0; return; } public static void OnSaveDbMsg(uint id, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, ref SSExchangeCodeSaveDbRes res) { if (res.Ret == -1) { TraceLog.Error("Exchange.OnSaveDbMsg error: codeId 生成激活码失败!"); Jsondata["ret"] = -1; Jsondata["msg"] = "生成激活码失败!"; rsp.IsWaitFor = false; return; } else if (res.Ret == -2) { TraceLog.Error("Exchange.OnSaveDbMsg error: codeId 错误,添加失败."); Jsondata["ret"] = 2; Jsondata["msg"] = "codeId 错误,添加失败, 请稍后重试"; rsp.IsWaitFor = false; return; } else if (res.Ret == -3) { TraceLog.Error("Exchange.OnSaveDbMsg error: 激活码保存失败"); Jsondata["ret"] = 0; Jsondata["msg"] = "生成激活码失败!请检查重试!"; rsp.IsWaitFor = false; return; } Jsondata["ret"] = 0; Jsondata["msg"] = "生成激活码成功!稍后生效"; rsp.IsWaitFor = false; return; } public static void OnMsgChange(uint id, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, ref SSExchangeStatusChangeRes res) { string msg = ""; if (res.Ret == -1) { msg = "关闭/开启 激活码失败!"; Jsondata["ret"] = 0; Jsondata["msg"] = msg; rsp.IsWaitFor = false; return; } msg = "修改成功"; Jsondata["ret"] = 0; Jsondata["msg"] = msg; rsp.IsWaitFor = false; return; } /// 添加 修改 [RequestMapping("添加CDK",PermissionCode.CDK_ADD, toLog: true)] public static int OnAddExchangeHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { var ExchangeCode = query.GetValue("ExchangeCode"); var Id = query.GetValue("Id").Toint32(); var data = OperationServerData.GetExchangeCodeCache(); #region 填充值 ExchangeConfigure Config = new ExchangeConfigure(); Config.Id = Id; Config.ExchangeCode.SetString(ExchangeCode); Config.BeginTime = query.GetValue("BeginTime").Tolong(); Config.EndTime = query.GetValue("EndTime").Tolong(); Config.Mainline = query.GetValue("Mainline").Toint32(); Config.Status = query.GetValue("Status").Toint32(); Config.MaxExchangeNum = query.GetValue("MaxExchangeNum").Toint32(); Config.Type = 0; var Content = query.GetValue("Content"); //"id|num#id|num# "; if(Config.EndTime != 0 && Config.BeginTime > Config.EndTime) { Jsondata["ret"] = 3; Jsondata["msg"] = "时间配置错误"; return 3; } foreach (var item in Content.Split('#')) { var arr = item.Split('|'); DropDataOne one = new DropDataOne { Reward = new TypeIDValueString() { Type = arr[0].Toint32(), Id = new FixedStructString64(arr[1]), Value = arr[2].Toint32() }, Param = 0, FromType = DropFromType.None }; Config.Content.AddGoods.Add(ref one); } #endregion OperationServerUtils.GetExchangeSvc().SaveExchangeCodeData(ref Config, httpContextId); Jsondata["ret"] = 1; Jsondata["msg"] = "操作成功"; return 0; } [RequestMapping("注销CDK",PermissionCode.CDK_DISABLE, toLog: true)] public static int OnDisableExchangeHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { var Status = query.GetValue("Status").Toint32(); var id = query.GetValue("Id").Toint32(); TraceLog.Debug("Exchange.OnDisableExchangeHttpReq httpContextId {0} id {1} Status {2}", httpContextId, id, Status); StructPacket dbPacket = new StructPacket(); dbPacket.Header.Type = (int)SSGameMsgID.ExchangeStatusChangeReq; StructMessageParser parser = new StructMessageParser(); dbPacket.Parser = parser; ref SSExchangeStatusChangeReq req = ref dbPacket.GetMessage(); req.Status = Status; req.ExchangeCodeId = id; req.ReqId = (int)httpContextId; OperationServerUtils.GetPacketSender().SendToPlayerOpServer((int)SSGameMsgID.ExchangeStatusChangeReq, ref req, 0); rsp.IsWaitFor = true; return 0; } [RequestMapping("创建CDK",PermissionCode.CDK_CREATE, toLog: true)] public static int OnCreateExchangeHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse srsp, HttpRequest request, HttpQueryParams query, uint reqId) { TraceLog.Debug("Exchange.OnCreateExchangeHttpReq"); List groupList; try { groupList = query.GetValue("exchangeGroup").ToJson>(); } catch (Exception e) { TraceLog.Error("Exchange.OnCreateExchangeHttpReq get groupList error: {0}.", e.Message); Jsondata["ret"] = 3; Jsondata["msg"] = "组内容配置错误"; return 3; } if(groupList.Count == 0 || groupList == null) { TraceLog.Error("Exchange.OnCreateExchangeHttpReq get groupList error: 组内容配置错误."); Jsondata["ret"] = 3; Jsondata["msg"] = "组内容配置错误"; return 3; } int SumCount = 0; string exchangeCodePre = query.GetValue("exchangeCodePre"); int codeId = query.GetValue("Id").Toint32(0); ExchangeConfigure Config = new ExchangeConfigure(); Config.ExchangeCode.SetString(exchangeCodePre); Config.BeginTime = query.GetValue("BeginTime").Tolong(); Config.EndTime = query.GetValue("EndTime").Tolong(); Config.Mainline = query.GetValue("Mainline").Toint32(); Config.Status = query.GetValue("Status").Toint32(); Config.MaxExchangeNum = query.GetValue("MaxExchangeNum").Toint32(); Config.Type = 1; if (Config.EndTime != 0 && Config.BeginTime > Config.EndTime) { TraceLog.Error("Exchange.OnCreateExchangeHttpReq error: 时间配置错误."); Jsondata["ret"] = 3; Jsondata["msg"] = "时间配置错误"; return 3; } srsp.IsWaitFor = true; // 这里要先校验 int groupId = 1; foreach (var groupItem in groupList) { int groupCodeCount = groupItem.codeCount; int maxExchangeNum = groupItem.maxExchangeNum; ExchangeGroup tempGroup = new ExchangeGroup { GroupId = groupId, MaxExchangeNum = maxExchangeNum, GroupCount = groupCodeCount }; var content = groupItem.content; SumCount += groupCodeCount; //"id|num#id|num# "; foreach (var item in content.Split('#')) { var arr = item.Split('|'); DropDataOne one = new DropDataOne { Reward = new TypeIDValueString() { Id = new FixedStructString64(arr[1]), Type = arr[0].Toint32(), Value = arr[2].Toint32() }, Param = 0, FromType = DropFromType.None }; tempGroup.AddGoods.Add(one); } Config.Content.Groups.Add(ref tempGroup); groupId++; } // 保存 同时校验前缀是否存在 OperationServerUtils.GetExchangeSvc().SaveExchangeCodeData(ref Config, reqId); return 0; } [RequestMapping("更新CDK组",PermissionCode.CDK_UPDATE_GROUP, toLog: true)] public static int OnUpdateExchangeGroupHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { List groupList; try { groupList = query.GetValue("exchangeGroup").ToJson>(); } catch (Exception e) { TraceLog.Error("Exchange.OnUpdateExchangeGroupHttpReq get groupList error: {0}.", e.Message); Jsondata["ret"] = 3; Jsondata["msg"] = "组内容配置错误"; return 3; } if (groupList == null || groupList.Count == 0) { TraceLog.Error("Exchange.OnUpdateExchangeGroupHttpReq get groupList error: 组内容配置错误."); Jsondata["ret"] = 3; Jsondata["msg"] = "组内容配置错误"; return 3; } var Id = query.GetValue("Id").Toint32(); ExchangeConfigure Config = new ExchangeConfigure(); Config.Id = Id; Config.BeginTime = query.GetValue("BeginTime").Tolong(); Config.EndTime = query.GetValue("EndTime").Tolong(); Config.Mainline = query.GetValue("Mainline").Toint32(); Config.Status = query.GetValue("Status").Toint32(); Config.Type = 1; Config.MaxExchangeNum = query.GetValue("MaxExchangeNum").Toint32(); int groupId = 1; foreach (var groupItem in groupList) { int groupCodeCount = groupItem.codeCount; int maxExchangeNum = groupItem.maxExchangeNum; ExchangeGroup tempGroup = new ExchangeGroup(); var content = groupItem.content; if(groupItem.groupId != 0) { tempGroup.GroupId = groupItem.groupId; } else { tempGroup.GroupId = groupId++; } tempGroup.MaxExchangeNum = groupItem.maxExchangeNum; foreach (var item in content.Split('#')) { var arr = item.Split('|'); DropDataOne one = new DropDataOne { Reward = new TypeIDValueString() { Id = new FixedStructString64(arr[1]), Type = arr[0].Toint32(), Value = arr[2].Toint32() }, Param = 0, FromType = DropFromType.None }; tempGroup.AddGoods.Add(one); } Config.Content.Groups.Add(ref tempGroup); } if (Config.Id == 0) { Jsondata["ret"] = 5; Jsondata["msg"] = "修改失败,参数错误"; return 5; } else //修改 { // 新数据,直接丢过去减速 OperationServerUtils.GetExchangeSvc().SaveExchangeCodeData(ref Config, httpContextId); } Jsondata["ret"] = 1; Jsondata["msg"] = "操作成功"; return 0; } [RequestMapping("导出CDK",PermissionCode.CDK_EXPORT, toLog: true)] public static int OnExportExchangeHttpReq(string httpApiCmd, JsonData Jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { // 通过groupId,codeId查询兑换码文件 int codeId = query.GetValue("codeId").Toint32(0); string exchangeCodePre = query.GetValue("exchangeCode"); // 文件名称:exchangeCode_ev_1.csv string filename = "exchangeCode_" + exchangeCodePre + "_" + codeId +".csv"; string filePathName = OperationServerUtils.GetServerConfig().codefliepath+filename; TraceLog.Debug("Exchange.OnExportExchangeHttpReq filePathName {0} ", filePathName); string data = ""; try { data = Convert.ToBase64String(File.ReadAllBytes(filePathName)); } catch (Exception ex) { TraceLog.Error("Exchange.OnExportExchangeHttpReq ReadAllBytes error : {0}", ex.Message); Jsondata["ret"] = 2; Jsondata["msg"] = "找不到该文件:" + filename; return 2; } Jsondata["ret"] = 0; Jsondata["data"] = data; return 0; } } }