/// <summary> /// 开始读取db /// </summary> private void LoadMails() { PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) { return; } SQLMailHandle.LoadCharRecvs(m_char_idx, OnLoadRecvMailEnd); SQLMailHandle.LoadMailList(m_char_idx, player.ss_data.spid, OnLoadMailEnd); }
private void OnUnitAttrModifyString(PacketBase packet) { ss2db.NotifyUpdatePlayerAttribString msg = packet as ss2db.NotifyUpdatePlayerAttribString; PlayerCache member = PlayerCacheManager.Instance.GetMember(msg.char_idx); if (member != null) { member.UpdateAttribute(msg.type, msg.value); } else { Log.Warning("OnUnitAttrModifyString - 未找到玩家缓存信息:" + msg.char_idx); } }
/// <summary> /// 加载单个玩家数据 /// </summary> /// <param name="char_idx"></param> public void LoadPlayer(long char_idx, Action <long> callback) { if (m_cache_members.ContainsKey(char_idx)) { Log.Debug("玩家数据已经加载:" + char_idx); if (callback != null) { callback(char_idx); } return; } Log.Debug("请求加载玩家数据:" + char_idx); PlayerCache member = CommonObjectPools.Spawn <PlayerCache>(); member.Load(char_idx, is_load => { if (is_load) { m_cache_members.Add(char_idx, member); if (m_cache_name_members.ContainsKey(member.ss_data.char_name)) { Log.Warning("存在相同玩家名:" + member.ss_data.char_name); } else { m_cache_name_members.Add(member.ss_data.char_name, char_idx); } if (callback != null) { callback(char_idx); } } else { Log.Warning("玩家数据加载失败:" + char_idx); CommonObjectPools.Despawn(member); if (callback != null) { callback(0); } } } ); }
public void Tick() { int update_count = 0; PlayerCache member = null; foreach (var obj in m_cache_members) { member = obj.Value; if (member.NeedSave()) { member.Save(); if (++update_count > 60) { break; //当次循环最大保存数量 } } } }
/// <summary> /// 读取列表完成 /// </summary> private void OnLoadMailEnd(List <MailInfo> list) { //可能读取db时,玩家下线了 if (m_char_idx == 0) { list.ForEach(info => CommonObjectPools.Despawn(info)); return; } //过滤掉已经领取过附件的全局邮件 for (int i = list.Count - 1; i >= 0; --i) { MailInfo mail_info = list[i]; if (mail_info.receiver_idx == 0 && m_had_recv_mails.Contains(mail_info.mail_idx)) { list.RemoveAt(i); } } //保存到集合 list.ForEach(info => m_all_mails.Add(info.mail_idx, info)); PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) {//可能读取db时,玩家下线了 MailCacheManager.Instance.HanldeLogoutClient(m_char_idx); return; } //转发给ss,每次发10封 while (list.Count > 0) { db2ss.MailList msg = PacketPools.Get(db2ss.msg.MAIL_LIST) as db2ss.MailList; msg.char_idx = m_char_idx; for (int i = list.Count - 1, j = 0; i >= 0 && j < 10; --i, ++j) { msg.mail_list.Add(list[i]); list.RemoveAt(i); } ForServerNetManager.Instance.Send(player.ss_uid, msg); } }
private void OnCharacterInfo(PacketBase packet) { ss2db.CharacterInfo msg = packet as ss2db.CharacterInfo; PlayerCache member = PlayerCacheManager.Instance.GetMember(msg.char_idx); if (member == null) { //由于数据包会放入对象池,所以在这先保存临时数据 ClientUID client_uid = msg.client_uid; InterServerID server_uid = msg.server_uid; long char_idx = msg.char_idx; PlayerCacheManager.Instance.LoadPlayer(msg.char_idx, idx => { OnCharacterInfoLoaded(client_uid, server_uid, char_idx); }); } else { OnCharacterInfoLoaded(msg.client_uid, msg.server_uid, msg.char_idx); } }
private void ProcessWrite(MailWriteInfo info) { PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) { return; } if (info.receiver_idx <= 0) { Log.Warning("错误的收件人id:" + info.receiver_idx); return; } //存db MailInfo mail_info = CommonObjectPools.Spawn <MailInfo>(); mail_info.mail_type = eMailType.NORMAL; mail_info.spid = 0;//普通邮件,不区分渠道id mail_info.receiver_idx = info.receiver_idx; mail_info.sender_idx = m_char_idx; mail_info.sender_name = player.ss_data.char_name; mail_info.send_time = Time.second_time; mail_info.expire_time = 0; mail_info.delivery_time = 0; mail_info.flags = (uint)eMailFlags.NONE; mail_info.subject = info.subject; mail_info.bin_mail_content.content_type = eMailContentType.NORMAL; mail_info.bin_mail_content.bin_normal_content.content = info.content; SQLMailHandle.CreateMail(mail_info); CommonObjectPools.Despawn(mail_info); //返回结果给ss db2ss.MailCommand rep_msg = PacketPools.Get(db2ss.msg.MAIL_COMMAND) as db2ss.MailCommand; rep_msg.mail_idx = 0; rep_msg.command_type = eMailCommandType.WRITE_MAIL; rep_msg.error_type = eMailCommandError.NONE; ForServerNetManager.Instance.Send(player.ss_uid, rep_msg); }
/// <summary> /// 写邮件 /// </summary> public void HandleWriteMail(MailWriteInfo info) { PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) { return; } if (info.receiver_type == MailWriteInfo.eReceiverType.CHARID) { ProcessWrite(info); } else if (info.receiver_type == MailWriteInfo.eReceiverType.CHARNAME) { if (String.IsNullOrEmpty(info.receiver_name)) { Log.Warning("错误的收件人name:" + info.receiver_name); return; } PlayerCache recv_player = PlayerCacheManager.Instance.GetMemberByName(info.receiver_name); if (recv_player != null) { info.receiver_idx = recv_player.char_idx; ProcessWrite(info); } else {//需要查db //TODO } } else { Log.Warning("错误的收件人type:" + info.receiver_type); return; } }
private void OnCharacterInfoLoaded(ClientUID client_uid, InterServerID server_uid, long char_idx) { PlayerCache member = PlayerCacheManager.Instance.GetMember(char_idx); if (member == null) { Log.Warning("OnPlayerInfoLoaded - 未找到玩家缓存信息:" + char_idx); return; } //设置服务器信息 member.client_uid = client_uid; member.ss_uid = server_uid.ss_uid; member.UpdateAttribute(eUnitModType.UMT_time_last_login, Time.second_time); //返回数据给ss db2ss.CharacterInfo rep_msg = PacketPools.Get(db2ss.msg.CHARACTER_INFO) as db2ss.CharacterInfo; rep_msg.server_uid = server_uid; rep_msg.client_uid = client_uid; rep_msg.data.Copy(member.ss_data); this.Send(rep_msg); //读取邮件 MailCacheManager.Instance.LoadMailbox(member.char_idx); }
/// <summary> /// 邮件操作:优先级依次是 读->领取附件->删除 /// </summary> public void HandleCommandMail(long mail_idx, eMailCommandType type) { PlayerCache player = PlayerCacheManager.Instance.GetMember(m_char_idx); if (player == null) { return; } switch (type) { case eMailCommandType.READ_MAIL: { MailInfo mail_info; if (m_all_mails.TryGetValue(mail_idx, out mail_info)) { if (m_del_mails.Contains(mail_idx)) { break; } if (m_save_mails.Contains(mail_idx)) { break; } if (mail_info.receiver_idx == 0) { if (!m_had_recv_mails.Contains(mail_idx)) { //同步到db,已经领取过全局邮件 m_had_recv_mails.Add(mail_idx); SQLMailHandle.UpdateCharRecvs(m_char_idx, m_had_recv_mails); } break; } //判断是否已经读取 if (!Utils.HasFlag(mail_info.flags, (uint)eMailFlags.READED)) { m_save_mails.Add(mail_idx); mail_info.flags = Utils.InsertFlag(mail_info.flags, (uint)eMailFlags.READED); m_is_dirty = true; } } } break; case eMailCommandType.DELETE_MAIL: { MailInfo mail_info; if (m_all_mails.TryGetValue(mail_idx, out mail_info)) { if (m_del_mails.Contains(mail_idx)) { break; } if (mail_info.receiver_idx == 0) { break; } m_del_mails.Add(mail_idx); m_save_mails.Remove(mail_idx); m_all_mails.Remove(mail_idx); CommonObjectPools.Despawn(mail_info); m_is_dirty = true; } } break; case eMailCommandType.TAKE_MAIL: case eMailCommandType.TAKE_MAIL_BUT_RETAIN: { MailInfo mail_info; if (m_all_mails.TryGetValue(mail_idx, out mail_info)) { if (m_del_mails.Contains(mail_idx)) { break; } if (mail_info.receiver_idx == 0) { break; } //修改状态,前提是带附件 if (Utils.HasFlag(mail_info.flags, (uint)eMailFlags.CARRY_ITEM)) { m_save_mails.Remove(mail_idx); mail_info.flags = Utils.InsertFlag(mail_info.flags, (uint)eMailFlags.READED); mail_info.flags = Utils.RemoveFlag(mail_info.flags, (uint)eMailFlags.CARRY_ITEM); m_is_dirty = true; } } } break; } }