/
settlers.html
446 lines (394 loc) · 12.4 KB
/
settlers.html
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<html lang="en">
<head>
<title>walk</title>
<style>
body{
padding: 0;
margin: 0;
background-color: #000;
color: #FFF;
}
header{
text-align: center;
height: 70px;
width: 100%;
font-size: 1.5em;
font-family: Georgia, "Georgia", serif;
background: #361016; /* Old browsers */
background: -moz-linear-gradient(top, #361016 0%, #3f1319 12%, #48161d 25%, #330f14 39%, #1f090c 50%, #000000 51%, #0c0405 60%, #1f090c 76%, #140608 91%, #0e0406 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#361016), color-stop(12%,#3f1319), color-stop(25%,#48161d), color-stop(39%,#330f14), color-stop(50%,#1f090c), color-stop(51%,#000000), color-stop(60%,#0c0405), color-stop(76%,#1f090c), color-stop(91%,#140608), color-stop(100%,#0e0406)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #361016 0%,#3f1319 12%,#48161d 25%,#330f14 39%,#1f090c 50%,#000000 51%,#0c0405 60%,#1f090c 76%,#140608 91%,#0e0406 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #361016 0%,#3f1319 12%,#48161d 25%,#330f14 39%,#1f090c 50%,#000000 51%,#0c0405 60%,#1f090c 76%,#140608 91%,#0e0406 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #361016 0%,#3f1319 12%,#48161d 25%,#330f14 39%,#1f090c 50%,#000000 51%,#0c0405 60%,#1f090c 76%,#140608 91%,#0e0406 100%); /* IE10+ */
background: linear-gradient(top, #361016 0%,#3f1319 12%,#48161d 25%,#330f14 39%,#1f090c 50%,#000000 51%,#0c0405 60%,#1f090c 76%,#140608 91%,#0e0406 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#361016', endColorstr='#0e0406',GradientType=0 ); /* IE6-9 */
}
header h1{
display: inline;
}
#holder {
width: 800px;
height: 600px;
margin: 0px auto;
}
#playerscores{
float: left;
width: 200px;
height: 100%;
margin: 20px auto;
}
#chat{
font: Georgia, Helvetica, serif;
float: right;
padding: 10px;
border: 1px solid #999;
width: 200px;
height: 80%;
border-radius: 5px;
}
#chat > h3 {
border-bottom: 1px solid #999;
}
#msg{
margin-top:10%;
height: 80%;
margin-bottom: 10%
overflow: scroll;
overflow-x: hidden;
word-wrap: break-word;
scrollbar-base-color: #CCCCCC;
scrollbar-arrow-color: #000000;
scrollbar-track-color: #999999;
scrollbar-3dlight-color: #EEEEEE;
scrollbar-highlight-color: #FFFFFF;
scrollbar-face-color: #CCCCCC;
scrollbar-shadow-color: #999999;
scrollbar-darkshadow-color: #666666;
}
#chatfooter{
position: absolute
bottom: 0px;
right: 0px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="http://cloud.github.com/downloads/processing-js/processing-js/processing-1.3.6.min.js"></script>
<script src="/nowjs/now.js"></script>
<script>
function disableDraggingFor(element) {
// this works for FireFox and WebKit in future according to http://help.dottoro.com/lhqsqbtn.php
element.draggable = false;
// this works for older web layout engines
element.onmousedown = function(event) {
event.preventDefault();
return false;
};
}
$(document).ready(function(){
disableDraggingFor($("#holder"));
//I have a local "now" variable that holds both information about this current instance and holds the server's instance. 'now.name' refers to THIS INSTANCE's name. Some of the fields in 'now' are local to me, while some of them are actually located in the server. The abstraction that now provides is to gloss over that previous fact, so you can write code that refers to the server with the same syntax as a local field.
now.name = prompt("What's your name?", "");
now.ready(function() {
console.log("now is ready!");
ready = true;
setupNow();
app().loop();
app().loaded();
});
now.updateBlock = function(){
$("#playerscores").empty();
for (var thing in players) {
var player = players[thing];
var playerdiv = $("<div></div>");
var brains = $("<div></div>");
var babies = $("<div></div>");
var kittens = $("<div></div>");
var souls = $("<div></div>");
var hearts = $("<div></div>");
$("#playerscores").append(playerdiv);
$("#playerscores").append(brains);
$("#playerscores").append(babies);
$("#playerscores").append(kittens);
$("#playerscores").append(souls);
$("#playerscores").append(hearts);
playerdiv.text( player.name + ":");
brains.text( "brains: " + player.res.brains );
babies.text( "babies: " + player.res.babies);
kittens.text( "kittens: " + player.res.kittens);
souls.text( "souls: " + player.res.souls);
hearts.text( "hearts: " + player.res.hearts);
/* + players[player].cards.victorypoints + " victory points, " + players[player].cards.clerics + " clerics, " + players[player].cards.greed + " demons of greed, " + players[player].cards.epidemic + " epidemics, " + players[player].cards.vampires + " vampire buddies. " + players[player].victorypoints + " victory points.");*/
};
};
now.receiveMessage = function(name, msg){
$("<div></div>").text(name + ": " + msg).appendTo("#msg");
$("#msg").animate({ scrollTop: $("#msg").attr("scrollHeight") }, 3000);
};
$("#send-button").click(function(){
now.distributeMessage($("#text-input").val());
$("#text-input").val("");
});
$("#text-input").keyup(function(event){
if(event.keyCode == 13){
$("#send-button").click();
}
});
});
function app() { return Processing.instances[0]; }
function attemptTrade(who, give, get) {
//Trade is of the form {name: "trade", (optionally doIt:"no"/"yes"), who: "zxcv", give: <resbundle>, get: <resbundle>}
return {"name": "trade",
"who": who,
"give": give,
"get": get}
}
function shrine(shrine) {
return {"name": "shrine",
"shrine": shrine};
}
</script>
</head>
<body>
<header>The <h1>Settlers</h1> of <h1>Satan</h1></header>
<div id="chat">
<h3>Chat</h3>
<div id="msg"></div>
<div id="chatfooter">
<input type="text" id="text-input">
<input type="button" value="Send" id="send-button">
</div>
</div>
<div id="playerscores"></div>
<div id="holder">
<script>
function callMe(str) {
console.log("called me! "+str);
}
function build(type) {
return {"name": "build",
"type": type};
}
//Acceptable values are "chamber", "cesspool", "trail", "devcard"
function cost(type) {
var costs = {chamber: {babies:1, brains:1, souls:1, hearts:1},
cesspool: {babies:2, kittens:3},
trail: { souls:1, hearts:1},
devcard: {babies:1, brains:1, kittens:1}
};
return costs[type];
}
/**
Returns true if the given resource bundle res has more than the resource bundle of cost.
*/
function afford(res, cost) {
for(var thing in cost) {
if(res[thing] < cost[thing]) return false;
}
return true;
}
function setupNow() {
console.log("setupNow()!");
now.getAction = function (json) {
// console.log("getAction called with ");
// console.log(json);
switch(json["name"]) {
case "build": //Someone (not necessarily you) is building something.
console.log("-----building!------");
players[json.owner].build(json.type);
break;
case "buy": //Someone (not necessarily you) is buying something. This can be a build, or a devcard.
break;
case "trade": //Someone is trading.
if(now.name == json.owner)
players[json.owner].tradeGive(json);
else if(now.name == json.who)
players[json.who].tradeRecieve(json);
break;
case "devcard": //Someone is playing a devcard
players[json.owner].playDevcard(json);
break;
}
}
now.updatePlayer = function (name, attrs) {
app().updatePlayer(name, attrs);
}
}
function attributes(p) {
var k = {"res": p.res, "buildings": p.buildings, "name": p.name};
return k;
}
function me() { return players[now.name]; }
callMe("hihihihi");
var players = {};
</script>
<script type="application/processing">
class Player {
var res = {babies:10, brains:10, souls:10, hearts:10, kittens:10};
var buildings = {chamber:0, cesspool:0, trail:0};
String name;
Player(String name) {
this.name = name;
}
void points() {
return buildings.chamber + buildings.cesspool*2;
}
//Acceptable values of btype are "chamber", "trail", and "cesspool"
void build(btype) {
console.log(name+" is building a "+btype+"!");
payResources(cost(btype));
buildings[btype]++;
}
void getResources(resources) {
console.log(now.name+": got resources: ");
for(r in resources) console.log("\t"+r+": "+resources[r]);
for(var thing in resources) {
res[thing] += resources[thing];
}
}
void payResources(cost) {
console.log(now.name+": paid resources: ");
for(r in cost) console.log("\t"+r+": "+cost[r]);
for(var thing in cost) {
res[thing] -= cost[thing];
}
}
// Trade is of the form {name: "trade", (optionally doIt:"no"/"yes"), who: "zxcv", give: <resbundle>, get: <resbundle>}
void makeTrade(trade) {
console.log("making trade: ");
console.log(trade);
console.log("Caller: " + arguments.callee.caller.name);
if(trade.who == now.name) {
tradeRecieve(trade);
} else if(trade.owner == now.name) {
tradeGive(trade);
}
}
// Handles trade requests where you're on the "recieving" end (e.g. you are the target of a trade).
void tradeRecieve(trade) {
console.log("trade being received! " + arguments.callee.caller.name);
if(!trade.doIt) { //you just got the request
var doIt = prompt("Make trade?");
if(doIt != 'yes') doIt = 'no';
now.takeAction($.extend(trade, {"doIt": doIt}));
} else if(trade.doIt == 'yes') { //you just accepted
payResources(trade.get);
getResources(trade.give);
}
}
//Handles trade requests where you're on the giving end (e.g. you are the initiator of a trade).
void tradeGive(trade) {
console.log("trade being given! " + arguments.callee.caller.name);
if(trade.doIt == 'yes') {
payResources(trade.give);
getResources(trade.get);
}
else if(trade.doIt == 'no') {
alert(trade.who+" cancelled the trade.");
}
}
void render() {
String resString = name+"\n";
for(var idx in res) {
resString += idx+": "+res[idx]+"\n";
}
resString += "\n\nPoints: "+points();
text(resString, 0, 0);
String bString = "";
for(var idx in buildings) {
bString += idx+": "+buildings[idx]+"\n";
}
text(bString, 120, 0);
now.updateBlock(players);
}
}
void loaded() {
now.broadcastAttributes(attributes(new Player(now.name)));
}
void showSelectPrompt(num, string, filter) {
if((typeof filter) == 'undefined') {
filter = function (x) { return true; }
}
alert("Please select "+n+" cards "+string+"!");
}
void discardHalf() {
for(var playerName in players) {
var player = players[playerName];
var cardCount = 0;
for(var resName in player.res) cardCount += player.res[resName];
if(cardCount > 7) {
showDiscardPrompt(Math.floor(cardCount/2));
}
}
}
void showDiscardPrompt(num) {
alert("Discarding "+num+" cards");
}
/**
Sets players[name] to have the given attributes; players[name] may be null beforehand.
*/
void updatePlayer(name, attrs) {
if(!players[name]) {
console.log("Newly instantiating "+name+"!");
players[name] = new Player(name);
}
// console.log("attrs: ");
// for(n in attrs) console.log("\t"+n+": "+attrs[n]);
$.extend(players[name], attrs);
}
void setup() {
console.log("Processing setup!");
size(800, 600);
noLoop();
}
void draw() {
if(!me()) return;
background(map(me().points(), 0, 10, 0, 124), 32, 32);
noFill(); stroke(255, 0, 0); strokeWeight(5); strokeJoin(ROUND);
beginShape();
for(int i = 0; i < 5; i++) {
float angle = -PI/2 + 2*TWO_PI/5*i;
vertex(width/2 + 200 * cos(angle), height/2 + 200*sin(angle));
}
endShape(CLOSE);
strokeWeight(1);
textAlign(CENTER, TOP);
textSize(20);
text(now.name, width/2, 10);
textSize(12);
stroke(255);
textAlign(CENTER, CENTER);
line(width/2, 0, width/2, height);
text("BUILD TORTURE CHAMBER", width/4, height/2);
text("TRADE", 3*width/4, height/2);
textAlign(LEFT, TOP);
for(var idx in players) {
players[idx].render();
translate(0, 200);
}
if(me() != null)
now.broadcastAttributes(attributes(me()));
}
void mouseClicked() {
if(mouseX < width/2) {
var type = "chamber";
if(afford(me().res, cost(type))) {
now.takeAction(build(type));
}
} else {
var who = prompt("Who do you want to trade with?");
if(!players[who]) alert(who+" isn't a player!");
if(players[who].name == me().name){
alert("You can't trade with yourself, silly!");
return;
}
var give = {babies: 1};
var get = {kittens: 1};
var trade = attemptTrade(who, give, get);
now.takeAction(trade);
}
}
</script>
<canvas id="canvas" style="border:5px solid black;"></canvas>
</div>
</body>
</html>