From 1c6a6818805377993c2eea07c30433994eab7a3e Mon Sep 17 00:00:00 2001 From: Eric Fredricksen Date: Fri, 2 Jul 2010 21:07:49 -0700 Subject: [PATCH] First pass at loading characters and a readable roster --- config.js | 10 +++++--- main.js | 73 ++++++++++++++++++++++++----------------------------- newguy.js | 3 ++- roster.html | 46 ++++++++------------------------- roster.js | 40 +++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 80 deletions(-) create mode 100644 roster.js diff --git a/config.js b/config.js index d84d65b..2ce11ba 100644 --- a/config.js +++ b/config.js @@ -124,11 +124,15 @@ function loadRoster() { if (!window.localStorage) { return; } else { - var roster = window.localStorage.getItem("roster"); - roster = roster ? JSON.parse(roster) : []; + var r = window.localStorage.getItem("roster"); + try { + r = JSON.parse(r); + } catch (err) { + r = []; + } //if (!$.isArray(roster)) // roster = []; - return roster; + return r; } } diff --git a/main.js b/main.js index 09cb83d..5744e29 100644 --- a/main.js +++ b/main.js @@ -555,7 +555,6 @@ function StrToIntDef(s, def) { $(document).ready(FormCreate); -$(document).ready(GoButtonClick); function WinSpell() { @@ -1019,51 +1018,45 @@ function FormCreate() { TaskBar.reposition(0); ExpBar.reposition(0); EncumBar.reposition(0); + + LoadCharacter(); } -function TMainForm_RollCharacter() { - var reuslt = true; - for (;;) { - if (! NewGuyForm.Go()) - return false; - Put(Traits, 'Name', NewGuyForm.Name.Text); - if (FileExists(GameSaveName()) && - (mrNo = MessageDlg('The saved game "' + GameSaveName() + '" already exists. Do you want to overwrite it?', mtWarning, [mbYes,mbNo], 0))) { - // go around again - 0; - } else { - f = FileCreate(GameSaveName()); - if (f == -1) { - ShowMessage("The thought police don't like the name '" + GameSaveName() + "'. Choose a name without \\ / : * ? \" < > || | in it."); - } else { - FileClose(f); - break; - } +function LoadCharacter() { + var result = true; + var name = window.location.href.split('#')[1]; + var games = loadRoster(); + var ch; + $.each(games, function (index,sheet) { + if (sheet.name === name) { + ch = sheet; + return true; } + }); + if (!ch.level) { + ch.level = 1; + ch['HP Max'] = Random(8) + ch.CON.div(6); + ch['MP Max'] = Random(8) + ch.INT.div(6); + ch.equips = {'Weapon': 'Sharp Stick'}; } + Put(Traits, 'Name', ch.name); + Put(Traits, 'Level', ch.level); + Put(Traits, 'Race', ch.race); + Put(Traits, 'Class', ch['class']); + Put(Stats, 'STR', ch.STR); + Put(Stats, 'CON', ch.CON); + Put(Stats, 'DEX', ch.DEX); + Put(Stats, 'INT', ch.INT); + Put(Stats, 'WIS', ch.WIS); + Put(Stats, 'CHA', ch.CHA); + Put(Stats, 'HP Max', ch['HP Max']); + Put(Stats, 'MP Max', ch['MP Max']); + Put(Equips, 'Weapon', ch.equips.Weapon); + Put(Inventory,'Gold',0); + ClearAllSelections(); - /* TODO: something like this - with (NewGuyForm) { - Put(Traits,'Name',Name.Text); - Put(Traits,'Race',Race[Race.ItemIndex]); - Put(Traits,'Class',Klass[Klass.ItemIndex]); - Put(Traits,'Level',1); - Put(Stats,'STR',STR.Tag); - Put(Stats,'CON',CON.Tag); - Put(Stats,'DEX',DEX.Tag); - Put(Stats,'INT',INT.Tag); - Put(Stats,'WIS',WIS.Tag); - Put(Stats,'CHA',CHA.Tag); - Put(Stats,'HP Max',Random(8) + CON.Tag.div(6)); - Put(Stats,'MP Max',Random(8) + INT.Tag.div(6)); - Put(Equips,'Weapon','Sharp Stick'); - Put(Inventory,'Gold',0); - ClearAllSelections(); - GoButtonClick(NewGuyForm); - } -*/ - return true; + GoButtonClick(); } diff --git a/newguy.js b/newguy.js index fb33d50..8fe4963 100644 --- a/newguy.js +++ b/newguy.js @@ -144,7 +144,8 @@ function sold() { $.each('STR,CON,DEX,INT,WIS,CHA'.split(','), function (index,value) { newguy[value] = stats[value]; }); - addToRoster(JSON.stringify(newguy)); + alert(JSON.stringify(newguy)); + addToRoster(newguy); } function GenClick() { diff --git a/roster.html b/roster.html index 1aebcb8..07c7c06 100644 --- a/roster.html +++ b/roster.html @@ -1,43 +1,17 @@ + + +Progress Quest X/web - Roster + + +
Here it goes
- - - \ No newline at end of file + + + + diff --git a/roster.js b/roster.js new file mode 100644 index 0000000..9ec819e --- /dev/null +++ b/roster.js @@ -0,0 +1,40 @@ +$(document).ready(function () { + + var games; + + function load() { + var roster = $("#roster"); + roster.empty(); + roster.text("Loading..."); + + games = loadRoster() + if (!games) { + roster.text("Err...no local storage"); + return; + } + $.each(games, function (index, c) { + var p = $("
"); + p.appendTo(roster); + p.text(c.name); + var a = $("", {href: "main.html#" + c.name}) + a.text(c.name); + a.appendTo(p); + p = $("
"); + p.appendTo(roster); + p.text("Level " + (c.level || 1) + " " + c.race + " " + c['class']); + }); + } + + load(); + + $("#test").click(function () { + games.push({testy:"cul", "name":"betsy"}); + storeRoster(games); + load(); + }); + + $("#clear").click(function () { + storeRoster([]); + load(); + }); +});