-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
126 lines (109 loc) · 3.72 KB
/
lib.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Dependencies
const fetch = require('node-fetch');
const fs = require("fs");
// Constants
const settings = { method: "Get" };
const MAPPREFIXES = ["arena_", "cp_", "ctf_", "koth_", "mvm_", "pass_", "pd_", "pl_", "plr_", "rd_", "sd_", "tc_", "tr_"];
//--- GETTING JSON STATS ---//
// Used to check if JSON is not an HTML response and parses it
async function safeParseJSON(response, res) {
const body = await response.text();
try {
return JSON.parse(body);
} catch (err) {
console.error("Error Parsing JSON:", err);
console.error("Response body:", body);
res.render('private_profile');
}
}
// Fetches player stats JSON from the Steam API
function fetchJson(id, req, resp, pfp, name) {
var url = `http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v2/?key=${process.env.API_KEY}&appid=440&steamid=${id}&count=1&format=json`;
try {
fetch(url, settings).catch((err) => {
resp.render('profile_not_found');
console.log("Error fetching JSON: " + err)
return;
})
.then(res => safeParseJSON(res, resp))
.then((json) => {
if (json != null) {
let allStats = formatStats(jsonToDict(json, resp));
renderStats(resp, allStats, pfp, name);
}
else {
resp.render('profile_not_found');
}
})
} catch (err) {
console.log("Error fetching JSON: " + err);
}
}
// Converts JSON from fetchJson() into a javascript dictionary
function jsonToDict(jsonStats, res) {
var stats = jsonStats.playerstats.stats;
var achivmentStats = jsonStats.playerstats.achivments;
let dictStats = {}
if (stats == null) {
res.render('profile_not_found');
return;
} else {
for (let i = 0; i < stats.length; i++) {
dictStats[stats[i]['name']] = stats[i]['value'];
}
return dictStats;
}
}
// Formats necessary stats from jsonToDict() and puts them into seperate ditionaries if needed
function formatStats(statsDict) {
let playtimeStats = {}
let mapPlaytimeStats = {}
for (var key in statsDict) {
// Class, map, and MvM playtime stats
if (key.endsWith(".accum.iPlayTime") && !MAPPREFIXES.some(substring => key.includes(substring))) {
// Convert to hours
statsDict[key] = (statsDict[key] / 3600).toFixed(2);
if (!key.endsWith(".mvm.accum.iPlayTime")) {
// Class playtime stats
playtimeStats[key.replace(".accum.iPlayTime", "")] = statsDict[key];
} else if (MAPPREFIXES.some(substring => key.includes(substring))) {
// Map playtime stats
mapPlaytimeStats[key.replace(".accum.iPlayTime", "")] = statsDict[key];
}
}
}
return statsDict;
}
// Render and send the stats to the ejs page
function renderStats(res, stats, pfp, name) {
res.render('index', {
playerStats: stats,
profilePicture: pfp,
nickName: name
});
}
//--- FILES READING, WRITING, CREATION ---//
function createFile(filename) {
if (!fs.existsSync(filename)) {
fs.closeSync(fs.openSync(filename, 'w'));
}
}
function readFile(filename) {
try {
const data = fs.readFileSync(filename, 'utf8')
return data;
} catch (err) {
console.error(err);
return 0;
}
}
function writeFile(filename, content) {
console.log(`Writing content ${content} to ${filename}`);
fs.writeFile(filename, content, err => {
if (err) {
console.error(err);
return;
}
});
}
module.exports = { fetchJson, createFile, readFile, writeFile };