Line data Source code
1 4 : (ns matchscore.application.stage.helpers
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.domain.advancement-rule.repository.postgres :as advancement-rule-postgres-repo]
6 : [matchscore.domain.bracket-match-slot.repository.postgres :as bms-postgres-repo]
7 : [matchscore.domain.bracket-match.repository.postgres :as bm-postgres-repo]
8 : [matchscore.domain.bracket-round.repository.postgres :as br-postgres-repo]
9 : [matchscore.domain.bracket.command :as bracket-cmd]
10 : [matchscore.domain.bracket.repository.postgres :as bracket-postgres-repo]
11 : [matchscore.domain.draw.repository.postgres :as draw-postgres-repo]
12 : [matchscore.domain.group-participant.repository.postgres :as gp-postgres-repo]
13 : [matchscore.domain.group.command :as group-cmd]
14 : [matchscore.domain.group.repository.postgres :as group-postgres-repo]
15 : [matchscore.domain.match-result.repository.postgres :as match-result-postgres-repo]
16 : [matchscore.domain.match-segment.repository.postgres :as match-segment-postgres-repo]
17 : [matchscore.domain.match.repository.postgres :as match-postgres-repo]
18 : [matchscore.domain.stage.repository.postgres :as stage-postgres-repo]
19 : [matchscore.utils.errors :as utils.errors]
20 : [matchscore.utils.nanoid :as nanoid]
21 : [matchscore.utils.uuid :as uuid-utils]))
22 :
23 :
24 4 : (defn delete-existing-stages!
25 : [{:keys [repos]} class-uuid]
26 8 : (let [repo (:stage-postgres-repo repos)
27 8 : stages (stage-postgres-repo/find-by-class-id repo class-uuid)]
28 34 : (doseq [{:keys [uuid]} stages] (stage-postgres-repo/delete! repo uuid))
29 6 : (either/right nil)))
30 :
31 :
32 4 : (defn next-power-of-2
33 : [n]
34 24 : (loop [p 1] (if (>= p n) p (recur (* 2 p)))))
35 :
36 :
37 4 : (defn group-name
38 : [idx]
39 16 : (str "Group " (char (+ (int \A) idx))))
40 :
41 :
42 4 : (defn generate-bracket-ranges
43 : [from to]
44 18 : (let [size (- to from -1)]
45 15 : (when (>= size 2)
46 15 : (let [current {:placement-from from
47 3 : :placement-to to}]
48 12 : (if (= size 2)
49 6 : [current]
50 : ;; Long/highestOneBit isolates the most-significant set bit (equivalent to
51 : ;; 2^floor(log2(n)) for positive n), and Long/numberOfTrailingZeros counts its
52 : ;; position — giving an exact integer log2 with no floating-point involvement.
53 21 : (let [rounds (Long/numberOfTrailingZeros (Long/highestOneBit (long size)))]
54 6 : (into
55 6 : [current]
56 6 : (mapcat
57 3 : (fn [r]
58 24 : (let [block (long (/ size (Math/pow 2 r)))]
59 36 : (generate-bracket-ranges (+ from block) (+ from (* block 2) -1))))
60 18 : (reverse (range 1 rounds))))))))))
61 :
62 :
63 4 : (defn upsert-or-fail
64 : [label upsert-fn data]
65 12 : (let [[ok? result error-message] (upsert-fn data)]
66 6 : (if ok?
67 9 : (either/right result)
68 0 : (either/left
69 0 : (utils.errors/build-error
70 : :database
71 0 : {:code (keyword (str "error." label) "upsert-failed")
72 0 : :message error-message})))))
73 :
74 :
75 4 : (defn create-group-participants!
76 : [gp-repo group-uuid group-size]
77 4 : (reduce
78 2 : (fn [acc slot]
79 16 : (c/mlet [_ acc]
80 8 : (upsert-or-fail
81 : "group-participant"
82 8 : #(gp-postgres-repo/upsert! gp-repo %)
83 24 : {:uuid (uuid-utils/generate-uuid7-str)
84 2 : :group-id group-uuid
85 : :participant-id nil
86 : :status "pending"
87 2 : :slot slot
88 : :source "matchscore"})))
89 6 : (either/right nil)
90 10 : (range 1 (inc group-size))))
91 :
92 :
93 4 : (defn- create-group-matches!
94 : [match-repo group-uuid stage-id group-size]
95 154 : (let [pairs (for [i (range 1 (inc group-size)) j (range (inc i) (inc group-size))] [i j])]
96 4 : (reduce
97 2 : (fn [acc [order _pair]]
98 16 : (c/mlet [_ acc]
99 8 : (upsert-or-fail
100 : "match"
101 8 : #(match-postgres-repo/save! match-repo %)
102 18 : {:uuid (uuid-utils/generate-uuid7-str)
103 2 : :stage-id stage-id
104 2 : :group-id group-uuid
105 2 : :match-order order
106 : :source "matchscore"})))
107 6 : (either/right nil)
108 4 : (map-indexed
109 2 : (fn [idx pair]
110 8 : [(inc idx) pair])
111 2 : pairs))))
112 :
113 :
114 4 : (defn create-groups!
115 : ([ctx gp-repo draw-uuid stage-id groups-count group-size]
116 18 : (create-groups! ctx gp-repo draw-uuid stage-id groups-count group-size group-name))
117 : ([ctx gp-repo draw-uuid stage-id groups-count group-size name-fn]
118 12 : (let [match-repo (:match-postgres-repo (:repos ctx))]
119 4 : (reduce
120 2 : (fn [acc idx]
121 58 : (c/mlet [_ acc
122 8 : group (group-cmd/create-group!
123 2 : ctx
124 20 : {:uuid (uuid-utils/generate-uuid7-str)
125 6 : :public-id (nanoid/generate 15)
126 2 : :draw-id draw-uuid
127 6 : :name (name-fn idx)
128 4 : :order (inc idx)
129 : :source "matchscore"})
130 16 : _ (create-group-participants! gp-repo (:uuid group) group-size)
131 16 : _ (create-group-matches! match-repo (:uuid group) stage-id group-size)]
132 6 : (c/return nil)))
133 6 : (either/right nil)
134 6 : (range groups-count)))))
135 :
136 :
137 4 : (defn- create-match-slots!
138 : [bms-repo match-uuid round-number match-idx prev-round-match-uuids loser-of-match-uuids]
139 42 : (let [slot-specs (cond (and (= round-number 1) (seq loser-of-match-uuids))
140 27 : [{:slot 1
141 : :source-type "loser_of"
142 : :source-seed nil
143 15 : :source-match-id (nth loser-of-match-uuids (* match-idx 2))}
144 24 : {:slot 2
145 : :source-type "loser_of"
146 : :source-seed nil
147 21 : :source-match-id (nth loser-of-match-uuids (+ (* match-idx 2) 1))}]
148 :
149 9 : (= round-number 1)
150 27 : [{:slot 1
151 : :source-type "seed"
152 15 : :source-seed (+ (* match-idx 2) 1)
153 : :source-match-id nil}
154 24 : {:slot 2
155 : :source-type "seed"
156 15 : :source-seed (+ (* match-idx 2) 2)
157 : :source-match-id nil}]
158 :
159 : :else
160 27 : [{:slot 1
161 : :source-type "winner_of"
162 : :source-seed nil
163 15 : :source-match-id (nth prev-round-match-uuids (* match-idx 2))}
164 24 : {:slot 2
165 : :source-type "winner_of"
166 : :source-seed nil
167 21 : :source-match-id (nth prev-round-match-uuids (+ (* match-idx 2) 1))}])]
168 6 : (reduce
169 3 : (fn [acc {:keys [slot source-type source-seed source-match-id]}]
170 24 : (c/mlet [_ acc]
171 12 : (upsert-or-fail
172 : "bracket-match-slot"
173 12 : #(bms-postgres-repo/upsert! bms-repo %)
174 33 : {:uuid (uuid-utils/generate-uuid7-str)
175 3 : :bracket-match-id match-uuid
176 3 : :slot slot
177 3 : :source-type source-type
178 3 : :source-seed source-seed
179 3 : :source-match-id source-match-id
180 : :source "matchscore"})))
181 9 : (either/right nil)
182 3 : slot-specs)))
183 :
184 :
185 4 : (defn- create-round!
186 : [repos
187 : bracket-uuid
188 : round-number
189 : matches-count
190 : prev-round-match-uuids
191 : loser-of-match-uuids
192 : stage-id]
193 12 : (let [br-repo (:bracket-round-postgres-repo repos)
194 9 : bm-repo (:bracket-match-postgres-repo repos)
195 9 : bms-repo (:bracket-match-slot-postgres-repo repos)
196 9 : match-repo (:match-postgres-repo repos)
197 6 : round-uuid (uuid-utils/generate-uuid7-str)]
198 33 : (c/mlet [_ (upsert-or-fail
199 : "bracket-round"
200 12 : #(br-postgres-repo/upsert! br-repo %)
201 24 : {:uuid round-uuid
202 3 : :bracket-id bracket-uuid
203 12 : :name (str "Round " round-number)
204 3 : :round-number round-number
205 : :source "matchscore"})]
206 6 : (reduce
207 3 : (fn [acc-either match-idx]
208 24 : (c/mlet [match-uuids acc-either]
209 9 : (let [match-uuid (uuid-utils/generate-uuid7-str)]
210 75 : (c/mlet [_ (upsert-or-fail
211 : "bracket-match"
212 12 : #(bm-postgres-repo/upsert! bm-repo %)
213 21 : {:uuid match-uuid
214 3 : :bracket-round-id round-uuid
215 6 : :match-number (inc match-idx)
216 : :source "matchscore"})
217 9 : _ (create-match-slots!
218 3 : bms-repo
219 3 : match-uuid
220 3 : round-number
221 3 : match-idx
222 3 : prev-round-match-uuids
223 3 : loser-of-match-uuids)
224 12 : _ (upsert-or-fail
225 : "match"
226 12 : #(match-postgres-repo/save! match-repo %)
227 27 : {:uuid (uuid-utils/generate-uuid7-str)
228 3 : :stage-id stage-id
229 3 : :bracket-match-id match-uuid
230 6 : :match-order (inc match-idx)
231 : :source "matchscore"})]
232 18 : (c/return (conj match-uuids match-uuid))))))
233 9 : (either/right [])
234 9 : (range matches-count)))))
235 :
236 :
237 4 : (defn create-bracket-rounds-and-matches!
238 : ([repos bracket-uuid bracket-size stage-id]
239 21 : (create-bracket-rounds-and-matches! repos bracket-uuid bracket-size stage-id nil))
240 : ([repos bracket-uuid bracket-size stage-id first-round-loser-source-uuids]
241 21 : (let [total-rounds (Long/numberOfTrailingZeros (Long/highestOneBit (long bracket-size)))]
242 9 : (loop [r 1
243 : prev-uuids nil
244 3 : rounds-map {}]
245 12 : (if (> r total-rounds)
246 9 : (either/right rounds-map)
247 24 : (let [matches-count (long (/ bracket-size (Math/pow 2 r)))
248 18 : loser-uuids (when (= r 1) first-round-loser-source-uuids)
249 : result
250 27 : (create-round! repos bracket-uuid r matches-count prev-uuids loser-uuids stage-id)]
251 12 : (if (either/left? result)
252 0 : result
253 12 : (let [match-uuids (c/extract result)]
254 3 : (recur (inc r) match-uuids (assoc rounds-map r match-uuids))))))))))
255 :
256 :
257 4 : (defn- find-parent-round
258 : "Returns [parent-placement-from parent-round-number] for a sub-bracket given already-created
259 : bracket ranges, or nil for the root bracket. Uses the formula: F = P + S / 2^R."
260 : [known-ranges {:keys [placement-from]}]
261 6 : (some
262 3 : (fn
263 : [{p-from :placement-from
264 : p-to :placement-to}]
265 18 : (let [p-size (- p-to p-from -1)
266 18 : rounds (Long/numberOfTrailingZeros (Long/highestOneBit (long p-size)))]
267 6 : (some
268 3 : (fn [r]
269 48 : (when (= placement-from (+ p-from (long (/ p-size (Math/pow 2 r))))) [p-from r]))
270 15 : (range 1 (inc rounds)))))
271 3 : known-ranges))
272 :
273 :
274 4 : (defn create-brackets-monrad!
275 : "Creates brackets for a Monrad draw, wiring sub-bracket round 1 slots to loser_of the
276 : corresponding parent bracket round. The root bracket (placement-from=1) keeps seed slots.
277 : label-offset shifts displayed bracket names to reflect global placement (e.g. a consolation
278 : draw covering global ranks 9-16 passes label-offset=8) without changing the
279 : placement-from/placement-to config, which stays local 1..N since the scoreboard walker
280 : (matchscore.application.scoreboard.monrad) relies on it to find each draw's root bracket
281 : and to range over its own placements. n is padded up to the next power of 2 for bracket
282 : sizing; actual-count (defaults to n) is the real, possibly non-power-of-2 entrant count and
283 : is used to clamp the root bracket's displayed upper bound so padded/bye slots aren't shown
284 : as real places."
285 8 : ([ctx draw-uuid n stage-id] (create-brackets-monrad! ctx draw-uuid n stage-id 0 n))
286 : ([ctx draw-uuid n stage-id label-offset]
287 0 : (create-brackets-monrad! ctx draw-uuid n stage-id label-offset n))
288 : ([ctx draw-uuid n stage-id label-offset actual-count]
289 21 : (let [ranges (generate-bracket-ranges 1 (next-power-of-2 n))
290 3 : name-fn (fn [{:keys [placement-from placement-to]}]
291 15 : (let [placement-to (if (= placement-from 1)
292 9 : (min placement-to actual-count)
293 3 : placement-to)]
294 12 : (str
295 9 : "Places " (+ placement-from label-offset)
296 9 : "-" (+ placement-to label-offset))))]
297 21 : (c/mlet [_result
298 6 : (reduce
299 3 : (fn [acc-either range-entry]
300 24 : (c/mlet [{:keys [known rounds-index]} acc-either]
301 6 : (let [{:keys [placement-from placement-to]} range-entry
302 15 : bracket-size (- placement-to placement-from -1)
303 12 : parent-info (find-parent-round known range-entry)
304 9 : loser-uuids (when parent-info
305 6 : (let [[p-from p-round] parent-info]
306 18 : (get-in rounds-index [p-from p-round])))]
307 48 : (c/mlet [bracket (bracket-cmd/create-bracket!
308 3 : ctx
309 30 : {:uuid (uuid-utils/generate-uuid7-str)
310 9 : :public-id (nanoid/generate 15)
311 3 : :draw-id draw-uuid
312 9 : :name (name-fn range-entry)
313 12 : :config {:placement-from placement-from
314 3 : :placement-to placement-to}
315 : :source "matchscore"})
316 6 : bracket-rounds-map (create-bracket-rounds-and-matches!
317 9 : (:repos ctx)
318 9 : (:uuid bracket)
319 3 : bracket-size
320 3 : stage-id
321 3 : loser-uuids)]
322 6 : (c/return
323 21 : {:known (conj known range-entry)
324 15 : :rounds-index (assoc rounds-index placement-from bracket-rounds-map)})))))
325 6 : (either/right
326 12 : {:known []
327 3 : :rounds-index {}})
328 3 : ranges)]
329 9 : (c/return nil)))))
330 :
331 :
332 4 : (defn create-brackets!
333 7 : ([ctx draw-uuid n stage-id] (create-brackets! ctx draw-uuid n stage-id nil))
334 : ([ctx draw-uuid n stage-id name-fn]
335 7 : (let [ranges (generate-bracket-ranges 1 (next-power-of-2 n))
336 4 : name-fn' (or
337 1 : name-fn
338 : (fn [{:keys [placement-from placement-to]}]
339 5 : (str placement-from "-" placement-to)))]
340 2 : (reduce
341 1 : (fn
342 : [acc
343 : {:keys [placement-from placement-to]
344 : :as range-entry}]
345 6 : (let [bracket-size (- placement-to placement-from -1)]
346 22 : (c/mlet [_ acc
347 3 : bracket (bracket-cmd/create-bracket!
348 1 : ctx
349 10 : {:uuid (uuid-utils/generate-uuid7-str)
350 3 : :public-id (nanoid/generate 15)
351 1 : :draw-id draw-uuid
352 3 : :name (name-fn' range-entry)
353 4 : :config {:placement-from placement-from
354 1 : :placement-to placement-to}
355 : :source "matchscore"})
356 2 : _ (create-bracket-rounds-and-matches!
357 3 : (:repos ctx)
358 3 : (:uuid bracket)
359 1 : bracket-size
360 1 : stage-id)]
361 3 : (c/return nil))))
362 3 : (either/right nil)
363 1 : ranges))))
364 :
365 :
366 4 : (defn create-quals-brackets!
367 : "Creates n independent size-2 brackets (one-match finals) in the draw.
368 : Returns Either<error, [match-uuid ...]> — one final-match UUID per bracket."
369 : [ctx draw-uuid stage-uuid n]
370 2 : (reduce
371 1 : (fn [acc i]
372 22 : (c/mlet [match-uuids acc
373 3 : bracket (bracket-cmd/create-bracket!
374 1 : ctx
375 10 : {:uuid (uuid-utils/generate-uuid7-str)
376 3 : :public-id (nanoid/generate 15)
377 1 : :draw-id draw-uuid
378 5 : :name (str "Qual " (inc i))
379 7 : :config {:placement-from (inc (* 2 i))
380 5 : :placement-to (+ 2 (* 2 i))}
381 : :source "matchscore"})
382 : rounds-map
383 10 : (create-bracket-rounds-and-matches! (:repos ctx) (:uuid bracket) 2 stage-uuid)]
384 10 : (c/return (conj match-uuids (first (get rounds-map 1))))))
385 3 : (either/right [])
386 3 : (range n)))
387 :
388 :
389 4 : (defn make-tx-repos
390 : [repos tx-opts]
391 8 : (merge
392 4 : repos
393 64 : {:stage-postgres-repo (stage-postgres-repo/make-repository tx-opts)
394 12 : :draw-postgres-repo (draw-postgres-repo/make-repository tx-opts)
395 12 : :group-postgres-repo (group-postgres-repo/make-repository tx-opts)
396 12 : :group-participant-postgres-repo (gp-postgres-repo/make-repository tx-opts)
397 12 : :bracket-postgres-repo (bracket-postgres-repo/make-repository tx-opts)
398 12 : :bracket-round-postgres-repo (br-postgres-repo/make-repository tx-opts)
399 12 : :bracket-match-postgres-repo (bm-postgres-repo/make-repository tx-opts)
400 12 : :bracket-match-slot-postgres-repo (bms-postgres-repo/make-repository tx-opts)
401 12 : :advancement-rule-postgres-repo (advancement-rule-postgres-repo/make-repository tx-opts)
402 12 : :match-postgres-repo (match-postgres-repo/make-repository tx-opts)
403 12 : :match-result-postgres-repo (match-result-postgres-repo/make-repository tx-opts)
404 12 : :match-segment-postgres-repo (match-segment-postgres-repo/make-repository tx-opts)}))
|