Allow v8 to simulate the game outside the browser. A little hacky

still but functional.
This commit is contained in:
Eric Fredricksen
2010-07-14 22:00:26 -07:00
parent 3bd9e90edc
commit 7e9648e192
3 changed files with 179 additions and 95 deletions
+49 -29
View File
@@ -405,8 +405,10 @@ function ProgressBar(id, tmpl) {
game[this.id].hint = template(this.tmpl, game[this.id]); game[this.id].hint = template(this.tmpl, game[this.id]);
// Update UI // Update UI
this.bar.css("width", 100 * this.Position() / this.Max() + "%"); if (this.bar) {
this.bar.parent().find(".hint").text(game[this.id].hint); this.bar.css("width", 100 * this.Position() / this.Max() + "%");
this.bar.parent().find(".hint").text(game[this.id].hint);
}
}; };
this.increment = function (inc) { this.increment = function (inc) {
@@ -436,12 +438,12 @@ function Value(tr) {
function ListBox(id, columns, fixedkeys) { function ListBox(id, columns, fixedkeys) {
this.id = id; this.id = id;
//this.box = $("#"+ id);
this.box = $("tbody#_, #_ tbody".replace(/_/g, id)); this.box = $("tbody#_, #_ tbody".replace(/_/g, id));
this.columns = columns; this.columns = columns;
this.fixedkeys = fixedkeys; this.fixedkeys = fixedkeys;
this.AddUI = function (caption) { this.AddUI = function (caption) {
if (!this.box) return;
var tr = $("<tr><td><input type=checkbox disabled> " + var tr = $("<tr><td><input type=checkbox disabled> " +
caption + "</td></tr>"); caption + "</td></tr>");
tr.appendTo(this.box); tr.appendTo(this.box);
@@ -450,10 +452,12 @@ function ListBox(id, columns, fixedkeys) {
}; };
this.ClearSelection = function () { this.ClearSelection = function () {
this.box.find("tr").removeClass("selected"); if (this.box)
this.box.find("tr").removeClass("selected");
}; };
this.PutUI = function (key, value) { this.PutUI = function (key, value) {
if (!this.box) return;
var item = this.rows().filter(function (index) { var item = this.rows().filter(function (index) {
return Key(this) === key; return Key(this) === key;
}); });
@@ -468,13 +472,23 @@ function ListBox(id, columns, fixedkeys) {
}; };
this.scrollToTop = function () { this.scrollToTop = function () {
this.box.parents(".scroll").scrollTop(0); if (this.box)
this.box.parents(".scroll").scrollTop(0);
}; };
this.rows = function () { this.rows = function () {
return this.box.find("tr").has("td"); return this.box.find("tr").has("td");
}; };
this.CheckAll = function (butlast) {
if (this.box) {
if (butlast)
this.rows().find("input:checkbox").not(':last').attr("checked","true");
else
this.rows().find("input:checkbox").attr("checked","true");
}
};
this.length = function () { this.length = function () {
return (this.fixedkeys || game[this.id]).length; return (this.fixedkeys || game[this.id]).length;
}; };
@@ -529,7 +543,8 @@ function StrToIntDef(s, def) {
} }
$(document).ready(FormCreate); if (!navigator.v8)
$(document).ready(FormCreate);
function WinSpell() { function WinSpell() {
@@ -632,7 +647,7 @@ function CompleteQuest() {
QuestBar.reset(50 + RandomLow(1000)); QuestBar.reset(50 + RandomLow(1000));
if (Quests.length()) { if (Quests.length()) {
Log('Quest completed: ' + game.bestquest); Log('Quest completed: ' + game.bestquest);
Quests.rows().find("input:checkbox").attr("checked", "true"); Quests.CheckAll();
[WinSpell,WinEquip,WinStat,WinItem][Random(4)](); [WinSpell,WinEquip,WinStat,WinItem][Random(4)]();
} }
while (Quests.length() > 99) while (Quests.length() > 99)
@@ -749,7 +764,7 @@ function toArabic(s) {
} }
function CompleteAct() { function CompleteAct() {
Plots.rows().find("input:checkbox").attr("checked", "true"); Plots.CheckAll();
game.act += 1; game.act += 1;
PlotBar.reset(60 * 60 * (1 + 5 * game.act)); // 1 hr + 5/act PlotBar.reset(60 * 60 * (1 + 5 * game.act)); // 1 hr + 5/act
Plots.AddUI((game.bestplot = 'Act ' + toRoman(game.act))); Plots.AddUI((game.bestplot = 'Act ' + toRoman(game.act)));
@@ -770,9 +785,10 @@ function Log(line) {
} }
function Task(caption, msec) { function Task(caption, msec) {
Kill.text(caption + '...'); game.kill = caption + "...";
Log(Kill.text()); if (Kill)
game.kill = Kill.text(); Kill.text(game.kill);
Log(game.kill);
TaskBar.reset(msec); TaskBar.reset(msec);
} }
@@ -860,7 +876,7 @@ function Timer1Timer() {
ClearAllSelections(); ClearAllSelections();
if (Kill.text() == 'Loading....') if (game.kill == 'Loading....')
TaskBar.reset(0); // Not sure if this is still the ticket TaskBar.reset(0); // Not sure if this is still the ticket
// gain XP / level up // gain XP / level up
@@ -924,13 +940,25 @@ function FormCreate() {
AllLists = [Traits,Stats,Spells,Equips,Inventory,Plots,Quests]; AllLists = [Traits,Stats,Spells,Equips,Inventory,Plots,Quests];
Kill = $("#Kill"); if (!navigator.v8) {
Kill = $("#Kill");
$("#quit").click(quit); $("#quit").click(quit);
$(document).keypress(FormKeyDown);
$(document).bind('beforeunload', function () {
if (!storage)
return "Are you sure you want to quit? All your progress will be lost!";
});
$(window).unload(function (event) {
StopTimer();
SaveGame();
});
}
LoadCharacter(); LoadCharacter();
$(document).keypress(FormKeyDown);
} }
@@ -958,7 +986,8 @@ function LoadCharacter() {
LoadGame(sheet); LoadGame(sheet);
$("#title").text("Progress Quest - " + GameSaveName()); if (!navigator.v8)
$("#title").text("Progress Quest - " + GameSaveName());
if (window.location.href.indexOf("?quit#") > 0) if (window.location.href.indexOf("?quit#") > 0)
quit(); // TODO: cheesy quit(); // TODO: cheesy
@@ -1078,10 +1107,11 @@ function LoadGame(sheet) {
game = sheet; game = sheet;
randseed(game.seed); randseed(game.seed);
$.each(AllBars.concat(AllLists), function (i, e) { e.load(game); }); $.each(AllBars.concat(AllLists), function (i, e) { e.load(game); });
Kill.text(game.kill); if (Kill)
Kill.text(game.kill);
ClearAllSelections(); ClearAllSelections();
$.each([Plots,Quests], function () { $.each([Plots,Quests], function () {
this.rows().find("input:checkbox").not(':last').attr("checked", "true"); this.CheckAll(true);
}); });
Log('Loaded game: ' + game.Traits.Name); Log('Loaded game: ' + game.Traits.Name);
if (!game.elapsed) if (!game.elapsed)
@@ -1089,16 +1119,6 @@ function LoadGame(sheet) {
StartTimer(); StartTimer();
} }
$(document).bind('beforeunload', function () {
if (!storage)
return "Are you sure you want to quit? All your progress will be lost!";
});
$(window).unload(function (event) {
StopTimer();
SaveGame();
});
function GameSaveName() { function GameSaveName() {
if (!game.saveName) { if (!game.saveName) {
game.saveName = Get(Traits, 'Name'); game.saveName = Get(Traits, 'Name');
+55 -40
View File
@@ -3,7 +3,8 @@
function Roll(stat) { function Roll(stat) {
stats[stat] = 3 + Random(6) + Random(6) + Random(6); stats[stat] = 3 + Random(6) + Random(6) + Random(6);
$("#"+stat).text(stats[stat]); if (!navigator.v8)
$("#"+stat).text(stats[stat]);
return stats[stat]; return stats[stat];
} }
@@ -17,15 +18,17 @@ function Choose(n, k) {
return result / d; return result / d;
} }
var stats = {"history":[]}; var stats = {};
var traits = {};
var total = 0;
var seedHistory = [];
function RollEm() { function RollEm() {
var Total = $("#Total");
stats.seed = randseed(); stats.seed = randseed();
stats.total = 0; total = 0;
var best = -1; var best = -1;
$.each(K.PrimeStats, function () { $.each(K.PrimeStats, function () {
stats.total += Roll(this); total += Roll(this);
if (best < stats[this]) { if (best < stats[this]) {
best = stats[this]; best = stats[this];
stats.best = this; stats.best = this;
@@ -33,27 +36,31 @@ function RollEm() {
}); });
stats['HP Max'] = Random(8) + stats.CON.div(6); stats['HP Max'] = Random(8) + stats.CON.div(6);
stats['MP Max'] = Random(8) + stats.INT.div(6); stats['MP Max'] = Random(8) + stats.INT.div(6);
Total.text(stats.total);
var color = var color =
(stats.total >= (63+18)) ? 'red' : (total >= (63+18)) ? 'red' :
(stats.total > (4 * 18)) ? 'yellow' : (total > (4 * 18)) ? 'yellow' :
(stats.total <= (63-18)) ? 'grey' : (total <= (63-18)) ? 'grey' :
(stats.total < (3 * 18)) ? 'silver' : (total < (3 * 18)) ? 'silver' :
'white'; 'white';
Total.css("background-color", color);
$("#Unroll").attr("disabled", !stats.history.length); if (!navigator.v8) {
var Total = $("#Total");
Total.text(total);
Total.css("background-color", color);
$("#Unroll").attr("disabled", !seedHistory.length);
}
} }
function RerollClick() { function RerollClick() {
stats.history.push(stats.seed); seedHistory.push(stats.seed);
RollEm(); RollEm();
} }
function UnrollClick() { function UnrollClick() {
randseed(stats.history.pop()); randseed(seedHistory.pop());
RollEm(); RollEm();
} }
@@ -61,41 +68,45 @@ function fill(e, a, n) {
var def = Random(a.length); var def = Random(a.length);
for (var i = 0; i < a.length; ++i) { for (var i = 0; i < a.length; ++i) {
var v = a[i].split("|")[0]; var v = a[i].split("|")[0];
var check = def == i ? " checked " : " "; var check = (def == i) ? " checked " : " ";
$("<div><input type=radio id='" + v + "' name=\"" + n + "\" value=\"" + v + "\" " + if (def == i) traits[n] = v;
check +"><label for='" + v + "'>" + v + "</label></div>").appendTo(e); if (!navigator.v8) {
$("<div><input type=radio id='" + v + "' name=\"" + n + "\" value=\"" + v + "\" " +
check +"><label for='" + v + "'>" + v + "</label></div>").appendTo(e);
}
} }
} }
function NewGuyFormLoad() { function NewGuyFormLoad() {
seed = new Alea();
RollEm();
GenClick();
fill("#races", K.Races, "Race"); fill("#races", K.Races, "Race");
fill("#classes", K.Klasses, "Class"); fill("#classes", K.Klasses, "Class");
$("#Reroll").click(RerollClick); if (!navigator.v8) {
$("#Unroll").click(UnrollClick); $("#Reroll").click(RerollClick);
$("#RandomName").click(GenClick); $("#Unroll").click(UnrollClick);
$('#Sold').click(sold); $("#RandomName").click(GenClick);
$('#quit').click(cancel); $('#Sold').click(sold);
$('#quit').click(cancel);
//var caption = 'Progress Quest - New Character'; //var caption = 'Progress Quest - New Character';
//if (MainForm.GetHostName != '') //if (MainForm.GetHostName != '')
// caption = caption + ' [' + MainForm.GetHostName + ']'; // caption = caption + ' [' + MainForm.GetHostName + ']';
if (!$("#Name").text()) {
GenClick();
$("#Name").focus(); $("#Name").focus();
$("#Name").select(); $("#Name").select();
} }
seed = new Alea();
RollEm();
if (window.location.href.indexOf("?sold") > 0) if (window.location.href.indexOf("?sold") > 0)
sold(); // TODO: cheesy sold(); // TODO: cheesy
} }
$(document).ready(NewGuyFormLoad); if (!navigator.v8)
$(document).ready(NewGuyFormLoad);
/* Multiplayer: /* Multiplayer:
function TNewGuyForm_ParseSoldResponse(body) { function TNewGuyForm_ParseSoldResponse(body) {
@@ -147,12 +158,7 @@ function TNewGuyForm_SoldClick() {
function sold() { function sold() {
var newguy = { var newguy = {
Traits: { Traits: traits,
Name: $("#Name").val(),
Race: $("input:radio[name=Race]:checked").val(),
Class: $("input:radio[name=Class]:checked").val(),
Level: 1
},
dna: stats.seed, dna: stats.seed,
seed: stats.seed, seed: stats.seed,
birthday: ''+new Date(), birthday: ''+new Date(),
@@ -185,6 +191,13 @@ function sold() {
] ]
}; };
if (!navigator.v8) {
newguy.Traits.Name = $("#Name").val();
newguy.Traits.Race = $("input:radio[name=Race]:checked").val();
newguy.Traits.Class = $("input:radio[name=Class]:checked").val();
}
newguy.Traits.Level = 1;
newguy.date = newguy.birthday; newguy.date = newguy.birthday;
newguy.stamp = newguy.birthstamp; newguy.stamp = newguy.birthstamp;
@@ -197,14 +210,16 @@ function sold() {
// TODO: cheesy // TODO: cheesy
var args = (window.location.href.indexOf("?sold") > 0) ? "?quit" : ""; var args = (window.location.href.indexOf("?sold") > 0) ? "?quit" : "";
window.location = "main.html" + args + "#" + newguy.Traits.Name; window.location.href = "main.html" + args + "#" + newguy.Traits.Name;
} }
function cancel() { function cancel() {
window.location = "roster.html"; window.location.href = "roster.html";
} }
function GenClick() { function GenClick() {
$("#Name").attr("value", GenerateName()); traits.Name = GenerateName();
if (!navigator.v8)
$("#Name").attr("value", traits.Name);
} }
+74 -25
View File
@@ -6,11 +6,13 @@ function nada() { return {}; }
var document = { var document = {
addEventListener: noop, addEventListener: noop,
createElement: function () { createElement: function () {
return { var result = {
style: {}, style: {},
getElementsByTagName: nada, getElementsByTagName: nada,
appendChild: noop, appendChild: noop,
}; };
result.lastChild = result;
return result;
}, },
documentElement: { documentElement: {
insertBefore: noop, insertBefore: noop,
@@ -24,20 +26,18 @@ var document = {
var window = { var window = {
location: {href: "#Woogle"}, location: {href: "#Woogle"},
localStorage: { localStorage: {
items: {},
getItem: function(key) { getItem: function(key) {
return JSON.stringify({Woogle: { return this.items[key];
Traits: {
Name: "Woogle"
}
}});
}, },
setItem: function (key, value) { setItem: function (key, value) {
this.items[key] = value;
}, },
removeItem: function (key) { removeItem: function (key) {
delete this.items[key];
} }
}, },
document: document, document: document,
@@ -46,8 +46,10 @@ var window = {
var location = window.location; var location = window.location;
/* var v8 = true;
var $ = function () { var $ = function () {
return null;
return { return {
click: noop, click: noop,
keypress: noop, keypress: noop,
@@ -61,30 +63,77 @@ var $ = function () {
select: noop, select: noop,
}; };
}; };
$.each = function (collection, callback) { $.isFunction = function (obj) {
isArray: function( obj ) { return toString.call(obj) === "[object Function]";
return toString.call(obj) === "[object Array]"; },
}, $.isArray = function (obj) {
return toString.call(obj) === "[object Array]";
}
$.each = function (object, callback) {
var name, i = 0,
length = object.length,
isObj = length === undefined || $.isFunction(object);
if (isObj) {
for (name in object) {
if (callback.call(object[name], name, object[name]) === false)
break;
}
} else {
for (var value = object[0];
i < length && callback.call(value, i, value) !== false;
value = object[++i]) { }
}
return object;
}
var navigator = {
v8: true,
userAgent: "V8 Shell"
}; };
*/
var navigator = { userAgent: "V8 Shell" };
var alert = function (m) { print("ALERT: " + m); }; var alert = function (m) { print("ALERT: " + m); };
function loadSheet(name) {
return { name: "bob" };
}
var storage = {
removeItem: function () {}
};
var randseed = function () {}; var randseed = function () {};
try {
window.localStorage.items = JSON.parse(read("local.storage"));
} catch (e) {
print(e);
window.localStorage.items = {};
}
load("jquery.js"); var now = 0;
var $ = window.$; var timers = [{}];
function setInterval(callback, interval) {
timers.push({callback:callback, interval:interval});
return timers.length-1;
}
//load("jquery.js");
//var $ = window.$;
load("config.js"); load("config.js");
load("newguy.js"); load("newguy.js");
//NewGuyFormLoad(); NewGuyFormLoad();
sold();
write("local.storage", JSON.stringify(window.localStorage.items));
var guy = window.location.href.split("#")[1];
load("main.js"); load("main.js");
//FormCreate(); FormCreate();
timeGetTime = function () {
return now;
}
for (var i = 0; i < 1000; ++i) {
//assert(timers.id == 1);// TODO: this is for simplicity
now += timers[1].interval;
timers[1].callback();
}
SaveGame();
print(guy, game.elapsed, game.tasks, game.task);//JSON.stringify(loadSheet(guy)));