-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.js
60 lines (58 loc) · 1.92 KB
/
bank.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
import { MonsterGameConfig } from "./bot";
import http from "http";
//TODO: Make the network requests actually work
export default class Bank {
constructor() {
this.options = {
hostname: MonsterGameConfig.get('bank.hostname'),
port: MonsterGameConfig.get('bank.port'),
}
this.remote = MonsterGameConfig.get('bank.remote');
this.bankStore = new Store({ path: './_data/bank-data.json' });
}
async getBalance(id) {
if (this.remote) {
this.options.path = '/balance/' + id;
this.options.method = 'GET';
http.request(this.options, res => {
if (res.statusCode == 200) {
return res.body;
}
else {
throw new Error('Balance with that user id does not exist');
}
});
} else {
return this.bankStore.get(id.toString());
}
}
async setBalance(id, balance) {
if (this.remote) {
this.options.path = '/balance/' + id;
this.options.method = 'PUT';
http.request(this.options, res => {
if (res.statusCode == 200) {
return res.body;
}
});
} else {
return this.bankStore.set(id.toString(), balance);
}
}
async addBalance(id, balance) {
if (this.remote) {
this.options.path = '/balance/' + id;
this.options.method = 'POST';
http.request(this.options, res => {
if (res.statusCode == 200) {
return res.body;
}
else {
throw new Error('Balance with that user id does not exist');
}
});
} else {
return this.bankStore.set(id.toString(), this.bankStore.get(id.toString()) + balance);
}
}
}