我智商爆棚
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

143 lines
5.0 KiB

import RYSDK from "./RYSDK";
export default class RYAD {
//protected _cached_custom = {};
public getAD_custom(id: string, complete: Function, useLocalRandom?: boolean) {
useLocalRandom = null == useLocalRandom ? true : useLocalRandom;
let self = this;
let para =
{
adv_key: id,
success: (data) => {
console.log("获取到广告数据 RYAD:", data);
if (200 == data.code) {
let datas = data.result.list;
if (datas == null) {
console.error("获取到广告数据 为NULL")
}
if (true == useLocalRandom) {
RYAD.sortDatas_custom(datas);
}
complete(datas)
}
else {
console.error("获取到广告数据 为NULL")
complete(null)
}
},
fail: (data) => {
complete(null)
},
complete: (data) => {
//complete()
},
};
if (null != RYSDK.Instance_custom.rysdk_custom) {
RYSDK.Instance_custom.rysdk_custom.ry_GetAdv(para);
}
else {
complete(null);
}
}
public static sortDatas_custom(datas: any): any {
if (null == datas || 0 == datas.length)
return [];
let dataDic: { [appid: string]: any[] } = {};
let keys = new Array<string>();
for (let i = 0; i < datas.length; ++i) {
let data = datas[i];
if (dataDic[data.appid] == null) {
dataDic[data.appid] = new Array();
dataDic[data.appid].push(data);
keys.push(data.appid);
} else {
dataDic[data.appid].push(data);
}
}
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
let randomIndex = Math.floor(Math.random() * keys.length);
let temp = keys[randomIndex];
keys[randomIndex] = key;
keys[i] = temp;
}
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
let dataArray = dataDic[key];
for (let j = 0; j < dataArray.length; ++j) {
let data = dataArray[j];
let randomIndex = Math.floor(Math.random() * dataArray.length);
let temp = dataArray[randomIndex];
dataArray[randomIndex] = data;
dataArray[j] = temp;
}
}
let res = new Array<any>();
while (keys.length > 0) {
if (keys.length == 1) {
let key = keys[0];
let isOk = false;
let dataArray = dataDic[key];
for (let i = 0; i < res.length; ++i) {
let curData = res[i];
let nextData = res[i + 1];
if (null != nextData) {
if (curData.appid != key && nextData.appid != key) {
let data = dataArray.shift();
if (null != data) {
let f = res.slice(0, i + 1);
let b = res.slice(i + 1, res.length);
res = f;
res.push(data);
for (let k = 0; k < b.length; ++k) {
res.push(b[k]);
}
}
isOk = true;
break;
}
}
else {
if (curData.appid != key) {
let data = dataArray.shift();
if (null != data) {
let f = res.slice(0, i + 1);
let b = res.slice(i + 1, res.length);
res = f;
res.push(data);
for (let k = 0; k < b.length; ++k) {
res.push(b[k]);
}
}
isOk = true;
break;
}
}
}
if (!isOk || dataArray.length <= 0) {
keys.splice(0, 1);
}
}
else {
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
let dataArray = dataDic[key];
let data = dataArray.shift();
res.push(data);
if (dataArray.length <= 0) {
keys.splice(i, 1);
--i;
}
}
}
}
return res;
}
}