-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibs.js
112 lines (90 loc) · 2.68 KB
/
libs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
global.storageCache = { }
global.globalCache = { };
const StorageTypes = {
Number: 1,
Object: 2,
String: 3
}
const getGroup = async (msg) => {
var chat = await msg.getChat();
if (chat.isGroup) return chat;
return undefined;
}
const userIsAdmin = async (chat, authorId) => {
for(let participant of chat.participants) {
if(participant.id._serialized === authorId && participant.isAdmin) {
return true;
}
}
return false;
}
class UserStorage {
constructor(id, type) {
this.id = id;
this.playerStorages = {};
this.type = type;
if(storageCache[id]) {
console.error(`Duplicated id for ${id}.`);
return;
}
storageCache[id] = this;
}
setStorage(id, newValue) {
this.playerStorages[id] = newValue;
}
increaseStorage(id, diff) {
if (this.type != StorageTypes.Number) return;
var currentStorage = this.playerStorages[id] || 0;
currentStorage += diff;
this.playerStorages[id] = currentStorage;
}
decreaseStorage(id, diff) {
if (this.type != StorageTypes.Number) return;
var currentStorage = this.playerStorages[id] || 0;
currentStorage -= diff;
this.playerStorages[id] = currentStorage;
}
getStorage(id) {
return this.playerStorages[id] || null;
}
}
const getUserStorage = (id) => {
return storageCache[id];
}
// server
class Storage {
constructor(id, callback, initialValue) {
this.id = id;
this.callback = callback;
this.value = initialValue;
if(globalCache[id]) {
console.error("Duplicated Storage for id " + id);
return;
}
globalCache[id] = this;
callback(this.value);
}
setValue(value) {
this.value = value;
this.callback(this.value);
}
}
const getStorageValue = (id) => globalCache[id] && globalCache[id].value;
const getStorage = (id) => globalCache[id];
const getRandomInt = (max) => {
return Math.floor(Math.random() * max);
}
const getRandomIntRange = (min, max) => {
return Math.floor(Math.random() * (max - min) + min);
}
const getTimeStamp = () => {
let currentdate = new Date();
let timeStamp = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
return timeStamp;
}
module.exports = { getTimeStamp, getUserStorage, UserStorage, StorageTypes, Storage, getStorage, getStorageValue, getRandomInt, getRandomIntRange, userIsAdmin, getGroup }