LCOV - code coverage report
Current view: top level - matchscore/application/scoreboard - monrad.clj Coverage Total Hit
Test: lcov.info Lines: 95.8 % 72 69
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.application.scoreboard.monrad
       2              :   "Walks a Monrad bracket draw to produce final 1..N placements.
       3              : 
       4              :    create-brackets-monrad! lays out one bracket per disjoint placement pair
       5              :    ({1-N}, {placement-from .. placement-to}). Each bracket's final round has
       6              :    exactly one match: its winner takes :placement-from and its loser takes
       7              :    :placement-from + 1. The combined output is therefore a single ordered
       8              :    placement list covering 1..N, with permanently-unfillable positions
       9              :    (byes/walkovers with no real opponent — see
      10              :    matchscore.application.advancement.bye) compacted out so the published
      11              :    ranking stays a dense 1..(real participant count) sequence instead of
      12              :    leaving numbered gaps."
      13              :   (:require
      14              :    [matchscore.application.advancement.bracket-reconcile :as bracket-reconcile]
      15              :    [matchscore.domain.bracket-match-slot.repository.postgres :as bms-postgres-repo]
      16              :    [matchscore.domain.bracket-match.repository.postgres :as bracket-match-postgres-repo]
      17              :    [matchscore.domain.bracket-round.repository.postgres :as bracket-round-postgres-repo]
      18              :    [matchscore.domain.bracket.repository.postgres :as bracket-postgres-repo]))
      19              : 
      20              : 
      21            4 : (defn- main-bracket
      22              :   "Returns the bracket with placement-from=1 (or the first bracket as fallback)."
      23              :   [brackets]
      24           19 :   (or (some #(when (= 1 (get-in % [:config :placement-from])) %) brackets) (first brackets)))
      25              : 
      26              : 
      27           18 : (defn- max-placement [brackets] (reduce max 0 (keep #(get-in % [:config :placement-to]) brackets)))
      28              : 
      29              : 
      30            4 : (defn- final-match
      31              :   [repos bracket]
      32            3 :   (let [rounds      (bracket-round-postgres-repo/find-by-bracket-id
      33            3 :                      (:bracket-round-postgres-repo repos)
      34            3 :                      (:uuid bracket))
      35            6 :         final-round (last (sort-by :round-number rounds))]
      36            3 :     (when final-round
      37            3 :       (let [matches (bracket-match-postgres-repo/find-by-bracket-round-id
      38            3 :                      (:bracket-match-postgres-repo repos)
      39            3 :                      (:uuid final-round))]
      40            6 :         (last (sort-by :match-number matches))))))
      41              : 
      42              : 
      43            8 : (def ^:private permanent-gap
      44              :   "Sentinel for a placement that will NEVER have a participant — as opposed
      45              :    to nil, which means merely 'not decided yet'. Distinguished by checking
      46              :    whether the final match's own slots were marked bye by
      47              :    matchscore.application.advancement.bye (a walkover's loser side is a
      48              :    permanent gap the moment the bye is marked, even before the winner side
      49              :    is actually decided; a bye-vs-bye void match is a permanent gap on both
      50              :    sides)."
      51              :   ::gap)
      52              : 
      53              : 
      54            4 : (defn- pending-match
      55              :   "Returns the known pairing that will decide the bracket's two placements,
      56              :    or nil until both participants are known. A final with a result, or one
      57              :    containing a bye, is not pending."
      58              :   [final-match slots outcome any-bye?]
      59            8 :   (let [participant-ids (mapv (comp str :participant-id) slots)]
      60            2 :     (when
      61           15 :       (and
      62            1 :        final-match
      63            4 :        (nil? outcome)
      64            3 :        (not any-bye?)
      65            4 :        (= 2 (count slots))
      66            2 :        (every? :participant-id slots))
      67           10 :       {:id     (str (:uuid final-match))
      68              :        :status "pending"
      69            1 :        :participant-ids participant-ids})))
      70              : 
      71              : 
      72            4 : (defn- bracket-placements
      73              :   "Returns each of the bracket's two placements as a participant id, known
      74              :    pending matchup, permanent gap, or nil. nil means the participants are not
      75              :    known yet; permanent-gap means structurally no participant will ever occupy
      76              :    that position."
      77              :   [repos bracket]
      78            7 :   (let [from      (get-in bracket [:config :placement-from])
      79            4 :         fm        (final-match repos bracket)
      80            3 :         slots     (when fm
      81            2 :                     (bms-postgres-repo/find-by-bracket-match-id
      82            3 :                      (:bracket-match-slot-postgres-repo repos)
      83            3 :                      (:uuid fm)))
      84           10 :         any-bye?  (boolean (some #(= "bye" (:source-type %)) slots))
      85           15 :         both-bye? (and (seq slots) (every? #(= "bye" (:source-type %)) slots))
      86           11 :         outcome   (when fm (bracket-reconcile/compute-source-outcome repos (str (:uuid fm))))
      87            6 :         pending   (pending-match fm slots outcome any-bye?)]
      88            4 :     (cond both-bye?
      89            0 :           {from       permanent-gap
      90            0 :            (inc from) permanent-gap}
      91              : 
      92            1 :           any-bye?
      93           10 :           {from       (some->
      94            3 :                         (:winner-id outcome)
      95            1 :                         str)
      96            3 :            (inc from) permanent-gap}
      97              : 
      98              :           :else
      99           10 :           (let [winner (some->
     100            3 :                          (:winner-id outcome)
     101            1 :                          str)
     102            9 :                 loser  (some->
     103            3 :                          (:loser-id outcome)
     104            1 :                          str)]
     105            8 :             {from       (or winner pending)
     106            8 :              (inc from) (or loser pending)}))))
     107              : 
     108              : 
     109            4 : (defn compute-monrad-placements
     110              :  "Returns a single-element vector wrapping the main bracket of the draw:
     111              : 
     112              :      [{:type \"bracket\"
     113              :        :name <main bracket name>
     114              :        :results [{:place 1 :participant-id <pid-or-nil>} ...]}]
     115              : 
     116              :    Placements 1..N are filled by walking each Monrad sub-bracket's final
     117              :    round. Positions that are merely not decided yet stay in the list as nil,
     118              :    with their pending matchup attached when both participants are known.
     119              :    Positions that can never be filled (fewer real participants than the
     120              :    bracket's structural capacity) are dropped and the remaining positions
     121              :    renumbered, so e.g. 6 real participants in an 8-place draw publish as a
     122              :    dense 1..6 instead of 1 2 3 4 5 nil 7 nil."
     123              :  [repos draw-uuid]
     124            7 :  (let [brackets (bracket-postgres-repo/find-by-draw-id (:bracket-postgres-repo repos) draw-uuid)]
     125            4 :    (if (empty? brackets)
     126            0 :      []
     127            4 :      (let [n        (max-placement brackets)
     128           12 :            by-place (transduce (map #(bracket-placements repos %)) merge {} brackets)
     129            3 :            main     (main-bracket brackets)
     130              :            ;; remove (not keep) — a pending position's value IS nil and must
     131              :            ;; survive; only the permanent-gap sentinel gets dropped.
     132           19 :            kept     (into [] (comp (map by-place) (remove #(= % permanent-gap))) (range 1 (inc n)))]
     133            6 :        [{:type    "bracket"
     134            3 :          :name    (:name main)
     135            2 :          :results (into
     136            1 :                    []
     137            2 :                    (map-indexed
     138            1 :                     (fn [idx pid]
     139            6 :                      (cond->
     140            5 :                        {:place (inc idx)
     141            7 :                         :participant-id (when-not (map? pid) pid)}
     142            5 :                        (map? pid) (assoc :pending-match pid))))
     143            1 :                    kept)}]))))
        

Generated by: LCOV version 2.0-1