forked from mtgred/netrunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffs.clj
More file actions
534 lines (486 loc) · 16 KB
/
diffs.clj
File metadata and controls
534 lines (486 loc) · 16 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
(ns game.core.diffs
(:require
[cond-plus.core :refer [cond+]]
[differ.core :as differ]
[game.core.board :refer [installable-servers]]
[game.core.card :refer :all]
[game.core.cost-fns :refer [card-ability-cost]]
[game.core.engine :refer [can-trigger?]]
[game.core.effects :refer [is-disabled-reg?]]
[game.core.installing :refer [corp-can-pay-and-install?
runner-can-pay-and-install?]]
[game.core.payment :refer [can-pay? ->c]]
[game.core.play-instants :refer [can-play-instant?]]
[game.utils :refer [dissoc-in]]
[jinteki.utils :refer [select-non-nil-keys]]
[medley.core :refer [update-existing]]))
(defn playable? [card state side]
(if (and ((if (= :corp side) corp? runner?) card)
(in-hand? card)
(not (:corp-phase-12 @state))
(not (:runner-phase-12 @state))
(cond+
[(or (agenda? card)
(asset? card)
(ice? card)
(upgrade? card))
(some
(fn [server]
(corp-can-pay-and-install?
state :corp {:source card :source-type :corp-install}
card server {:base-cost [(->c :click 1)]
:action :corp-click-install
:no-toast true}))
(installable-servers state card))]
[(or (hardware? card)
(program? card)
(resource? card))
(and (not (:run @state))
(runner-can-pay-and-install?
state :runner {:source card :source-type :runner-install}
card {:base-cost [(->c :click 1)]
:no-toast true}))]
[(or (event? card)
(operation? card))
(and (not (:run @state))
(can-play-instant?
state side {:source card :source-type :play}
card {:base-cost [(->c :click 1)]
:silent true}))])
true)
(assoc card :playable true)
card))
(defn ability-playable? [ability ability-idx state side card]
(let [cost (card-ability-cost state side ability card)
eid {:source card
:source-type :ability
:source-info {:ability-idx ability-idx}}]
(if (and (or (active? card)
(:autoresolve ability))
;; using the reg instead of any-effect here because this function gets called
;; a LOT, and this is very close to linear time and should (I think)
;; always be correct when this fn is called.
;; If that's ever an issue, we can switch this to:
;; (is-disabled? state side card)
;; --n kelly, apr 2024
(not (is-disabled-reg? state card))
;; actions cannot be used during runs
(not (and (:action ability)
(:run @state)))
(can-pay? state side eid card nil cost)
(can-trigger? state side eid ability card nil))
(assoc ability :playable true)
ability)))
(def ability-keys
[:cost-label
:dynamic
:index
:keep-menu-open
:label
:msg
:playable
:source])
(defn ability-summary [state side card ab-idx ability]
(-> ability
(ability-playable? ab-idx state side card)
(select-non-nil-keys ability-keys)))
(defn abilities-summary [abilities card state side]
(some->> (seq abilities)
(map-indexed (fn [ab-idx ab] (ability-summary state side card ab-idx ab)))
(into [])))
(def subroutine-keys
[:broken
:fired
:label
:msg
:resolve])
(defn subroutines-summary [subroutines]
(when (seq subroutines)
(mapv #(select-non-nil-keys % subroutine-keys) subroutines)))
(defn card-abilities-summary [card state side]
(cond-> card
(:abilities card) (update :abilities abilities-summary card state side)
(:corp-abilities card) (update :corp-abilities abilities-summary card state side)
(:runner-abilities card) (update :runner-abilities abilities-summary card state side)
(:subroutines card) (update :subroutines subroutines-summary)))
(def card-keys
[:abilities
:advance-counter
:advanceable
:advancementcost
:agendapoints
:card-target
:cid
:code
:corp-abilities
:cost
:counter
:current-advancement-requirement
:current-points
:current-strength
:disabled
:extra-advance-counter
:face
:faces
:facedown
:host
:hosted
:icon
:images
:implementation
:installed
:new
:normalizedtitle
:playable
:printed-title
:rezzed
:runner-abilities
:seen
:selected
:side
:strength
:subroutines
:subtype-target
:poison
:highlight-in-discard
:subtypes
:title
:type
:zone])
(def private-card-keys
[:advance-counter
:cid
:counter
:extra-advance-counter
:host
:hosted
:icon
:new
:side
:zone])
(defn private-card
"Returns only the public information of a given card when it's in a private state,
for example, when it's facedown or in the hand"
[card]
(select-non-nil-keys card private-card-keys))
(declare cards-summary)
(defn card-summary [card state side]
(if (is-public? card side)
(-> (cond-> card
(:host card) (-> (dissoc-in [:host :hosted])
(update :host card-summary state side))
(:hosted card) (update :hosted cards-summary state side))
(playable? state side)
(card-abilities-summary state side)
(select-non-nil-keys card-keys))
(-> (cond-> card
(:host card) (-> (dissoc-in [:host :hosted])
(update :host card-summary state side))
(:hosted card) (update :hosted cards-summary state side))
(private-card))))
(defn cards-summary [cards state side]
(when (seq cards)
(mapv #(card-summary % state side) cards)))
(def prompt-keys
[:msg
:choices
:card
:prompt-type
:show-discard
;; traces
:player
:base
:bonus
:strength
:unbeatable
:beat-trace
:link
:corp-credits
:runner-credits])
(defn prompt-summary
[prompt same-side?]
(when same-side?
(-> prompt
(update :card #(not-empty (select-non-nil-keys % card-keys)))
(update :choices (fn [choices]
(if (sequential? choices)
(->> choices
(mapv
(fn [choice]
(if (-> choice :value :cid)
(update choice :value select-non-nil-keys card-keys)
choice)))
(not-empty))
choices)))
(select-non-nil-keys prompt-keys)
(not-empty))))
(defn toast-summary
[toast same-side?]
(when same-side?
toast))
(def player-keys
[:aid
:user
:identity
:basic-action-card
:deck
:deck-id
:hand
:discard
:scored
:rfg
:play-area
:current
:set-aside
:click
:credit
:toast
:hand-size
:keep
:quote
:trash-like-cards
:prompt-state
:agenda-point
:agenda-point-req])
(defn player-summary
[player state side same-side? additional-keys]
(-> player
(update :identity card-summary state side)
(update :basic-action-card card-summary state side)
(update :current cards-summary state side)
(update :play-area cards-summary state side)
(update :rfg cards-summary state side)
(update :scored cards-summary state side)
(update :set-aside cards-summary state side)
(update :prompt-state prompt-summary same-side?)
(update :toast toast-summary same-side?)
(select-non-nil-keys (into player-keys additional-keys))))
(def corp-keys
[:servers
:bad-publicity])
(defn servers-summary
[state side]
(reduce-kv
(fn [servers current-server-kw current-server]
(assoc servers
current-server-kw
{:content (cards-summary (:content current-server) state side)
:ices (cards-summary (:ices current-server) state side)}))
{}
(:servers (:corp @state))))
(defn prune-cards [cards]
(mapv #(select-non-nil-keys % card-keys) cards))
(defn deck-summary
"Is the player's deck publicly visible?"
[deck same-side? player]
(if (and same-side? (:view-deck player))
(prune-cards deck)
[]))
(defn hand-summary
"Is the player's hand publicly visible?"
[hand state same-side? side player]
(if (or same-side? (:openhand player))
(cards-summary hand state side)
[]))
(defn discard-summary
[discard state same-side? side player]
(if (or same-side? (:openhand player))
(cards-summary discard state :corp)
(cards-summary discard state side)))
(defn corp-summary
[corp state side]
(let [corp-player? (= side :corp)
install-list (:install-list corp)]
(-> (player-summary corp state side corp-player? corp-keys)
(update :deck deck-summary corp-player? corp)
(update :hand hand-summary state corp-player? :corp corp)
(update :discard discard-summary state corp-player? side corp)
(assoc
:deck-count (count (:deck corp))
:hand-count (count (:hand corp))
:servers (servers-summary state side))
(cond-> (and corp-player? install-list) (assoc :install-list install-list)))))
(def runner-keys
[:rig
:run-credit
:link
:tag
:memory
:brain-damage])
(defn rig-summary
[state side]
(-> (:rig (:runner @state))
(update :hardware cards-summary state side)
(update :facedown cards-summary state side)
(update :program cards-summary state side)
(update :resource cards-summary state side)))
(defn runner-summary
[runner state side]
(let [runner-player? (= side :runner)
runnable-list (:runnable-list runner)]
(-> (player-summary runner state side runner-player? runner-keys)
(update :deck deck-summary runner-player? runner)
(update :hand hand-summary state runner-player? :runner runner)
(update :discard prune-cards)
(assoc
:deck-count (count (:deck runner))
:hand-count (count (:hand runner))
:rig (rig-summary state side))
(cond-> (and runner-player? runnable-list) (assoc :runnable-list runnable-list)))))
(def options-keys
[:alt-arts
:background
:card-resolution
:language
:pronouns
:show-alt-art])
(defn options-summary [options]
(when (seq options)
(select-non-nil-keys options options-keys)))
(def user-keys
[:_id
:username
:emailhash
:options
:special])
(defn user-summary [user]
(-> user
(update-existing :options options-summary)
(select-non-nil-keys user-keys)))
(def run-keys
[:server
:position
:corp-auto-no-action
:cannot-jack-out
:phase
:next-phase
:no-action
:source-card
:approached-ice-in-position?])
(defn run-summary
[state]
(when-let [run (:run @state)]
(-> run
(assoc :approached-ice-in-position? (when (= :approach-ice (:phase run))
(some? (get-card state (:current-ice run)))))
(select-non-nil-keys run-keys))))
(defn encounter-ice-summary
[ice state]
(when-let [ice (get-card state ice)]
(card-summary ice state :corp)))
(def encounter-keys
[:encounter-count
:ice
:no-action])
(defn encounters-summary
[state]
(let [encounters (:encounters @state)
current-encounter (peek encounters)
encounter-count (count encounters)]
(when current-encounter
(-> current-encounter
(update :ice encounter-ice-summary state)
(assoc :encounter-count encounter-count)
(select-non-nil-keys encounter-keys)))))
(def state-keys
[:active-player
;; :angel-arena-info
:corp
:corp-phase-12
:decklists
:encounters
:end-turn
:gameid
:last-revealed
:log
:mark
:options
:psi
:reason
:room
:run
:runner
:runner-phase-12
:sfx
:sfx-current-id
:start-date
:stats
:trace
:turn
:typing
:winning-user
:winner])
(defn strip-state
[state]
(-> @state
(update-in [:corp :user] user-summary)
(update-in [:runner :user] user-summary)
(assoc :stats (when (:winner @state) (:stats @state)))
(assoc :run (run-summary state))
(assoc :encounters (encounters-summary state))
(select-non-nil-keys state-keys)))
(defn state-summary
[stripped-state state side]
(-> stripped-state
(update :corp corp-summary state side)
(update :runner runner-summary state side)))
(defn strip-for-replay
[stripped-state corp-player runner-player]
(assoc stripped-state
:corp (:corp corp-player)
:runner (:runner runner-player)))
(defn strip-for-spectators
[stripped-state corp-state runner-state]
(let [spectator-hands? (-> stripped-state :options :spectatorhands)]
(-> stripped-state
(assoc :corp (if spectator-hands? (:corp corp-state) (:corp runner-state)))
(assoc :runner (if spectator-hands? (:runner runner-state) (:runner corp-state))))))
(defn strip-for-corp-spect
[stripped-state corp-state runner-state]
(assoc stripped-state :corp (:corp corp-state) :runner (:runner corp-state)))
(defn strip-for-runner-spect
[stripped-state corp-state runner-state]
(assoc stripped-state :corp (:corp runner-state) :runner (:runner runner-state)))
(defn public-states
"Generates privatized states for the Corp, Runner, any spectators, and the history from the base state.
If `:spectatorhands` is on, all information is passed on to spectators as well.
note that when joining or starting a game, all states are always generated.
Otherwise when computing diffs, only the relevant states are needed, and we can skip computing the other ones."
([state] (public-states state true true true))
([state spectators? corp-spectators? runner-spectators?]
(let [stripped-state (strip-state state)
corp-state (state-summary stripped-state state :corp)
runner-state (state-summary stripped-state state :runner)
replay-state (strip-for-replay stripped-state corp-state runner-state)]
;; corp, runner, spectator, history
{:corp-state corp-state
:runner-state runner-state
:spect-state (when spectators? (strip-for-spectators replay-state corp-state runner-state))
:corp-spect-state (when corp-spectators? (strip-for-corp-spect replay-state corp-state runner-state))
:runner-spect-state (when runner-spectators? (strip-for-runner-spect replay-state corp-state runner-state))
:hist-state replay-state})))
(defn public-diffs [old-state new-state spectators? corp-spectators? runner-spectators?]
(let [{old-corp :corp-state old-runner :runner-state
old-spect :spect-state old-hist :hist-state
old-corp-spect :corp-spect-state
old-runner-spect :runner-spect-state} (when old-state (public-states (atom old-state) spectators? corp-spectators? runner-spectators?))
{new-corp :corp-state new-runner :runner-state
new-spect :spect-state new-hist :hist-state
new-corp-spect :corp-spect-state
new-runner-spect :runner-spect-state} (public-states new-state spectators? corp-spectators? runner-spectators?)]
{:runner-diff (differ/diff old-runner new-runner)
:corp-diff (differ/diff old-corp new-corp)
:spect-diff (when spectators? (differ/diff old-spect new-spect))
:runner-spect-diff (when runner-spectators? (differ/diff old-runner-spect new-runner-spect))
:corp-spect-diff (when corp-spectators? (differ/diff old-corp-spect new-corp-spect))
:hist-diff (differ/diff old-hist new-hist)}))
(defn message-diffs [old-state new-state]
(let [old-messages (select-keys old-state [:log])
new-messages (select-keys @new-state [:log])
message-diff (differ/diff old-messages new-messages)]
{:runner-diff message-diff
:corp-diff message-diff
:spect-diff message-diff
:runner-spect-diff message-diff
:corp-spect-diff message-diff
:hist-diff message-diff}))