-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.cljs
472 lines (399 loc) · 12.3 KB
/
app.cljs
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
(ns examples.cljidle.app
"Clojure Idle is a meme idle game to demonstrate the use of relic in the browser."
(:require [com.wotbrew.relic :as rel]
[reagent.dom :as rdom]
[reagent.core :as r]))
;; ----
;; reference data
(def seed-state
"This is the seed data for our database."
{:Player [{:money 0, :wealth 0, :completed 0}]
:Project [{:name "Install Clojure", :base-reward 10.0 :base-difficulty 10}]})
(def projects
"The list of possible projects, an element is selected at random every time you complete a project."
[{:name "Bug fix", :base-difficulty 10, :base-reward 10}
{:name "Feature Request", :base-difficulty 15, :base-reward 20}
{:name "Demo", :base-difficulty 25, :base-reward 15}
{:name "Documentation", :base-difficulty 30, :base-reward 20}
{:name "Improve tests", :base-difficulty 20, :base-reward 25}])
(def improvements
"The list of possible improvements, to generate parens and $$$."
[{:name "Mechanical key grease"
:clicks 1
:base-cost 10}
{:name "(repeatedly #(write-code))"
:pps 0.1
:base-cost 30}
{:name "(repeatedly #(future (write-code)))"
:pps 1.0
:base-cost 400}
{:name "Write a component library"
:pps 2.0
:base-cost 1500}
{:name "Encourage coffee donations"
:base-cost 25000
:passive-income 100.0}
{:name "Babashka lambda functions"
:pps 20.0
:base-cost 100000}
{:name "Invest in a startup"
:base-cost 500000
:passive-income 1000.0}
{:name "Buy Nubank"
:base-cost 3000000
:pps 200.0}
{:name "Rich Hickey cloning vats"
:base-cost 90000000
:pps 2000.0}])
(def upgrades
"The list of possible upgrades, to improve your improvements."
[{:name "Switch color theme"
:cost 1
:pps-mul 2.0}
{:name "Re-watch simple made easy"
:cost 100
:pps-mul 2.0}
{:name "Fresh init.el"
:cost 1000
:pps-mul 2.0}
{:name "Go to the conj"
:cost 10000
:pps-mul 4.0}
{:name "Switch editor"
:cost 20000
:pps-mul 4.0}
{:name "Learn transducers"
:pps-mul 8.0
:cost 400000}
{:name "Macros that write macros"
:pps-mul 16.0
:cost 8000000}])
;; -----
;; essential state and constraints
(def PlayerSchema
"The player table will hold some counters"
[[:from :Player]
[:check
;; the amount of money we have to spend
[number? :money]
[<= 0 :money]
;; the amount of money we have to spend + in assets
[number? :wealth]
[<= 0 :wealth]
;; the number of projects completed
[nat-int? :completed]]])
(defn exactly-one-row
"Returns a constraint for query 'q', that ensures exactly one row exists."
[q]
[[:from q]
[:agg [] [:n count]]
[:check [= :n 1]]])
(def ExactlyOnePlayer (exactly-one-row :Player))
(def ProjectSchema
"The product table will hold the project being worked on."
[[:from :Project]
[:check
;; the name of the project
[string? :name]
;; used to determine the reward for completion
[number? :base-reward]
[pos? :base-reward]
;; used to determine how many parens are needed to complete the project
[number? :base-difficulty]
[pos? :base-difficulty]]])
(def ExactlyOneProject (exactly-one-row :Project))
(def WatchSchema
"Watch is going to be used by our reagent integration to invalid reagent atoms as query results change."
[[:from :Watch]
[:constrain
[:check
[some? :q]
[some? :ratom]]
;; only one row per :q, reuse the same atom if it is the same query.
[:unique :q]]])
(def EffectSchema
"Effect handlers are callbacks that will be invoked by our reagent integration when rows are added/deleted to a query.
f will be called like this (f db added deleted)."
[[:from :Effect]
[:constrain
[:check
[some? :key]
[some? :q]
[fn? :f]]
;; only one effect per :key, so we can remove them later
[:unique :key]]])
(def PlayerUpgradeSchema
"Upgrades are purchased by inserting a row into the :Upgrade table."
[[:from :Upgrade]
[:constrain
[:check [contains? (set (map :name upgrades)) :name]]
[:unique :name]]])
(def PlayerImprovementSchema
"Improvements are purchased by inserting or updating a row into the :Improvement table, with a :name and :qty. "
[[:from :Improvement]
[:constrain
[:check
[contains? (set (map :name improvements)) :name]
[number? :qty]
[pos? :qty]]
[:unique :name]]])
;; ----
;; derived data
(defn- project-scale-factor [completed-projects]
(+ 1 (long (/ completed-projects 5))))
(def ProjectScale
[[:from :Player]
[:select [:project-scale-factor [project-scale-factor :completed]]]])
(def Project
[[:from :Project]
[:join ProjectScale]
[:select
:name
:progress
;; scale the reward / difficulty with the number of completed projects
;; so number goes up.
[:reward [* :project-scale-factor :base-reward]]
[:difficulty [* :project-scale-factor :base-difficulty]]
;;
[:finished [>= :progress :difficulty]]]])
(def Upgrade
[[:const upgrades]
[:join :Player]
[:select
:name
:cost
[:affordable [<= :cost :money]]
[:unlocked [some? [rel/sel1 :Upgrade {:name :name}]]]
[:show [:and [not :unlocked] [<= :cost [* :wealth 2]]]]
[:active-pps-mul [:if :unlocked :pps-mul]]]])
(def ParenMultiplier
[[:from Upgrade]
[:agg [] [:pps-mul [rel/sum :active-pps-mul]]]
[:select [:pps-mul [max 1.0 :pps-mul]]]])
(defn- improvement-description [im]
(cond
(:pps im) (str "+" (.toFixed (:unit-pps im) 2) " parens per second")
(:clicks im) (str "+" (:clicks im) " parens per click")
(:passive-income im) (str "+$" (:passive-income im) " per second")))
(defn improvement-cost [base-cost qty]
(+ base-cost (* 0.1 base-cost qty)))
(def Improvement
[[:const improvements]
[:join :Player {} ParenMultiplier {}]
[:left-join :Improvement {:name :name}]
[:extend
[:cost [improvement-cost :base-cost :qty]]
[:show [<= :cost [* :wealth 2]]]
[:affordable [<= :cost :money]]
[:qty [:or :qty 0]]
[:unit-pps [:? * :pps-mul :pps]]
[:active-pps [:? * :qty :unit-pps]]
[:active-passive-income [:? * :qty :passive-income]]
[:click-boost [:? * :qty :clicks]]
[:desc improvement-description]]])
(def ShownImprovement
[[:from Improvement]
[:where :show]
[:sort [:base-cost :asc]]])
(def ShownUpgrade
[[:from Upgrade]
[:where :show]
[:sort [:cost :asc]]])
(def Stats
[[:from Improvement]
[:union Upgrade]
[:agg []
[:pps [rel/sum :active-pps]]
[:income [rel/sum :active-passive-income]]
[:click-boost [rel/sum :click-boost]]]])
;; ----
;; here we materialize just the constraints
(def state
(atom
(-> {}
(rel/transact seed-state)
(rel/mat
WatchSchema
EffectSchema
PlayerSchema
ProjectSchema
ExactlyOnePlayer
ExactlyOneProject
PlayerImprovementSchema
PlayerUpgradeSchema))))
;; ----
;; our reagent integration
;; using :Watch and :Effect
(defn- fire-signals! [db changes]
(let [feedback (volatile! [])]
(reduce-kv
(fn [_ q {:keys [added deleted]}]
(when (or (seq added)
(seq deleted))
(doseq [{:keys [ratom]} (rel/q db [[:from :Watch] [:where [= :q [:_ q]]]])]
(reset! ratom (rel/q db q)))
(doseq [{:keys [f]} (rel/q db [[:from :Effect] [:where [= :q [:_ q]]]])]
(when-some [tx (f db added deleted)]
(vswap! feedback conj tx)))))
nil
changes)
(not-empty @feedback)))
(defn t!
"Queues a transaction, fires any associated signals if their dependencies change."
[& tx]
(loop [tx tx]
(let [{:keys [db changes]} (apply rel/track-transact @state tx)]
(reset! state db)
(let [feedback (fire-signals! db changes)]
(when (seq feedback)
(recur feedback)))))
nil)
(defn listen! [k q f]
(swap! state rel/watch q)
(t! [:insert-or-replace :Effect {:q q, :key k, :f f}]))
(defn q!
"Returns a reagent derefable for the results of query `q`. Optionally supply a function to apply to the results.
e.g (q! :Customer first) "
([q] (q! q identity))
([q f]
(let [new-atom (r/atom nil)
_ (t! [:insert-ignore :Watch {:q q, :ratom new-atom}])
db (swap! state rel/watch q)
{:keys [ratom]} (rel/row db :Watch [= :q [:_ q]])
_ (if (identical? ratom new-atom)
(let [new (rel/q db q)]
(reset! ratom new)))]
(r/track (fn [] (f @ratom))))))
;; ---
;; event handlers
(listen! ::on-project-completed-issue-reward
(conj Project [:where :finished])
(fn [_ added]
(when-some [{:keys [reward]} (first added)]
(list
[:replace-all :Project (rand-nth projects)]
[:update :Player {:money [+ :money reward]
:wealth [+ :wealth reward]
:completed [inc :completed]}]))))
;; ---
;; transactions
(defn progress
([] (progress 1.0))
([n] (progress n 0.0))
([parens income]
(list [:update :Project {:progress [+ :progress parens]}]
(when (and income (pos? income))
[:update :Player {:money [+ :money income]
:wealth [+ :wealth income]}]))))
(defn buy
[improvement-name]
(fn [db]
(let [{:keys [affordable
name
cost]}
(rel/row db Improvement [= :name improvement-name])]
(when affordable
(list
[:insert-or-update :Improvement {:qty [inc :qty]}
{:name name
:qty 1}]
[:update :Player {:money [- :money cost]}])))))
(defn unlock [upgrade-name]
(fn [db]
(let [{:keys [affordable cost]} (rel/row db Upgrade [= :name upgrade-name])]
(when affordable
(list [:insert-ignore :Upgrade {:name upgrade-name}]
[:update :Player {:money [- :money cost]}])))))
;; ----
;; reagent code
(defn project []
(let [p (q! Project first)]
(fn []
[:table.table
[:tbody
[:tr [:th "Project"] [:td (:name @p)]]
[:tr [:th "Progress"] [:td (long (:progress @p 0)) "/" (:difficulty @p)]]
[:tr [:th "Reward"] [:td "$" (:reward @p)]]]])))
(defn paren []
(let [clicks (q! Stats first)]
(fn []
[:button.button {:style {:width "96px" :height "96px"} :on-click #(t! (progress (+ 1 (:click-boost @clicks))))}
"()"])))
(defn money []
(let [player (q! :Player first)]
(fn []
[:span "$" (.toFixed (:money @player 0) 2)])))
(defn pps []
(let [ti (q! Stats first)]
(fn []
[:span (.toFixed (:pps @ti 0) 2) " parens per second"])))
(defn status []
[:table.table.is-fullwidth
[:thead
[:tr
[:th [paren]]
[:th [money]]
[:th [pps]]]]])
(defn improvement-list []
[]
(let [im (q! ShownImprovement)]
(fn []
[:table.table
[:tbody
(for [im @im]
^{:key (:name im)}
[:tr
[:th (:name im)]
[:th (:qty im)]
[:td (:desc im)]
[:td "$" (.toFixed (:cost im 0) 2)]
[:td (if (:affordable im)
[:button.button {:on-click #(t! (buy (:name im)))} "Buy"]
[:button.button {:disabled true} "Buy"])]])]])))
(defn upgrade-list []
(let [up (q! ShownUpgrade)]
(fn []
[:table.table
[:tbody
(for [up @up]
^{:key (:name up)}
[:tr
[:th (:name up)]
[:td (:desc up)]
[:td "$" (:cost up)]
[:td (if (:affordable up)
[:button.button {:on-click #(t! (unlock (:name up)))} "Unlock"]
[:button.button {:disabled true} "Unlock"])]])]])))
(defn app []
(fn []
[:section.section
[:div.container
[:div.columns
[:div.column
[status]]
[:div.column
[project]]]
[:div.columns
[:div.column
[improvement-list]]
[:div.column
[upgrade-list]]]]]))
(defn tick! []
(let [db @state
{:keys [pps income]} (rel/row db Stats)]
(when pps
(t! (progress pps income))
(let [db @state]
(set! (.-title js/document) (str "CLJ IDLE $" (.toFixed (:money (rel/row db :Player) 0) 2)))))))
(defn init []
(when-not (.-cljidleTicking js/window)
(.setInterval js/window (fn [] (tick!)) 1000)
(set! (.-cljidleTicking js/window) true))
(rdom/render
[app]
(js/document.getElementById "app")))
(init)
(comment
(t! (progress 0 100))
(init)
,)