Skip to content

Commit f8097e8

Browse files
committed
Format
1 parent 87931da commit f8097e8

File tree

6 files changed

+319
-282
lines changed

6 files changed

+319
-282
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
root = true
3+
4+
[{src,scripts}/**.{ts,json,js}]
5+
end_of_line = crlf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
indent_style = space
10+
indent_size = 2

src/bank.ts

+39-39
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import fetch from 'node-fetch';
2-
var currentPrice = 10;
3-
var dead = false;
4-
5-
const UPDATE_INTERVAL = 1000;
6-
7-
export function isDead() {
8-
return dead;
9-
}
10-
11-
export function getCurrentPrice() {
12-
return currentPrice;
13-
}
14-
15-
export async function updateCurrentPrice() {
16-
return fetch("https://tibyte.net/sandi.txt?r="+Math.random())
17-
.then(x => {
18-
if (!x.ok) {
19-
throw Error(x.statusText);
20-
}
21-
return x;
22-
})
23-
.then(x => x.text())
24-
.then(text => {
25-
dead = false;
26-
currentPrice = parseFloat(text);
27-
})
28-
.catch(e => {
29-
dead = true;
30-
});
31-
}
32-
33-
export function startBankWorker() {
34-
setInterval(() => {
35-
updateCurrentPrice().catch(e => {
36-
console.error(e);
37-
});
38-
}, UPDATE_INTERVAL);
39-
}
1+
import fetch from "node-fetch";
2+
var currentPrice = 10;
3+
var dead = false;
4+
5+
const UPDATE_INTERVAL = 1000;
6+
7+
export function isDead() {
8+
return dead;
9+
}
10+
11+
export function getCurrentPrice() {
12+
return currentPrice;
13+
}
14+
15+
export async function updateCurrentPrice() {
16+
return fetch("https://tibyte.net/sandi.txt?r=" + Math.random())
17+
.then((x) => {
18+
if (!x.ok) {
19+
throw Error(x.statusText);
20+
}
21+
return x;
22+
})
23+
.then((x) => x.text())
24+
.then((text) => {
25+
dead = false;
26+
currentPrice = parseFloat(text);
27+
})
28+
.catch((e) => {
29+
dead = true;
30+
});
31+
}
32+
33+
export function startBankWorker() {
34+
setInterval(() => {
35+
updateCurrentPrice().catch((e) => {
36+
console.error(e);
37+
});
38+
}, UPDATE_INTERVAL);
39+
}

src/consts.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
2-
export const DEFAULT_INDEX_AMOUNT = 20.0;
1+
export const DEFAULT_INDEX_AMOUNT = 20.0;

src/db.ts

+60-56
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
1-
import * as fs from 'fs';
2-
import * as util from 'util';
3-
import { v4 as uuidv4 } from 'uuid';
4-
var db = {};
5-
6-
const UPDATE_INTERVAL = 1000;
7-
8-
async function saveDB() {
9-
return util.promisify(fs.writeFile)('data.json', JSON.stringify(db));
10-
}
11-
12-
export function loadDB() {
13-
if (fs.existsSync('data.json')) {
14-
db = JSON.parse(fs.readFileSync('data.json', 'utf8'));
15-
console.log("data.json loaded");
16-
} else {
17-
console.log("data.json not loaded. starting with default database");
18-
}
19-
}
20-
21-
export function startDBWorker() {
22-
setInterval(() => {
23-
saveDB().catch(e => {
24-
console.error(e);
25-
});
26-
}, UPDATE_INTERVAL);
27-
}
28-
29-
export function logTransaction(type, id, amount, currentPrice) {
30-
(async () => {
31-
if (!fs.existsSync('logs')) {
32-
fs.mkdirSync('logs');
33-
}
34-
const data = {
35-
time: Date.now(),
36-
currentPrice: currentPrice,
37-
userId: id,
38-
type: type,
39-
amount: amount
40-
};
41-
return util.promisify(fs.writeFile)(`logs/${Date.now()}-${uuidv4()}.json`, JSON.stringify(data));
42-
})().catch(e => {console.error(e)});
43-
}
44-
45-
export function existsUser(id) {
46-
return id in db;
47-
}
48-
49-
export function getUser(id) {
50-
return db[id];
51-
}
52-
53-
export function setUser(id, user) {
54-
db[id] = user;
55-
}
56-
1+
import * as fs from "fs";
2+
import * as util from "util";
3+
import { v4 as uuidv4 } from "uuid";
4+
var db = {};
5+
6+
const UPDATE_INTERVAL = 1000;
7+
8+
async function saveDB() {
9+
return util.promisify(fs.writeFile)("data.json", JSON.stringify(db));
10+
}
11+
12+
export function loadDB() {
13+
if (fs.existsSync("data.json")) {
14+
db = JSON.parse(fs.readFileSync("data.json", "utf8"));
15+
console.log("data.json loaded");
16+
} else {
17+
console.log("data.json not loaded. starting with default database");
18+
}
19+
}
20+
21+
export function startDBWorker() {
22+
setInterval(() => {
23+
saveDB().catch((e) => {
24+
console.error(e);
25+
});
26+
}, UPDATE_INTERVAL);
27+
}
28+
29+
export function logTransaction(type, id, amount, currentPrice) {
30+
(async () => {
31+
if (!fs.existsSync("logs")) {
32+
fs.mkdirSync("logs");
33+
}
34+
const data = {
35+
time: Date.now(),
36+
currentPrice: currentPrice,
37+
userId: id,
38+
type: type,
39+
amount: amount,
40+
};
41+
return util.promisify(fs.writeFile)(
42+
`logs/${Date.now()}-${uuidv4()}.json`,
43+
JSON.stringify(data)
44+
);
45+
})().catch((e) => {
46+
console.error(e);
47+
});
48+
}
49+
50+
export function existsUser(id) {
51+
return id in db;
52+
}
53+
54+
export function getUser(id) {
55+
return db[id];
56+
}
57+
58+
export function setUser(id, user) {
59+
db[id] = user;
60+
}

0 commit comments

Comments
 (0)