简体   繁体   English

每次用户加载页面时,如何存储整个Javascript文件并加载更改的版本?

[英]How can I store an entire Javascript file and load the changed version each time the user loads the page?

I am making a text based game, like Zork or A Hitchhiker's Guide to the Galaxy , that is web based, for a school project over a school year. 我正在为一个学年的学校项目制作基于文本的游戏,例如Zork《银河漫游指南》 Right now I have constructed a parser, and my game's basic structure is complete. 现在,我已经构造了一个解析器,并且我的游戏的基本结构已经完成。 My issue is that I need to save data after the player reloads the page. 我的问题是,播放器重新加载页面后,我需要保存数据。

I have a javascript file called "content.js" which stores all of the data for the game, like the player's location, what is in the areas, etc. 我有一个名为“ content.js”的javascript文件,该文件存储了游戏的所有数据,例如玩家的位置,所在区域等。
"content.js" contains object declarations, and functions that correspond to those objects, like "onPickup()". “ content.js”包含对象声明和与这些对象相对应的函数,例如“ onPickup()”。

I want to save this file by executing some function, and each time the user loads the page, it will load this saved version of "content.js", not the original version. 我想通过执行某些功能来保存此文件,并且每次用户加载页面时,它将加载此保存的“ content.js”版本,而不是原始版本。 For example, if the user changed something in an area, I would need to save their progress, and on reloading the page, that change would carry over. 例如,如果用户更改了某个区域中的某些内容,则需要保存他们的进度,并且在重新加载页面时,该更改将继续。

Normally I would just store all of these objects in an array, and try to save that array, but because I created the game structure before the actual game (the story, areas, etc.), I don't know how many things I will have to save, and it would be prone to error if I forgot about one of the objects when creating such an array. 通常,我只是将所有这些对象存储在一个数组中,然后尝试保存该数组,但是由于我是在实际游戏(故事,区域等)之前创建游戏结构的,所以我不知道我有多少东西将必须保存,如果在创建这样的数组时忘记了其中一个对象,则很容易出错。

Is there any way for me to save the current version of "content.js" and have the user load that version after it is saved? 我有什么办法可以保存“ content.js”的当前版本,并在保存后让用户加载该版本?
(Note: I am not using JQuery for this project) (注意:我没有为此项目使用JQuery)

You should have a look at HTML5 storage: https://www.html5rocks.com/en/features/storage 您应该看看HTML5存储: https//www.html5rocks.com/en/features/storage

You can do something like this: 您可以执行以下操作:

var gamedata = localStorage.getItem("gamedata");
// modify your game state
// ...
localStorage.setItem("gamedata", gamedata);

There is a simple demo at http://html5demos.com/storage . http://html5demos.com/storage有一个简单的演示。 You can enter values, then close the window and open in a new window in the same browser and your previous values will be displayed. 您可以输入值,然后关闭窗口并在同一浏览器中在新窗口中打开,将显示您以前的值。

You should definitly separate concerns of your application. 您应该明确区分应用程序的关注点。 Javascript is not intended to be the place to store things. Javascript并不是要存储的地方。 If you are not planning to add database support server side with an API to store your users progress the hint Paul gave ist the best choice. 如果您不打算通过API添加数据库支持服务器端来存储用户进度,则Paul会提供最佳选择。 The LocalStorage is the browsers own database, but be careful since that maybe not permanent - same applies for cookies as the user may delete that data on a regular basis. LocalStorage是浏览器自己的数据库,但要小心,因为它可能不是永久的-Cookie也是如此,因为用户可以定期删除该数据。 Storing the progress client side may also allow cheating since the data is stored locally and may be edited. 存储进度客户端也可能会作弊,因为数据存储在本地并且可以编辑。 Same applies for storage in cookies. 同样适用于Cookie中的存储。

If you have the time consider using serverside storage. 如果有时间,请考虑使用服务器端存储。 If you are not familar with php or any other serverside language have a look at meteor which uses javascript serverside (node.js) and comes with mongo db. 如果您不熟悉php或任何其他服务器端语言,请查看使用JavaScript服务器端(node.js)并随附mongo db的流星。 That would definitly result in an A+ and impress your teachers :) 这肯定会导致A +并给您的老师留下深刻的印象:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM