This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
79 lines (70 loc) · 2.1 KB
/
api.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
const path = require('path');
const config = require(path.join(process.cwd(), 'config.js'));
// const config = require('./data/config2.js');
const low = require('lowdb');
const _ = require('lodash');
const { default: SlippiGame } = require('@slippi/slippi-js');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
const { copyFile } = require('fs');
const { niceData } = require('./timestamp');
// Set some defaults (required if your JSON file is empty)
db.defaults({ timestamps: [], user: {}, count: 0 }).write();
function getAllTimestampsArr(ensure_metadata = false) {
let all_ts = db.get('timestamps').value();
if (!ensure_metadata) {
return all_ts;
}
}
function getMetaStats(timestamp) {
try {
const game = new SlippiGame(timestamp.path);
return [ game.getMetadata(), game.getStats() ];
} catch (error) {
console.log(error.message);
console.log("can't get metadata on ", timestamp.path);
return [ {}, {} ];
}
}
function pushTimestamp(ts) {
db.get('timestamps').push(ts).write();
}
function getTimestampById(id) {
return db.get('timestamps').find({ uid: id }).value();
}
function getRecentTimestamp() {
let tsarr = getAllTimestampsArr();
return tsarr[tsarr.length - 1];
}
function updateTimestamp(ts, ensure_metadata = false) {
if (ensure_metadata) {
const metastat = getMetaStats(ts);
if (!_.isEmpty(metastat[0])) {
_.set(ts, 'meta.metadata', metastat[0]);
}
// _.set(ts, 'meta.nice', niceData(ts)); // hard to import niceData
// _.set(ts, 'meta.stats', metastat[1]); //don't really care about stats, actually
}
db
.get('timestamps')
.find({ uid: ts.uid }) // Lodash shorthand syntax
.assign(ts)
.write();
}
function deleteTimestamp(ts) {
db
.get('timestamps')
.remove({ uid: ts.uid }) // Lodash shorthand syntax
.write();
}
// console.log(getTimestampById('0.4466991633600961'));
// console.log(getAllTimestampsArr());
module.exports = {
getAllTimestampsArr,
getTimestampById,
pushTimestamp,
updateTimestamp,
deleteTimestamp,
getRecentTimestamp
};