<commit_before>(ns mtg-pairings-backend.mtg-util
  (:require [mtg-pairings-backend.util :refer [map-values]]))

(defn ^:private add-result [acc res]
  (let [result {:match-points (cond
                                (= (:wins res) (:losses res)) 1
                                (> (:wins res) (:losses res)) 3
                                :else 0)
               :game-points (+ (* 3 (:wins res))
                               (* 1 (:draws res)))
               :games-played (+ (:wins res) (:draws res) (:losses res))
               :matches-played 1}]
    (merge-with + acc result)))

(defn ^:private calculate-points-pgw [matches]
  (let [reduced (reduce add-result {:match-points 0, :game-points 0, :games-played 0, :matches-played 0} matches)]
    {:points (:match-points reduced)
     :pmw (max 0.33 (/ (:match-points reduced) (* 3 (:matches-played reduced))))
     :pgw (max 0.33 (/ (:game-points reduced) (* 3 (:games-played reduced))))
     :opponents (keep :team-2 matches)}))

(defn ^:private calculate-omw-ogw [teams-results]
  (map-values (fn [results] 
                (let [opponents (for [opp (:opponents results)]
                                  (teams-results opp))
                      cnt (count opponents)
                      omw (/ (reduce + 0 (map :pmw opponents)) cnt)
                      ogw (/ (reduce + 0 (map :pgw opponents)) cnt)]
                  (assoc results
                         :omw omw
                         :ogw ogw)))
              teams-results))

(defn calculate-standings [rounds]
  (let [matches (apply concat (vals rounds))
        all-matches (apply concat (for [match matches]
                                    [match
                                     {:team-1 (:team-2 match)
                                      :team-2 (:team-1 match)
                                      :wins (:losses match)
                                      :losses (:wins match)
                                      :draws (:draws match)}]))
        grouped-matches (group-by :team-1 all-matches)
        teams-results (map-values calculate-points-pgw grouped-matches)
        results (calculate-omw-ogw teams-results)
        lst (for [[team result] results]
              (assoc result :team team))]
    (reverse (sort-by (juxt :points :omw :pgw :ogw) lst))))<commit_msg>Lisätty standigseihin rank<commit_after>(ns mtg-pairings-backend.mtg-util
  (:require [mtg-pairings-backend.util :refer [map-values]]))

(defn ^:private add-result [acc res]
  (let [result {:match-points (cond
                                (= (:wins res) (:losses res)) 1
                                (> (:wins res) (:losses res)) 3
                                :else 0)
               :game-points (+ (* 3 (:wins res))
                               (* 1 (:draws res)))
               :games-played (+ (:wins res) (:draws res) (:losses res))
               :matches-played 1}]
    (merge-with + acc result)))

(defn ^:private calculate-points-pgw [matches]
  (let [reduced (reduce add-result {:match-points 0, :game-points 0, :games-played 0, :matches-played 0} matches)]
    {:points (:match-points reduced)
     :pmw (max 0.33 (/ (:match-points reduced) (* 3 (:matches-played reduced))))
     :pgw (max 0.33 (/ (:game-points reduced) (* 3 (:games-played reduced))))
     :opponents (keep :team-2 matches)}))

(defn ^:private calculate-omw-ogw [teams-results]
  (map-values (fn [results] 
                (let [opponents (for [opp (:opponents results)]
                                  (teams-results opp))
                      cnt (count opponents)
                      omw (/ (reduce + 0 (map :pmw opponents)) cnt)
                      ogw (/ (reduce + 0 (map :pgw opponents)) cnt)]
                  (assoc results
                         :omw omw
                         :ogw ogw)))
              teams-results))

(defn calculate-standings [rounds]
  (let [matches (apply concat (vals rounds))
        all-matches (apply concat (for [match matches]
                                    [match
                                     {:team-1 (:team-2 match)
                                      :team-2 (:team-1 match)
                                      :wins (:losses match)
                                      :losses (:wins match)
                                      :draws (:draws match)}]))
        grouped-matches (group-by :team-1 all-matches)
        teams-results (map-values calculate-points-pgw grouped-matches)
        results (calculate-omw-ogw teams-results)
        lst (for [[team result] results]
              (assoc result :team team))
        sorted (reverse (sort-by (juxt :points :omw :pgw :ogw) lst))]
    (map (fn [idx res] (assoc res :rank idx)) (iterate inc 1) sorted)))
{'fraction_non_alphanumeric': 0.1337968815844922, 'fraction_numerical': 0.011167298777918246, 'mean_word_length': 1.4749739311783108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137075', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1322, 'n_words': 382}