Line data Source code
1 4 : (ns matchscore.application.advancement.bye
2 : "Introduces a real 'bye' concept for bracket seed slots that can never be
3 : filled — the target bracket has more round-1 seed slots than real
4 : participants were ultimately promoted into it — and auto-resolves any
5 : match paired against a bye via a synthetic 'walkover' match_result,
6 : cascading the winner forward through the existing winner_of/loser_of
7 : propagation (bracket-reconcile/execute!). A large enough deficit can walk
8 : the same participant over several rounds in one pass (a bye chain)."
9 : (:require
10 : [matchscore.application.advancement.bracket-reconcile :as bracket-reconcile]
11 : [matchscore.domain.bracket-match-slot.repository.postgres :as bms-repo]
12 : [matchscore.domain.bracket-match.repository.postgres :as bracket-match-repo]
13 : [matchscore.domain.bracket-round.repository.postgres :as bracket-round-repo]
14 : [matchscore.domain.bracket.repository.postgres :as bracket-repo]
15 : [matchscore.domain.match-result.command :as match-result-command]
16 : [matchscore.domain.match-result.repository.postgres :as match-result-repo]
17 : [matchscore.domain.match.repository.postgres :as match-repo]))
18 :
19 :
20 : ;; ---------------------------------------------------------------------------
21 : ;; Traversal helpers (bracket -> rounds -> matches -> slots)
22 : ;; ---------------------------------------------------------------------------
23 :
24 :
25 4 : (defn- bracket-ids-for-draw
26 : [repos target-draw-id]
27 36 : (mapv :uuid (bracket-repo/find-by-draw-id (:bracket-postgres-repo repos) target-draw-id)))
28 :
29 :
30 4 : (defn- rounds-for-bracket
31 : "Bracket rounds ordered by round-number ascending (own numbering per
32 : bracket — Monrad placement sub-brackets each restart at round 1)."
33 : [repos bracket-id]
34 24 : (bracket-round-repo/find-by-bracket-id (:bracket-round-postgres-repo repos) bracket-id))
35 :
36 :
37 4 : (defn- match-ids-for-round
38 : [repos round-id]
39 12 : (mapv
40 : :uuid
41 24 : (bracket-match-repo/find-by-bracket-round-id (:bracket-match-postgres-repo repos) round-id)))
42 :
43 :
44 4 : (defn- slots-for-matches
45 : [repos bracket-match-ids]
46 16 : (if (empty? bracket-match-ids)
47 2 : []
48 8 : (bms-repo/find-by-bracket-match-ids
49 12 : (:bracket-match-slot-postgres-repo repos)
50 4 : bracket-match-ids)))
51 :
52 :
53 24 : (defn- slot-bye? [slot] (= "bye" (:source-type slot)))
54 :
55 :
56 4 : (defn- mark-bye!
57 : [repos slot]
58 40 : (bms-repo/upsert! (:bracket-match-slot-postgres-repo repos) (assoc slot :source-type "bye")))
59 :
60 :
61 4 : (defn- all-match-ids-for-draw
62 : "Every bracket_match id across every bracket/round in target-draw-id.
63 : Monrad placement sub-brackets share the draw with the main bracket, so
64 : this spans bracket boundaries — needed because a sub-bracket's round-1
65 : slots are wired loser_of the parent bracket's round, not seed slots."
66 : [repos target-draw-id]
67 8 : (mapcat
68 4 : (fn [bracket-id]
69 8 : (mapcat
70 4 : (fn [round]
71 24 : (match-ids-for-round repos (:uuid round)))
72 16 : (rounds-for-bracket repos bracket-id)))
73 16 : (bracket-ids-for-draw repos target-draw-id)))
74 :
75 :
76 : ;; ---------------------------------------------------------------------------
77 : ;; Bye marking (idempotent, cascades draw-wide — across rounds AND across
78 : ;; brackets, since Monrad placement sub-brackets take their round-1
79 : ;; participants via loser_of the parent bracket rather than via seeding)
80 : ;; ---------------------------------------------------------------------------
81 :
82 :
83 4 : (defn- mark-empty-seeds-bye!
84 : "Flips every remaining empty seed slot across the whole draw to
85 : source-type=bye. Round-1 seed slots only exist on the root bracket of a
86 : Monrad tree (or on flat, non-Monrad brackets) — sub-bracket round-1 slots
87 : are loser_of, handled by the cascade below."
88 : [repos target-draw-id]
89 8 : (let [empty-seed? (fn [s]
90 60 : (and (= "seed" (:source-type s)) (nil? (:participant-id s))))]
91 140 : (doseq [s (slots-for-matches repos (all-match-ids-for-draw repos target-draw-id))
92 12 : :when (empty-seed? s)]
93 16 : (mark-bye! repos s))))
94 :
95 :
96 4 : (defn- cascade-byes-once!
97 : "Flips dependent slots to bye per source-match resolution status:
98 : - winner_of: bye iff the source match is void (BOTH of its own slots
99 : are bye) — no winner will ever emerge.
100 : - loser_of: bye iff the source match has AT LEAST ONE bye slot — a
101 : walkover match (one real, one bye) has a real eventual winner, but
102 : structurally never has a loser (nobody played, so nobody lost), so
103 : its loser_of dependent is permanently unfillable immediately, before
104 : the walkover itself is even completed.
105 : Returns true if anything changed, so callers can iterate to a fixed point."
106 : [repos target-draw-id]
107 32 : (let [slots (slots-for-matches repos (all-match-ids-for-draw repos target-draw-id))
108 28 : by-match (group-by (comp str :bracket-match-id) slots)
109 4 : void? (fn [bm-id]
110 12 : (every? slot-bye? (get by-match bm-id)))
111 4 : any-bye? (fn [bm-id]
112 12 : (some slot-bye? (get by-match bm-id)))
113 8 : to-flip (filterv
114 4 : (fn [s]
115 28 : (and
116 20 : (not (slot-bye? s))
117 12 : (case (:source-type s)
118 14 : "winner_of" (void? (str (:source-match-id s)))
119 14 : "loser_of" (any-bye? (str (:source-match-id s)))
120 : false)))
121 4 : slots)]
122 94 : (doseq [s to-flip] (mark-bye! repos s))
123 16 : (boolean (seq to-flip))))
124 :
125 :
126 4 : (defn mark-permanent-byes!
127 : "Idempotent. Flips remaining empty seed slots across target-draw-id to
128 : source-type=bye, then cascades that bye status to a fixed point wherever
129 : a dependent slot's source bracket_match can structurally never supply it
130 : a participant — including across bracket boundaries (a Monrad placement
131 : sub-bracket's round-1 loser_of slot becomes bye when its parent's
132 : corresponding round-1 match is void OR a walkover, since a walkover has
133 : no loser). Callers must only invoke this once every candidate that could
134 : still fill a remaining empty seed is settled — either promoted or
135 : structurally impossible (see
136 : matchscore.application.advancement.reconcile/rule-has-pending-candidates?
137 : for the continuous path; the one-shot stage-play path is always settled)
138 : — an empty seed slot marked bye here is treated as permanently
139 : unfillable."
140 : [repos target-draw-id]
141 16 : (mark-empty-seeds-bye! repos target-draw-id)
142 24 : (let [bound (count (all-match-ids-for-draw repos target-draw-id))]
143 8 : (loop [n 0]
144 48 : (when (and (<= n bound) (cascade-byes-once! repos target-draw-id)) (recur (inc n))))))
145 :
146 :
147 : ;; ---------------------------------------------------------------------------
148 : ;; Walkover completion
149 : ;; ---------------------------------------------------------------------------
150 :
151 :
152 4 : (defn walkover-completable-matches
153 : "Bracket_matches for target-draw-id where exactly one slot holds a real
154 : participant and the sibling slot is a permanent bye, and no match_result
155 : has been recorded yet for the underlying match. Returns
156 : [{:match-uuid <str> :winner-id <str>} ...]."
157 : [repos target-draw-id]
158 12 : (let [by-match (group-by
159 16 : (comp str :bracket-match-id)
160 28 : (slots-for-matches repos (all-match-ids-for-draw repos target-draw-id)))
161 8 : candidates (keep
162 4 : (fn [[bracket-match-id slots]]
163 24 : (when (= 2 (count slots))
164 20 : (let [byes (filter slot-bye? slots)
165 28 : real (filter (comp some? :participant-id) slots)]
166 56 : (when (and (= 1 (count byes)) (= 1 (count real)))
167 16 : {:bracket-match-id bracket-match-id
168 28 : :winner-id (str (:participant-id (first real)))}))))
169 4 : by-match)]
170 8 : (keep
171 4 : (fn [{:keys [bracket-match-id winner-id]}]
172 4 : (let [matches
173 24 : (match-repo/find-by-bracket-match-id (:match-postgres-repo repos) bracket-match-id)]
174 18 : (when (seq matches)
175 16 : (let [match-ids (mapv (comp str :uuid) matches)
176 4 : results (match-result-repo/find-by-match-ids
177 6 : (:match-result-postgres-repo repos)
178 2 : match-ids)]
179 10 : (when (empty? results)
180 20 : {:match-uuid (str (:uuid (first matches)))
181 2 : :winner-id winner-id})))))
182 4 : candidates)))
183 :
184 :
185 4 : (defn- complete-walkover!
186 : [{:keys [repos pg]} {:keys [match-uuid winner-id]}]
187 4 : (match-result-command/save-match-result!
188 8 : {:repos repos
189 6 : :pg {:datasource pg}}
190 12 : {:match-id match-uuid
191 2 : :winner-id winner-id
192 : :outcome "walkover"}))
193 :
194 :
195 4 : (defn resolve-byes-and-walkovers!
196 : "Marks any permanently-unfillable bracket seed slots for target-draw-id as
197 : byes (see mark-permanent-byes!), then drains any resulting
198 : walkover-completable matches: writes a synthetic 'walkover' match_result
199 : for the lone real participant and cascades the win forward via the
200 : existing bracket-reconcile propagation, repeating until no more matches
201 : complete (a bye chain can resolve several rounds in one pass).
202 :
203 : ctx must carry :repos and :pg exactly as bracket-reconcile/execute!
204 : expects (a raw datasource, or the connection of a transaction already in
205 : progress — pass the latter when calling from inside an existing
206 : with-transaction to avoid opening a redundant nested one)."
207 : [{:keys [repos]
208 : :as ctx}
209 : target-draw-id]
210 16 : (mark-permanent-byes! repos target-draw-id)
211 16 : (let [round-count-bound (transduce
212 36 : (map (comp count (partial rounds-for-bracket repos)))
213 4 : max
214 : 0
215 16 : (bracket-ids-for-draw repos target-draw-id))]
216 8 : (loop [n 0]
217 20 : (when (<= n round-count-bound)
218 20 : (let [completable (walkover-completable-matches repos target-draw-id)]
219 18 : (when (seq completable)
220 60 : (doseq [c completable] (complete-walkover! ctx c))
221 64 : (doseq [c completable] (bracket-reconcile/execute! ctx (:match-uuid c)))
222 2 : (recur (inc n))))))))
|