[games/chessament] src/tournament: Store arbiter in JSON

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

Store arbiter in JSON

M  +25   -46   src/tournament/arbiter.cpp
M  +4    -8    src/tournament/arbiter.h

https://invent.kde.org/games/chessament/-/commit/1555cc89a22a2be99de7ce078d347231da79141a

diff --git a/src/tournament/arbiter.cpp b/src/tournament/arbiter.cpp
index 5f05e44..928908a 100644
--- a/src/tournament/arbiter.cpp
+++ b/src/tournament/arbiter.cpp
@@ -9,102 +9,85 @@
 using namespace Qt::StringLiterals;
 
 Arbiter::Arbiter(const QString &name)
-    : m_name(name)
 {
+    setName(name);
 }
 
-Arbiter::Role Arbiter::role()
+Arbiter::Role Arbiter::role() const
 {
-    return m_role;
+    return Arbiter::Role(m_json["role"_L1].toInt());
 }
 
 void Arbiter::setRole(Arbiter::Role role)
 {
-    if (m_role == role) {
+    if (this->role() == role) {
         return;
     }
-    m_role = role;
+    m_json["role"_L1] = std::to_underlying(role);
+    Q_EMIT roleChanged();
 }
 
 QString Arbiter::title() const
 {
-    return m_title;
+    return m_json["title"_L1].toString();
 }
 
 void Arbiter::setTitle(const QString &title)
 {
-    if (m_title == title) {
+    if (this->title() == title) {
         return;
     }
-    m_title = title;
+    m_json["title"_L1] = title;
     Q_EMIT titleChanged();
 }
 
 QString Arbiter::name() const
 {
-    return m_name;
+    return m_json["name"_L1].toString();
 }
 
 void Arbiter::setName(const QString &name)
 {
-    if (m_name == name) {
+    if (this->name() == name) {
         return;
     }
-    m_name = name;
+    m_json["name"_L1] = name;
     Q_EMIT nameChanged();
 }
 
 QString Arbiter::arbiterId() const
 {
-    return m_id;
+    return m_json["id"_L1].toString();
 }
 
 void Arbiter::setArbiterId(const QString &arbiterId)
 {
-    if (m_id == arbiterId) {
+    if (this->arbiterId() == arbiterId) {
         return;
     }
-    m_id = arbiterId;
+    m_json["id"_L1] = arbiterId;
     Q_EMIT arbiterIdChanged();
 }
 
-QByteArray Arbiter::extraString() const
-{
-    const auto doc = QJsonDocument{m_extra};
-    return doc.toJson(QJsonDocument::JsonFormat::Compact);
-}
-
-void Arbiter::setExtra(const QByteArray &extra)
-{
-    const auto doc = QJsonDocument::fromJson(extra);
-
-    Q_ASSERT(doc.isObject());
-
-    m_extra = doc.object();
-}
-
 QJsonObject Arbiter::toJson() const
 {
-    return {
-        {u"role"_s, std::to_underlying(m_role)},
-        {u"title"_s, m_title},
-        {u"name"_s, m_name},
-        {u"id"_s, m_id},
-    };
+    return m_json;
 }
 
 QString Arbiter::toTrf() const
 {
     QString result;
 
-    if (!m_title.isEmpty()) {
-        result += m_title % u' ';
+    const auto arbiterTitle = title();
+    if (!arbiterTitle.isEmpty()) {
+        result += arbiterTitle % u' ';
     }
 
-    result.append(m_name);
+    result.append(name());
 
-    if (!m_id.isEmpty()) {
-        result += " ("_L1 % m_id % u')';
+    const auto id = arbiterId();
+    if (!id.isEmpty()) {
+        result += " ("_L1 % id % u')';
     }
 
     return result;
@@ -113,11 +96,7 @@ QString Arbiter::toTrf() const
 std::unique_ptr<Arbiter> Arbiter::fromJson(const QJsonObject &obj)
 {
     auto arbiter = std::make_unique<Arbiter>();
-    arbiter->setRole(Arbiter::Role(obj.value("role"_L1).toInt()));
-    arbiter->setName(obj.value("name"_L1).toString());
-    arbiter->setTitle(obj.value("title"_L1).toString());
-    arbiter->setArbiterId(obj.value("id"_L1).toString());
-
+    arbiter->m_json = obj;
     return arbiter;
 }
 
@@ -125,7 +104,7 @@ std::unique_ptr<Arbiter> Arbiter::fromTrf(const QString &text)
 {
     auto arbiter = std::make_unique<Arbiter>();
 
-    static QRegularExpression re{R"(^([A-Z]{2} )?([^\(\n]+)(\((\d+)\))?$)"_L1};
+    static const QRegularExpression re{R"(^([A-Z]{2} )?([^\(\n]+)(\((\d+)\))?$)"_L1};
 
     const auto match = re.match(text);
     if (match.hasMatch()) {
diff --git a/src/tournament/arbiter.h b/src/tournament/arbiter.h
index d54cfb1..766188b 100644
--- a/src/tournament/arbiter.h
+++ b/src/tournament/arbiter.h
@@ -13,6 +13,7 @@ class Arbiter : public QObject
     QML_ELEMENT
     QML_UNCREATABLE("")
 
+    Q_PROPERTY(Arbiter::Role role READ role WRITE setRole NOTIFY roleChanged)
     Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
     Q_PROPERTY(QString arbiterId READ arbiterId WRITE setArbiterId NOTIFY arbiterIdChanged)
@@ -29,11 +30,10 @@ public:
 
     explicit Arbiter(const QString &name);
 
-    Arbiter::Role role();
+    [[nodiscard]] Arbiter::Role role() const;
     [[nodiscard]] QString title() const;
     [[nodiscard]] QString name() const;
     [[nodiscard]] QString arbiterId() const;
-    [[nodiscard]] QByteArray extraString() const;
 
     [[nodiscard]] QJsonObject toJson() const;
     [[nodiscard]] QString toTrf() const;
@@ -46,17 +46,13 @@ public Q_SLOTS:
     void setTitle(const QString &title);
     void setName(const QString &name);
     void setArbiterId(const QString &arbiterId);
-    void setExtra(const QByteArray &extra);
 
 Q_SIGNALS:
+    void roleChanged();
     void titleChanged();
     void nameChanged();
     void arbiterIdChanged();
 
 private:
-    Arbiter::Role m_role{};
-    QString m_title;
-    QString m_name;
-    QString m_id;
-    QJsonObject m_extra;
+    QJsonObject m_json;
 };
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.