Reduce size of save by half or so

This commit is contained in:
Eric Fredricksen
2010-07-09 21:06:54 -07:00
parent cd7f62fd7d
commit 39061d3460
3 changed files with 86 additions and 45 deletions
+9 -5
View File
@@ -1,11 +1,15 @@
// TODO These code bits don't really belong here, but this is the only shared bit of js // TODO These code bits don't really belong here, but this is the only shared bit of js
function tabulate(html) { function tabulate(list) {
var result = ''; var result = '';
$("<table>").html(html).find("tr").each(function () { $.each(list, function (index) {
result += " " + $(this).children().first().text() + ": " + if (this.length == 2) {
$(this).children().last().text() + "\n"; if (this[1].length)
result += " " + this[0] + ": " + this[1] + "\n";
} else {
result += " " + this + "\n";
}
}); });
return result; return result;
} }
@@ -17,7 +21,7 @@ function template(tmpl, data) {
$.each(p1.split("."), function (i,v) { $.each(p1.split("."), function (i,v) {
if (!dict) return true; if (!dict) return true;
if (v == "___") { if (v == "___") {
dict = tabulate(dict.html); dict = tabulate(dict);
} else { } else {
dict = dict[v.replace("_"," ")]; dict = dict[v.replace("_"," ")];
} }
+69 -32
View File
@@ -431,6 +431,17 @@ function ProgressBar(id) {
} }
function Key(tr) {
return $(tr).children().first().text();
}
function Value(tr) {
return $(tr).children().last().text();
}
function ListBox(id, columns, fixedkeys) { function ListBox(id, columns, fixedkeys) {
this.id = id; this.id = id;
//this.box = $("#"+ id); //this.box = $("#"+ id);
@@ -443,7 +454,6 @@ function ListBox(id, columns, fixedkeys) {
caption + "</td></tr>"); caption + "</td></tr>");
tr.appendTo(this.box); tr.appendTo(this.box);
tr.each(function () {this.scrollIntoView();}); tr.each(function () {this.scrollIntoView();});
game[this.id].last = caption;
return tr; return tr;
}; };
@@ -454,7 +464,7 @@ function ListBox(id, columns, fixedkeys) {
this.item = function (key) { this.item = function (key) {
if (typeof key == typeof 'string') { if (typeof key == typeof 'string') {
var result = this.rows().filter(function (index) { var result = this.rows().filter(function (index) {
return $(this).children().first().text() === key; return Key(this) === key;
}); });
if (!result.length) { if (!result.length) {
result = $("<tr><td>" + key + "</td><td/></tr>"); result = $("<tr><td>" + key + "</td><td/></tr>");
@@ -486,25 +496,49 @@ function ListBox(id, columns, fixedkeys) {
this.box.find("tr").eq(n).remove(); this.box.find("tr").eq(n).remove();
}; };
this.save = function (game, compact) {
if (!game[this.id]) this.save = function (game) {
game[this.id] = {}; var that = this;
var dict = game[this.id]; var dict = game[this.id];
dict.html = compact ? null : this.box.html(); if (this.fixedkeys) {
if (this.columns == 2) { if (!dict) dict = {};
this.rows().each(function (index) { this.rows().each(function (index) {
dict[$(this).children().first().text()] = dict[Key(this)] = Value(this);
$(this).children().last().text(); });
} else {
// Unfixed keys -- need to maintain order
dict = [];
this.rows().each(function (index) {
if (that.columns == 2)
dict.push([Key(this),Value(this)]);
else
dict.push($.trim($(this).text()));
});
}
game[this.id] = dict;
};
this.load = function (game) {
var that = this;
var dict = game[this.id];
if (this.fixedkeys) {
this.rows().each(function (index) {
Put(that, Key(this), dict[Key(this)]);
});
} else {
$.each(dict, function (index, row) {
if (that.columns == 2)
Put(that, row[0], row[1]);
else
that.Add(row);
}); });
} }
}; };
this.load = function (game) {
this.box.html(game[this.id].html);
};
this.label = function (n) { this.label = function (n) {
return this.item(n).children().first().text(); return Key(this.item(n));
}; };
} }
@@ -531,8 +565,8 @@ function InitializeCharacter(sheet) {
Put(Stats, 'HP Max', Random(8) + sheet.CON.div(6)); Put(Stats, 'HP Max', Random(8) + sheet.CON.div(6));
Put(Stats, 'MP Max', Random(8) + sheet.INT.div(6)); Put(Stats, 'MP Max', Random(8) + sheet.INT.div(6));
game.Equips = {best: 'Sharp Stick'}; game.bestequip = 'Sharp Stick';
Put(Equips, 'Weapon', game.Equips.best); Put(Equips, 'Weapon', game.bestequip);
Put(Inventory,'Gold',0); Put(Inventory,'Gold',0);
@@ -556,7 +590,7 @@ function InitializeCharacter(sheet) {
Q('plot|2|Loading'); Q('plot|2|Loading');
PlotBar.reset(26); PlotBar.reset(26);
Plots.Add('Prologue'); Plots.Add((game.bestplot = 'Prologue'));
game.act = 0; game.act = 0;
//debugger; //debugger;
@@ -636,8 +670,8 @@ function WinEquip() {
if (plus > 0) name = '+' + name; if (plus > 0) name = '+' + name;
Put(Equips, posn, name); Put(Equips, posn, name);
game.Equips.best = name; game.bestequip = name;
if (posn > 1) game.Equips.best += ' ' + Equips.label(posn); if (posn > 1) game.bestequip += ' ' + Equips.label(posn);
} }
@@ -651,16 +685,16 @@ function WinStat() {
// Favor the best stat so it will tend to clump // Favor the best stat so it will tend to clump
var t = 0; var t = 0;
Stats.rows().each(function (index,elt) { Stats.rows().each(function (index,elt) {
t += Square(StrToInt($(this).children().last().text())); t += Square(StrToInt(Value(this)));
}); });
t = Random(t); t = Random(t);
Stats.rows().each(function (index,elt) { Stats.rows().each(function (index,elt) {
i = this; i = this;
t -= Square(StrToInt($(this).children().last().text())); t -= Square(StrToInt(Value(this)));
return t < 0; return t < 0;
}); });
} }
Add(Stats, $(i).find("td").first().text(), 1); Add(Stats, Key(i), 1);
} }
function SpecialItem() { function SpecialItem() {
@@ -732,7 +766,7 @@ function CompleteQuest() {
game.questmonster = ''; // We're trying to placate them, after all game.questmonster = ''; // We're trying to placate them, after all
break; break;
} }
Quests.Add(caption); Quests.Add((game.bestquest = caption));
Log('Commencing quest: ' + caption); Log('Commencing quest: ' + caption);
@@ -800,7 +834,8 @@ function CompleteAct() {
PlotBar.reset(60 * 60 * (1 + 5 * game.act)); // 1 hr + 5/act PlotBar.reset(60 * 60 * (1 + 5 * game.act)); // 1 hr + 5/act
while (Plots.length() > 99) while (Plots.length() > 99)
Plots.remove(0); Plots.remove(0);
Plots.Add('Act ' + toRoman(game.act)); Plots.Add((game.bestplot = 'Act ' + toRoman(game.act)));
if (game.act > 1) { if (game.act > 1) {
WinItem(); WinItem();
@@ -846,7 +881,7 @@ function AddR(list, key, value) {
function Get(list, key) { function Get(list, key) {
var item = list.item(key); var item = list.item(key);
return item.children().last().text(); return Value(item);
} }
function GetI(list, key) { function GetI(list, key) {
@@ -860,7 +895,7 @@ function Min(a,b) {
function Sum(list) { function Sum(list) {
var result = 0; var result = 0;
list.rows().each(function (index) { list.rows().each(function (index) {
result += StrToInt($(this).children().last().text()); result += StrToInt(Value(this));
}); });
return result; return result;
} }
@@ -1070,7 +1105,10 @@ $(function() {
Add(Inventory,'Gold',Random(100)); Add(Inventory,'Gold',Random(100));
}); });
cheat("Save", SaveGame); cheat("Save", function () {
SaveGame();
alert(JSON.stringify(game).length);
});
cheat("Quit", quit); cheat("Quit", quit);
@@ -1086,7 +1124,7 @@ function HotOrNot() {
(best+flat) * toArabic(Get(Spells,best))) (best+flat) * toArabic(Get(Spells,best)))
best = i; best = i;
} }
game.Spells.best = Spells.label(best) + ' ' + Get(Spells, best); game.bestspell = Spells.label(best) + ' ' + Get(Spells, best);
/// And which stat is best? /// And which stat is best?
best = 0; best = 0;
@@ -1094,13 +1132,13 @@ function HotOrNot() {
if (GetI(Stats,i) > GetI(Stats,best)) if (GetI(Stats,i) > GetI(Stats,best))
best = i; best = i;
} }
game.Stats.best = Stats.label(best) + ' ' + GetI(Stats, best); game.beststat = Stats.label(best) + ' ' + GetI(Stats, best);
} }
function SaveGame(compact) { function SaveGame() {
Log('Saving game: ' + GameSaveName()); Log('Saving game: ' + GameSaveName());
$.each(AllBars.concat(AllLists), function (i, e) { e.save(game, compact); }); $.each(AllBars.concat(AllLists), function (i, e) { e.save(game); });
HotOrNot(); HotOrNot();
game.date = ''+new Date(); game.date = ''+new Date();
game.stamp = +new Date(); game.stamp = +new Date();
@@ -1179,14 +1217,13 @@ function LFSR(pt, salt) {
function Brag(trigger) { function Brag(trigger) {
SaveGame();
if (game.isonline) { if (game.isonline) {
SaveGame(true); // Save more compact version
game.bragtrigger = trigger; game.bragtrigger = trigger;
$.post("webrag.php", game, function (data, textStatus, request) { $.post("webrag.php", game, function (data, textStatus, request) {
if (data.alert) if (data.alert)
alert(data.alert); alert(data.alert);
}, "json"); }, "json");
} }
SaveGame(); // Save whole game
} }
+7 -7
View File
@@ -126,10 +126,10 @@ Testing:
<td><a class="icon go">&#x2694;</a></td> <td><a class="icon go">&#x2694;</a></td>
<td> <td>
<div> <div>
<a class="go"><b>$Traits.Name</b></a> the $Traits.Race ($Plots.last) <a class="go"><b>$name</b></a> the $race ($bestplot)
</div> </div>
<div class="rc">Level $Traits.Level $Traits.Class</div> <div class="rc">Level $Traits.Level $class</div>
<div class="bs">$Equips.best / $Spells.best / $Stats.best</div> <div class="bs">$bestequip / $bestspell / $beststat</div>
</td> </td>
<td> <td>
<a class="x" href="#">&#x2620</a><br> <a class="x" href="#">&#x2620</a><br>
@@ -141,13 +141,13 @@ Testing:
<script type="text/charsheet" id="sheet"> <script type="text/charsheet" id="sheet">
$Traits.Name the $Traits.Race [$hostname] $name the $race [$hostname]
"$motto" "$motto"
Level $Traits.Level $Traits.Class (exp. $ExpBar.position/$ExpBar.max) Level $Traits.Level $class (exp. $ExpBar.position/$ExpBar.max)
Plot stage: $Plots.last ($PlotBar.hint) Plot stage: $bestplot ($PlotBar.hint)
Quest: $Quests.last ($QuestBar.hint) Quest: $bestquest ($QuestBar.hint)
Stats: Stats:
STR $Stats.STR STR $Stats.STR