-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheconomy-sim.html
150 lines (148 loc) · 5.29 KB
/
economy-sim.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
<!DOCTYPE html>
<html>
<head>
<title>Economy Simulator - Lame City</title>
<meta charset="UTF-8">
<meta name="description" content="monetary unit exchanges; testing a system for the actual game"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style></style>
</head>
<body>
<p>you have <strong id="mudisplay"></strong> monetary unit(s). you earn <strong id="rate"></strong> MU / min. population: <strong id="pop"></strong></p>
<p>you can beg in the nearby cities to get some free MU <button id="beg">beg</button></p>
<h2>shop</h2>
<dl>
<dt>haus simple - 100 MU <button class="purchase" data-buys="simpleHouse" data-price="100" data-ppl="2">purchase</button></dt>
<dd>good starter house - stores 2 people</dd>
<dt>domus kompleks - 500 MU <button class="purchase" data-buys="complexHouse" data-price="500" data-ppl="4">purchase</button></dt>
<dd>fits some more people - 4 people can easily fit in it</dd>
</dl>
<h2>ur city</h2>
<ul id="inv"></ul>
<script>
const muDisplay = document.querySelector("#mudisplay"),
rateDisplay = document.querySelector("#rate"),
inventoryList = document.querySelector("#inv"),
populationDisplay = document.querySelector("#pop"),
begBtn = document.querySelector("#beg"),
maleNames = ["Bob", "Sam", "Michael", "Bill", "William", "George", "Charles"],
girlNames = ["Sally", "Linda", "Alice", "Julia", "Christina", "Monica"];
let monetaryUnits = 150,
inventory = {},
muPerMin = 0,
people = [],
maxPeople = 0;
function updateDisplay() {
muDisplay.textContent = monetaryUnits;
rate.textContent = muPerMin;
populationDisplay.textContent = people.length;
let innerHTML = "";
for (let building in inventory) {
innerHTML += `<li>${inventory[building]}x ${building}</li>`;
}
innerHTML += `<li><strong>Your people:</strong> ${people.map(person => person.name).join(", ") || "nobody likes you"}</li>`;
inventoryList.innerHTML = innerHTML;
}
updateDisplay();
let purchaseBtns = document.getElementsByClassName("purchase");
for (let i = purchaseBtns.length; i--;) {
let price = +purchaseBtns[i].dataset.price,
buys = purchaseBtns[i].dataset.buys,
capacity = +purchaseBtns[i].dataset.ppl;
purchaseBtns[i].addEventListener("click", e => {
if (price <= monetaryUnits) {
inventory[buys] = (inventory[buys] || 0) + 1;
monetaryUnits -= price;
maxPeople += capacity;
updateDisplay();
} else {
alert("u need " + (price - monetaryUnits) + " more MU to purchase");
}
}, false);
}
begBtn.addEventListener("click", e => {
if (Math.floor(Math.random() * 10) === 0) {
let person;
if (people.length > 0) {
person = people.splice(Math.floor(Math.random() * people.length), 1)[0];
muPerMin -= person.tax;
}
monetaryUnits -= 100;
updateDisplay();
alert(`The local law enforcement caught you begging. You had to pay a fine of 100 MU.`);
if (people.length > 0) alert(`Your popularity has decreased, and ${person.name} left, disappointed in you.`);
} else {
let begMU = Math.floor(Math.random() * 100);
monetaryUnits += begMU;
updateDisplay();
alert(`You earned ${begMU} MU from begging on the streets.`);
}
}, false);
function arrayFind(array, fn) {
for (let i = array.length; i--;) if (fn(array[i])) return array[i];
return null;
}
function makeName() {
let hasY = Math.floor(Math.random() * 2),
name;
if (hasY) name = maleNames[Math.floor(Math.random() * maleNames.length)];
else name = girlNames[Math.floor(Math.random() * girlNames.length)];
if (~people.indexOf(name)) name += " #" + (people.filter(a => a.name === name).length + 1);
return [name, hasY];
}
let lastTime = Date.now();
console.log(setInterval(() => {
let now = Date.now(),
wouldEarn = muPerMin * (now - lastTime) / 60000;
if (muPerMin === 0) {
lastTime = now;
} else if (wouldEarn >= 1) {
monetaryUnits += Math.floor(wouldEarn);
updateDisplay();
lastTime = now - (wouldEarn % 1) * 60000 / muPerMin;
}
if (people.length < maxPeople) {
if (Math.floor(Math.random() * 2) === 0) {
let tax = Math.floor(Math.random() * 80),
[name, hasY] = makeName();
if (confirm(`${name} would like to be a citizen of your city.
They are willing to pay ${tax} MU / min.
Allow them to immigrate?`)) {
people.push({
name: name,
hasY: hasY,
wontReproduce: false,
tax: tax
});
muPerMin += tax;
updateDisplay();
return;
}
}
let male = arrayFind(people, a => a.hasY),
female = arrayFind(people, a => !a.hasY);
if (male && female) {
let [name, hasY] = makeName();
if (confirm(`${male.name} and ${female.name} have given birth to ${name}.
However, the newborn is unable to contribute to your wealth.
Kill?`)) {
male.wontReproduce = female.wontReproduce = true;
let person = people.splice(Math.floor(Math.random() * people.length), 1)[0];
muPerMin -= person.tax;
updateDisplay();
alert(`Your popularity has decreased, and ${person.name} left, disappointed in you.`);
} else {
people.push({
name: name,
hasY: hasY,
tax: 0
});
updateDisplay();
}
}
}
}, 1000));
</script>
</body>
<!-- MADE BY SEAN -->
</html>