ConfigBase.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. cc.Class({
  2. name: 'ConfigBase',
  3. properties: {
  4. mainKey: "",
  5. indexsCache:null,
  6. multiple:[],
  7. multipleCache:null,
  8. table:null,
  9. },
  10. ctor(table, mainKey, multipleKey) {
  11. this.table = table;
  12. this.mainKey = mainKey;
  13. this.multiple = multipleKey;
  14. this.indexsCache = {};
  15. this.multipleCache = {};
  16. },
  17. // getObjectValue(object, key) {
  18. // if (object.hasOwnProperty(key))
  19. // {
  20. // return object[key]
  21. // }
  22. // return undefined;
  23. // },
  24. getByMainKey(idx) {
  25. let data = this.indexsCache[idx]
  26. if (data != undefined) {
  27. return data;
  28. }
  29. for (let d of this.table) {
  30. if (d[this.mainKey] == idx) {
  31. data = d;
  32. break;
  33. }
  34. }
  35. this.indexsCache[data[this.mainKey]] = data;
  36. return data;
  37. },
  38. getByMultipleKey() {
  39. if (arguments.length != this.multiple.length) {
  40. if (arguments.length <= 0) {
  41. cc.log("multiple key is null");
  42. } else {
  43. cc.log("multiple key num not match curNum :" + arguments.length + " need num " + this.multiple.length);
  44. }
  45. return null;
  46. }
  47. let curCache = this.findMultipleCache(arguments);
  48. if (curCache != null)
  49. {
  50. return curCache;
  51. }
  52. return this.getAndAddMultiple(arguments);
  53. },
  54. getAndAddMultiple(args)
  55. {
  56. var result = [];
  57. // 结果拿出来
  58. for (let d of this.table)
  59. {
  60. let isMatch = true;
  61. for (let i = 0; i < this.multiple.length; i++) {
  62. const k = this.multiple[i];
  63. if (d[k] != args[i]) {
  64. isMatch = false;
  65. break
  66. }
  67. }
  68. if (isMatch)
  69. {
  70. result.push(d);
  71. }
  72. }
  73. // 添加到缓存区
  74. let curCache = this.multipleCache;
  75. for (let i = 0; i < args.length - 1; i++) {
  76. let k = args[i];
  77. let tmpCache = curCache[k];
  78. if (tmpCache == undefined)
  79. {
  80. curCache[k] = tmpCache = {}
  81. }
  82. curCache = tmpCache;
  83. }
  84. curCache[args[args.length - 1]] = result;
  85. return result;
  86. },
  87. findMultipleCache(args)
  88. {
  89. // 索引缓存
  90. let curCache = this.multipleCache;
  91. let isFindCache = true;
  92. for(let p of args) {
  93. if (curCache[p] == undefined) {
  94. isFindCache = false
  95. break
  96. }
  97. curCache = curCache[p]
  98. }
  99. if (isFindCache)
  100. {
  101. return curCache;
  102. }
  103. return null;
  104. },
  105. })