You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

344 lines
15 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sog;
using ProtoCSStruct;
namespace PlayerOp
{
public class ExchangeOp : BaseReloadableService
{
public override int GetServiceType()
{
return PlayerOpServiceType.ExchangeOp;
}
public override void Dispose()
{
}
public void OnExchangeRecordSaveDbReq(uint remoteAppID, StructPacket packet, DBOperator dbOperator)
{
ref SSExchangeCodeRecordSaveDbReq req = ref packet.GetMessage<SSExchangeCodeRecordSaveDbReq>();
for (int i = 0; i < req.Data.Count; i++)
{
ExchangeRecordConfigure data;
try
{
data = req.Data.Get(i);
}
catch (Exception e)
{
TraceLog.Error("ExchangeOp.OnExchangeRecordSaveDbReq error req.Data.Get index : i={0} - error: {1} ", i, e.Message);
break;
}
bool retRecord = dbOperator.SaveExchangeCodeRecordData(ref data);
if (!retRecord)
{
TraceLog.Error("ExchangeOp.OnExchangeRecordSaveDbReq SaveExchangeCodeRecordData error index:{0} ,GroupId:{1}, ExchangeCode:{2}",
data.Index, data.GroupId, data.Code.GetString());
break;
}
}
TraceLog.Trace("ExchangeOp.OnExchangeRecordSaveDbReq succ {0}-{1}", req.Data[0].Index, req.Data[req.Data.Count - 1].Index);
}
public bool OnExchangeRecordUpdateDbReq(uint remoteAppID, StructPacket packet, DBOperator dbOperator)
{
ref SSExchangeRecordQueryDbReq req = ref packet.GetMessage<SSExchangeRecordQueryDbReq>();
string code = req.Code.GetString();
int codeId = req.CodeId;
int groupId = req.GroupId;
int index = req.Index;
long uid = req.Uid;
int maxCount = req.MaxExchangeNum;
int type = 0;
ExchangeContent exchangeContent = new ExchangeContent();
// 模拟回包
StructPacket dbPacket = new StructPacket();
dbPacket.Header.Type = (int)SSGameMsgID.ExchangeRecordQueryDbRes;
dbPacket.Header.ServerID = packet.Header.ServerID;
dbPacket.Header.ObjectID = packet.ObjectID;
StructMessageParser<SSExchangeRecordQueryDbRes> parser = new StructMessageParser<SSExchangeRecordQueryDbRes>();
dbPacket.Parser = parser;
ref SSExchangeRecordQueryDbRes dbReq = ref dbPacket.GetMessage<SSExchangeRecordQueryDbRes>();
dbReq.Code.SetString(code);
dbReq.Uid = uid;
if(!PlayerOpServerUtils.GetExchangeSvc().CodeCheck(code, 17, 4))
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Debug("ExchangeOp.CodeCheck error code:{0}-uid:{1}", groupId, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
CSErrCode retCode = CSErrCode.None;
if (!dbOperator.QueryExchangeCode(code, out codeId, out maxCount, out type, ref exchangeContent, out retCode))
{
if (retCode == CSErrCode.None)
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
}
else
{
dbReq.Ret = (int)retCode;
}
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq QueryExchangeCode error code:{0}-uid:{1}", code, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
if(type == 1)
{
int exLen = 4; // 前缀长度默认为4
string tempcode = code.ToLower().Substring(exLen, code.Length- exLen);
string preCode = code.ToLower().Substring(0, exLen);
// 解析 groupId和codeId
bool retVerify;
try
{
retVerify = DynamicExchangeSvc.VerifyCode(tempcode, out groupId, out index);
}
catch (Exception ex)
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq QueryExchangeCode error code:{0}-uid:{1}", groupId, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
// 校验失败返回错误
if (!retVerify)
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq QueryExchangeCode error code:{0}-uid:{1}", groupId, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
}
dbReq.CodeId = codeId;
dbReq.GroupId = groupId;
dbReq.Index = index;
dbReq.Content.CopyFrom(ref exchangeContent);
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq: GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}", groupId, codeId, index, uid);
// groupId == 0 表示为普通兑换码 每个玩家只能兑换 1 次,但是可以多个玩家兑换
if(groupId == 0)
{
if (!dbOperator.QueryCommonExchangeCodeRecord(codeId, uid))
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeReceived;
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq QueryCommonExchangeCodeRecord error GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}",
groupId, codeId, index, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
bool ret = dbOperator.UpdateCommonExchangeCodeRecord(codeId, uid);
if (!ret)
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Error("ExchangeOp.OnExchangeRecordUpdateDbReq UpdateCommonExchangeCodeRecord error GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}",
groupId, codeId, index, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
}
else
{
// 该种类兑换码可兑换最大数量
if(maxCount > 0)
{
if (!dbOperator.SelectUserExchangCount(codeId, uid, maxCount))
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeBeOver;
TraceLog.Debug("ExchangeOp.OnExchangeRecordUpdateDbReq SelectUserExchangCount error GroupId:{0}-CodeId:{1}-uid:{2}: Exceed the maximum!",
groupId, codeId, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
}
if (!dbOperator.QueryExchangeCodeRecord(groupId, codeId, index, uid, code))
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Error("ExchangeOp.OnExchangeRecordUpdateDbReq QueryExchangeCodeRecord error GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}",
groupId, codeId, index, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
bool ret = dbOperator.UpdateExchangeCodeRecord(groupId, codeId, index, uid, code);
if (!ret)
{
dbReq.Ret = (int)CSErrCode.ExchangeCodeErr;
TraceLog.Error("ExchangeOp.OnExchangeRecordUpdateDbReq UpdateExchangeCodeRecord error GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}",
groupId, codeId, index, uid);
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return false;
}
}
// 兑换次数 + 1
if (!dbOperator.UpdateExchangCount(codeId, 1))
{
// +1 未成功
TraceLog.Error("ExchangeOp.OnExchangeRecordUpdateDbReq UpdateExchangCount error GroupId:{0}-CodeId:{1}-Index:{2}-uid:{3}",
groupId, codeId, index, uid);
}
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return true;
}
public bool OnSaveExchangeCode(uint remoteAppID, StructPacket packet, DBOperator dbOperator)
{
ref SSExchangeCodeSaveDbReq req = ref packet.GetMessage<SSExchangeCodeSaveDbReq>();
StructPacket dbPacket = new StructPacket();
dbPacket.Header.Type = (int)SSGameMsgID.ExchangeSaveDbRes;
dbPacket.Header.ServerID = packet.Header.ServerID;
dbPacket.Header.ObjectID = packet.ObjectID;
StructMessageParser<SSExchangeCodeSaveDbRes> parser = new StructMessageParser<SSExchangeCodeSaveDbRes>();
dbPacket.Parser = parser;
ref SSExchangeCodeSaveDbRes dbRes = ref dbPacket.GetMessage<SSExchangeCodeSaveDbRes>();
dbRes.Seq = req.Seq;
int codeId = req.Data.Id;
dbRes.Ret = 0;
TraceLog.Trace("ExchangeOp.OnSaveExchangeCode Id {0}", req.Data.Id);
// 新增
if (req.Data.Id == 0)
{
if (!dbOperator.SaveExchangeCodeData(ref req.Data, out codeId))
{
TraceLog.Error("ExchangeOp.OnSaveExchangeCode error {0}-{1}", req.Data.Id, req.Data.ExchangeCode);
dbRes.Ret = -1;
}
else
{
TraceLog.Trace("ExchangeOp.OnSaveExchangeCode succ {0}-{1}", req.Data.Id, req.Data.ExchangeCode);
if (!PlayerOpServerUtils.GetExchangeSvc().GetExchangeCodeManager().SetCodeId(codeId))
{
TraceLog.Error("ExchangeOp.OnSaveExchangeCode error: codeId {0} 错误,添加失败.", codeId);
dbRes.Ret = -2;
}
else
{
ExchangeConfigure config = new ExchangeConfigure();
config.CopyFrom(ref req.Data);
try
{
var createCodeTask = Task.Run(() => PlayerOpServerUtils.GetExchangeSvc().CreateGroupCode(ref config));
}
catch (Exception ex)
{
TraceLog.Error("ExchangeOp.OnSaveExchangeCode error: 激活码保存失败. error:{0}", ex.Message);
dbRes.Ret = -3;
}
}
}
}
else // 修改
{
if (!dbOperator.UpdateExchangeCodeData(ref req.Data))
{
TraceLog.Error("ExchangeOp.OnExchangeSaveDbReq error {0}-{1}", req.Data.Id, req.Data.ExchangeCode);
dbRes.Ret = -1;
}
else
{
TraceLog.Trace("ExchangeOp.OnExchangeSaveDbReq succ {0}-{1}", req.Data.Id, req.Data.ExchangeCode);
}
}
if(dbRes.Ret == 0)
{
dbRes.Data = req.Data;
dbRes.CodeId = codeId;
}
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
return true;
}
public void OnExchangeQueryDbReq(uint remoteAppID, StructPacket packet, DBOperator dbOperator)
{
ref SSExchangeCodeQueryDbReq req = ref packet.GetMessage<SSExchangeCodeQueryDbReq>();
StructPacket dbPacket = new StructPacket();
dbPacket.Header.Type = (int)SSGameMsgID.ExchangeQueryDbRes;
dbPacket.Header.ServerID = packet.Header.ServerID;
dbPacket.Header.ObjectID = packet.ObjectID;
StructMessageParser<SSExchangeCodeQueryDbRes> parser = new StructMessageParser<SSExchangeCodeQueryDbRes>();
dbPacket.Parser = parser;
ref SSExchangeCodeQueryDbRes dbRes = ref dbPacket.GetMessage<SSExchangeCodeQueryDbRes>();
dbRes.CodeId = req.CodeId;
dbRes.ReqId = req.ReqId;
dbRes.BeginIdx = req.BeginIdx;
dbRes.EndIdx = req.EndIdx;
if (!dbOperator.SelectExchangeCode(ref dbRes))
{
TraceLog.Error("ExchangeOp.SSExchangeCodeQueryDbReq error {0}-{1}-{2}", req.CodeId, req.BeginIdx, req.EndIdx);
dbRes.Ret = -1;
}
int count = 0;
if(!dbOperator.SelectExchangCount(out count))
{
TraceLog.Error("ExchangeOp.SSExchangeCodeQueryDbReq SelectExchangCount error!");
}
dbRes.Count = count;
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
}
public void OnExchangeChangeStatus(uint remoteAppID, StructPacket packet, DBOperator dbOperator)
{
ref SSExchangeStatusChangeReq req = ref packet.GetMessage<SSExchangeStatusChangeReq>();
var id = req.ExchangeCodeId;
var status = req.Status;
StructPacket dbPacket = new StructPacket();
dbPacket.Header.Type = (int)SSGameMsgID.ExchangeStatusChangeRes;
dbPacket.Header.ServerID = packet.Header.ServerID;
dbPacket.Header.ObjectID = packet.ObjectID;
StructMessageParser<SSExchangeStatusChangeRes> parser = new StructMessageParser<SSExchangeStatusChangeRes>();
dbPacket.Parser = parser;
ref SSExchangeStatusChangeRes dbRes = ref dbPacket.GetMessage<SSExchangeStatusChangeRes>();
dbRes.Ret = 0;
dbRes.ReqId = req.ReqId;
TraceLog.Debug("ExchangeOp.OnExchangeChangeStatus: exchangeCodeId:{0}, status:{1}", id, status);
if (!dbOperator.UpdateExchangStatus(id, status))
{
TraceLog.Error("ExchangeOp.OnExchangeChangeStatus: exchangeCodeId:{0}, status:{1}", id, status);
dbRes.Ret = -1;
}
PlayerOpServerUtils.GetExchangeSvc().AddRequestPacket(remoteAppID, dbPacket);
}
}
}