0
\$\begingroup\$

So, I have a project, its a "cookie clicker" by definition, and I wish to work out how I can protect the game from users being able to edit <button id="cooker" onclick="cookieClick(1)"></button> and setting (1) to like (123129).

I tried a few things first: JQuery: I tried using an old on() function with a dom searching function, and then to run an alert saying "Dumb Hacker! Im corrupting your save data" and then of course it would run Function clearDat() then function corrupt() which clears the localstorage data and sets it to an invalid value (usually wounds up as NaN) which prevents them from Playing.

Unfortunately the cookieclicker was editing the program so hastily, that it was "changing" the button above, and I wound up getting a million alerts (And because I was using a windows based HTML editor, I was getting WINDOW alerts, which i had to spam esc and try to delete the alert function and rerun the whole game, it was a pain)

I also tried a similar method using an old JS function, but it wound up not working.

I think you may see where im getting at, I'm really stuck in a pickle.

Heres the current code: https://hatebin.com/tgsdqzhzlc

\$\endgroup\$
4
  • 1
    \$\begingroup\$ No client-side anticheat will defeat a determined hacker. Your time and energy is likely better-spent validating game logic on your server, where you know users can't modify your code. \$\endgroup\$
    – DMGregory
    Jun 22, 2019 at 14:34
  • 1
    \$\begingroup\$ In a cookie clicker, why do you care about cheating? It's a single player game; a change one player makes won't affect anyone else. \$\endgroup\$
    – Alex F
    Jun 22, 2019 at 17:44
  • 1
    \$\begingroup\$ hacker doesn't need to modify your button he can just go to console and run cookieClick(1500100900) it would be a little bit more interesting if you would hide your game under private scoped variables but with your current code there is no need for such solution as you try to do there \$\endgroup\$
    – Xesenix
    Jun 24, 2019 at 9:27
  • \$\begingroup\$ you need to learn a lot more to even start trying to make hackers life misserable \$\endgroup\$
    – Xesenix
    Jun 24, 2019 at 9:31

1 Answer 1

0
\$\begingroup\$

Try to set the onclick function in your JS code. Example (pure browser JS):

document.querySelector("#cooker").onclick = () => { cookieClick(1); } // need to get loaded behind the button in the code

with JQuery, this would be

$(document).ready(()=>{// make sure assign this only after loading the button in the browser
    $("#cooker").click(() => { cookieClick(1); }); // hook button with function
});

In addition, I recommend you to obfuscate your code with a tool like this.

To recreate the button and make it harder to cheat, try something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TestPage</title>
</head>
<body>
    <div --button-position><!--Here Spawns your Button--></div>
    <script>
        // OBFUSCATE THIS
        (()=>{
            // HERE ARE YOUR FUNCTIONS
            function recreateButton(){
                const div = document.querySelector("[--button-position]"),
                 btn = document.createElement("button");
                div.innerHTML=""; // KILL OLD THINGS
                btn.id = "cooker";
                btn.innerText = "Click"; // TEXT IN THE BUTTON
                btn.onclick = () => {
                    alert("You clicked the button");// DO WHATEVER YOU WANT TO DO ON BUTTON CLICK
                };
                div.append(btn);
            }
            recreateButton(); //CREATE BUTTON FIRST TIME, CALL THIS FUNCTION IF YOU WANT TO RECREATE IT, LIKE ON CLICK, OR EVERY 10 SEC
            // HERE YOUR CODE TO EXECUTE ON LAUNCH
        })();
        // UNTIL HERE
    </script>
</body>
</html>

NOTE: you can only make it harder to cheat, but not impossible, because all of the code will be running on client.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .