530 lines
17 KiB
C#
530 lines
17 KiB
C#
/********************************************************************************
|
||
* 文件名: RelationList.cs
|
||
* 全路径: \Script\Player\Relation\RelationList.cs
|
||
* 创建人: 李嘉
|
||
* 创建时间:2014-02-14
|
||
*
|
||
* 功能说明:游戏玩家关系人数据列表
|
||
* 修改记录:
|
||
*********************************************************************************/
|
||
using UnityEngine;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Games.GlobeDefine;
|
||
using GCGame.Table;
|
||
|
||
public class RelationList
|
||
{
|
||
public RelationList()
|
||
{
|
||
m_RelationDataList = new Dictionary<UInt64, Relation>();
|
||
}
|
||
|
||
private Dictionary<UInt64, Relation> m_RelationDataList; //关系人数据列表
|
||
public Dictionary<UInt64, Relation> RelationDataList
|
||
{
|
||
get
|
||
{
|
||
return m_RelationDataList;
|
||
}
|
||
set { m_RelationDataList = value; }
|
||
}
|
||
|
||
public void CleanUp()
|
||
{
|
||
if (null != m_RelationDataList)
|
||
{
|
||
m_RelationDataList.Clear();
|
||
}
|
||
}
|
||
//关系人列表接口
|
||
|
||
//添加关系人
|
||
public bool AddRelation(Relation _relation)
|
||
{
|
||
//非法ID,则返回
|
||
if (null == _relation || _relation.Guid == GlobeVar.INVALID_GUID)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//如果发现已存在,则先remove
|
||
bool bCanAdd = true;
|
||
if (m_RelationDataList.ContainsKey(_relation.Guid))
|
||
{
|
||
bCanAdd = m_RelationDataList.Remove(_relation.Guid);
|
||
}
|
||
|
||
if (!bCanAdd)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//发现可添加,则加入关系人
|
||
m_RelationDataList.Add(_relation.Guid, _relation);
|
||
return true;
|
||
}
|
||
|
||
//删除关系人
|
||
public void DelRelation(UInt64 relationGuid)
|
||
{
|
||
//非法ID,则返回
|
||
if (relationGuid == GlobeVar.INVALID_GUID)
|
||
{
|
||
return;
|
||
}
|
||
|
||
//如果发现已存在,则remove
|
||
if (m_RelationDataList.ContainsKey(relationGuid))
|
||
{
|
||
m_RelationDataList.Remove(relationGuid);
|
||
}
|
||
|
||
|
||
if (FriendAndMailRoot.Instance())
|
||
{
|
||
FriendAndMailRoot.Instance().UpdateRedCheck();
|
||
FriendAndMailRoot.Instance()._FriendRootLogic.UpdateCheckTip();
|
||
}
|
||
|
||
if (ChatFrameLogic.Instance())
|
||
ChatFrameLogic.Instance().UpdateRedDotTip();
|
||
}
|
||
|
||
//获得关系人数量
|
||
public int GetRelationNum()
|
||
{
|
||
return m_RelationDataList.Count;
|
||
}
|
||
|
||
//获得某种状态关系人数量
|
||
public int GetStateRelationNum(int relationState)
|
||
{
|
||
int nCount = 0;
|
||
foreach(KeyValuePair<UInt64, Relation> _relation in m_RelationDataList)
|
||
{
|
||
if (_relation.Value.State == relationState)
|
||
{
|
||
nCount++;
|
||
}
|
||
}
|
||
|
||
return nCount;
|
||
}
|
||
|
||
//更新关系人,封装一层接口,方便调用函数时候辨认;
|
||
public void UpdateRelation(Relation _relation)
|
||
{
|
||
AddRelation(_relation);
|
||
}
|
||
|
||
//更新关系人状态
|
||
public void UpdateRelationState(UInt64 relationGuid, int state)
|
||
{
|
||
//非法ID,则返回
|
||
if (relationGuid == GlobeVar.INVALID_GUID)
|
||
{
|
||
return;
|
||
}
|
||
|
||
//bool needToSortByState = false;
|
||
//如果存在,则获取联系人并设置状态
|
||
Relation _relation = null;
|
||
if (true == m_RelationDataList.TryGetValue(relationGuid, out _relation))
|
||
{
|
||
if(_relation.State != state)
|
||
{
|
||
_relation.State = state;
|
||
//needToSortByState = true;
|
||
}
|
||
}
|
||
|
||
// if(needToSortByState)
|
||
// {
|
||
// SortByRelationState ((int)CharacterDefine.RELATION_TYPE.ONLINE);
|
||
// }
|
||
}
|
||
|
||
//更新关系人实时状态
|
||
public void UpdateRelationUserInfo(Relation _relation)
|
||
{
|
||
if (null == _relation || _relation.Guid == GlobeVar.INVALID_GUID)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// bool needToSortByState = false;
|
||
//更新信息
|
||
if (m_RelationDataList.ContainsKey(_relation.Guid))
|
||
{
|
||
Relation relation = m_RelationDataList[_relation.Guid];
|
||
if (null != relation)
|
||
{
|
||
//由于UserInfo只更新少数数据,所以不能直接
|
||
relation.Level = _relation.Level;
|
||
relation.Profession = _relation.Profession;
|
||
// needToSortByState = (relation.State != _relation.State);
|
||
relation.State = _relation.State;
|
||
relation.CombatNum = _relation.CombatNum;
|
||
relation.Name = _relation.Name;
|
||
relation.TimeInfo = _relation.TimeInfo;
|
||
relation.FriendPoint = _relation.FriendPoint;
|
||
relation.HeadType = _relation.HeadType;
|
||
relation.ChatPopType = _relation.ChatPopType;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
m_RelationDataList.Add(_relation.Guid, _relation);
|
||
}
|
||
|
||
if (ChatFrameLogic.Instance())
|
||
ChatFrameLogic.Instance().UpdateRedDotTip();
|
||
|
||
// if(needToSortByState)
|
||
// {
|
||
// SortByRelationState ((int)CharacterDefine.RELATION_TYPE.ONLINE);
|
||
// }
|
||
}
|
||
|
||
//更新关系人所有数据
|
||
//由于消息包不同,所以该方法会被重载
|
||
public void RebuildRelationList(GC_SYC_FULL_FRIEND_LIST packet)
|
||
{
|
||
|
||
m_RelationDataList.Clear();
|
||
|
||
// "系统信息" 好友
|
||
Relation sysFriend = new Relation();
|
||
sysFriend.Guid = GlobeVar.SYSFRIEND_GUID;
|
||
sysFriend.Name = StrDictionary.GetClientDictionaryString("#{5213}");
|
||
sysFriend.State = 1;
|
||
sysFriend.TimeInfo = 0;
|
||
AddRelation(sysFriend);
|
||
|
||
// "小精灵" 好友
|
||
//Relation helpFriend = new Relation();
|
||
//helpFriend.Guid = GlobeVar.HELPFRIEND_GUID;
|
||
//helpFriend.Name = StrDictionary.GetClientDictionaryString("#{5214}");
|
||
//helpFriend.State = 1;
|
||
//helpFriend.TimeInfo = 1;
|
||
//AddRelation(helpFriend);
|
||
|
||
if (null == m_RelationDataList || null == packet)
|
||
{
|
||
return;
|
||
}
|
||
|
||
for (int i = 0; i < packet.guidCount; ++i)
|
||
{
|
||
Relation _relation = new Relation();
|
||
_relation.Guid = packet.GetGuid(i);
|
||
_relation.Level = packet.GetLevel(i);
|
||
_relation.Name = packet.GetName(i);
|
||
_relation.Profession = packet.GetProf(i);
|
||
_relation.CombatNum = packet.GetCombat(i);
|
||
_relation.State = packet.GetState(i);
|
||
_relation.TimeInfo = packet.GetTimeInfo(i);
|
||
_relation.FriendPoint = packet.GetIntimacyDegree(i);
|
||
// 修改消息后要加上timeInfo
|
||
|
||
if (_relation.IsValid())
|
||
{
|
||
m_RelationDataList.Add(_relation.Guid, _relation);
|
||
}
|
||
}
|
||
|
||
// SortByRelationState ((int)CharacterDefine.RELATION_TYPE.ONLINE);
|
||
}
|
||
|
||
public void RebuildRelationList(GC_SYC_FULL_BLACK_LIST packet)
|
||
{
|
||
if (null == m_RelationDataList || null == packet)
|
||
{
|
||
return;
|
||
}
|
||
|
||
m_RelationDataList.Clear();
|
||
for (int i = 0; i < packet.guidCount; ++i)
|
||
{
|
||
Relation _relation = new Relation();
|
||
_relation.Guid = packet.GetGuid(i);
|
||
_relation.Level = packet.GetLevel(i);
|
||
_relation.Name = packet.GetName(i);
|
||
_relation.Profession = packet.GetProf(i);
|
||
_relation.CombatNum = packet.GetCombat(i);
|
||
_relation.State = packet.GetState(i);
|
||
_relation.TimeInfo = packet.GetTimeInfo(i);
|
||
|
||
if (_relation.IsValid())
|
||
{
|
||
m_RelationDataList.Add(_relation.Guid, _relation);
|
||
}
|
||
}
|
||
|
||
// SortByRelationState ((int)CharacterDefine.RELATION_TYPE.ONLINE);
|
||
}
|
||
|
||
public void RebuildRelationList(GC_SYC_FULL_HATE_LIST packet)
|
||
{
|
||
if (null == m_RelationDataList || null == packet)
|
||
{
|
||
return;
|
||
}
|
||
|
||
m_RelationDataList.Clear();
|
||
for (int i = 0; i < packet.GuidCount; ++i)
|
||
{
|
||
Relation _relation = new Relation();
|
||
_relation.Guid = packet.GetGuid(i);
|
||
_relation.Level = packet.GetLevel(i);
|
||
_relation.Name = packet.GetName(i);
|
||
_relation.Profession = packet.GetProf(i);
|
||
_relation.CombatNum = packet.GetCombat(i);
|
||
_relation.State = packet.GetState(i);
|
||
_relation.TimeInfo = packet.GetTimeInfo(i);
|
||
|
||
if (_relation.IsValid())
|
||
{
|
||
m_RelationDataList.Add(_relation.Guid, _relation);
|
||
}
|
||
}
|
||
|
||
//SortByRelationState ((int)CharacterDefine.RELATION_TYPE.ONLINE);
|
||
}
|
||
|
||
//按照某个状态整理关系人状态
|
||
//多次排序之后,可以保证按照最后一次Sort排列,并保留部分前次排序结果
|
||
public void SortByRelationState(int _state)
|
||
{
|
||
// "系统信息",无论是否在线,一直在第一位,暂时取离待排字典。
|
||
Relation sysFriendRelation = null;
|
||
if (m_RelationDataList.ContainsKey(GlobeVar.SYSFRIEND_GUID))
|
||
{
|
||
sysFriendRelation = m_RelationDataList[GlobeVar.SYSFRIEND_GUID];
|
||
m_RelationDataList.Remove(sysFriendRelation.Guid);
|
||
}
|
||
|
||
// "小精灵" 同上
|
||
Relation helpFriendRelation = null;
|
||
if (m_RelationDataList.ContainsKey(GlobeVar.SYSFRIEND_GUID))
|
||
{
|
||
helpFriendRelation = m_RelationDataList[GlobeVar.HELPFRIEND_GUID];
|
||
m_RelationDataList.Remove(helpFriendRelation.Guid);
|
||
}
|
||
|
||
//临时队列
|
||
Dictionary<UInt64, Relation> tempListInState = new Dictionary<UInt64, Relation>();
|
||
Dictionary<UInt64, Relation> tempListOutState = new Dictionary<UInt64, Relation>();
|
||
foreach (KeyValuePair<UInt64, Relation> _relation in m_RelationDataList)
|
||
{
|
||
//如果发现状态不符合,则将关系暂时放入临时列表中
|
||
if (_relation.Value.State == _state)
|
||
{
|
||
if(tempListInState.ContainsKey(_relation.Key))
|
||
continue;
|
||
tempListInState.Add(_relation.Key, _relation.Value);
|
||
}
|
||
else
|
||
{
|
||
if(tempListOutState.ContainsKey(_relation.Key))
|
||
continue;
|
||
tempListOutState.Add(_relation.Key, _relation.Value);
|
||
}
|
||
}
|
||
|
||
// 按照时间信息排列已经按照state分流的,两个分支
|
||
SortByTimeInfo (tempListInState);
|
||
SortByTimeInfo (tempListOutState);
|
||
|
||
m_RelationDataList.Clear ();
|
||
|
||
// 装回字典
|
||
if (sysFriendRelation != null)
|
||
{
|
||
m_RelationDataList.Add(GlobeVar.SYSFRIEND_GUID, sysFriendRelation);
|
||
}
|
||
if (helpFriendRelation != null)
|
||
{
|
||
m_RelationDataList.Add(GlobeVar.HELPFRIEND_GUID, helpFriendRelation);
|
||
}
|
||
|
||
foreach (KeyValuePair<UInt64, Relation> _tmpRelation in tempListInState)
|
||
{
|
||
//插入老数据
|
||
m_RelationDataList.Add(_tmpRelation.Key, _tmpRelation.Value);
|
||
}
|
||
foreach (KeyValuePair<UInt64, Relation> _tmpRelation in tempListOutState)
|
||
{
|
||
//插入老数据
|
||
m_RelationDataList.Add(_tmpRelation.Key, _tmpRelation.Value);
|
||
}
|
||
}
|
||
|
||
// 优先级规则:
|
||
// 婚姻关系 -> 师徒 (先师傅,再徒弟) -> 结拜 -> 好友度(相同则随意)
|
||
public void SortRelation()
|
||
{
|
||
// "系统信息",无论是否在线,一直在第一位,暂时取离待排字典。
|
||
Relation sysFriendRelation = null;
|
||
if (m_RelationDataList.ContainsKey(GlobeVar.SYSFRIEND_GUID))
|
||
{
|
||
sysFriendRelation = m_RelationDataList[GlobeVar.SYSFRIEND_GUID];
|
||
m_RelationDataList.Remove(sysFriendRelation.Guid);
|
||
}
|
||
|
||
// "小精灵" 同上
|
||
Relation helpFriendRelation = null;
|
||
if (m_RelationDataList.ContainsKey(GlobeVar.SYSFRIEND_GUID))
|
||
{
|
||
helpFriendRelation = m_RelationDataList[GlobeVar.HELPFRIEND_GUID];
|
||
m_RelationDataList.Remove(helpFriendRelation.Guid);
|
||
}
|
||
|
||
// 重新装填到List,便于排序
|
||
List<KeyValuePair<UInt64, Relation>> onlineList = new List<KeyValuePair<UInt64, Relation>>();
|
||
List<KeyValuePair<UInt64, Relation>> offlineList = new List<KeyValuePair<UInt64, Relation>>();
|
||
foreach (KeyValuePair<UInt64, Relation> item in m_RelationDataList)
|
||
{
|
||
if (item.Value.State == (int)CharacterDefine.RELATION_TYPE.ONLINE)
|
||
{
|
||
onlineList.Add(item);
|
||
}
|
||
else
|
||
{
|
||
offlineList.Add(item);
|
||
}
|
||
}
|
||
|
||
m_RelationDataList.Clear();
|
||
|
||
SortRelation(onlineList);
|
||
SortRelation(offlineList);
|
||
|
||
|
||
// 装回字典
|
||
if (sysFriendRelation != null)
|
||
{
|
||
m_RelationDataList.Add(GlobeVar.SYSFRIEND_GUID, sysFriendRelation);
|
||
}
|
||
if (helpFriendRelation != null)
|
||
{
|
||
m_RelationDataList.Add(GlobeVar.HELPFRIEND_GUID, helpFriendRelation);
|
||
}
|
||
foreach(KeyValuePair<UInt64, Relation> item in onlineList)
|
||
{
|
||
m_RelationDataList.Add(item.Key, item.Value);
|
||
}
|
||
|
||
foreach (KeyValuePair<UInt64, Relation> item in offlineList)
|
||
{
|
||
m_RelationDataList.Add(item.Key, item.Value);
|
||
}
|
||
}
|
||
|
||
public void SortRelation(List<KeyValuePair<UInt64, Relation>> dicItemsList)
|
||
{
|
||
for (int i = dicItemsList.Count - 1; i >= 1; i--)
|
||
{
|
||
for (int j = i; j >= 1; j--)
|
||
{
|
||
bool needChange = false;
|
||
|
||
if (dicItemsList[j].Value.State == (int)CharacterDefine.RELATION_TYPE.ONLINE
|
||
&& dicItemsList[j - 1].Value.State != (int)CharacterDefine.RELATION_TYPE.ONLINE)
|
||
{
|
||
needChange = true;
|
||
}
|
||
else
|
||
{
|
||
// 结婚对象
|
||
if (dicItemsList[j].Key == GameManager.gameManager.PlayerDataPool.LoverGUID)
|
||
{
|
||
needChange = true;
|
||
//break;
|
||
}
|
||
else if (dicItemsList[j - 1].Key != GameManager.gameManager.PlayerDataPool.LoverGUID)
|
||
{
|
||
// 师傅
|
||
if (GameManager.gameManager.PlayerDataPool.m_MasterInfo.IsGuidisMaster(dicItemsList[j].Key) == true)
|
||
{
|
||
needChange = true;
|
||
//break;
|
||
}
|
||
else if (GameManager.gameManager.PlayerDataPool.m_MasterInfo.IsGuidisMaster(dicItemsList[j - 1].Key) == false)
|
||
{
|
||
// 徒弟
|
||
if (GameManager.gameManager.PlayerDataPool.m_MasterInfo.IsGuidisApprentice(dicItemsList[j].Key) == true &&
|
||
GameManager.gameManager.PlayerDataPool.m_MasterInfo.IsGuidisApprentice(dicItemsList[j - 1].Key) == false)
|
||
{
|
||
needChange = true;
|
||
//break;
|
||
}
|
||
else if (GameManager.gameManager.PlayerDataPool.m_MasterInfo.IsGuidisApprentice(dicItemsList[j - 1].Key) == false)
|
||
{
|
||
// 结拜兄弟
|
||
if (GameManager.gameManager.PlayerDataPool._SwornBrother.IsMySwornBro(dicItemsList[j].Key) == true &&
|
||
GameManager.gameManager.PlayerDataPool._SwornBrother.IsMySwornBro(dicItemsList[j - 1].Key) == false)
|
||
{
|
||
needChange = true;
|
||
//break;
|
||
}
|
||
else if (GameManager.gameManager.PlayerDataPool._SwornBrother.IsMySwornBro(dicItemsList[j - 1].Key) == false)
|
||
{
|
||
// 好友值
|
||
if (dicItemsList[j].Value.FriendPoint > dicItemsList[j - 1].Value.FriendPoint)
|
||
{
|
||
needChange = true;
|
||
//break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 交换
|
||
if (needChange == true)
|
||
{
|
||
KeyValuePair<UInt64, Relation> temp = dicItemsList[j];
|
||
dicItemsList[j] = dicItemsList[j - 1];
|
||
dicItemsList[j - 1] = temp;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void SortByTimeInfo(Dictionary<UInt64, Relation> needSortList)
|
||
{
|
||
SortedDictionary<UInt32, Relation> tempList = new SortedDictionary<UInt32, Relation>(new MySortURverse());
|
||
foreach (KeyValuePair<UInt64, Relation> _relation in needSortList)
|
||
{
|
||
if(tempList.ContainsKey(_relation.Value.TimeInfo))
|
||
{
|
||
continue;
|
||
}
|
||
tempList.Add(_relation.Value.TimeInfo, _relation.Value);
|
||
}
|
||
|
||
needSortList.Clear ();
|
||
|
||
foreach (KeyValuePair<UInt32, Relation> _relation in tempList)
|
||
{
|
||
needSortList.Add(_relation.Value.Guid,_relation.Value);
|
||
}
|
||
}
|
||
|
||
// 排序顺序优先级为 有未读信息 > 在线 > 离线
|
||
public void SoryByLastChat()
|
||
{
|
||
|
||
}
|
||
|
||
//是否存在某个关系人
|
||
public bool IsExist(UInt64 key)
|
||
{
|
||
return m_RelationDataList.ContainsKey(key);
|
||
}
|
||
}
|