How do I, a non-programmer, write a script to click in sequence..
October 13, 2024 9:01 AM   Subscribe

Hey, remember me and the shark game...I've kept at it on and off... I want to do the lots of everything thing again. This time I want to get up to a high climate level first. To do this, I want to just click through worlds to get to a high climate level. Can I use some sort of auto-clicker or program something simple myself to do this? So you don't need to know the shark game: THe question is, how do I get my computer to read the screen and repetitively click the right thing.

So basically I would need to do the following in a loop. The loop could start anywhere, I suppose.

1. Click the word "Skip"
2. Click "Ok" in the dialogue box that pops up.
3. Click the word "Worlds"
4. Click on one of the worlds with the higher number. This is the tricky part. I don't think I want to just click on a world at random or the same spot every time as that could end up reducing my climate level. I need to keep my climate level going up. So this needs to "read" the numbers somehow and choose a high one.
5. Repeat.

Continue overnight and I wake up and am at a superhigh climate level where I can eat a gazillion fish really quickly. I am climate level 2.4K now, but that's mainly because I don't have the patience to do this by hand so I only occassionally over the years have gone up little by little. I'd like to get up to at least 5K which would basically double my "interest" rate and really push back the point at which things start getting expensive (why, yes, I have plotted out the logorithmic curves of prices and income in excel to figure out how this all works, why do you ask?).

Numbers 1-3 could just be location-based clicking -- like an autoclicker that just clicked in the same spot, but #4 is the tricky part.

My game is in firefox, if that matters. I am in windows.

Credit and blame to Boo Radley, RIP, as always.
posted by If only I had a penguin... to Computers & Internet (11 answers total) 2 users marked this as a favorite
 
I’m not sure this is feasible without some programming— you could get part of the way with Selenium IDE which is a browser automation plugin, but you’ll probably need to write some code to find the “increase world level” button. I’ve never used Selenium IDE but I have written Appium code (which uses Selenium under the hood) so I’ll see if I can find a way to do it.

If not, perhaps I can use standard Selenium— I don’t think it will be too hard for you to set it up and it’s probably worth doing if it works.

(Unless someone else chimes in with a simpler/better way).
posted by cape at 9:51 AM on October 13


Someone's almost definitely going to have to type some code. While Selenium is probably the "correct" route in some sense, I would lean towards or AutoIt as the platform for that, since it's what I used (I think) to automate some tasks in Squid Ink. It's got a WebDriver which is probably designed for this kind of task (though I haven't used it), and I seem to remember it can record mouse/keyboard macros too.

AutoHotkey is another thing in the same vein.

Whatever you do to get the world numbers into your code is probably going to come down to one of these options:
* Getting the numbers directly from the webpage's HTML/JS code
* Getting the numbers via the Windows screenreader/accessibility system, which hopefully the shark game doesn't break.
* (Last, most horrific resort) Automatically taking screenshots, cropping to the region where the world numbers appear, and feeding them through OCR.
posted by polytope subirb enby-of-piano-dice at 10:39 AM on October 13


MS' VS Code with PlayWright plugin to record where to click and give you the script.

Follow a W3Schools Javascript guide to create the loop that the sequence of instructions must go round and round.

Conceptually, you need the framing that your own clicking goes through the sequence you describe, and PlayWright will create the steps you follow while the Javascript tutorial will show how to restart that sequence each time it's fine through the steps. You may have an ongoing challenge to keep the buttons you want it to click the ones it actually clicks, but that's the game here. Good luck!
posted by k3ninho at 1:50 PM on October 13


I’m not seeing any skip button. I checked the “dev” and “remaster” versions as well. Are there prerequisite conditions in the game before it displays?
posted by cape at 2:31 PM on October 13


Response by poster: I’m not seeing any skip button. I checked the “dev” and “remaster” versions as well. Are there prerequisite conditions in the game before it displays?

Cape, sorry, I've been off having thanksgiving...anyway, I think it shows up after you successfully get through the first ocean. I have another super-low-level instance of the game running on chrome that i started just to check what the initial math conditions were. You can load it using this code and then you can see what the layout looks like:

[Note: I attempted to paste the code here but the special characters would not paste. If anyone else is interested in a code to load in an in-progress just barely game, feel free to memail me. I memailed the code.]

Go to options then import then paste in that code. It will have the skip option at the top. It says save/options/skip. That is the original game. I am playing my "real" game in "remaster." but the layout is the same.

All, I definitely understood that there would have to be a line or two of code involved. What i was hopeing for was basically an autoclicker meets excel type solution where I could mostly autoclick by location and then then be like ok...see these three locations? They each have a number. Of the ones with the highest number, pick one randomly. And yes, it would be a couple of lines of code, but closer to writing in an excel formula (cell reference, if function, action if true). Repeat 3000 times.
posted by If only I had a penguin... at 8:06 PM on October 13


The people above suggesting playwright or selenium aren't wrong, that is what those tools are for, they just may be a bit of overkill if really all you want to do is click 4 things repeatedly. The game has jQuery (a javascript library) bundled into it already, which would allow you to do something like this in the console from the developer tools (I explained that in one of your earlier questions):
//define the autoclicker routine
window.autoClicker = function() {
  //selects the catch-a-fish button and clicks it once; if the button isn't visible does nothing
  //because the first part before the . resolves to an empty collection, and clicking on nothing
  //helpfully does nothing
  $("#catchFish").click();
}

//define a function to stop a previously started autoclicker
window.stopAutoClicker = function() {
  //clear the previous set interval if defined
  if (window.intervalHandle) clearInterval(window.intervalHandle);
}

//define a function to start the autoclicker
window.startAutoClicker = function() {
  //every 100ms this will call window.autoClicker()
  window.intervalHandle = setInterval(window.autoClicker, 100); 
}

//ok start!
window.startAutoClicker();
OK so what is this doing? Well this is a very simple autoclicker that just presses the "catch a fish" button every 100ms. Probably 10 fish per second isn't going to do what you want, but I'm not sure what that is exactly since I don't play the game so I don't see it in the state you do. But you can learn about what all jQuery lets you do in their documentation site. How did I find out the name of the catch-a-fish button? I right clicked it and chose "inspect" from the developer tools menu (I'm in a chromium browser but there's the same tool on FF, though it might be arranged differently). It highlighted this:

<button id="catchFish" style="display: block; opacity 1;">[The stuff inside the button]</button>

That part that says id="catchFish" is the part that tells me how to select the button in question (the # in front of it means to select by id; if i instead had written say $("button") that would mean every button on the page, which means I can click them all in one terse line but probably that's not a good idea). Now, there's a lot of things you can do with jQuery that are more advanced, but simply "find a button and click it" isn't that hard (without comments the above is 10 lines of code). You may need to adjust the timing etc. (start with maybe 10000ms while you figure things out) but this is enough to get you started. When you want to stop the autoClicker you just type this in the console:

window.stopAutoClicker()

To start again:

window.startAutoClicker()

A word of warning, always save your game state before you mess around with anything. And it seems that like other clicker games there's a "buy N" and "buy max" type of control, which means your autoclicker has to account for that lest you overbuy something. Also this approach is kind of dependent on knowing/being able to teach yourself (1) very basically how css selectors work, though the simple #id based selector is straightforward and may get you there, (2) the basics of JS (3) how to recognize/interpret the HTML DOM so you can understand the controls you want to manipulate. This is way more of a topic than I can put in a comment but the web has all kind of learning materials you can avail yourself of if you are motivated.
posted by axiom at 10:04 PM on October 13


Response by poster: Thanks axiom...but I dont want to click catch a fish. I would know how to do that. And as you can see in my history ive actuallly broken the game doing a more complicated version of that in the past.

The hard part is I want it to find three buttons, read the content of the buttons and then choose the button with the highest number in it. Since most of the time there will be none than one button with the highest number it should probably select randomly from those.

Unless it can "see" the actual number in which case select the highest. (I.e the numbers might both be displayed as 2.4k but they are rounded so one might actually be 2398 and the other is 2403. If the clicking script can see that then obv choose 2403. There are ways for me to figure that out but ot involves way more clicking. So when to selecting manually I just pick one of the highest displayed-numbers.
posted by If only I had a penguin... at 4:57 AM on October 14


Response by poster: Hey Axiom I just realized you gave me code to try to fix it when I broke it before... I can't remember if that worked. I don't think it did. I think I had to revert to a saved state, but thanks for trying.
posted by If only I had a penguin... at 10:37 AM on October 14


If it's the only number on the button you can pretty easily find it using a regular expression (which is the thing inside /.../):
let match = /([0-9.]+)/.exec($("#getShark").text());
if (match.length > 0) console.log("The shark cost is currently this many fish: "+match[1]);
OK so now we can, say, find the biggest of 3 buttons' values and click the corresponding button (there are fancier ways to get the same effect but I'm going for simplicity here):
let matchA = /([0-9.]+)/.exec($("#buttonA").text());
let matchB = /([0-9.]+)/.exec($("#buttonB").text());
let matchC = /([0-9.]+)/.exec($("#buttonC").text());
let valA = 0, valB = 0, valC = 0;
if (matchA.length > 0) valA = parseFloat(matchA[1]); //[1] means just the number within the match from the first set of parentheses in the regular expression, i.e., some digits with possibly a decimal
if (matchB.length > 0) valB = parseFloat(matchB[1]);
if (matchC.length > 0) valC = parseFloat(matchC[1]);

//find the biggest value, click the corresponding button
if (valA > valB) {
  if (valA > valC) {
    $("#buttonA").click();
  } else {
    $("#buttonC").click();
  }
} else {
  if (valB > valC) {
    $("#buttonB").click();
  } else {
    $("#buttonC").click();
  }
}
Here if there's a tie it favors C then B then A. We're also assuming all 3 numbers are written the same way; if there's something like "Foo 500" vs "Bar 1.5k" then Foo will win because 500 > 1.5.

I'd not be surprised if the thing I did before to try to fix your game didn't work, it relied on treating every number in the game state the same way and that's probably not valid, but really there's not much that can be done to fix that kind of problem (short of forking the game's github repositiory and replacing all its number handling code with something else that doesn't overflow like that).
posted by axiom at 3:28 PM on October 14


Best answer: axiom's suggestion about the developer console got me thinking about the source code. I don't know JS at all but the code is simple enough that finding the commands to manipulate game state is not too difficult. If you just want to skip all the way to level 5000 or so, you can open the dev tools console and run these two commands which will drop you right into the gateway with three planets to choose at ~5k climate level.

w.planetLevel = 5000;
m.endGame();

(w and m are variables assigned to SharkGame.World and SharkGame.Main)

Any time you modify state you may introduce bugs / unexpected behaviour but based on my read of the source these should be ok. Technically you may be supposed to set SharkGame.wonGame = false; before calling endGame(); (the skipLevel function does) but it looks like it is only checked in order to decide whether to award you "essence", and it doesn't get awarded when I test so it must be false/null by default.

I think the number display formatting is handled by beautify and that's why it can be difficult to tell the precise value, e.g. 5001 rounded to "5.00K". I didn't track it down but saw some beautify references in the code and making an assumption based on context basically.

There are a few solutions here but you will need to modify the source code. This isn't too hard in some ways because the Developer Tools allows you to set override files. The process is: open dev tools (view->developer->developer tools), click 'Sources', click the 'js' folder to expand it, right-click whatever file you want to modify e.g. gateway.js. Then 'select folder' to choose local storage location, then make your edits either here or in another editor, save, and reload. I found using Chromium I had to always choose 'save as' and overwrite the existing local copy, didn't find a simple 'save' option there.

gateway.js creates three worlds (defined by NUM_PLANETS_TO_SHOW) and puts them into an array called planetPool. The math to set the climate level is just one line, and one small change would mean it always creates worlds with climate level higher than the current world. So you wouldn't need to know the precise number in order to be sure you're always progressing to higher climate levels. This is the line:
const newLevel = Math.floor(Math.max(w.planetLevel + (Math.random() * 10 - 4), 1));

You could remove the -4 so that it will never subtract and create a lower level, and/or replace the trailing 1 with 'w.planetLevel', which ensures the minimum world level is the current level (by passing that as the second argument to Math.max, if the randomized world level is lower than that value, it will use it instead-- basically max means "take the larger of these" and it's 1 in this code to prevent the generation of planets with negative climate levels). Or just set it to w.planetLevel + 100 if you want to go up 100 levels each time-- the level growth doesn't seem to accelerate with time, it's always going to be -4 to 6 levels, and I'm going to assume when you reach 5000+ climate level these increases are too small to be meaningful so maybe setting to +100 or more every round is best. The randomness you get in the first few rounds seems like it won't matter in extremely high climate levels:
const newLevel = w.planetLevel + 100;

(I didn't actually test modifying this, just based on my reading of the source and assumptions.)

Alternatively, find where the amounts are being set to "x.xxK" and modify that, either in the gateway or globally, in the CSS, etc. so that you can see the precise value.

Note: I found modify/override of JS using FireFox to be nonworking and found a few posts about it on Mozilla forums/bugtracker. So I installed Chrome and it worked fine. I used ungoogled-chromium installed via homebrew on macOS.
posted by cape at 12:28 PM on October 15


Response by poster: I did it in firefox. Worked just fine. I did say the game won was false.
posted by If only I had a penguin... at 1:02 PM on October 15


« Older Samsung Dryer heating element keeps failing...does...   |   Down ballot Newer »

You are not logged in, either login or create an account to post comments