UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const promise_1 = require("./promise");
4function compareList(a, key, value) {
5 if (!a || !a.hasOwnProperty(key))
6 return -1;
7 if (a[key] > value)
8 return 1;
9 if (a[key] == value)
10 return 0;
11 return -1;
12}
13/**
14 * 开始二分法查找
15 * @param list 查找用的列表
16 * @param key 查找的单位元素
17 * @param value 比较用的数值
18 */
19function orderListFind(list, key, value, desc = false) {
20 if (list.length == 0)
21 return 0;
22 var small = -1, big = list.length;
23 while (true) {
24 var ret = 0;
25 var center = Math.floor((small + big) / 2);
26 if (small == big) {
27 if (small == -1 || big == list.length)
28 return small;
29 switch (compareList(list[big], key, value)) {
30 case 0: return big;
31 case -1: return desc ? big - 1 : big + 1;
32 case 1: return desc ? big + 1 : big;
33 }
34 }
35 else if (small + 1 == big) {
36 // 差一个的时候比较一下小的
37 // switch (compareList<T>(list[big], key, value)) {
38 // case 0: return big;
39 // case -1: return desc ? small : (big == list.length ? big : big + 1);
40 // case 1: return desc ? big + 1 : small;
41 // }
42 // 这里比较一下,先从小的开始
43 var sr = -2, br = -2;
44 if (small >= 0) {
45 sr = compareList(list[small], key, value);
46 }
47 if (big < list.length) {
48 br = compareList(list[big], key, value);
49 }
50 if (desc) {
51 if (sr != -2 && br != -2) {
52 if (sr == -1) {
53 return small - 1;
54 }
55 else if (sr == 0) {
56 return small;
57 }
58 else if (br == 0) {
59 return big;
60 }
61 else if (br == -1) {
62 // 这里 比小的大,但是比大的小
63 return small;
64 }
65 else {
66 return big;
67 }
68 }
69 else if (br != -2) {
70 // 这里小的是不存在的,就是说大的是第 0 位
71 if (br == -1) {
72 return small;
73 }
74 else {
75 return big;
76 }
77 }
78 else if (sr != -2) {
79 // 这里表示大的不存在,就是说小的是最后一个
80 if (sr == -1) {
81 return small - 1;
82 }
83 else {
84 return small;
85 }
86 }
87 else {
88 return -1;
89 }
90 }
91 else {
92 if (sr != -2 && br != -2) {
93 if (sr == 1) {
94 return small - 1;
95 }
96 else if (sr == 0) {
97 return small;
98 }
99 else if (br == 0) {
100 return big;
101 }
102 else if (br == 1) {
103 // 这里 比小的大,但是比大的小
104 return small;
105 }
106 else {
107 return big;
108 }
109 }
110 else if (br != -2) {
111 // 这里小的是不存在的,就是说大的是第 0 位
112 if (br == 1) {
113 return small;
114 }
115 else {
116 return big;
117 }
118 }
119 else if (sr != -2) {
120 // 这里表示大的不存在,就是说小的是最后一个
121 if (sr == 1) {
122 return small - 1;
123 }
124 else {
125 return small;
126 }
127 }
128 else {
129 return -1;
130 }
131 }
132 }
133 else {
134 ret = compareList(list[center], key, value);
135 }
136 if (desc) {
137 switch (ret) {
138 case 0: // 中间值是相等的数值的 当成 1处理
139 big = center;
140 break;
141 case 1:
142 small = center;
143 break;
144 case -1:
145 big = center;
146 break;
147 }
148 }
149 else {
150 switch (ret) {
151 case 0: // 中间值是相等的数值的 当成 1处理
152 big = center;
153 break;
154 case 1:
155 big = center;
156 break;
157 case -1:
158 small = center;
159 break;
160 }
161 }
162 }
163}
164function orderListInsert(insertValue, list, key, value, desc = false) {
165 // 先找一下位置
166 var index = orderListFind(list, key, value, desc);
167 list.splice(index + 1, 0, insertValue);
168}
169function orderListRemove(list, key, value, desc = false) {
170 var index = orderListFind(list, key, value, desc);
171 for (var i = index; i < list.length; i++) {
172 var rkInfo = list[i];
173 if (rkInfo) {
174 if (rkInfo[key] == value) {
175 list.splice(i, 1);
176 }
177 else if ((rkInfo[key] > value && !desc) || (rkInfo[key] < value && desc)) {
178 break;
179 }
180 else if (i != index) {
181 console.log("err");
182 }
183 }
184 }
185}
186class TeMap {
187 constructor(_data) {
188 this._data = {};
189 if (_data) {
190 this._data = _data;
191 }
192 }
193 has(key) {
194 return this._data.hasOwnProperty(key.toString());
195 }
196 get(key) {
197 return this._data[key];
198 }
199 set(key, v) {
200 this._data[key] = v;
201 }
202 get keys() {
203 return Object.keys(this._data);
204 }
205 del(key) {
206 var obj = this._data[key];
207 if (obj && typeof obj == 'object' && obj['destory'] && typeof obj['destory'] == 'function') {
208 obj['destory']();
209 }
210 delete this._data[key];
211 }
212 rand() {
213 var keys = this.keys;
214 var tid = keys[Math.floor(Math.random() * keys.length)];
215 return this.get(tid);
216 }
217 clear() {
218 this._data = {};
219 }
220}
221exports.TeMap = TeMap;
222/**
223 * list 的方式实现map
224 */
225class MapList {
226 constructor(mkey) {
227 this._data = [];
228 this.mkey = mkey;
229 }
230 _find_index(v) {
231 var num = orderListFind(this._data, this.mkey, v);
232 if (num < 0 || num >= this._data.length)
233 return -1;
234 var rInfo = this._data[num];
235 if (rInfo[this.mkey] != v) {
236 return -1;
237 }
238 return num;
239 }
240 has(k) {
241 if (this._find_index(k.toString()) >= 0) {
242 return true;
243 }
244 return false;
245 }
246 at(idx) {
247 return this._data[idx];
248 }
249 get(v) {
250 v = v.toString();
251 var num = this._find_index(v);
252 return (num == -1) ? null : this._data[num];
253 }
254 set(k, v) {
255 k = k.toString();
256 var num = this._find_index(k);
257 if (num >= 0) {
258 // 找到了就使用
259 this._data[num] = v;
260 }
261 else {
262 // 没找到就要新增一个
263 orderListInsert(v, this._data, this.mkey, k);
264 }
265 }
266 remove(v) {
267 v = v.toString();
268 orderListRemove(this._data, this.mkey, v);
269 }
270 get length() {
271 return this._data.length;
272 }
273 clear() {
274 this._data = [];
275 }
276}
277exports.MapList = MapList;
278var showUpdate = false;
279function promise_setInterval(...args) {
280 let [name, callback, time] = args;
281 if (typeof name != 'string') {
282 name = '';
283 callback = args[0];
284 time = args[1];
285 }
286 let handle;
287 function update() {
288 if (showUpdate)
289 console.log(handle.name, callback.name, 'start');
290 promise_1.createPromise(callback()).finally(function () {
291 if (showUpdate)
292 console.log(handle.name, callback.name, 'finish');
293 handle && handle.refresh();
294 });
295 }
296 handle = setTimeout(update, time);
297 handle['name'] = name;
298 return {
299 stop: function () {
300 clearTimeout(handle);
301 }
302 };
303}
304class TeRandom {
305 constructor(seed = Math.random()) {
306 this._seed = 5;
307 this._seed = seed;
308 }
309 reset(seed) {
310 this._seed = seed;
311 }
312 get seed() {
313 return this._seed;
314 }
315 random(min, max, msg = "") {
316 max = (max == undefined) ? 1 : max;
317 min = (min == undefined) ? 0 : min;
318 this._seed = (this._seed * 9301 + 49297) % 233280;
319 var rnd = this._seed / 233280.0;
320 // console.log(gMain.iGameTime,this._seed,min,max,min + rnd * (max - min),msg);
321 return min + rnd * (max - min);
322 }
323 ;
324 randInt(min, max, msg = "") {
325 return Math.floor(this.random(min, max, msg));
326 }
327}
328exports.TeRandom = TeRandom;
329TeRandom.DEFAULT_SEED = 6364;
330class HashMap {
331 constructor() {
332 this._data = {};
333 }
334 get(key) {
335 key = key.toString();
336 return this._data[key] || [];
337 }
338 add(key, v) {
339 if (typeof key == 'number') {
340 key = key.toString();
341 }
342 if (!this._data[key]) {
343 this._data[key] = [];
344 }
345 this._data[key].push(v);
346 }
347 get keys() {
348 return Object.keys(this._data);
349 }
350 set(key, v) {
351 key = key.toString();
352 this._data[key] = v;
353 }
354 clear() {
355 this._data = {};
356 }
357 has(key, v) {
358 key = key.toString();
359 if (this._data.hasOwnProperty(key)) {
360 if (v && this.get(key).indexOf(v) < 0)
361 return false;
362 return true;
363 }
364 return false;
365 }
366 sort(fn) {
367 for (var key in this._data) {
368 var r = this._data[key];
369 r.sort(fn);
370 }
371 }
372}
373exports.HashMap = HashMap;
374// 拷贝函数 是否包括函数拷贝
375function func_copy(obj, bFunc = false) {
376 var out;
377 if (obj instanceof Array) {
378 out = [];
379 // 数组这里就
380 for (let i = 0; i < obj.length; i++) {
381 out[i] = func_copy(obj[i], bFunc);
382 }
383 }
384 else if (obj instanceof Buffer) {
385 // buffer 这里就不拷贝算了
386 out = Buffer.from(obj);
387 }
388 else if (typeof obj == 'object') {
389 out = {};
390 for (var key in obj) {
391 if (key == 'clone' || key == 'global') {
392 continue;
393 }
394 if (typeof obj[key] == 'function' && !bFunc) {
395 continue;
396 }
397 if (obj[key] == null) {
398 out[key] = null;
399 }
400 else
401 out[key] = func_copy(obj[key], false);
402 }
403 }
404 else {
405 out = obj;
406 }
407 return out;
408}
409exports.Util = {
410 Sort: {
411 orderListFind: orderListFind,
412 orderListRemove: orderListRemove,
413 orderListInsert: orderListInsert
414 },
415 Map: TeMap,
416 MapList: MapList,
417 setInterval: promise_setInterval,
418 switchShowInterval: function (b) {
419 showUpdate = b;
420 },
421 Random: TeRandom,
422 HashMap: HashMap,
423 copy: func_copy
424};