Skip to content

Commit

Permalink
Fix 2 major sources of casino.js slowness
Browse files Browse the repository at this point in the history
1. We forgot our mantra to create no temp files, so we were going through expensive cleanup / kill-all-scripts logic each time.
2. We had no quick-detection of a game that was not yet over - we were waiting for all win/lose/tie detections to time out.
  • Loading branch information
alainbryden committed Oct 22, 2024
1 parent 3e03414 commit 83d7713
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions casino.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ export async function main(ns) {
}
let inputWager, btnStartGame;

// Note, we deliberately DO NOT pre-emptively check our active source files, or do any kind of RAM dodging
// const unlockedSFs = await getActiveSourceFiles(ns, true); // See if we have SF4 to travel automatically
// Why? Because this creates "Temp files", and we want to keep the safe file as small as possible for fast saves and reloads.
// We use an empty temp folder as a sign that we previously ran and killed all scripts and can safely proceed.

// Step 2: Try to navigate to the blackjack game (with retries in case of transient errors)
const unlockedSFs = await getActiveSourceFiles(ns, true); // See if we have SF4 to travel automatically
let priorAttempts = 0;
while (true) {
if (priorAttempts > 0)
Expand All @@ -137,15 +141,11 @@ export async function main(ns) {
if (ns.getPlayer().money < 200000)
throw new Error("Sorry, you need at least 200k to travel to the casino.");
let travelled = false;
if (4 in unlockedSFs) { // With SF4, we can travel automatically
try {
travelled = await getNsDataThroughFile(ns, 'ns.singularity.travelToCity(ns.args[0])', null, ["Aevum"]);
} catch { }
if (!travelled)
log(ns, "WARN: Failed to travel to Aevum automatically (perhaps RAM / SF4 level is too low?). " +
"We will try to go there manually for now.", true, 'warning');
} else
log(ns, `INFO: We must "manually" travel to Aevum since we don't have SF4`, true);
try {
travelled = await getNsDataThroughFile(ns, 'ns.singularity.travelToCity(ns.args[0])', null, ["Aevum"]);
} catch { }
if (!travelled) // Note: While it would be nice to confirm whether we have SF4 for the message, it's not worth the cost.
log(ns, "INFO: Canot use singularity travel to Aevum via singularity. We will try to go there manually for now.", true);
// If automatic travel failed or couldn't be attempted, try clicking our way there!
if (!travelled) {
await click(ns, await findRequiredElement(ns, "//div[@role='button' and ./div/p/text()='Travel']"));
Expand All @@ -165,13 +165,12 @@ export async function main(ns) {
await click(ns, await findRequiredElement(ns, "//span[@aria-label = 'Iker Molina Casino']"));
} catch (err) { // Try to use SF4 as a fallback (if available) - it's more reliable.
let success = false, err2;
try { success = 4 in unlockedSFs ? await getNsDataThroughFile(ns, 'ns.singularity.goToLocation(ns.args[0])', null, ["Iker Molina Casino"]) : false; }
try { success = await getNsDataThroughFile(ns, 'ns.singularity.goToLocation(ns.args[0])', null, ["Iker Molina Casino"]); }
catch (singErr) { err2 = singErr; }
if (!success)
throw new Error("Failed to travel to the casino both using UI navigation and using SF4 as a fall-back." +
`\nUI navigation error was: ${getErrorInfo(err)}\n` + (err2 ? `Singularity error was: ${getErrorInfo(err2)}` :
(4 in unlockedSFs) ? '`ns.singularity.goToLocation("Iker Molina Casino")` returned false, but no error...' :
"And we don't have SF4 so couldn't travel with singularity."));
'`ns.singularity.goToLocation("Iker Molina Casino")` returned false, but no error...'));
}

// Step 2.4: Try to start the blackjack game
Expand Down Expand Up @@ -353,6 +352,8 @@ async function getWinLoseOrTie(ns) {
// 1+2+3+4+5=15 total retries, but all with small ms wait times (<20ms), so should still only take a second
let retries = 0;
while (retries++ < 5) {
if (await tryfindElement(ns, "//button[text() = 'Hit']", retries))
return null; // Game is not over yet, we can still hit
if (await tryfindElement(ns, "//p[contains(text(), 'lost')]", retries))
return "lose";
// Annoyingly, when we win with blackjack, "Won" is Title-Case, but normal wins is just "won".
Expand Down

0 comments on commit 83d7713

Please sign in to comment.