Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
arttuka committed Jul 22, 2018
2 parents 48849f2 + 5f6c638 commit faa93d8
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Vagrant.configure("2") do |config|

# Kehitystyön tietokantapalvein
config.vm.define "pairingsdb" do |db|
db.vm.box = "CentOS-6.4-x86_64-v20131103.box"
db.vm.box_url = "http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20131103.box"
db.vm.box = "bento/debian-9.5"
db.vm.box_version = "201807.12.0"

db.vm.synced_folder "env", "/env"
db.vm.provision "shell", inline: "cd /env && ./postgresql.sh"
Expand All @@ -15,4 +15,4 @@ Vagrant.configure("2") do |config|
db.vm.network "forwarded_port", host: 5432, guest: 5432
db.vm.network "private_network", ip: "192.168.50.51"
end
end
end
21 changes: 7 additions & 14 deletions env/postgresql.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
#!/bin/bash
set -eu

# konfiguroidaan postgre repo
rpm -i http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-centos92-9.2-6.noarch.rpm

# nyt yum osaa installoida postgren
yum -y install postgresql92-server postgresql92-contrib
# asennetaan postgre
apt-get install -y postgresql-9.6

# alustetaan postgre
service postgresql-9.2 initdb
service postgresql initdb

# postgre kuuntelemaan ja ottamaan vastaan yhteyksia muualta kuin localhostista
PG_HBA=/var/lib/pgsql/9.2/data/pg_hba.conf
PG_HBA=/etc/postgresql/9.6/main/pg_hba.conf

echo 'local all all peer' > "$PG_HBA"
echo 'host all all all md5' >> "$PG_HBA"

chown postgres:postgres "$PG_HBA"

echo "listen_addresses = '*'" >> /var/lib/pgsql/9.2/data/postgresql.conf

# poistetaan palomuuri
iptables -F
service iptables save
echo "listen_addresses = '*'" >> /etc/postgresql/9.6/main/postgresql.conf

# kayntiin
service postgresql-9.2 start
chkconfig postgresql-9.2 on
service postgresql start
update-rc.d postgresql defaults

sudo -u postgres psql --file=db.sql
1 change: 1 addition & 0 deletions mtg-pairings-server/resources/private/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ create table round(
id serial primary key,
num int not null,
tournament int not null references tournament(id),
playoff boolean default false,
unique (tournament, num)
);

Expand Down
85 changes: 85 additions & 0 deletions mtg-pairings-server/resources/public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,68 @@ i.pull-left {
text-align: center;
}

#bracket {
margin-left: 10px;
}

#bracket .bracket-round {
display: inline-block;
vertical-align: top;
}

#bracket .bracket-match .team {
padding: 20px 20px 0 10px;
line-height: 20px;
min-width: 200px;
}

#bracket .bracket-match .team span {
vertical-align: bottom;
}

#bracket .bracket-match .team2 {
border-top: 1px solid #333;
border-right: 1px solid #333;
border-bottom: 1px solid #333;
}

#bracket .bracket-match .winner {
font-weight: bold;
}

#bracket .bracket-match .rank {
color: #999;
margin-right: 10px;
}

#bracket .bracket-match .wins {
float: right;
margin-left: 10px;
}

#bracket .bracket-round.matches-4 .team {
height: 44px;
padding-top: 20px;
}

#bracket .bracket-round.matches-2 .team {
height: 88px;
padding-top: 66px;
}

#bracket .bracket-round.matches-2 {
margin-top: -22px;
}

#bracket .bracket-round.matches-1 .team {
height: 176px;
padding-top: 154px;
}

#bracket .bracket-round.matches-1 {
margin-top: -66px;
}

h3 {
font-size:20px !important;
}
Expand Down Expand Up @@ -437,6 +499,10 @@ h3 {
width: 100px;
}

.tournament-row a.wide {
width: 210px;
}

#deck-construction .pod {
display: inline-block;
min-width: 30%;
Expand Down Expand Up @@ -489,6 +555,25 @@ h3 {
.pairing.no-round {
height: 60px;
}

#bracket .bracket-round.matches-2 .team,
#bracket .bracket-round.matches-1 .team {
height: 44px;
padding-top: 20px;
}

#bracket .bracket-round.matches-2,
#bracket .bracket-round.matches-1 {
margin-top: 0;
}

#bracket h3 {
margin-bottom: 0;
}

#bracket {
padding-bottom: 20px;
}
}

@media (min-width: 768px) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
:summary "Lisää kierroksen pairingit"
:query-params [key :- String]
(validate-request sanctionid key
(add-pairings sanctionid round (:pairings pairings))
(add-pairings sanctionid round (:playoff pairings) (:pairings pairings))
(broadcast-tournament sanctionid true)
{:status 204}))
(PUT "/:sanctionid/round-:round/results" []
Expand Down
5 changes: 5 additions & 0 deletions mtg-pairings-server/src/clj/mtg_pairings_server/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
(GET "/tournaments/:id/standings-:round" [] (loading-page))
(GET "/tournaments/:id/pods-:round" [] (loading-page))
(GET "/tournaments/:id/seatings" [] (loading-page))
(GET "/tournaments/:id/bracket" [] (loading-page))
(GET "/tournaments/:id/organizer" [] (loading-page))
(GET "/tournaments/:id/organizer/menu" [] (loading-page))
(GET "/tournaments/:id/organizer/deck-construction" [] (loading-page))
Expand Down Expand Up @@ -100,6 +101,10 @@
[{uid :uid, id :?data}]
(ws/send! uid [:server/seatings [id (tournament/seatings id)]]))

(defmethod ws/event-handler :client/bracket
[{uid :uid, id :?data}]
(ws/send! uid [:server/bracket [id (tournament/bracket id)]]))

(defmethod ws/event-handler :client/organizer-tournament
[{uid :uid, id :?data}]
(broadcast/watch uid id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
(->
(sql/select* db/tournament)
(sql/fields :rounds :day :name :organizer :id
(sql/raw "exists (select 1 from seating where \"tournament\" = \"tournament\".\"id\") as \"seatings\""))
(sql/raw "exists (select 1 from seating where \"tournament\" = \"tournament\".\"id\") as \"seatings\"")
(sql/raw "exists (select 1 from round where \"tournament\" = \"tournament\".\"id\" and playoff) as \"playoff\""))
(sql/order :day :DESC)
(sql/order :name :ASC)))

Expand Down Expand Up @@ -72,8 +73,9 @@
:results])
(sql/order :num)
(sql/where
(sql/sqlfn "exists" (sql/subselect db/pairing
(sql/where {:round :round.id})))))
(and (sql/sqlfn "exists" (sql/subselect db/pairing
(sql/where {:round :round.id})))
(not :playoff))))
(sql/with db/standings
(sql/where {:hidden false})
(sql/fields [:round :num])
Expand All @@ -97,8 +99,9 @@
:results])
(sql/order :num)
(sql/where
(sql/sqlfn "exists" (sql/subselect db/pairing
(sql/where {:round :round.id})))))
(and (sql/sqlfn "exists" (sql/subselect db/pairing
(sql/where {:round :round.id})))
(not :playoff))))
(util/group-kv :tournament #(select-keys % [:num :results])))
standings (->>
(sql/select db/standings
Expand Down Expand Up @@ -261,14 +264,15 @@
(map #(select-keys % [:rank :team_name :points :omw :pgw :ogw Double])
(standings tournament-id round-num hidden?)))

(defn ^:private get-or-add-round [tournament-id round-num]
(defn ^:private get-or-add-round [tournament-id round-num playoff?]
(if-let [old-round (first (sql/select db/round
(sql/where {:tournament tournament-id
:num round-num})))]
(:id old-round)
(:id (sql/insert db/round
(sql/values {:tournament tournament-id
:num round-num})))))
:num round-num
:playoff playoff?})))))

(defn teams-by-dci [tournament-id]
(let [team-players (sql/select db/team-players
Expand Down Expand Up @@ -310,14 +314,14 @@
(sql/where {:tournament tournament-id
:round round-num}))))

(defn add-pairings [sanction-id round-num pairings]
(defn add-pairings [sanction-id round-num playoff? pairings]
(let [tournament-id (sanctionid->id sanction-id)
dci->id (teams-by-dci tournament-id)
team->points (if-let [standings (standings tournament-id (dec round-num) true)]
(into {} (for [row standings]
[(:team row) (:points row)]))
(constantly 0))
round-id (get-or-add-round tournament-id round-num)]
round-id (get-or-add-round tournament-id round-num playoff?)]
(when (seq pairings)
(delete-results round-id)
(delete-pairings round-id)
Expand Down Expand Up @@ -397,9 +401,9 @@

(defn add-results [sanction-id round-num results]
(let [tournament-id (sanctionid->id sanction-id)
round-id (:id (first (sql/select db/round
(sql/where {:tournament tournament-id
:num round-num}))))]
{round-id :id, playoff? :playoff} (first (sql/select db/round
(sql/where {:tournament tournament-id
:num round-num})))]
(when (seq results)
(delete-results round-id)
(delete-standings tournament-id round-num)
Expand All @@ -410,7 +414,8 @@
:team1_wins (:team1_wins res)
:team2_wins (:team2_wins res)
:draws (:draws res)})))
(calculate-standings tournament-id round-num))))
(when-not playoff?
(calculate-standings tournament-id round-num)))))

(defn publish-results [sanction-id round-num]
(let [tournament-id (sanctionid->id sanction-id)]
Expand Down Expand Up @@ -552,3 +557,41 @@
(delete-seatings tournament-id)
(sql/insert db/seating
(sql/values seatings))))

(defn ^:private match-with-team [team-id matches]
(util/some-value #(or (= team-id (:team1 %))
(= team-id (:team2 %)))
matches))

(defn ^:private add-ranks [team->rank round]
(for [match round]
(assoc match :team1_rank (team->rank (:team1 match))
:team2_rank (team->rank (:team2 match)))))

(defn ^:private add-empty-rounds [bracket]
(loop [num (/ (count (last bracket)) 2)
bracket (vec bracket)]
(if (>= num 1)
(recur (/ num 2) (conj bracket (repeat num {})))
bracket)))

(defn bracket [tournament-id]
(let [playoff-rounds (sql/select db/round
(sql/fields :id :num)
(sql/where {:tournament tournament-id
:playoff true})
(sql/order :num :DESC))
final-standings (standings tournament-id (dec (:num (last playoff-rounds))) false)
team->rank (into {} (map (juxt :team :rank)) final-standings)
playoff-matches (map (comp (partial add-ranks team->rank)
results-of-round
:id)
playoff-rounds)]
(when (seq playoff-matches)
(loop [acc (take 1 playoff-matches)
[current-matches & rounds] (rest playoff-matches)]
(if current-matches
(let [team-ids (mapcat (juxt :team1 :team2) (first acc))
round (map #(match-with-team % current-matches) team-ids)]
(recur (cons round acc) rounds))
(add-empty-rounds acc))))))
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
:results [Long]
:standings [Long]
:pods [Long]
:seatings Boolean}))
:seatings Boolean
:playoff Boolean}))

(s/defschema InputTournament (-> BaseTournament
(dissoc :id)
Expand Down Expand Up @@ -64,7 +65,8 @@
:team2 (s/maybe [String])
:table_number Long})

(s/defschema InputPairings {:pairings [InputPairing]})
(s/defschema InputPairings {:pairings [InputPairing]
:playoff Boolean})

(s/defschema InputResult {:team1 InputTeam
:team2 (s/maybe InputTeam)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
(defmethod event-handler :default [{:keys [event]}]
(log/debugf "Unhandled event %s" event)))

#?(:clj
(defmethod event-handler :chsk/ws-ping [_]))

;; Event router

(def ^:private ws-state
Expand Down
Loading

0 comments on commit faa93d8

Please sign in to comment.