Fixes cookie clicker addon for very large numbers

This commit is contained in:
2023-09-14 17:54:28 -04:00
parent 44e8013428
commit 0abc566ca0
3 changed files with 133 additions and 91 deletions

View File

@@ -4,16 +4,18 @@ header("Access-Control-Allow-Origin: *");
if (!array_key_exists("type", $_POST)) {
$_POST["type"] = "";
}
if ($_POST["type"] === "view" and array_key_exists("username", $_POST) and array_key_exists("cookies", $_POST) and array_key_exists("cookiesPs", $_POST) and array_key_exists("room", $_POST) and array_key_exists("time", $_POST)) {
if ($_POST["type"] === "view" and array_key_exists("username", $_POST) and array_key_exists("cookies", $_POST) and array_key_exists("cookiesPs", $_POST) and array_key_exists("room", $_POST) and array_key_exists("time", $_POST) and array_key_exists("powerOfCookies", $_POST) and array_key_exists("powerOfCookiesPs", $_POST)) {
$username = $_POST["username"];
$cookies = floatval($_POST["cookies"]);
$cookiesPs = floatval($_POST["cookiesPs"]);
$cookies = intval($_POST["cookies"]);
$powerOfCookies = intval($_POST["powerOfCookies"]);
$cookiesPs = intval($_POST["cookiesPs"]);
$powerOfCookiesPs = intval($_POST["powerOfCookiesPs"]);
$room = $_POST["room"];
$time = $_POST["time"];
if (dbRequest2("SELECT * FROM cookieClicker WHERE room='$room' AND username='$username'")) {
dbCommand("UPDATE cookieClicker SET cookies = '$cookies', cookiesPs = '$cookiesPs', lastUpdate='$time' WHERE room='$room' AND username='$username';");
dbCommand("UPDATE cookieClicker SET cookies = '$cookies', cookiesPs = '$cookiesPs', powerOfCookies = '$powerOfCookies', powerOfCookiesPs = '$powerOfCookiesPs', lastUpdate='$time' WHERE room='$room' AND username='$username';");
} else {
dbCommand("INSERT INTO cookieClicker (username, room, cookies, cookiesPs, lastUpdate) VALUES ('$username', '$room', $cookies, $cookiesPs, '$time')");
dbCommand("INSERT INTO cookieClicker (username, room, cookies, powerOfCookies, cookiesPs, powerOfCookiesPs, lastUpdate) VALUES ('$username', '$room', $cookies, $powerOfCookies, $cookiesPs, $powerOfCookiesPs, '$time')");
}
echo json_encode(["leaderboard" => dbRequest2("SELECT * FROM cookieClicker WHERE room='$room' ORDER BY cookiesPs DESC, cookies DESC")]);
} else {

View File

@@ -2,103 +2,141 @@
hostname = document.getElementById("hostname").src;
hostname = hostname.replace("/cookieClicker/index.js", "");
// Will add jquery and another script
var script = document.createElement('script');
var script = document.createElement("script");
script.src = `${hostname}/javascript/functions.js`;
document.head.appendChild(script);
var script = document.createElement('script');
var script = document.createElement("script");
script.src = `${hostname}/javascript/jquery.js`;
document.head.appendChild(script);
// Used to store all values to make storage not overlap with cookie clicker values
var multiplayer = {
startMenu: function() { // Will generate the startup menu
this.clear()
$("#multiplayer").append(`<h1 class='title' style='font-size:150%'>Welcome to the Online Cookie Clicker Addon</h1><br>
startMenu: function () {
// Will generate the startup menu
this.clear();
$("#multiplayer")
.append(`<h1 class='title' style='font-size:150%'>Welcome to the Online Cookie Clicker Addon</h1><br>
<p>You will see everyone's number of cookies and cookies per second that are in the same room.</p>
<label for="room">Room ID:</label>
<input type="text" id="room" name="room"/>
<a id='joinButton' class='option'>Join room</a>`);
// Will run the code for when the user clicks join
$("#joinButton").click(function() {
multiplayer.room = $("#room").val();
multiplayer.gameMenu();
})
},
clear: function() { // Will clear the menu area for this
$("#multiplayer").empty();
},
room: null, // This stores the room id
gameMenu: function() { // Will generate the game menu and run the actual loop
this.clear();
$("#multiplayer").append(`<h1 class='title' style='font-size:150%'>Welcome to ${this.room}</h1><br>
// Will run the code for when the user clicks join
$("#joinButton").click(function () {
multiplayer.room = $("#room").val();
multiplayer.gameMenu();
});
},
clear: function () {
// Will clear the menu area for this
$("#multiplayer").empty();
},
room: null, // This stores the room id
gameMenu: function () {
// Will generate the game menu and run the actual loop
this.clear();
$("#multiplayer")
.append(`<h1 class='title' style='font-size:150%'>Welcome to ${this.room}</h1><br>
<p>If table stops updating leave and join the room.</p>
<table id='leaderboard' style='width:100%;'></table>
<a id='leave' class='option'>Leave room</a>`)
this.intervalFetch = setInterval(this.fetchData, 2000);
this.intervalFakeLive = setInterval(this.fakeLive, 30);
$("#leave").click(function() {
clearInterval(multiplayer.intervalFetch);
clearInterval(multiplayer.intervalFakeLive);
multiplayer.startMenu();
})
},
intervalFakeLive: null,
intervalFetch: null,
fetchData: function() { // Used to fetch data from server and update the server
let ajax = new XMLHttpRequest();
ajax.onload = function() {
let jsonData = JSON.parse(this.response);
multiplayer.internalCookies = jsonData["leaderboard"];
let commands = jsonData["commands"];
if (commands) { // Will run all commands that are sent
commands.forEach(command => {
eval(command["javascript"]);
});
}
}
ajax.open("POST", `${this.hostname}/api/cookieClicker.php`);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(`username=${Game.bakeryName}&cookies=${Math.round(Game.cookies)}&cookiesPs=${Math.round(Game.cookiesPs)}&room=${multiplayer.room}&type=view&time=${Date.now()}`);
},
fakeLive: function() {// Will make it look like you are live
let html = `<tr><th>Username</th><th>Cookies</th><th>Per Second</th><th>Last Update</th></tr>`;
if (multiplayer.internalCookies) {
multiplayer.internalCookies.forEach(data => {
let username = data["username"]; // Stores the username for that user
let age = (Date.now()-parseInt(data["lastUpdate"]))/1000; // Stores the age of the information
let cookies = Beautify(parseInt(parseFloat(data["cookies"]) + (parseFloat(data["cookiesPs"]) * age))) // Uses the age to make it look more like it is live
let cookiesPs = Beautify(parseInt(parseFloat(data["cookiesPs"]) * 10) / 10) // Stores the amount of cookies per second
let style = "";
let button = "";
if (age > 30) {
style = "color:grey";
} else {
if (username == Game.bakeryName) {
cookies = Beautify(Game.cookies);
cookiesPs = Beautify(Game.cookiesPs);
age = 0;
}
}
html += `<tr style='${style}'><td>${username}</td><td>${cookies}</td><td>${cookiesPs}</td><td>${humanReadableTime(age)}</td><td>${button}</td></tr>`;
});
<a id='leave' class='option'>Leave room</a>`);
this.intervalFetch = setInterval(this.fetchData, 2000);
this.intervalFakeLive = setInterval(this.fakeLive, 30);
$("#leave").click(function () {
clearInterval(multiplayer.intervalFetch);
clearInterval(multiplayer.intervalFakeLive);
multiplayer.startMenu();
});
},
intervalFakeLive: null,
intervalFetch: null,
fetchData: function () {
// Used to fetch data from server and update the server
let ajax = new XMLHttpRequest();
ajax.onload = function () {
let jsonData = JSON.parse(this.response);
multiplayer.internalCookies = jsonData["leaderboard"].map((e) => {
e.cookies = e.cookies * 10 ** e.powerOfCookies;
e.cookiesPs = e.cookiesPs * 10 ** e.powerOfCookiesPs;
return e;
});
let commands = jsonData["commands"];
if (commands) {
// Will run all commands that are sent
commands.forEach((command) => {
eval(command["javascript"]);
});
}
};
ajax.open("POST", `${this.hostname}/api/cookieClicker.php`);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Sets the power variables correctly
let powerOfCookies = 0;
let cookies = Game.cookies;
while (cookies >= 1000000) {
powerOfCookies++;
cookies /= 10;
}
let cookiesPs = Game.cookiesPs;
let powerOfCookiesPs = 0;
while (cookiesPs >= 1000000) {
powerOfCookiesPs++;
cookiesPs /= 10;
}
ajax.send(
`username=${Game.bakeryName}&cookies=${Math.round(
cookies
)}&powerOfCookies=${powerOfCookies}&cookiesPs=${Math.round(
cookiesPs
)}&powerOfCookiesPs=${powerOfCookiesPs}&room=${
multiplayer.room
}&type=view&time=${Date.now()}`
);
},
fakeLive: function () {
// Will make it look like you are live
let html = `<tr><th>Username</th><th>Cookies</th><th>Per Second</th><th>Last Update</th></tr>`;
if (multiplayer.internalCookies) {
multiplayer.internalCookies.forEach((data) => {
let username = data["username"]; // Stores the username for that user
let age = (Date.now() - parseInt(data["lastUpdate"])) / 1000; // Stores the age of the information
let cookies = Beautify(data["cookies"] + data["cookiesPs"] * age); // Uses the age to make it look more like it is live
let cookiesPs = Beautify(data["cookiesPs"]); // Stores the amount of cookies per second
let style = "";
let button = "";
if (age > 30) {
style = "color:grey";
} else {
if (username == Game.bakeryName) {
cookies = Beautify(Game.cookies);
cookiesPs = Beautify(Game.cookiesPs);
age = 0;
}
}
$("#leaderboard").empty();
$("#leaderboard").append(html);
},
internalCookies: null, // Used to store a more precise cookie amount
hostname: hostname,
lastFetch: null, // Says the last time that the data was updated
}
html += `<tr style='${style}'><td>${username}</td><td>${cookies}</td><td>${cookiesPs}</td><td>${humanReadableTime(
age
)}</td><td>${button}</td></tr>`;
});
} else {
html = "<p>Loading...</p>";
}
$("#leaderboard").empty();
$("#leaderboard").append(html);
},
internalCookies: null, // Used to store a more precise cookie amount
hostname: hostname,
lastFetch: null, // Says the last time that the data was updated
};
// This will make sure that Jquery is loaded before starting everything
var waitForJQuery = setInterval(function () {
if (typeof $ != 'undefined' && typeof getCookie != "undefined") {
let element = document.getElementById("centerArea");
// Will create the multiplayer element
let div = document.createElement('div');
div.id = "multiplayer";
div.style = "text-align:center;background:rgba(0,0,0,1);position:relative;z-index:100;padding-top:20px;padding-bottom:20px";
element.insertBefore(div, element.firstChild);
multiplayer.startMenu();
console.log("Import succesful");
clearInterval(waitForJQuery);
}
if (typeof $ != "undefined" && typeof getCookie != "undefined") {
let element = document.getElementById("centerArea");
// Will create the multiplayer element
let div = document.createElement("div");
div.id = "multiplayer";
div.style =
"text-align:center;background:rgba(0,0,0,1);position:relative;z-index:100;padding-top:20px;padding-bottom:20px";
element.insertBefore(div, element.firstChild);
multiplayer.startMenu();
console.log("Import succesful");
clearInterval(waitForJQuery);
}
}, 10);