From 0fc665c98772ebccf36399a4ee9141a228631f42 Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Sat, 29 Dec 2018 18:51:00 +0800 Subject: [PATCH] saveIds --- libs/actions.js | 54 ++++++++++++++---------------------------- libs/control.js | 62 +++++++++++++++++++++++++++++++++++-------------- libs/core.js | 13 +++++++++-- libs/utils.js | 10 ++++++++ 4 files changed, 83 insertions(+), 56 deletions(-) diff --git a/libs/actions.js b/libs/actions.js index 4d9731ae..d97120b5 100644 --- a/libs/actions.js +++ b/libs/actions.js @@ -2041,9 +2041,9 @@ actions.prototype.clickSyncSave = function (x,y) { }) } else { - // core.setLocalStorage("save"+core.status.saveIndex, data); - core.setLocalForage("save"+core.status.saveIndex, data, function() { - core.drawText("同步成功!\n单存档已覆盖至存档"+core.status.saveIndex); + // core.setLocalStorage("save"+core.saves.saveIndex, data); + core.setLocalForage("save"+core.saves.saveIndex, data, function() { + core.drawText("同步成功!\n单存档已覆盖至存档"+core.saves.saveIndex); }) } }, function () { @@ -2178,30 +2178,10 @@ actions.prototype.clickLocalSaveSelect = function (x,y) { var topIndex = 6 - parseInt((choices.length - 1) / 2); - var saves=null; - if (y>=topIndex && y number) { + if (index >= number) { if (core.isset(callback)) callback(saves); return; } - core.getLocalForage("save"+index, null, function (data) { + core.getLocalForage("save"+ids[index], null, function (data) { saves.push(data); load(index+1, callback); }, function(err) { @@ -2396,6 +2397,33 @@ control.prototype.getSaves = function (index, callback) { load(1, callback); } +////// 获得所有存在存档的存档位 ////// +control.prototype.getSaveIndexes = function (callback) { + var indexes = {}; + + var getIndex = function (name) { + var e = new RegExp('^'+core.firstData.name+"_(save\\d+|autoSave)$").exec(name); + if (e!=null) { + if (e[1]=='autoSave') indexes[0]=true; + else indexes[parseInt(e[1].substring(4))] = true; + } + }; + + if (!core.platform.useLocalForage) { + Object.keys(localStorage).forEach(function (key) { + getIndex(key); + }); + callback(indexes); + } + else { + localforage.iterate(function (value, key, n) { + getIndex(key) + }, function () { + callback(indexes); + }) + } +} + ////// 设置勇士属性 ////// control.prototype.setStatus = function (statusName, statusVal) { if (statusName == 'exp') statusName = 'experience'; diff --git a/libs/core.js b/libs/core.js index d2d62bfc..b2b5f39d 100644 --- a/libs/core.js +++ b/libs/core.js @@ -93,7 +93,11 @@ function core() { height: 13, tempCanvas: null, // A temp canvas for drawing } - this.paint = {} + this.paint = {}; + this.saves = { + "saveIndex": null, + "ids": {} + } this.initStatus = { 'played': false, 'gameOver': false, @@ -151,7 +155,6 @@ function core() { }, // event事件 - 'saveIndex': null, 'shops': {}, 'event': { 'id': null, @@ -350,6 +353,12 @@ core.prototype.init = function (coreData, callback) { core.bigmap.tempCanvas = document.createElement('canvas').getContext('2d'); + ////// 记录所有的存档编号!!! ////// + core.saves.saveIndex = core.getLocalStorage('saveIndex', 1); + core.control.getSaveIndexes(function (indexes) { + core.saves.ids = indexes; + }); + core.loader.load(function () { console.log(core.material); // 设置勇士高度 diff --git a/libs/utils.js b/libs/utils.js index c0521ef2..f7539ca4 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -111,6 +111,11 @@ utils.prototype.unshift = function (a,b) { ////// 设置本地存储 ////// utils.prototype.setLocalStorage = function(key, value) { try { + if (!core.isset(value)) { + this.removeLocalStorage(key); + return; + } + var str = JSON.stringify(value); var compressed = LZString.compress(str); @@ -171,6 +176,11 @@ utils.prototype.setLocalForage = function (key, value, successCallback, errorCal return; } + if (!core.isset(value)) { + this.removeLocalForage(key); + return; + } + // Save to localforage var compressed = LZString.compress(JSON.stringify(value)); localforage.setItem(core.firstData.name+"_"+key, compressed, function (err) {