-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecon.js
More file actions
144 lines (131 loc) · 3.32 KB
/
econ.js
File metadata and controls
144 lines (131 loc) · 3.32 KB
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
var city = [{
"name": "New York",
"pop": 1000000,
"popprod": 0,
"popconsum": 0,
"popprice": 5,
"food": 1000000,
"foodprod": 1000000,
"foodconsum": 1000000,
"foodprice": 1
}, {
"name": "Los Angeles",
"pop": 1000000,
"popprod": 0,
"popconsum": 0,
"popprice": 5,
"food": 1000000,
"foodprod": 1000000,
"foodconsum": 1000000,
"foodprice": 1
}, {
"name": "Dallas",
"pop": 1000000,
"popprod": 0,
"popconsum": 0,
"popprice": 5,
"food": 1000000,
"foodprod": 1000000,
"foodconsum": 1000000,
"foodprice": 1
}, {
"name": "Chicago",
"pop": 1000000,
"popprod": 0,
"popconsum": 0,
"popprice": 5,
"food": 1000000,
"foodprod": 1000000,
"foodconsum": 1000000,
"foodprice": 1
}, {
"name": "Atlanta",
"pop": 1000000,
"popprod": 0,
"popconsum": 0,
"popprice": 5,
"food": 1000000,
"foodprod": 1000000,
"foodconsum": 1000000,
"foodprice": 1
}];
var date = new Date(2030, 2, 7);
function start() {
$("#date").text(date.toString());
drawPriceTable();
println("Let's go! You can ship food around. That's all you can do right now.");
mainMenu();
}
function drawPriceTable() {
$("#prices tr").remove();
$("#prices")
.append(
"<tr><th>City</th><th>Pop.</th><th>Food</th><th>Food Price</th></tr>");
$.each(city, function (i, c) {
$("#prices").append(
"<tr id=" + c.name + "><td>" + c.name + "</td><td>" + c.pop
+ "</td><td>" + c.food + "</td><td>" + c.foodprice
+ "</td></tr>");
})
}
function incDate(date, amount) {
var tmpDate = new Date(date);
tmpDate.setDate(tmpDate.getDate() + amount)
return tmpDate;
}
function timePasses() {
// food
$.each(city, function (i, c) {
c.food = c.food + c.foodprod - c.pop;
var foodpricemodifier;
if (c.pop === c.food) {
foodpricemodifier = 0;
} else {
foodpricemodifier = (c.pop / c.food) - 1;
}
c.foodprice = c.foodprice + foodpricemodifier;
})
// pop
println("Time passes....");
date = incDate(date, 1);
$("#date").text(date.toString());
drawPriceTable();
}
function getCity(name) {
var toReturn;
$.each(city, function (i, c) {
if (c.name === name) {
toReturn = c;
return false;
}
})
return toReturn;
}
function doShipment() {
selectedCity = $("#shipFrom :selected").text();
c = getCity(selectedCity);
c.food = c.food - 10;
selectedCity = $("#shipTo :selected").text();
c = getCity(selectedCity);
c.food = c.food + 10;
timePasses();
}
function mainMenu() {
$("#menu").empty();
$("#menu")
.append(
"<li><button onclick=\"doShipment()\">Ship!</button> - From: <select id=\"shipFrom\"/> - To: <select id=\"shipTo\"/>");
$("#menu").append(
"<li><button onclick=\"timePasses()\">End Turn.</button></li>");
genCityDropDown($("#shipFrom"));
genCityDropDown($("#shipTo"));
}
function genCityDropDown(element) {
$.each(city, function (i, c) {
element.append("<option value=" + c.name + ">" + c.name + "</option>");
})
}
function println(line) {
$("#output").append(line).append("<br>");
$("#output").scrollTop = $("#output").scrollHeight;
}