Files
test/Assets/PerfectWorld/Scripts/Common/EC_C2SCmdCache.cs
T
2026-01-15 19:22:30 +07:00

431 lines
15 KiB
C#

using BrewMonster.Network;
using BrewMonster.Scripts.Managers;
using CSNetwork.C2SCommand;
using CSNetwork.Protocols;
using CSNetwork.S2CCommand;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BrewMonster.Common
{
using CounterTable = Dictionary<int, CECCounter>;
public class CECC2SCmdCache
{
public struct presentInfo
{
public int roleid;
public int mailid;
public int itemid;
public int index;
public int slot;
};
int m_idLastPickUpItem; // ID of picked up item last time
int m_idLastSelTarget; // ID of selected item last time
bool m_bGetExpProps;
bool m_bEnterSanctuary;
CounterTable m_CounterMap = new CounterTable();
CounterTable m_CounterMap2 = new CounterTable();
List<cmd_use_item> m_UseItemCmdList = new List<cmd_use_item>();
List<getplayerbriefinfo> m_GetPlayerBriefInfoList = new List<getplayerbriefinfo>();
List<int> m_PlayerBaseInfoList = new List<int>();
List<int> m_EnterSanctuaryList = new List<int>();
List<presentInfo> m_PresentInfoList = new List<presentInfo>();
public CECC2SCmdCache()
{
m_idLastPickUpItem = 0;
m_idLastSelTarget = 0;
m_bGetExpProps = false;
m_bEnterSanctuary = false;
InitCounters();
}
// Initialize counters
bool InitCounters()
{
// 'Use item' command time counter
CECCounter pCnt = new CECCounter();
pCnt.SetPeriod(200);
pCnt.Reset(true);
if (m_CounterMap.ContainsKey((int)CommandID.USE_ITEM))
{
m_CounterMap[(int)CommandID.USE_ITEM] = pCnt;
}
else
{
m_CounterMap.Add((int)CommandID.USE_ITEM, pCnt);
}
// 'Pickup item' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(500);
m_CounterMap[(int)CommandID.PICKUP] = pCnt;
// 'Select target' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(250);
m_CounterMap[(int)CommandID.SELECT_TARGET] = pCnt;
// 'Get extend properties' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.GET_EXT_PROP] = pCnt;
// 'Cast skill' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(200);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.CAST_SKILL] = pCnt;
// 'Revive ask' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(500);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.REVIVE_VILLAGE] = pCnt;
// 'Enter sanctuary' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
m_CounterMap[(int)CommandID.ENTER_SANCTUARY] = pCnt;
// 'Enter instance' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.ENTER_INSTANCE] = pCnt;
// 'Rush fly' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(500);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.ACTIVE_RUSH_FLY] = pCnt;
// 'Cancel action' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(200);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.CANCEL_ACTION] = pCnt;
// 'Control pet' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(400);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.PET_CTRL] = pCnt;
// 'Hello' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.SEVNPC_HELLO] = pCnt;
// 'Present' command time counter
pCnt = new CECCounter();
pCnt.SetPeriod(1000);
pCnt.Reset(true);
m_CounterMap[(int)CommandID.PLAYER_GIVE_PRESENT] = pCnt;
// 'GetPlayerBriefInfo' time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
pCnt.Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_GETPLAYERBRIEFINFO] = pCnt;
// 'PlayerBaseInfo' time counter
pCnt = new CECCounter();
pCnt.SetPeriod(2000);
pCnt.Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_PLAYERBASEINFO] = pCnt;
pCnt = new CECCounter();
pCnt.SetPeriod(10 * 1000);
pCnt.Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_FACTIONRESOURCEBATTLEGETMAP] = pCnt;
pCnt = new CECCounter();
pCnt.SetPeriod(10 * 1000);
pCnt.Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_FACTIONRESOURCEBATTLEGETRECORD] = pCnt;
return true;
}
// Remove all un-sent commands in cache
void RemoveAllCmds()
{
m_UseItemCmdList.Clear();
// 重置 C2S 命令计时器
m_CounterMap[(int)CommandID.USE_ITEM].Reset(true);
m_EnterSanctuaryList.Clear();
m_CounterMap[(int)CommandID.ENTER_SANCTUARY].Reset(true);
m_bEnterSanctuary = false;
m_PresentInfoList.Clear();
m_CounterMap[(int)CommandID.PLAYER_GIVE_PRESENT].Reset(true);
// 重置协议计时器
m_GetPlayerBriefInfoList.Clear();
m_CounterMap2[(int)ProtocolType.PROTOCOL_GETPLAYERBRIEFINFO].Reset(true);
m_PlayerBaseInfoList.Clear();
m_CounterMap2[(int)ProtocolType.PROTOCOL_PLAYERBASEINFO].Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_FACTIONRESOURCEBATTLEGETMAP].Reset(true);
m_CounterMap2[(int)ProtocolType.PROTOCOL_FACTIONRESOURCEBATTLEGETRECORD].Reset(true);
}
// Tick routine
bool Tick(float dwDeltaTime)
{
float dwRealTime = Time.realtimeSinceStartup;
//{
// CounterTable::iterator it = m_CounterMap.begin();
// for (; it != m_CounterMap.end(); ++it)
// ((CECCounter*)it.second).IncCounter(dwRealTime);
// for(int i = 0; i < m_CounterMap.Count; i++)
// {
// m_CounterMap[i].IncCounter(dwRealTime);
// }
//}
//{
// CounterTable::iterator it = m_CounterMap2.begin();
// for (; it != m_CounterMap2.end(); ++it)
// ((CECCounter*)it.second).IncCounter(dwRealTime);
//}
for (int i = 0; i < m_CounterMap.Count; i++)
{
m_CounterMap[i].IncCounter(dwRealTime);
}
for (int i = 0; i < m_CounterMap2.Count; i++)
{
m_CounterMap2[i].IncCounter(dwRealTime);
}
// Try to send 'use item' command in cache
SendCachedCmdUseItem();
// Try to send 'get extend properties' command
SendCachedCmdGetExtProp();
// Try to send 'GetPlayerBriefInfo'
SendCachedGetPlayerBriefInfo();
// Try to send 'PlayerBaseInfo'
SendCachedPlayerBaseInfo();
// Try to send 'PresentInfo'
SendCachedPresentInfo();
if (m_bEnterSanctuary)
{
CECCounter pCnt = m_CounterMap[(int)CommandID.ENTER_SANCTUARY];
if (pCnt.IsFull())
{
for(int i = 0; i < m_EnterSanctuaryList.Count; i++)
{
int id = m_EnterSanctuaryList[i];
UnityGameSession.c2s_SendCmdEnterSanctuary(id);
}
m_EnterSanctuaryList.Clear();
m_bEnterSanctuary = false;
}
}
return true;
}
// Send 'use item' command
void SendCachedCmdUseItem()
{
CECCounter pCnt = m_CounterMap[(int)CommandID.USE_ITEM];
if (!pCnt.IsFull() || m_UseItemCmdList.Count == 0)
return;
pCnt.Reset();
// Send the first item
cmd_use_item Cmd = m_UseItemCmdList[0];
UnityGameSession.c2s_SendCmdUseItem(Cmd.where, (byte)Cmd.index, Cmd.item_id, Cmd.byCount);
m_UseItemCmdList.Clear();
}
// Send cached 'get extend properties' command
void SendCachedCmdGetExtProp()
{
CECCounter pCnt = m_CounterMap[(int)CommandID.GET_EXT_PROP];
if (!m_bGetExpProps || !pCnt.IsFull())
return;
pCnt.Reset();
m_bGetExpProps = false;
UnityGameSession.c2s_SendCmdGetExtProps();
}
// Send cached 'GetPlayerBriefInfo' command
void SendCachedGetPlayerBriefInfo()
{
CECCounter pCnt = m_CounterMap2[(int)ProtocolType.PROTOCOL_GETPLAYERBRIEFINFO];
if (!pCnt.IsFull() || m_GetPlayerBriefInfoList.Count == 0)
return;
pCnt.Reset();
getplayerbriefinfo p = m_GetPlayerBriefInfoList[0];
if (p.Playerlist.Count != 0)
{
// 获取第一个玩家id并向服务器发送协议
getplayerbriefinfo temp = p;
temp.Playerlist.Clear();
temp.Playerlist.Add(p.Playerlist[0]);
UnityGameSession.SendNetData(temp);
// 从列表中清除
p.Playerlist.Remove(p.Playerlist[0]);
}
if (p.Playerlist.Count == 0)
m_GetPlayerBriefInfoList.Remove(p);
}
// Remove the cached 'PlayerBaseInfo' request
void RemovePlayerBaseInfo(int iRoleID)
{
int pos = m_PlayerBaseInfoList.Find(x => x == iRoleID);
if (pos != 0)
{
m_PlayerBaseInfoList.RemoveAt(pos);
}
}
// Send cached 'PlayerBaseInfo' protocol
void SendCachedPlayerBaseInfo()
{
CECCounter pCnt = m_CounterMap2[(int)ProtocolType.PROTOCOL_PLAYERBASEINFO];
if (!pCnt.IsFull() || m_PlayerBaseInfoList.Count == 0)
return;
pCnt.Reset();
List<int> aRoles = new List<int>();
for(int i = 0; i < m_PlayerBaseInfoList.Count; i++)
{
aRoles.Add(m_PlayerBaseInfoList[i]);
}
UnityGameSession.GetRoleBaseInfo(aRoles.Count, aRoles);
}
void SendCachedPresentInfo()
{
CECCounter pCnt = m_CounterMap[(int)CommandID.PLAYER_GIVE_PRESENT];
if (!pCnt.IsFull() || m_PresentInfoList.Count == 0)
return;
pCnt.Reset();
presentInfo info = m_PresentInfoList[0];
UnityGameSession.c2s_SendCmdGivePresent(info.roleid, info.mailid, info.itemid, info.index, info.slot);
m_PresentInfoList.Remove(info);
}
// Send commands ...
void SendCmdUseItem(byte byPackage, byte bySlot, int tid, byte byCount)
{
CECCounter pCnt = m_CounterMap[(int)CommandID.USE_ITEM];
if (m_UseItemCmdList.Count == 0 && pCnt.IsFull())
{
pCnt.Reset();
UnityGameSession.c2s_SendCmdUseItem(byPackage, bySlot, tid, byCount);
return;
}
cmd_use_item Cmd = new cmd_use_item();
if (m_UseItemCmdList.Count == 0)
{
Cmd.where = byPackage;
Cmd.index = bySlot;
Cmd.item_id = tid;
Cmd.byCount = byCount;
m_UseItemCmdList.Add(Cmd);
return;
}
int idx = -1;
for(int i = 0; i< m_UseItemCmdList.Count; i++)
{
Cmd = m_UseItemCmdList[i];
if (Cmd.where == byPackage && Cmd.index == bySlot)
{
CECHostPlayer pHost = EC_Game.GetGameRun().GetHostPlayer();
EC_Inventory pPack = pHost.GetPack(byPackage);
if (pPack == null) return;
EC_IvtrItem pItem = pPack.GetItem(bySlot);
if (pItem == null || !pItem.CheckUseCondition())
return;
int iTotal = Cmd.byCount + byCount;
if (iTotal >= pItem.GetCount())
iTotal = pItem.GetCount();
AAssist.a_ClampRoof(ref iTotal, 255);
Cmd.byCount = (byte)iTotal;
break;
}
idx++;
}
if(idx < 0)
{
Cmd = new cmd_use_item();
Cmd.where = byPackage;
Cmd.index = bySlot;
Cmd.item_id = tid;
Cmd.byCount = byCount;
m_UseItemCmdList.Add(Cmd);
}
// Try to send command in cache
SendCachedCmdUseItem();
}
/* Send 'pick up item' command
The strategy to send 'pick up item' command:
1. if the item is just the one which was to be picked up, check whether
enough time has passed since last command was sent. If true, send
command again, otherwise just throw command
2. if the item isn't the one which was to be picked up, send command
directly.
*/
void SendCmdPickUp(int idItem, int tid);
void SendCmdSelectTarget(int id);
void SendCmdExtProps();
void SendCmdReviveVillage(int param = 0);
void SendCmdReviveItem(int param = 0);
void SendCmdCastSkill(int idSkill, byte byPVPMask, int iNumTarget, int* aTargets);
void SendCmdCastInstantSkill(int idSkill, byte byPVPMask, int iNumTarget, int* aTargets);
void SendCmdEnterSanctuary(int id);
void SendCmdEnterInstance(int iTransIdx, int idInst);
void SendCmdActiveRushFly(bool bActive);
void SendCmdCancelAction();
void SendCmdPetCtrl(int idTarget, int cmd, void* pParamBuf, int iParamLen);
void SendCmdNPCSevHello(int nid);
void SendCmdFactionPVPQueryInfo(int idFaction);
// Send protocols ...
void SendGetPlayerBriefInfo(int iNumPlayer, int* aIDs, int iReason);
void SendGetPlayerBaseInfo(int iNumRole, const int* aRoleIDs);
void SendGivePresentProtocol(int roleid, int mailid, int itemid, int index, int slot);
void SendFactionPVPGetMap();
void SendFactionPVPGetRank();
}
}