即使正在编码的值发生变化,Base64代码也保持不变

时间:2018-07-04 14:24:19

标签: javascript html base64

我目前正在创建一个增量游戏,类似于Cookie Clicker。没有它可以保存的保存代码的Cookie Clicker将会是什么。我有接受所有变量atomsclickvalueelmtsmolecules等值的代码。它把它们放在带间隔的字符串中。然后将其编码为Base64。这是问题开始的地方。最初,Bace64解码的值与编码时的值相同,但是如果您更改了这些值,则再次对它们进行编码和解码,这些值将与第一次相同。无论尝试多少次,这都不会发生。

index.js:

var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
//Clicking
var atoms = 15;
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() {

    atoms = atoms + (elmts * 1) + (molecules * 2);
    document.getElementById('atoms').innerHTML = abbrNum(atoms , true , 2);

    document.title  = "AC Home :: Atoms: " + abbrNum(atoms , true , 2);

    var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
}, 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() {
    localStorage.removeItem("save")
    atoms = 0;
    clickValue = 1;
    elmts = 0
    elementCost = 10;
    molecules = 0;
    moleculeCost = 100;
}

// base64 encoding
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

//set code
var data = atoms + "%-%" + clickValue + "%-%" + elmts + "%-%" + molecules;
var encodedString = Base64.encode(data);

function save() {
    // Encode the String
    var encodedString = Base64.encode(data);
    console.log(encodedString);
    alert("Save Code: " + encodedString);
}
function load() {
    // Decode the String
    var decodedString = Base64.decode(encodedString);
    console.log(decodedString);
    var splitRst = decodedString.split("%-%");
    console.log(splitRst)
    alert("Values: " + splitRst);
} 

index.php:

<?php
    include_once 'header.php';
?>
<!DOCTYPE html>

</html>

<head>
  <link rel="stylesheet" type="text/css" href="index.css" />
  <title>AC Home :: Atoms: 0</title>
</head>

<body>
  <div class="content">
    <div class="inline" id="clicker">
      <br> Atoms: <span id="atoms">0</span>
      <br>
      <input type="image" class="atom-image" src="https://theatomandperiodictable.wikispaces.com/file/view/220px-Stylised_Lithium_Atom.svg.png/297637780/220px-Stylised_Lithium_Atom.svg.png" onClick="atomClick()">
    </div>

    <div class="inline" id="upgrades">
      <h3>upgrades</h3>
    </div>

    <div class="inline" id="modifiers">
      <button onClick="buyElement()" id="BuyModifier" style="height: 50px; width: 201px;">Buy Element</button><br /> elmts: <span id="elmts">0</span>
      <br />
      Cost: <span id="elementCost">10</span>
      <br />

      <button onClick="buyMolecule()" id="BuyModifier" style="height: 50px; width: 201px;">Buy Molecule</button>
      <br />
      Molecules: <span id="molecules">0</span>
      <br />
      Cost: <span id="moleculeCost">100</span>
    </div>
  </div>
  <br class="clearBoth" />
  <script type="text/javascript" src="index.js"></script>
</body>

</html>

<?php
    include_once 'footer.php';
?>

2 个答案:

答案 0 :(得分:0)

为什么你不能做这样的事情:

const data = {
    atoms: atoms,
    molecules: molecules,
    elements: elmts,
    clicks: clickValue 
};
const text = JSON.stringify(data);
const base64 = btoa(text);

然后解码

const text = atob(base64);
const data = JSON.parse(text);
consol.log(data.clicks);  // your click value here

没有与%-%奇怪的连接。

答案 1 :(得分:0)

您的问题是var的函数中data =之前的setInterval。您正在遮盖全局data,并且正在写入局部变量,该局部变量在函数结束时将不复存在。