[games/chessament] src/tournament: Store time control period in JSON

Manuel Alcaraz Zambrano <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 6b89612a7869d99cc4e221e19112adac3ac155c8 by Manuel Alcaraz Zambrano.
Committed on 06/08/2026 at 12:36.
Pushed by manuelal into branch 'master'.

Store time control period in JSON

M  +32   -33   src/tournament/timecontrol.cpp
M  +3    -5    src/tournament/timecontrol.h

https://invent.kde.org/games/chessament/-/commit/6b89612a7869d99cc4e221e19112adac3ac155c8

diff --git a/src/tournament/timecontrol.cpp b/src/tournament/timecontrol.cpp
index 2f32803..c332834 100644
--- a/src/tournament/timecontrol.cpp
+++ b/src/tournament/timecontrol.cpp
@@ -10,73 +10,72 @@
 using namespace std::chrono_literals;
 using namespace Qt::StringLiterals;
 
+TimeControlPeriod::TimeControlPeriod()
+    : m_json{
+          {"time"_L1, 1},
+          {"increment"_L1, 0},
+      }
+{
+}
+
 TimeControlPeriod::TimeControlPeriod(std::optional<int> moves, int time, int increment)
-    : m_moves(moves)
-    , m_time(time)
-    , m_increment(increment)
+    : m_json{
+          {"time"_L1, time},
+          {"increment"_L1, increment},
+      }
 {
+    if (moves) {
+        m_json["moves"_L1] = moves.value();
+    }
 }
 
 std::optional<int> TimeControlPeriod::moves() const
 {
-    return m_moves;
+    if (m_json.contains("moves"_L1)) {
+        return m_json["moves"_L1].toInt();
+    }
+
+    return std::nullopt;
 }
 
 int TimeControlPeriod::time() const
 {
-    return m_time;
+    return m_json["time"_L1].toInt();
 }
 
 int TimeControlPeriod::increment() const
 {
-    return m_increment;
+    return m_json["increment"_L1].toInt();
 }
 
 QJsonObject TimeControlPeriod::toJson() const
 {
-    auto result = QJsonObject{{
-        {u"time"_s, m_time},
-        {u"increment"_s, m_increment},
-    }};
-
-    if (m_moves) {
-        result[u"moves"_s] = m_moves.value();
-    }
-
-    return result;
+    return m_json;
 }
 
 QString TimeControlPeriod::toTrf() const
 {
     QString result;
 
-    if (m_moves) {
-        result += QString::number(m_moves.value()) % u'/';
+    const auto periodMoves = moves();
+    if (moves()) {
+        result += QString::number(periodMoves.value()) % u'/';
     }
 
-    result += QString::number(m_time);
+    result += QString::number(time());
 
-    if (m_increment != 0) {
-        result += u'+' % QString::number(m_increment);
+    const auto periodIncrement = increment();
+    if (periodIncrement != 0) {
+        result += u'+' % QString::number(periodIncrement);
     }
 
     return result;
 }
 
-TimeControlPeriod TimeControlPeriod::fromJson(QJsonObject json)
+TimeControlPeriod TimeControlPeriod::fromJson(const QJsonObject &json)
 {
     TimeControlPeriod period{};
-
-    if (const auto value = json["moves"_L1]; value.isDouble()) {
-        period.m_moves = value.toInt();
-    }
-    if (const auto value = json["time"_L1]; value.isDouble()) {
-        period.m_time = value.toInt();
-    }
-    if (const auto value = json["increment"_L1]; value.isDouble()) {
-        period.m_increment = value.toInt();
-    }
-
+    period.m_json = json;
     return period;
 }
 
diff --git a/src/tournament/timecontrol.h b/src/tournament/timecontrol.h
index eea418b..50f476b 100644
--- a/src/tournament/timecontrol.h
+++ b/src/tournament/timecontrol.h
@@ -9,7 +9,7 @@
 #include <vector>
 
 struct TimeControlPeriod {
-    explicit TimeControlPeriod() = default;
+    explicit TimeControlPeriod();
 
     explicit TimeControlPeriod(std::optional<int> moves, int time, int increment);
 
@@ -23,12 +23,10 @@ struct TimeControlPeriod {
 
     [[nodiscard]] QString toTrf() const;
 
-    static TimeControlPeriod fromJson(QJsonObject json);
+    static TimeControlPeriod fromJson(const QJsonObject &json);
 
 private:
-    std::optional<int> m_moves{std::nullopt};
-    int m_time{};
-    int m_increment{};
+    QJsonObject m_json;
 };
 
 struct TimeControl {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.