Line data Source code
1 4 : (ns matchscore.application.scoreboard.get
2 : "Application service that builds a tournament scoreboard for a class:
3 : loads the last stage, iterates its draws, and dispatches per draw type
4 : to compute final placements wrapped under group/bracket entries."
5 : (:require
6 : [cats.monad.either :as either]
7 : [matchscore.application.scoreboard.groups :as scoreboard-groups]
8 : [matchscore.application.scoreboard.monrad :as scoreboard-monrad]
9 : [matchscore.application.scoreboard.participants :as scoreboard-participants]
10 : [matchscore.domain.class.query :as class-query]
11 : [matchscore.domain.draw.query :as draw-query]
12 : [matchscore.domain.stage.query :as stage-query]
13 : [matchscore.utils.errors :as utils.errors]))
14 :
15 :
16 4 : (defn- draw-results
17 : "Returns a seq of result entries (group or bracket) for a single draw."
18 : [repos draw]
19 5 : (case (:type draw)
20 6 : "group" (scoreboard-groups/compute-group-placements repos (:uuid draw))
21 6 : "bracket" (scoreboard-monrad/compute-monrad-placements repos (:uuid draw))
22 0 : []))
23 :
24 :
25 4 : (defn- attach-participants
26 : [entries enriched-by-id]
27 2 : (mapv
28 1 : (fn [entry]
29 3 : (update
30 1 : entry
31 : :results
32 1 : (fn [results]
33 2 : (mapv
34 1 : (fn [{:keys [place participant-id pending-match]}]
35 6 : (cond->
36 4 : {:place place
37 6 : :participant (when participant-id (get enriched-by-id participant-id))}
38 2 : pending-match (assoc :pending-match
39 2 : (->
40 1 : pending-match
41 3 : (dissoc :participant-ids)
42 4 : (assoc :participants (mapv
43 3 : #(get enriched-by-id %)
44 3 : (:participant-ids pending-match)))))))
45 1 : results))))
46 1 : entries))
47 :
48 :
49 4 : (defn- collect-participant-ids
50 : [draw-entries]
51 1 : (->>
52 1 : draw-entries
53 3 : (mapcat :results)
54 3 : (mapcat :results)
55 1 : (mapcat
56 1 : (fn [{:keys [participant-id pending-match]}]
57 11 : (cond-> (:participant-ids pending-match) participant-id (conj participant-id))))))
58 :
59 :
60 4 : (defn execute!
61 : "Builds the scoreboard for a class. Returns Either<Error, [draw-entry]>.
62 :
63 : Each draw entry:
64 : {:draw-uuid <str>
65 : :draw-name <str>
66 : :draw-type <str>
67 : :results [<group-or-bracket-entry> ...]}"
68 : [{:keys [repos]} event-uuid class-uuid]
69 4 : (let [class-repo (:class-postgres-repo repos)
70 3 : stage-repo (:stage-postgres-repo repos)
71 3 : draw-repo (:draw-postgres-repo repos)
72 4 : class (class-query/get-class class-repo class-uuid)]
73 18 : (cond (or (nil? class) (not= event-uuid (str (:event-id class))))
74 2 : (either/left
75 3 : (utils.errors/build-error
76 : :not-found
77 6 : {:code :error.class/not-found
78 : :message "Class not found."
79 4 : :context {:class-uuid class-uuid
80 1 : :event-uuid event-uuid}}))
81 :
82 : :else
83 5 : (let [stages (stage-query/get-stages-by-class stage-repo class-uuid)
84 3 : last-stage (last stages)]
85 5 : (if (nil? last-stage)
86 3 : (either/right [])
87 7 : (let [draws (draw-query/get-draws-by-stage draw-repo (:uuid last-stage))
88 2 : draw-entries (mapv
89 1 : (fn [draw]
90 10 : {:draw-uuid (str (:uuid draw))
91 3 : :draw-name (:name draw)
92 3 : :draw-type (:type draw)
93 4 : :results (draw-results repos draw)})
94 1 : draws)
95 3 : pids (collect-participant-ids draw-entries)
96 4 : enriched (scoreboard-participants/load-enriched-by-ids repos pids)
97 2 : with-resolved (mapv
98 1 : (fn [entry]
99 6 : (update entry :results attach-participants enriched))
100 1 : draw-entries)]
101 3 : (either/right with-resolved)))))))
|