/**
* 创建动态属性 封装get set 处理
*/
let createProp = function(obj,key){
if(obj.hasOwnProperty(key)){
return;
}
Object.defineProperty(obj,key,{
// enumerable:true,
//writable:true,
set:function(v){
if(!obj[`_${key}key`]){
obj[`_${key}key`] = 0
}
if(!obj[`_${key}_index`]){
obj[`_${key}_index`] = 0
}
if(!obj[`_${key}_max`]){
//内存地址阈值
obj[`_${key}_max`] = 3
}
if(!obj[`_${key}_indexs`]){
obj[`_${key}_indexs`] = Array( obj[`_${key}_max`]);
}
//记录浮点数 被放大的倍数
if(!obj[`_${key}_scale`]){
obj[`_${key}_scale`] = 1;
}
//记录浮点数 被放大的倍数
if(!obj[`_${key}_scale`]){
obj[`_${key}_scale`] = 1;
}
//检查是否需要放大
let scale = 1
if(v != parseInt(v)){
let str = v.toString();
let len = str.substring(str.indexOf(".")+1,str.length)
v = v * Math.pow(10,len.length)
obj[`_${key}_scale`] = Math.pow(10,len.length)
}
obj[`_${key}key`] =v * Math.random() * scale;
obj[`_${key}_index`] += 1
obj[`_${key}_index`] = obj[`_${key}_index`]> obj[`_${key}_max`]? 0: obj[`_${key}_index`];
obj[`_${key}_indexs`][obj[`_${key}_index`]] = v ^ obj[`_${key}key`];
//console.log("存储的值",key,v ^ obj[`_${key}key`])
},
get:function(){
return (obj[`_${key}_indexs`][obj[`_${key}_index`]] ^ obj[`_${key}key`]) / obj[`_${key}_scale`];
}
})
}
//同步属性
let syncProp = function(src,obj){
//copy 属性
for (const key in obj) {
const info = obj[key];
if( typeof info == "object"){
if(!src[key]){
src[key] = {}
}
syncProp(src[key],info)
}else{
if(typeof info != "number"){
src[key] = info;
continue;
}
//加密只对数值
createProp(src,key);
src[key] = info;
}
}
}
//准备一个PLYAER_INFO
let PLYAER_INFO = {}
//服务器发送过来的数据
let serverInfo = {
skills:{
"G1S1": 10,
"G1S2": 1,
"G1S3": 1,
"G1S4": 1,
},
prop:{
"test1": 0.123,
"test2": 0.99,
"test3": 0.9999,
"test3": 1,
"test4": 4,
"test5": "hhhhh",
},
x:999,
y:120,
yidao:888,
//不重要的
flag:true,
name:"狗不理"
}
syncProp(PLYAER_INFO,serverInfo)
syncProp(PLYAER_INFO,serverInfo)
console.log(PLYAER_INFO.prop.test1)
console.log(PLYAER_INFO.prop.test2)
//console.log(PLYAER_INFO)
// console.log(PLYAER_INFO.skills["G1S1"]);
// //对G1S1赋值
// PLYAER_INFO.skills["G1S1"] = 10000
// //获取G1S1赋值
// console.log(PLYAER_INFO.skills["G1S1"]);
// //动态生成属性
// let dontaiProp = {mj:1,mj2:2}
// syncProp(PLYAER_INFO,dontaiProp)
// console.log(PLYAER_INFO.mj)
// console.log(PLYAER_INFO.mj2)
// PLYAER_INFO.mj = 100
// console.log(PLYAER_INFO.mj )
// // PLYAER_INFO.x = 111
// // console.log(PLYAER_INFO.x)
// console.log(PLYAER_INFO.name)
// console.log(PLYAER_INFO.flag)
// //console.log(PLYAER_INFO)
console