I am trying to create a game like cookie clicker. I am now attempting to use bota
and atob
to crate and decode the save code. My save code is a string wit all the variables separated by a period. I can encode the string with bota
without incident. When I put the encoded string into base64 decode I get 9999.1.0.0 witch is the desired result. When I go to my site (Localhost) and put in the save code I get
Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
I am only using numbers and a period so it shouldn't be a problem with there being non ascii characters. can somebody please help?
index.js:
//on page load function
function onLoad() {
var name = prompt("Please name your Universe");
}
//Clicking
var atoms = 9999;
var clickValue = 1;
function atomClick() {
atoms = atoms + clickValue;
document.getElementById("atoms").innerHTML = abbrNum(atoms , true , 2);
};
//upgrades
function upgradeClickValue () {
clickValue = clickValue * 2;
};
//Auto click modifiers
//create veriables
var elmts = 0;
var molecules = 0;
function buyElement() {
var elementCost = Math.floor(10 * Math.pow(1.1,elmts));
if(atoms >= elementCost) {
elmts++;
atoms = atoms - elementCost;
document.getElementById('elmts').innerHTML = abbrNum(elmts , true , 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
};
var nextECost = Math.floor(10 * Math.pow(1.1,elmts));
document.getElementById('elementCost').innerHTML = abbrNum(nextECost , true , 2);
};
function buyMolecule() {
var moleculeCost = Math.floor(100 * Math.pow(1.2,molecules));
if(atoms >= moleculeCost) {
molecules++;
atoms = atoms - moleculeCost;
document.getElementById('molecules').innerHTML = abbrNum(molecules , true , 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
};
var nextMCost = Math.floor(100 * Math.pow(1.2,molecules));
document.getElementById('moleculeCost').innerHTML = abbrNum(nextMCost , true , 2);
};
window.setInterval(function() {
data = atoms + "." + clickValue + "." + elmts + "." + molecules;
atoms = atoms + (elmts * 1) + (molecules * 2);
document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);
document.title = "Atoms: " + abbrNum(atoms , true , 2);
}, 1000);
//round numbers
const COUNT_ABBRS = [ '', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'Se', 'Sp', 'Ot', 'No', 'De'];
function abbrNum(count, withAbbr = false, decimals = 2) {
const i = 0 === count ? count : Math.floor(Math.log(count) / Math.log(1000));
let result = parseFloat((count / Math.pow(1000, i)).toFixed(decimals));
if(withAbbr) {
result += `${COUNT_ABBRS[i]}`;
}
return result;
}
function reset() {
atoms = 0;
clickValue = 1;
elmts = 0;
elementCost = 10;
molecules = 0;
moleculeCost = 100;
}
//initalising save var
var data = atoms + "." + clickValue + "." + elmts + "." + molecules;
console.log(data);
var encodedString = btoa(data);
function save() {
// Encode the String
encodedString = btoa(data);
console.log(encodedString);
alert("Save Code: " + encodedString);
}
function load() {
var pastedValue = prompt("Past Your Save Code Here.");
// Decode the String
if (pastedValue === "") {
alert("You left the text box blank.");
} else {
if (pastedValue = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/) {
var decodedString = atob(pastedValue);
console.log(decodedString);
var splitRslt = decodedString.split(".");
console.log(splitRslt)
atoms = splitRslt[0];
clickValue = splitRslt[1];
elmts = splitRslt[2];
molecules = splitRst[3];
} else {
alert("Please enter a valid save code.");
}
}
}
Look at this line in your code:
if (pastedValue = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/) {
You're assigning a regular expression to pastedValue
(and the result of this if
is always true).
What you want to do is match pastedValue
on the regex:
if (pastedValue.match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/)) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.