From dd1263f7eedeee3946256b0fd0c10ac32be343c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Setni=C4=8Dka?= <setnicka@seznam.cz>
Date: Sat, 20 Nov 2021 21:00:44 +0100
Subject: [PATCH] =?UTF-8?q?DB=20a=20Score:=20P=C5=99id=C3=A1n=C3=AD=20scor?=
 =?UTF-8?q?e=5Fsuborder=20do=20tabulky=20participations=20a=20t=C5=99?=
 =?UTF-8?q?=C3=ADd=C4=9Bn=C3=AD=20podle=20n=C4=9Bj?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Issue #210
---
 db/db.ddl               | 1 +
 db/upgrade-20211120.sql | 4 ++++
 mo/db.py                | 1 +
 mo/score.py             | 5 +++--
 4 files changed, 9 insertions(+), 2 deletions(-)
 create mode 100644 db/upgrade-20211120.sql

diff --git a/db/db.ddl b/db/db.ddl
index 7e8b2506..24a6d828 100644
--- a/db/db.ddl
+++ b/db/db.ddl
@@ -177,6 +177,7 @@ CREATE TABLE participations (
 	contest_id	int		NOT NULL REFERENCES contests(contest_id),
 	place_id	int		NOT NULL REFERENCES places(place_id),	-- konkrétní soutěžní místo (default: region contestu)
 	state		part_state	NOT NULL,
+	score_suborder	int		DEFAULT NULL,
 	PRIMARY KEY (user_id, contest_id)
 );
 
diff --git a/db/upgrade-20211120.sql b/db/upgrade-20211120.sql
new file mode 100644
index 00000000..0aa0c4e6
--- /dev/null
+++ b/db/upgrade-20211120.sql
@@ -0,0 +1,4 @@
+SET ROLE 'mo_osmo';
+
+ALTER TABLE participations ADD COLUMN
+	score_suborder	int	DEFAULT NULL;
diff --git a/mo/db.py b/mo/db.py
index 7a98d8d1..6d16fc05 100644
--- a/mo/db.py
+++ b/mo/db.py
@@ -494,6 +494,7 @@ class Participation(Base):
     contest_id = Column(Integer, ForeignKey('contests.contest_id'), primary_key=True, nullable=False)
     place_id = Column(Integer, ForeignKey('places.place_id'), nullable=False)
     state = Column(Enum(PartState, name='part_state'), nullable=False)
+    score_suborder = Column(Integer, nullable=True)
 
     contest = relationship('Contest', primaryjoin='Participation.contest_id == Contest.contest_id')
     place = relationship('Place', primaryjoin='Participation.place_id == Place.place_id')
diff --git a/mo/score.py b/mo/score.py
index 961de099..f497e95e 100644
--- a/mo/score.py
+++ b/mo/score.py
@@ -273,7 +273,7 @@ class Score:
         if self.round.score_mode == db.RoundScoreMode.basic:
             # Základní mód - jen podle celkových bodů, se sdílenými místy
             for result in self._results.values():
-                result._order_key = [-result.get_total_points()]
+                result._order_key = [-result.get_total_points(), result.pion.score_suborder or 0]
 
         elif self.round.score_mode == db.RoundScoreMode.mo:
             self._add_mo_order_key()
@@ -382,7 +382,7 @@ class Score:
             # Pokud jsme našli stejný klíč, opakujeme cyklus s minulým kolem
             step += 1
 
-        # Na konec třídícího klíče přidáme ročník (menší ročník první)
+        # Na konec třídícího klíče přidáme ročník (menší ročník první) a dotřiďovací klíč
         for result in self._results.values():
             grade = normalize_grade(result.pant.grade)
             if grade == -1:
@@ -391,6 +391,7 @@ class Score:
                     f"Účastník {result.user.first_name} {result.user.last_name} má neplatný ročník {result.pant.grade}"
                 )
             result._order_key.append(grade)
+            result._order_key.append(result.pion.score_suborder or 0)
 
         if self._exists_same_order_key():
             self._add_message(
-- 
GitLab