Skip to content

Commit 893ef70

Browse files
committed
Initial Commit
0 parents  commit 893ef70

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Shardbyte
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# LiveBitcoinPricesREDUX
2+
3+
**Dynamically update the price of bitcoin to match it's current value in live.**
4+
5+
- This mod has been updated to support SPT versions 3.9.0-3.9.x
6+
- A few small language fixes have been applied.
7+
8+
This is a pretty straight-forward mod to dynamically update the price of bitcoin to match the current price in live.
9+
10+
Data for the current bitcoin price is fetched from [Tarkov DEV](tarkov.dev)
11+
12+
The original version of this mod can be found [here](https://hub.sp-tarkov.com/files/file/1767-live-bitcoin-prices).
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Shardbyte
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# LiveBitcoinPricesREDUX
2+
3+
**Dynamically update the price of bitcoin to match it's current value in live.**
4+
5+
- This mod has been updated to support SPT versions 3.9.0-3.9.x
6+
- A few small language fixes have been applied.
7+
8+
This is a pretty straight-forward mod to dynamically update the price of bitcoin to match the current price in live.
9+
10+
Data for the current bitcoin price is fetched from [Tarkov DEV](tarkov.dev)
11+
12+
The original version of this mod can be found [here](https://hub.sp-tarkov.com/files/file/1767-live-bitcoin-prices).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"nextUpdate": 0
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "LiveBitcoinPricesREDUX",
3+
"version": "1.0.0",
4+
"sptVersion": "~3.9",
5+
"loadBefore": [],
6+
"loadAfter": [],
7+
"incompatibilities": [],
8+
"isBundleMod": false,
9+
"main": "src/mod.js",
10+
"scripts": {
11+
"setup": "npm i",
12+
"build": "node ./build.mjs",
13+
"buildinfo": "node ./build.mjs --verbose"
14+
},
15+
"devDependencies": {
16+
"@types/node": "20.11",
17+
"@typescript-eslint/eslint-plugin": "7.2",
18+
"@typescript-eslint/parser": "7.2",
19+
"archiver": "^6.0",
20+
"eslint": "8.57",
21+
"fs-extra": "11.2",
22+
"ignore": "^5.2",
23+
"tsyringe": "4.8.0",
24+
"typescript": "5.4",
25+
"winston": "3.12"
26+
},
27+
"author": "Shardbyte",
28+
"contributors": [ "Chomp", "Square" ],
29+
"license": "MIT"
30+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// LiveBitcoinPricesREDUX (LBPR)
2+
// Author: Shardbyte
3+
import { DependencyContainer } from "tsyringe";
4+
import { IPostDBLoadModAsync } from "@spt/models/external/IPostDBLoadModAsync";
5+
import { DatabaseServer } from "@spt/servers/DatabaseServer";
6+
import { ILogger } from "@spt/models/spt/utils/ILogger";
7+
import { LogTextColor } from "@spt/models/spt/logging/LogTextColor";
8+
import { request } from "https";
9+
import * as fs from "node:fs";
10+
import * as path from "node:path";
11+
12+
class LBPR implements IPostDBLoadModAsync {
13+
private static bitcoin: any
14+
private static logger: ILogger
15+
private static config: Config;
16+
private static therapistCoef: number;
17+
private static configPath = path.resolve(__dirname, "../config/config.json");
18+
private static pricePath = path.resolve(__dirname, "../config/price.json");
19+
20+
public async postDBLoadAsync(container: DependencyContainer): Promise<void> {
21+
LBPR.logger = container.resolve<ILogger>("WinstonLogger");
22+
LBPR.config = JSON.parse(fs.readFileSync(LBPR.configPath, "utf-8"));
23+
const db = container.resolve<DatabaseServer>("DatabaseServer");
24+
25+
const tables = db.getTables();
26+
const handbook = tables.templates.handbook;
27+
LBPR.therapistCoef = (100 - tables.traders["54cb57776803fa99248b456e"].base.loyaltyLevels[0].buy_price_coef) / 100;
28+
LBPR.bitcoin = handbook.Items.find(x => x.Id == "59faff1d86f7746c51718c9c");
29+
30+
// Update price on startup
31+
const currentTime = Math.floor(Date.now() / 1000);
32+
if (!await LBPR.getPrice(currentTime > LBPR.config.nextUpdate)) {
33+
return;
34+
}
35+
36+
// Get new price every hour
37+
setInterval(LBPR.getPrice, (60 * 60 * 1000));
38+
39+
return;
40+
}
41+
42+
static async getPrice(fetchPrices = true): Promise<boolean> {
43+
return new Promise((resolve, reject) => {
44+
if (!fetchPrices) {
45+
// Load last saved price
46+
const lastValue = JSON.parse(fs.readFileSync(LBPR.pricePath, "utf-8"))[`${LBPR.bitcoin.Id}`];
47+
if (lastValue === undefined) {
48+
LBPR.logger.logWithColor(`No last price saved, keeping bitcoin price at: ${LBPR.bitcoin.Price}`, LogTextColor.MAGENTA);
49+
} else {
50+
LBPR.bitcoin.Price = lastValue;
51+
LBPR.logger.logWithColor(`Updated bitcoin to ${LBPR.bitcoin.Price} from price path`, LogTextColor.MAGENTA);
52+
}
53+
resolve(true);
54+
} else {
55+
const req = request(
56+
"https://api.tarkov.dev/graphql",
57+
{
58+
method: "POST"
59+
},
60+
(res) => {
61+
res.setEncoding("utf8");
62+
let rawData = "";
63+
res.on("data", (chunk) => { rawData += chunk; });
64+
res.on("end", () => {
65+
try {
66+
const parsedData = JSON.parse(rawData);
67+
const price = parsedData.data.item.sellFor.find((x) => x.vendor.name === "Therapist").priceRUB
68+
const inRub = price / LBPR.therapistCoef;
69+
LBPR.bitcoin.Price = inRub;
70+
71+
// Store the prices to disk for next time
72+
const jsonString: string = `{"${LBPR.bitcoin.Id}": ${LBPR.bitcoin.Price}}`
73+
fs.writeFileSync(LBPR.pricePath, JSON.stringify(JSON.parse(jsonString)));
74+
75+
// Update config file with the next update time
76+
LBPR.config.nextUpdate = Math.floor(Date.now() / 1000) + 3600;
77+
fs.writeFileSync(LBPR.configPath, JSON.stringify(LBPR.config, null, 4));
78+
LBPR.logger.logWithColor("Updated bitcoin to match live price", LogTextColor.MAGENTA);
79+
resolve(true);
80+
} catch (e) {
81+
console.error(e.message);
82+
resolve(false);
83+
}
84+
});
85+
});
86+
87+
req.on("error", (e) => {
88+
console.error(e.message);
89+
reject(e);
90+
})
91+
92+
req.write(JSON.stringify({
93+
query: `{
94+
item(id: "59faff1d86f7746c51718c9c")
95+
{
96+
sellFor {
97+
priceRUB
98+
vendor {
99+
name
100+
}
101+
}
102+
}
103+
}`
104+
}));
105+
req.end();
106+
}
107+
})
108+
}
109+
110+
}
111+
112+
interface Config {
113+
nextUpdate: number,
114+
}
115+
116+
export const mod = new LBPR();

0 commit comments

Comments
 (0)