summaryrefslogtreecommitdiff
path: root/orig/pq-web/sim.js
blob: a6e497667fafe5e9a8aaa84ce07788e531cd2f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// This is an in-console simulation of Progress Quest. It's rather a
// hack job.
//
// Run it with node.js like so:
//   $ node sim.js
// or using a modified v8 shell, like so:
//   $ git clone git://github.com/grumdrig/v8
//   $ (cd v8; scons sample=shell)
//   $ v8/shell sim.js
//
// It's not realtime - simulation runs at maximum speed and the
// virtual time elapsed is displayed at each level-up.

var CHARACTER = "Shienzid";
//var guy = window.location.href.split("#")[1];

var window = {
  location: {href: "#Woogle"},

  localStorage: {
    items: null,

    getItem: function(key) {
      if (!this.items) {
        try {
          this.items = JSON.parse(read("local.storage"));
        } catch (e) {
          print(e);
          this.items = {};
        }
      }
      return this.items[key];
    },
  
    setItem: function (key, value) {
      this.items[key] = value;
      write("local.storage", JSON.stringify(this.items));
    },

    removeItem: function (key) {
      delete this.items[key];
      write("local.storage", JSON.stringify(this.items));
    }
  },
};

var navigator = { userAgent: "v8" };

var location = window.location;

var $ = function () { return null; };
$.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 document = null;

var fs = require("fs");
var load = function (filename) {
  var content = fs.readFileSync(filename);
  require("vm").runInThisContext(content, filename);
  //global.eval.call(global, String(content));
};
var print = function () {
    args = Array.prototype.slice.call(arguments);
    console.log(args.join(" "));
};
var read = function (f) { return fs.readFileSync(f); };
var write = function (f,c) { fs.writeFileSync(f,c); };

global.window = window;
global.document = document;
global.navigator = navigator;
global.$ = $;

var alert = global.alert = function (m) { print("ALERT: " + m); };

var now = 0;
var timers = [{}];
var setInterval  = global.setInterval = function (callback, interval) {
  timers.push({callback:callback, interval:interval});
  return timers.length-1;
};
var setTimeout = global.setTimeout = setInterval;  // TODO: distinguish!

load("config.js");

var cs = 0;
storage.loadRoster(function (cs) { for (var c in cs) cs++; });
print(cs, "characters");


var timeGetTime = global.timeGetTime = function () {
  return now;
}

load("main.js");
FormCreate();

function charsheet(game) {
  print(game.Traits.Name, 
        game.Traits.Level,
        game.tasks,
        RoughTime(game.elapsed));
}

// It takes 18 sec to simulate 18 hours of play when I just checked (to 
// level 10)

for (var j = 1, t = 0; j < 1001; ++j) {
  t += LevelUpTime(j);
  if (j % 100 == 0)
    print(j, RoughTime(LevelUpTime(j))+",", RoughTime(t));
}

var tmpl = read("charsheet.txt");
storage.loadSheet(CHARACTER, function (sheet) {
  if (!sheet) {
    load("newguy.js");
    NewGuyFormLoad();
    traits.Name = CHARACTER;
    sold();
    sheet = storage.games[CHARACTER];
  }
  //write("local.storage", JSON.stringify(window.localStorage.items));

  game = sheet;
  LoadGame(game);
  print(template(''+tmpl, sheet));

  var l = 0;
  for (var i = 0; i < 1000000000; ++i) {
    if (game.Traits.Level != l) {
      SaveGame();
      charsheet(game);
      l = game.Traits.Level;
      //if (l >= 5) break;
    }
    //assert(timers.length == 2);// TODO: this is for simplicity
    now += timers[1].interval;
    timers[1].callback();
  }
});