aboutsummaryrefslogtreecommitdiff
path: root/core/src/participant.cpp
blob: 242b1b2e0a156c1b537c23ef76bcac6fd1964164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "turns/core/participant.hpp"

#include "turns/core/disposition.hpp"
#include "turns/core/json_ext.hpp"

#include <glibmm/object.h>
#include <glibmm/objectbase.h>
#include <glibmm/refptr.h>
#include <glibmm/ustring.h>

#include <nlohmann/json.hpp>

#include <compare>
#include <string>

namespace turns::core
{
  auto Participant::create(Glib::ustring name, float priority, core::Disposition disposition) -> Glib::RefPtr<Participant>
  {
    return Glib::make_refptr_for_instance(new Participant{name, priority, disposition});
  }

  auto Participant::create(nlohmann::json const & serialized) -> Glib::RefPtr<Participant>
  {
    auto disposition = serialized.value("disposition", Disposition::neutral);
    auto priority = serialized.value("priority", 0.0f);
    auto name = serialized.value("name", std::string{});

    auto instance = create(name, priority, disposition);
    instance->property_is_active() = serialized.value("is-active", false);
    instance->property_is_defeated() = serialized.value("is-defeated", false);
    ;

    return instance;
  }

  Participant::Participant()
      : Glib::ObjectBase{"TurnsParticipant"}
      , Glib::Object{}
  {
  }

  Participant::Participant(Glib::ustring name, float priority, core::Disposition disposition)
      : Participant()
  {
    m_name = name;
    m_priority = priority;
    m_disposition = disposition;
  }

  auto Participant::operator<=>(Participant const & other) const noexcept -> std::partial_ordering
  {
    return m_priority <=> other.m_priority;
  }

  auto Participant::get_disposition() const -> Disposition
  {
    return m_disposition.get_value();
  }

  auto Participant::get_is_active() const -> bool
  {
    return m_is_active.get_value();
  }

  auto Participant::get_is_defeated() const -> bool
  {
    return m_is_defeated.get_value();
  }

  auto Participant::get_name() const -> Glib::ustring
  {
    return m_name.get_value();
  }

  auto Participant::get_priority() const -> float
  {
    return m_priority.get_value();
  }

  auto Participant::set_disposition(Disposition value) -> void
  {
    return m_disposition.set_value(value);
  }

  auto Participant::set_is_active(bool value) -> void
  {
    return m_is_active.set_value(value);
  }

  auto Participant::set_is_defeated(bool value) -> void
  {
    return m_is_defeated.set_value(value);
  }

  auto Participant::set_name(Glib::ustring const & value) -> void
  {
    return m_name.set_value(value);
  }

  auto Participant::set_priority(float value) -> void
  {
    return m_priority.set_value(value);
  }

  auto Participant::serialize() -> nlohmann::json
  {
    return nlohmann::json{
        {"disposition", m_disposition},
        {"is-active",   m_is_active  },
        {"is-defeated", m_is_defeated},
        {"name",        m_name       },
        {"priority",    m_priority   },
    };
  }
}  // namespace turns::core