Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shardbyte committed Jul 26, 2024
0 parents commit 893ef70
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Shardbyte

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# LiveBitcoinPricesREDUX

**Dynamically update the price of bitcoin to match it's current value in live.**

- This mod has been updated to support SPT versions 3.9.0-3.9.x
- A few small language fixes have been applied.

This is a pretty straight-forward mod to dynamically update the price of bitcoin to match the current price in live.

Data for the current bitcoin price is fetched from [Tarkov DEV](tarkov.dev)

The original version of this mod can be found [here](https://hub.sp-tarkov.com/files/file/1767-live-bitcoin-prices).
21 changes: 21 additions & 0 deletions user/mods/Shardbyte-LiveBitcoinPricesREDUX/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Shardbyte

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions user/mods/Shardbyte-LiveBitcoinPricesREDUX/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# LiveBitcoinPricesREDUX

**Dynamically update the price of bitcoin to match it's current value in live.**

- This mod has been updated to support SPT versions 3.9.0-3.9.x
- A few small language fixes have been applied.

This is a pretty straight-forward mod to dynamically update the price of bitcoin to match the current price in live.

Data for the current bitcoin price is fetched from [Tarkov DEV](tarkov.dev)

The original version of this mod can be found [here](https://hub.sp-tarkov.com/files/file/1767-live-bitcoin-prices).
3 changes: 3 additions & 0 deletions user/mods/Shardbyte-LiveBitcoinPricesREDUX/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nextUpdate": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
30 changes: 30 additions & 0 deletions user/mods/Shardbyte-LiveBitcoinPricesREDUX/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "LiveBitcoinPricesREDUX",
"version": "1.0.0",
"sptVersion": "~3.9",
"loadBefore": [],
"loadAfter": [],
"incompatibilities": [],
"isBundleMod": false,
"main": "src/mod.js",
"scripts": {
"setup": "npm i",
"build": "node ./build.mjs",
"buildinfo": "node ./build.mjs --verbose"
},
"devDependencies": {
"@types/node": "20.11",
"@typescript-eslint/eslint-plugin": "7.2",
"@typescript-eslint/parser": "7.2",
"archiver": "^6.0",
"eslint": "8.57",
"fs-extra": "11.2",
"ignore": "^5.2",
"tsyringe": "4.8.0",
"typescript": "5.4",
"winston": "3.12"
},
"author": "Shardbyte",
"contributors": [ "Chomp", "Square" ],
"license": "MIT"
}
116 changes: 116 additions & 0 deletions user/mods/Shardbyte-LiveBitcoinPricesREDUX/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// LiveBitcoinPricesREDUX (LBPR)
// Author: Shardbyte
import { DependencyContainer } from "tsyringe";
import { IPostDBLoadModAsync } from "@spt/models/external/IPostDBLoadModAsync";
import { DatabaseServer } from "@spt/servers/DatabaseServer";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { LogTextColor } from "@spt/models/spt/logging/LogTextColor";
import { request } from "https";
import * as fs from "node:fs";
import * as path from "node:path";

class LBPR implements IPostDBLoadModAsync {
private static bitcoin: any
private static logger: ILogger
private static config: Config;
private static therapistCoef: number;
private static configPath = path.resolve(__dirname, "../config/config.json");
private static pricePath = path.resolve(__dirname, "../config/price.json");

public async postDBLoadAsync(container: DependencyContainer): Promise<void> {
LBPR.logger = container.resolve<ILogger>("WinstonLogger");
LBPR.config = JSON.parse(fs.readFileSync(LBPR.configPath, "utf-8"));
const db = container.resolve<DatabaseServer>("DatabaseServer");

const tables = db.getTables();
const handbook = tables.templates.handbook;
LBPR.therapistCoef = (100 - tables.traders["54cb57776803fa99248b456e"].base.loyaltyLevels[0].buy_price_coef) / 100;
LBPR.bitcoin = handbook.Items.find(x => x.Id == "59faff1d86f7746c51718c9c");

// Update price on startup
const currentTime = Math.floor(Date.now() / 1000);
if (!await LBPR.getPrice(currentTime > LBPR.config.nextUpdate)) {
return;
}

// Get new price every hour
setInterval(LBPR.getPrice, (60 * 60 * 1000));

return;
}

static async getPrice(fetchPrices = true): Promise<boolean> {
return new Promise((resolve, reject) => {
if (!fetchPrices) {
// Load last saved price
const lastValue = JSON.parse(fs.readFileSync(LBPR.pricePath, "utf-8"))[`${LBPR.bitcoin.Id}`];
if (lastValue === undefined) {
LBPR.logger.logWithColor(`No last price saved, keeping bitcoin price at: ${LBPR.bitcoin.Price}`, LogTextColor.MAGENTA);
} else {
LBPR.bitcoin.Price = lastValue;
LBPR.logger.logWithColor(`Updated bitcoin to ${LBPR.bitcoin.Price} from price path`, LogTextColor.MAGENTA);
}
resolve(true);
} else {
const req = request(
"https://api.tarkov.dev/graphql",
{
method: "POST"
},
(res) => {
res.setEncoding("utf8");
let rawData = "";
res.on("data", (chunk) => { rawData += chunk; });
res.on("end", () => {
try {
const parsedData = JSON.parse(rawData);
const price = parsedData.data.item.sellFor.find((x) => x.vendor.name === "Therapist").priceRUB
const inRub = price / LBPR.therapistCoef;
LBPR.bitcoin.Price = inRub;

// Store the prices to disk for next time
const jsonString: string = `{"${LBPR.bitcoin.Id}": ${LBPR.bitcoin.Price}}`
fs.writeFileSync(LBPR.pricePath, JSON.stringify(JSON.parse(jsonString)));

// Update config file with the next update time
LBPR.config.nextUpdate = Math.floor(Date.now() / 1000) + 3600;
fs.writeFileSync(LBPR.configPath, JSON.stringify(LBPR.config, null, 4));
LBPR.logger.logWithColor("Updated bitcoin to match live price", LogTextColor.MAGENTA);
resolve(true);
} catch (e) {
console.error(e.message);
resolve(false);
}
});
});

req.on("error", (e) => {
console.error(e.message);
reject(e);
})

req.write(JSON.stringify({
query: `{
item(id: "59faff1d86f7746c51718c9c")
{
sellFor {
priceRUB
vendor {
name
}
}
}
}`
}));
req.end();
}
})
}

}

interface Config {
nextUpdate: number,
}

export const mod = new LBPR();

0 comments on commit 893ef70

Please sign in to comment.