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