CookieWrapper = CookieWrapper or BaseClass() local CookieWrapper = CookieWrapper CookieVersion = 1 CookieAccountInfo = "AccountInfo" -- 层次类型 CookieLevelType = { Common = 1,--和机器绑定 Account = 2,--和账号绑定 Server = 3,--和服务器绑定 } -- 时间类型 CookieTimeType = { TYPE_DAY = 1, TYPE_ALWAYS = 2, TYPE_DAY2 = 3, -- 跨天清(0点) } CookieKey = { DEVELOP_MODE_ACCOUNT_AND_SERVER = "DEVELOP_MODE_ACCOUNT_AND_SERVER", --开发模式下账号和服务器 DEVELOP_MODE_ACCOUNT_OUT_LIST = "DEVELOP_MODE_ACCOUNT_OUT_LIST", --开发模式下外服帐号列表 LAST_LOGIN_ROLE_ID = "LAST_LOGIN_ROLE_ID", --上次登录的角色 LOGIN_USER_NAME = "ACCOUNT_USER_NAME", --用户名 LOGIN_USER_PASSWORD = "ACCOUNT_USER_PASSWORD", --密码 SERVER_LIST_MD5 = "SERVER_LIST_MD5", --服务器列表文件版本md5 SERVER_LIST_DATA = "SERVER_LIST_DATA", --服务器列表数据 SCENE_X_Y = "SCENE_X_Y", --玩家位置信息 ACCOUNT_HISTORY = "ACCOUNT_HISTORY", --历史账号 REMEMBER_PASSWORD = "REMEMBER_PASSWORD", --记住密码 AUTO_FIGHT_STATE = "AUTO_FIGHT_STATE", --挂机状态 AUTO_FIGHT_ATTACK_FIRST_STATE = "AUTO_FIGHT_ATTACK_FIRST_STATE", --自动挂机攻击优先顺序 AUTO_EAT_STATE = "AUTO_EAT_STATE", --自动挂机吃烤肉 AUTO_FIAGHT_SKILL_LIST = "AUTO_FIAGHT_SKILL_LIST", --自动挂机技能设置 SYSTEM_SETTING = "SYSTEM_SETTING", --系统设置 SOUND_SETTING = "SOUND_SETTING", --声音设置 BLOCK_SETTING = "BLOCK_SETTING", --屏蔽设置 CHAT_HISTROY = "CHAT_HISTROY", --聊天记录 CECRET_HISTROY = "CECRET_HISTROY", --密聊玩家记录 STORY_TIMES = "STORY_TIMES", --剧情次数记录 HELPER_TIMES = "HELPER_TIMES", --新手引导 SINGLE_HELPER_TIMES = "SINGLE_HELPER_TIMES", --新手引导 GAME_VERSION = "GAME_VERSION", --游戏版本号 SILENT_ASSETS_FINISH = "SILENT_ASSETS_FINISH", --是否已完成静默资源下载 CHAT_CUSTOM_TEXT = "CHAT_CUSTOM_TEXT", --聊天自定义文本 NOTICE_LIST_MD5 = "NOTICE_LIST_MD5", --公告列表文件版本md5 POP_NOTICE_TIME = "POP_NOTICE_TIME", --弹出公告的时间戳 STAR_FIGHT_TEAM = "STAR_FIGHT_TEAM", --星灵试炼的队伍 TEST_DETECT_PLAYER_ATTR = "TEST_DETECT_PLAYER_ATTR", -- 数值策划测试所用 记录角色一开始到某阶段的属性信息 STAR_FIGHT_RED_SHOW = "STAR_FIGHT_RED_SHOW", --星灵试炼每天的小红点提示 EMPTY_PACK_LOAD_NEWPLAYER_FINISH = "EMPTY_PACK_LOAD_NEWPLAYER_FINISH", --空包加载过新手漫游剧情状态 NEWPLAYER_MAP_RES_DOWNLOAD_DATA = "NEWPLAYER_MAP_RES_DOWNLOAD_DATA", --新手地图资源静默下载记录信息 SUPER_VIP_SHOW_STATE = "SUPER_VIP_SHOW_STATE", --超级VIP是否推送过记录信息 CHEAT_HISTORY = "CHEAT_HISTORY", --秘籍记录 ------------------------- MATE_GAME_TIP_SHOW_TAG = "MATE_GAME_TIP_SHOW_TAG", --羁绊小游戏提示界面 MATE_GAME_RED_SHOW = "MATE_GAME_RED_SHOW", --羁绊小游戏每日红点 EQUIP_STAR_RED_SHOW = "EQUIP_STAR_RED_SHOW", --装备星级套装红点 VIP_RED_SHOW = "VIP_RED_SHOW", --vip红点 FOSTER_HIDE_TAG = "FOSTER_HIDE_TAG", --进阶显示模型控制 DAILY_DEGREE_SHOW = "DAILY_DEGREE_SHOW", --学位显示 FORTUNECAT_AD_HAS_SHOWN = "FORTUNECAT_AD_HAS_SHOWN", --招财猫广告界面是否已经打开过 GOD_BOOK_SUIT_SHOW_REDID = "GOD_BOOK_SUIT_SHOW_REDID", --式神套装图鉴红点 HOSTING_SETTING_RED_SHOW = "HOSTING_SETTING_RED_SHOW", --托管打开红点 GUILD_ORDER_NOTICE = "GUILD_ORDER_NOTICE", --社团派单通知 GUILD_SCENE_AUCION = "GUILD_SCENE_AUCION", --社团拍卖提示 STAGE_EXCHANGE_RED = "STAGE_EXCHANGE_RED", --阶段兑换活动一次性红点 RECHARGE_ADD_RED = "RECHARGE_ADD_RED", --首充附加红点 REVERSI_RED = "REVERSI_RED", --黑白棋一次性红点 FESTIVAL_BOSS_RED = "FESTIVAL_BOSS_RED", --节日幻魔活动红点 GROUP_BUYING = "GROUP_BUYING", --团购红点 NEWER_TASK_END_TIME = "NEWER_TASK_END_TIME", --新手任务结束时间 NEWER_TASK_SHOW_FLAG = "NEWER_TASK_SHOW_FLAG", --新手任务首次打开标识 PSIONIC_EFFECT_SHOW_STATUS = "PSIONIC_EFFECT_SHOW_STATUS", --圣物系统魂环展示标志 DEVICE_FIRST_SETUP_TIME = "DEVICE_FIRST_SETUP_TIME", --设备首次启动时间(游戏首次运行时间) BOSS_MONEY_FIRST_IN = "BOSS_MONEY_FIRST_IN", --首次进入赏金幻魔场景 READ_USER_AGREEMENT = "READ_USER_AGREEMENT", --是否已经勾选用户协议 EQUIP_AOTU_BUY_SHOP = "EQUIP_AOTU_BUY_SHOP", --装备自动购买商品 POWER_TURN_TABLE_SCORE_RED = "POWER_TURN_TABLE_SCORE_RED", --源能转盘积分红点 SAKURA_GIFT_FIRST_LOGIN_RED = "SAKURA_GIFT_FIRST_LOGIN_RED", -- 源樱之礼首次登陆红点 SAKURA_GIFT_FIRST_EXCHANGE_RED = "SAKURA_GIFT_FIRST_EXCHANGE_RED", -- 源樱之礼每天首登兑换红点 VIP_NOBLE_SKILL_PLAY = "VIP_NOBLE_SKILL_PLAY", -- vip特权技能播放 GUILD_NEXT_MERGE_TIP_TIME = "GUILD_NEXT_MERGE_TIP_TIME", -- 社团下一次社团合并提示时间 CSGWAR_SHOW_ICON_LOGIN_RED = "CSGWAR_SHOW_ICON_LOGIN_RED", -- 跨国团战公示图标的每天首次登陆红点 ZERO_GIFT_VIEW_FIRST_OPEN = "ZERO_GIFT_VIEW_FIRST_OPEN", -- 零元礼包界面首次打开 KF_FORSTER_STAGE_ACT_RED = "KF_FORSTER_STAGE_ACT_RED", --开服进阶每日首次登陆红点 OP_JI_ZI_RED_DOT_TIP = "OP_JI_ZI_RED_DOT_TIP", --集字红点提示cookie MATE_FIRST_GET_FRIEND = "MATE_FIRST_GET_FRIEND", --首次结成羁绊一次性红点 SEC_KILL_NEED_REMIND = "SEC_KILL_NEED_REMIND", --限购秒杀 每个场次是否需要提醒 SEC_KILL_BUY_TIPS = "SEC_KILL_BUY_TIPS", --限购秒杀 购买时无需提示彩钻购买 AUTO_CHNAGE_DAILY_SKILL = "AUTO_CHNAGE_DAILY_SKILL", --自动切换幻光技能 FORSTER_FIRST_ADVANCE_TIPS = "FORSTER_FIRST_ADVANCE_TIPS", --首次进阶提示 CONTRACT_WEEK_LOGIN_RED = "CONTRACT_WEEK_LOGIN_RED", -- 万物宝典首次登陆红点 FORTUNE_CAT_LOGIN_RED = "FORTUNE_CAT_LOGIN_RED", --招财猫每日首次登陆红点 DAYLIY_RECHARGE_LOGIN_RED = "DAYLIY_RECHARGE_LOGIN_RED", --每日累充活动首次登陆红点 } function CookieWrapper:__init() if CookieWrapper.Instance ~= nil then return end CookieWrapper.Instance = self self.cookie_root_path = cookiesManager:GetCookiePath() self.account_info_path = self.cookie_root_path .. "account_info/" self.chat_histroy_path = self.cookie_root_path .. "chat_histroy/" self:InitCommonValue() self.chat_histroy_id = nil self.account_info_init = false self.account_name = nil self.is_dirty = false self.role_pos_is_dirty = false end function CookieWrapper:InitCommonValue() cookiesManager:LoadCookie("LoginInfo", nil) cookiesManager:LoadCookie("Server", nil) if not Cookies["Server"] then Cookies["Server"] = {} end cookiesManager:LoadCookie("Common", nil) if not Cookies["Common"] then Cookies["Common"] = {} end local now_version = Cookies["Common"]["__Version"] if not now_version or now_version ~= CookieVersion then Cookies["Common"] = {} Cookies["Common"]["__Version"] = CookieVersion end self:ClearOverdueItem(Cookies["Common"]) end function CookieWrapper:InitAccountValue(account_name) if not account_name then return end self.chat_histroy_id = nil self.account_name = tostring(account_name) cookiesManager:LoadCookie(self.account_name, self.account_info_path) if not Cookies[CookieAccountInfo] then Cookies[CookieAccountInfo] = {} end if not Cookies[CookieAccountInfo][self.account_name] then Cookies[CookieAccountInfo][self.account_name] = {} end local now_version = Cookies[CookieAccountInfo][self.account_name]["__Version"] if not now_version or now_version ~= CookieVersion then Cookies[CookieAccountInfo][self.account_name] = {} Cookies[CookieAccountInfo][self.account_name]["__Version"] = CookieVersion end if not Cookies[CookieAccountInfo][self.account_name]["Common"] then Cookies[CookieAccountInfo][self.account_name]["Common"] = {} end self:ClearOverdueItem(Cookies[CookieAccountInfo][self.account_name]["Common"]) self.account_info_init = true GlobalEventSystem:Fire(EventName.COOKIE_INIT_FINISH) end function CookieWrapper:SaveCookie(level_type, cache_type, key, value) if not level_type or not cache_type or not key or value == nil then return end if key == CookieKey.SCENE_X_Y then self.role_pos_is_dirty = true else self.is_dirty = true end local item = {} item.value = value item.time_type = cache_type item.server_time = TimeUtil:getServerTime() if level_type == CookieLevelType.Common then Cookies["Common"][key] = item elseif level_type == CookieLevelType.Server then Cookies["Server"][key] = item elseif level_type == CookieLevelType.Account then if self.account_info_init then Cookies[CookieAccountInfo][self.account_name]["Common"][key] = item end end end function CookieWrapper:ClearCookie( level_type, key) if level_type == CookieLevelType.Common then Cookies["Common"][key] = nil elseif level_type == CookieLevelType.Server then Cookies["Server"][key] = nil elseif level_type == CookieLevelType.Account then if self.account_info_init then Cookies[CookieAccountInfo][self.account_name]["Common"][key] = nil end end end function CookieWrapper:GetCookie(level_type, key) if not level_type or not key then return end local handle_item = nil if level_type == CookieLevelType.Common then handle_item = Cookies["Common"][key] elseif level_type == CookieLevelType.Server then handle_item = Cookies["Server"][key] elseif level_type == CookieLevelType.Account then if self.account_info_init then handle_item = Cookies[CookieAccountInfo][self.account_name]["Common"][key] end end if handle_item then return handle_item.value end end function CookieWrapper:ClearOverdueItem(handle_table) local del_arr = {} local server_time = TimeUtil:getServerTime() local cur_date = os.date("*t", server_time) for key, item in pairs(handle_table) do if type(item) == "table" and item.time_type == CookieTimeType.TYPE_DAY and --24小时后清 server_time - item.server_time > 3600 * 24 then table.insert(del_arr, key) elseif type(item) == "table" and item.time_type == CookieTimeType.TYPE_DAY2 and not (os.date("*t", item.server_time).day == cur_date.day and os.date("*t", item.server_time).month == cur_date.month and os.date("*t", item.server_time).year == cur_date.year) then -- 跨天清(0点) table.insert(del_arr, key) end end for _, key in pairs(del_arr) do handle_table[key] = nil end end function CookieWrapper:WriteAccount() if self.account_name ~= nil then cookiesManager:WriteCookie(self.account_name, CookieAccountInfo .. "." .. self.account_name, self.account_info_path) end end function CookieWrapper:WriteAll() local save_pos_state = false if self.role_pos_is_dirty then self.role_pos_is_dirty = false save_pos_state = true self:WriteAccount() end if not self.is_dirty then return end self.is_dirty = false cookiesManager:WriteCookie("LoginInfo", "LoginInfo", "") cookiesManager:WriteCookie("Common", "Common", "") cookiesManager:WriteCookie("Server", "Server", "") if not save_pos_state then self:WriteAccount() end end function CookieWrapper:__delete() self:WriteAll() if self.timer_id ~= 0 then GlobalTimerQuest:CancelQuest(self.timer_id) self.timer_id = 0 end --添加保存聊天记录 self:WriteChatHistroy() if self.chat_timer_id ~= 0 then GlobalTimerQuest:CancelQuest(self.chat_timer_id) self.chat_timer_id = 0 end end function CookieWrapper:initChatValue(playerid) --这里层次结构为[str][自身id][密聊玩家id] --以cookiekey为第一层,没使用cookietype if not self.account_name or not playerid then return end playerid = tostring(playerid) local path = self.chat_histroy_path..self.account_name.."/" cookiesManager:LoadCookie(tostring(playerid), path) if not Cookies[CookieKey.CHAT_HISTROY] then Cookies[CookieKey.CHAT_HISTROY] = {} end if not Cookies[CookieKey.CHAT_HISTROY][self.account_name] then Cookies[CookieKey.CHAT_HISTROY][self.account_name] = {} end if not Cookies[CookieKey.CHAT_HISTROY][self.account_name][playerid] then Cookies[CookieKey.CHAT_HISTROY][self.account_name][playerid] = {} end end function CookieWrapper:WriteChatHistroy() --保存场景为,每一分钟/关闭chatview/密聊返回btn/switchbar -- if not self.chat_histroy_id or not self.account_name then return end -- if IsTableEmpty(Cookies[CookieKey.CHAT_HISTROY][self.account_name][self.chat_histroy_id]) then return end -- local table_name = CookieKey.CHAT_HISTROY.."."..self.account_name.."."..self.chat_histroy_id -- local path = self.chat_histroy_path..self.account_name.."/" -- cookiesManager:WriteCookie(tostring(self.chat_histroy_id), table_name, path) if Cookies[CookieKey.CHAT_HISTROY] and Cookies[CookieKey.CHAT_HISTROY][self.account_name] then for k,v in pairs(Cookies[CookieKey.CHAT_HISTROY][self.account_name]) do local table_name = CookieKey.CHAT_HISTROY.."."..self.account_name.."."..k local path = self.chat_histroy_path..self.account_name.."/" cookiesManager:WriteCookie(k, table_name, path) end end end function CookieWrapper:SaveChatHistroy(playerid,cache_type,value) --这里不做增删操作,value就是整个table --value需要经过简单处理 --修改:value传的只是增操作,退出窗口私聊内容的列表就清空了... if not playerid or not cache_type or not value then return end playerid = tostring(playerid) self:GetChatHistroy(playerid) local item = value Cookies[CookieKey.CHAT_HISTROY][self.account_name][playerid] = item end function CookieWrapper:GetChatHistroy(playerid,all) if not playerid then return end playerid = tostring(playerid) --2020.10.15 CookieKey.DEL_CHAT_COOKIE都没了,这段祖传代码也去掉吧 --全部玩家手动删除一次聊天cookie 已经解决cookie数据错误的问题了, 这段代码去掉 -- local tb = CookieWrapper.Instance:GetCookie(CookieLevelType.Account,CookieKey.DEL_CHAT_COOKIE) or {} -- if not tb[playerid] then -- local file_path = Util.DataPath..CookieWrapper.Instance.chat_histroy_path..CookieWrapper.Instance.account_name.."/"..playerid..".cfg" -- local result,log = os.remove(file_path) -- tb[playerid] = true -- CookieWrapper.Instance:SaveCookie(CookieLevelType.Account,CookieTimeType.TYPE_ALWAYS, CookieKey.DEL_CHAT_COOKIE, tb) -- end -- if self.chat_histroy_id ~= playerid then -- self:initChatValue(playerid) -- self.chat_histroy_id = playerid -- end if not Cookies[CookieKey.CHAT_HISTROY] or not Cookies[CookieKey.CHAT_HISTROY][self.account_name] or not Cookies[CookieKey.CHAT_HISTROY][self.account_name][playerid] then self:initChatValue(playerid) end if all then return Cookies[CookieKey.CHAT_HISTROY][self.account_name] else return Cookies[CookieKey.CHAT_HISTROY][self.account_name][playerid] end end function CookieWrapper:DeleteChatHistroyFile(playerid) if not playerid then return end local file_path = Util.DataPath..self.chat_histroy_path..self.account_name.."/"..playerid..".cfg" local result,log = os.remove(file_path) CookieWrapper.Instance:SaveChatHistroy(playerid,CookieTimeType.TYPE_ALWAYS,{}) self:WriteAll() end