-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiled_app.js
292 lines (262 loc) · 10.4 KB
/
compiled_app.js
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
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var counter = 0;
var VERSION = [3, 5, 0];
var MIN_VERSION = [3, 5, 0];
function SaveGameJSONHelper(property, value) {
if (typeof value == 'function') {
return undefined;
}
return value;
}
var savedGame = null;
if ("savedGame" in localStorage) {
var data = JSON.parse(localStorage.getItem("savedGame"));
if (data.version >= MIN_VERSION) {
savedGame = data.state;
var itemCounts = {};
for (var i = 0; i < savedGame.items.length; i++) {
itemCounts[savedGame.items[i].name] = savedGame.items[i].count;
}
savedGame.items = itemCounts;
}
}
function generateItem(name, costFunction, impactFunction) {
var count = savedGame ? savedGame.items[name] : 0;
return {
count: count,
name: name,
cost: costFunction,
impact: impactFunction
};
}
function generateItemFromItem(item) {
var newItem = generateItem(item.name, item.cost, item.impact);
newItem.count = item.count;
return newItem;
}
function ProductivityButton(props) {
return React.createElement(
"div",
{ className: "productivity-button" },
React.createElement(
"button",
{ onClick: props.click },
"Complete Task(s)"
)
);
}
function Stat(props) {
return React.createElement(
"div",
{ className: "stats-panel-item" },
props.name,
": ",
props.count
);
}
function AdvancedStat(props) {
return React.createElement(
"div",
{ className: "stats-panel-item" },
props.item.name,
": ",
props.item.count
);
}
function Logo(props) {
return React.createElement(
"div",
{ className: "logo" },
"Productivity Counter"
);
}
function StatsPanel(props) {
return React.createElement(
"div",
{ className: "stats-panel" },
React.createElement(Logo, null),
React.createElement(Stat, { name: "Tasks", count: props.tasksCount }),
React.createElement(Stat, { name: "Productivity", count: props.productivityCount }),
props.items.map(function (item) {
return React.createElement(AdvancedStat, { item: item, key: item.name + 'stat' });
}),
React.createElement(ProductivityButton, { click: props.click })
);
}
function UpgradeItem(props) {
return React.createElement(
"div",
{ className: "upgrade-item", onClick: function onClick() {
return props.buyItem(props.item);
} },
React.createElement(
"div",
{ className: "task-text" },
props.item.name,
" (Costs ",
props.item.cost(props.item),
" productivity.)"
)
);
}
function UpgradePanel(props) {
return React.createElement(
"div",
{ className: "upgrade-panel" },
React.createElement("img", { src: "backdrop.jpg", style: { width: 100 + "%" } }),
props.items.map(function (item) {
return React.createElement(UpgradeItem, { item: item, buyItem: props.buyItem, key: item.name + 'upgrade' });
})
);
}
var App = function (_React$Component) {
_inherits(App, _React$Component);
function App(props) {
_classCallCheck(this, App);
var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));
_this.generateState();
_this.tick = _this.tick.bind(_this);
_this.click = _this.click.bind(_this);
_this.buyItem = _this.buyItem.bind(_this);
window.setInterval(_this.tick, 1000);
return _this;
}
_createClass(App, [{
key: "generateState",
value: function generateState() {
var tasksCount = savedGame ? savedGame.tasksCount : 100;
var productivityCount = savedGame ? savedGame.productivityCount : 100;
this.state = {
tasksCount: tasksCount,
productivityCount: productivityCount,
items: [generateItem('Legal Pads', function (item) {
return item.count + 1;
}, function (item) {
return { tasks: item.count };
}), generateItem('Todo Apps', function (item) {
return item.count * 35 + 35;
}, function (item) {
return { tasks: item.count * 5 };
}), generateItem('Multitasking', function (item) {
return 50 * Math.pow(item.count, 2) + 100;
}, function (item) {
return {};
}), generateItem('Python Scripts', function (item) {
return item.count * 3500 + 1000;
}, function (item) {
return { productivity: Math.pow(item.count, 3) };
})]
};
}
}, {
key: "buyItem",
value: function buyItem(itemType) {
var _this2 = this;
var cost = itemType.cost(itemType);
if (this.state.productivityCount >= cost) {
this.setItemCount(itemType, 1);
this.setState(function (currentState) {
return {
productivityCount: _this2.state.productivityCount - cost
};
});
}
}
}, {
key: "setItemCount",
value: function setItemCount(itemType, count) {
var increment = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
this.setState(function (currentState) {
// clone dictionary
var items = [];
for (var _i = 0; _i < currentState.items.length; _i++) {
var item = generateItemFromItem(currentState.items[_i]);
if (item.name == itemType.name) {
item.count = increment ? item.count + count : item.count;
}
items.push(item);
}
return {
items: items
};
});
}
}, {
key: "getItemByName",
value: function getItemByName(name) {
for (var _i2 = 0; _i2 < this.state.items.length; _i2++) {
if (this.state.items[_i2].name == name) {
return this.state.items[_i2];
}
}
return null;
}
}, {
key: "click",
value: function click() {
var conversion = Math.min(1 + Math.pow(this.getItemByName("Multitasking").count, 2), this.state.tasksCount);
this.setState(function (currentState) {
return {
tasksCount: currentState.tasksCount - conversion,
productivityCount: currentState.productivityCount + conversion
};
});
}
}, {
key: "saveGame",
value: function saveGame() {
localStorage.setItem("savedGame", JSON.stringify({
version: VERSION,
state: this.state
}, SaveGameJSONHelper));
}
}, {
key: "tick",
value: function tick() {
counter += 1;
if (counter % 30 == 1) {
this.saveGame();
}
this.setState(function (currentState) {
var newTasksCount = currentState.tasksCount + 1;
var newProductivityCount = currentState.productivityCount;
for (var _i3 = 0; _i3 < currentState.items.length; _i3++) {
if (currentState.items[_i3].impact(currentState.items[_i3]).tasks) {
newTasksCount += currentState.items[_i3].impact(currentState.items[_i3]).tasks;
}
if (currentState.items[_i3].impact(currentState.items[_i3]).productivity) {
newProductivityCount += currentState.items[_i3].impact(currentState.items[_i3]).productivity;
}
}
return {
tasksCount: newTasksCount,
productivityCount: newProductivityCount
};
});
}
}, {
key: "render",
value: function render() {
return React.createElement(
"div",
{ className: "app-container" },
React.createElement(StatsPanel, {
tasksCount: this.state.tasksCount,
productivityCount: this.state.productivityCount,
items: this.state.items,
click: this.click
}),
React.createElement(UpgradePanel, {
items: this.state.items,
buyItem: this.buyItem
})
);
}
}]);
return App;
}(React.Component);
ReactDOM.render(React.createElement(App, null), document.getElementById('app'));