import MsgAlert from "../../game/msg/MsgAlert"; export default class SocketUtil { // 發送記錄 private static sendRecord: any = {}; // 發送接口白名單 private static sendWhiteList: any = { "c2s_aoi_move": 1,//移動 "c2s_aoi_stop": 1,//移動 "c2s_horse_upgrade": 1,//坐騎升級 "c2s_ask_lottery_info": 1,//高級藏寶圖、山河社稷圖 "c2s_lottery_go": 1,//高級藏寶圖、山河社稷圖 "c2s_ask_dial_info": 1,//風雨同舟 "c2s_dial_go": 1,//風雨同舟 "c2s_btl_act": 1,//戰鬥 "c2s_equip_info": 1,//請求裝備信息 "c2s_pet_info": 1,//請求寵物信息 "c2s_creat_equip": 1,//請求發送新手裝備 "c2s_mail_action": 1,//郵件操作 "c2s_upgrade_skill": 1,//坐騎提陞技能熟練度 }; // 清除訪問記錄間隔 public static clearInterval: number = 60;//60秒 // 發送校驗的時間段 private static checkTime: number = 2;//2秒 // 校驗時間段內最大發送次數 private static sendMax: number = 3;//3次 // 發送太快提示內容 private static tipStr: string = "您操作太快啦!"; /** * 獲取是否可以發送 * @param interfaceName 接口名稱 */ public static getCanSend(interfaceName: string, properties: any): boolean { // 白名單接口,不校驗 if (SocketUtil.sendWhiteList[interfaceName]) return true; if (this.specialCheck(interfaceName, properties)) return true; // 當前時間 var curT = new Date().getTime(); // 沒有找到發送記錄 if (!SocketUtil.sendRecord[interfaceName]) { var tempObj = { recordList: [curT], lastTime: curT } SocketUtil.sendRecord[interfaceName] = tempObj; return true; } // 有找到發送記錄 if (SocketUtil.sendRecord[interfaceName]) { var sendData = SocketUtil.sendRecord[interfaceName]; // 小於校驗時間段內最大發送次數 if (sendData.recordList.length < SocketUtil.sendMax) { sendData.recordList.push(curT); sendData.lastTime = curT; SocketUtil.sendRecord[interfaceName] = sendData; return true; } // 發送記錄大于等于最大发送次数 var times = sendData.recordList.length; //發送記錄條數 var checkTime = sendData.recordList[times - SocketUtil.sendMax];//需要校驗的發送記錄 checkTime = checkTime ? checkTime : 0; if (checkTime + SocketUtil.checkTime * 1000 > curT) { // 在校驗時間段內發送次數大於最大發送次數 MsgAlert.addMsg(SocketUtil.tipStr); return false; } // 在校驗時間段內發送次數小於最大發送次數 sendData.recordList.push(curT); sendData.lastTime = curT; SocketUtil.sendRecord[interfaceName] = sendData; return true; } return true; } private static specialCheck(interfaceName: string, properties: any): boolean { if (interfaceName == "c2s_use_bagitem") { // 超級夥伴修煉冊 if (properties.itemid == 10204) return true; // 夥伴修煉冊 if (properties.itemid == 10202) return true; // 超級神獸丹 if (properties.itemid == 10110) return true; // 靈獸丹 if (properties.itemid == 10112) return true; // 聖獸丹 if (properties.itemid == 10113) return true; // 神獸丹 if (properties.itemid == 10114) return true; // 高級親密丹 if (properties.itemid == 10120) return true; // 親密丹 if (properties.itemid == 10111) return true; } return false; } public static clearSendRecord() { var curT = new Date().getTime(); for (let key in SocketUtil.sendRecord) { var sendData = SocketUtil.sendRecord[key]; // 最後一次發送記錄和當前時間差已經超過校驗時間段 if (sendData.lastTime + SocketUtil.checkTime * 1000 < curT) { delete SocketUtil.sendRecord[key]; continue; } // 保留檢測時間段的發送記錄 var tempList = []; for (var i = 0; i < sendData.recordList.length; i++) { if (sendData.recordList[i] + SocketUtil.checkTime * 1000 >= curT) tempList.push(sendData.recordList[i]); } SocketUtil.sendRecord[key].recordList = tempList; } } public static updateWhiteList(list) { for (let i in list) { if (!this.sendWhiteList[list[i]]) { // 白名單中沒有該接口 this.sendWhiteList[list[i]] = 1; } } } }