-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 893ef70
Showing
8 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
3
user/mods/Shardbyte-LiveBitcoinPricesREDUX/config/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"nextUpdate": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |