123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- cc.Class({
- name: 'ConfigBase',
- properties: {
- mainKey: "",
- indexsCache:null,
- multiple:[],
- multipleCache:null,
- table:null,
- },
- ctor() {
- },
- initTable(table, mainKey, multipleKey) {
- this.table = table;
- this.mainKey = mainKey;
- this.multiple = multipleKey;
- this.indexsCache = {};
- this.multipleCache = {};
- return this
- },
- // getObjectValue(object, key) {
- // if (object.hasOwnProperty(key))
- // {
- // return object[key]
- // }
- // return undefined;
- // },
- getByMainKey(idx) {
- let data = this.indexsCache[idx]
- if (data != undefined) {
- return data;
- }
-
- for (let d of this.table) {
- if (d[this.mainKey] == idx) {
- data = d;
- break;
- }
- }
- this.indexsCache[data[this.mainKey]] = data;
- return data;
- },
- getByMultipleKey() {
- if (arguments.length != this.multiple.length) {
- if (arguments.length <= 0) {
- cc.log("multiple key is null");
- } else {
- cc.log("multiple key num not match curNum :" + arguments.length + " need num " + this.multiple.length);
- }
- return null;
- }
- let curCache = this.findMultipleCache(arguments);
- if (curCache != null)
- {
- return curCache;
- }
- return this.getAndAddMultiple(arguments);
- },
- getAndAddMultiple(args)
- {
- var result = [];
- // 结果拿出来
- for (let d of this.table)
- {
- let isMatch = true;
- for (let i = 0; i < this.multiple.length; i++) {
- const k = this.multiple[i];
- if (d[k] != args[i]) {
- isMatch = false;
- break
- }
- }
- if (isMatch)
- {
- result.push(d);
- }
- }
- // 添加到缓存区
- let curCache = this.multipleCache;
- for (let i = 0; i < args.length - 1; i++) {
- let k = args[i];
- let tmpCache = curCache[k];
- if (tmpCache == undefined)
- {
- curCache[k] = tmpCache = {}
- }
- curCache = tmpCache;
- }
- curCache[args[args.length - 1]] = result;
- return result;
- },
- findMultipleCache(args)
- {
- // 索引缓存
- let curCache = this.multipleCache;
- let isFindCache = true;
- for(let p of args) {
- if (curCache[p] == undefined) {
- isFindCache = false
- break
- }
- curCache = curCache[p]
- }
- if (isFindCache)
- {
- return curCache;
- }
- return null;
- },
- })
|