-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguildService.js
76 lines (66 loc) · 1.76 KB
/
guildService.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
const persistance = require('./persistance')
const { Guild } = require('./Guild')
const defaultServer = {
"categoryId": "undefined"
}
function createDefault(id) {
mission_counter = { ...defaultServer }
mission_counter["id"] = id
return new Promise((resolve, reject) => {
persistance.create(mission_counter).then((data, err) => {
if (err) {
reject(err);
return;
}
console.log
const tmp = new Guild(data.id, data.categoryId)
resolve(tmp);
})
})
}
function save(guild) {
const obj = {
"id": guild.id,
"categoryId": guild.categoryId,
}
return persistance.save(obj);
}
const idToServerMap = new Map();
function get(id) {
return new Promise((resolve, reject) => {
persistance.find(id).then((data, err) => {
if (err) {
reject(err);
} else {
if (!data) {
resolve(data);
return;
}
const tmp = new Guild(data.id, data.categoryId)
resolve(tmp);
}
})
})
}
function login(id) {
if (!idToServerMap.has(id)) {
get(id).then(resultGuild => {
if (resultGuild) {
idToServerMap.set(id, resultGuild)
idToServerMap.get(id)
} else {
createDefault(id).then((resultDefault) => {
idToServerMap.set(id, resultDefault);
idToServerMap.get(id)
})
}
})
} else {
idToServerMap.get(id)
}
return idToServerMap
}
exports.createDefault = createDefault;
exports.get = get;
exports.login = login;
exports.save = save;