ConfigBase.js 2.8 KB

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