问题描述
任何玩家都可以通过浏览器控制台更改纸杯蛋糕的数量,轻松欺骗这款游戏。我想知道是否有办法防止用户这样做。这也发布在上。
Any player can easily cheat on this game by changing the amount of cupcakes through the browser console. I wanted to know if there was a way to prevent the user from doing so. This is also posted on github.
var numberOfCupcakesDisplay = document.getElementById("numberOfCupcakes");
var cupcake = document.getElementById("cupcake");
function updateValues() {
numberOfCupcakesDisplay.innerHTML = amountOfCupcakes.toString();
//displays number of cupcakes to screen
document.getElementById("amountOfToasters").innerHTML = amountOfToasters.toString();
//displays number of toasters to screen
document.getElementById("toasterButton").innerHTML = "        " + Math.round(costOfToasters).toString() + " cc";
//changes cost of toaster every time it is bought by 110%
}
cupcake.addEventListener('webkitAnimationEnd', function(e){
e.preventDefault();
this.style.webkitAnimationName = '';
}, false);
function clickCupcake() {
++amountOfCupcakes;
cupcake.style.animation = "shiftCupcake";
cupcake.style.webkitAnimationName = "shiftCupcake";
}
updateValues();
推荐答案
是啊!正如其他答案提到的 IMPOSSIBLE 可防止用户更改浏览器控制台的值。换句话说,你只是要求如何编写JS代码,以便用户无法调试它。
Yeah!! As other answers mention it is IMPOSSIBLE to prevent users from changing the values from browser console. In other words, You are simply asking How to write JS code so that user can not debug it. You can always use some methods which make life hard for people who want to cheat on the game.
查看这些链接以获取有关模糊处理的更多信息。
Look at these links for more information on obfuscation.
- SO: How to obfuscate Javascript
- Jsobfuscate.com
不要将控制值存储在全局变量中。相反,定义一个类,并将这些变量作为它的私有因此,用户必须深入挖掘,以找出在哪里放置断点。
Do not store control values in global variables. Instead define a class and have these variables as private to it So that user has to dig in deep in order to find out where to put the breakpoints.
混淆或多或少也包括缩图。
Obfuscation more or less covers minification as well.
这篇关于阻止用户在浏览器控制台中更改游戏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!