-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
79 lines (71 loc) · 2.54 KB
/
preload.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 { contextBridge } = require('electron');
const { login, createUser } = require('./backend/userApi');
const {
getIndCompanies,
getUsCompanies,
getCompanyIndexes,
currentStockPrice,
} = require('./backend/queryCompanies');
const { executeOrder } = require('./backend/orders');
const {
transactionsOnComp,
allTransactions,
} = require('./backend/portfolioQueries');
const { fundTransaction, fundTransactionHistory } = require('./backend/funds');
const fs = require('fs');
const initSqlJs = require('sql.js');
// when every time the app loads calculate new prices for the stocks.
const changePercentFunc = (min, max) => {
let num = Math.random() * (max - min) + min;
num = Math.floor(num * 100) / 100;
if (Math.round(Math.random()) == 1) {
num -= num * 2;
}
return (num / 100).toFixed(4);
};
const marketSimulation = () => {
initSqlJs().then(function (SQL) {
//! read the database to the JS memory
const filebuffer = fs.readFileSync('./backend/database.sqlite'); // location relative to the root file where the application runs
const db = new SQL.Database(filebuffer);
var comp = db.prepare('SELECT * FROM COMPANY_INDEXES;');
var count = 1;
while (comp.step()) {
var row = comp.getAsObject();
var changePercent = changePercentFunc(0, 1);
var price_yesterday = row.price_today;
var changeAmount = (row.price_today * Number(changePercent)).toFixed(2);
var price_today = Number(
(row.price_today + Number(changeAmount)).toFixed(2)
);
db.run(
'UPDATE COMPANY_INDEXES SET price_yesterday = $price_yesterday, price_today = $price_today,change_percentage = $change_percentage WHERE sl_no = $sl_no',
{
$price_yesterday: price_yesterday,
$price_today: price_today,
$change_percentage: Number((changePercent * 100).toFixed(2)),
$sl_no: row.sl_no,
}
);
count++;
}
//! write the database file back to storage from JS memory
var data = db.export();
var buffer = new Buffer.from(data);
fs.writeFileSync('./backend/database.sqlite', buffer);
});
};
marketSimulation();
contextBridge.exposeInMainWorld('backend', {
login: login,
createUser: createUser,
getIndCompanies: getIndCompanies,
getUsCompanies: getUsCompanies,
getCompanyIndexes: getCompanyIndexes,
executeOrder: executeOrder,
transactionsOnComp: transactionsOnComp,
fundTransaction: fundTransaction,
fundTransactionHistory: fundTransactionHistory,
allTransactions: allTransactions,
currentStockPrice: currentStockPrice,
});