eris 1.4.0
A WorldForge client library.
Task.cpp
1#include <utility>
2
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif
6
7#include "Task.h"
8#include "View.h"
9#include "Entity.h"
10
11#include <Atlas/Message/Element.h>
12
13#include <wfmath/timestamp.h>
14
15typedef Atlas::Message::MapType AtlasMapType;
16
17namespace Eris {
18
19Task::Task(Entity& owner, std::string name) :
20 m_name(std::move(name)),
21 m_owner(owner),
22 m_progress(0.0),
23 m_progressRate(-1.0) {
24
25}
26
27Task::~Task() {
28 m_progressRate = -1.0;
29 // force it to be un-registered
30 ProgressRateChanged.emit();
31}
32
33bool Task::isComplete() const {
34 return (m_progress >= 1.0);
35}
36
37void Task::updateFromAtlas(const AtlasMapType& d) {
38 auto progress = m_progress;
39 auto progressRate = m_progressRate;
40 auto it = d.find("progress");
41 if (it != d.end() && it->second.Float()) {
42 m_progress = it->second.asFloat();
43 if (m_progress != progress) {
44 progressChanged();
45 }
46 }
47
48 it = d.find("rate");
49 if (it != d.end() && it->second.isFloat()) {
50 m_progressRate = it->second.Float();
51 if (m_progressRate != progressRate) {
52 ProgressRateChanged.emit();
53 }
54 }
55
56 std::vector<TaskUsage> usages;
57 it = d.find("usages");
58 if (it != d.end() && it->second.isList()) {
59 for (auto& entry : it->second.List()) {
60 if (entry.isMap()) {
61 TaskUsage usage;
62 auto nameI = entry.Map().find("name");
63 if (nameI != entry.Map().end() && nameI->second.isString()) {
64 usage.name = nameI->second.String();
65 }
66 auto paramsI = entry.Map().find("params");
67 if (paramsI != entry.Map().end() && paramsI->second.isMap()) {
68 auto& params = paramsI->second.Map();
69 for (auto& paramEntry : params) {
70 UsageParameter param;
71 if (paramEntry.second.isMap()) {
72 auto& paramMap = paramEntry.second.Map();
73 {
74 auto I = paramMap.find("max");
75 if (I != paramMap.end() && I->second.isInt()) {
76 param.max = I->second.Int();
77 } else {
78 param.max = 1;
79 }
80 }
81 {
82 auto I = paramMap.find("min");
83 if (I != paramMap.end() && I->second.isInt()) {
84 param.min = I->second.Int();
85 } else {
86 param.min = 1;
87 }
88 }
89 {
90 auto I = paramMap.find("constraint");
91 if (I != paramMap.end() && I->second.isString()) {
92 param.constraint = I->second.String();
93 }
94 }
95 {
96 auto I = paramMap.find("type");
97 if (I != paramMap.end() && I->second.isString()) {
98 param.type = I->second.String();
99 }
100 }
101 }
102 usage.params.emplace(paramEntry.first, param);
103 }
104 }
105 usages.emplace_back(std::move(usage));
106 }
107 }
108 if (usages != m_usages) {
109 m_usages = std::move(usages);
110 UsagesChanged.emit();
111 }
112 }
113}
114
115void Task::progressChanged() {
116 Progressed.emit();
117 if (isComplete()) {
118 Completed.emit();
119
120 // remove from progression updating
121 m_progressRate = -1;
122 ProgressRateChanged.emit();
123 }
124}
125
126void Task::updatePredictedProgress(const WFMath::TimeDiff& dt) {
127 if (isComplete()) return;
128
129 m_progress += m_progressRate * (dt.milliseconds() / 1000.0);
130 m_progress = std::min(m_progress, 1.0);
131
132 Progressed.emit();
133 // note we will never signal completion here, but instead we wait for
134 // the server to notify us.
135}
136
137}
Entity is a concrete (instantiable) class representing one game entity.
Definition: Entity.h:56
double progress() const
Return the current progress of the task. Value will always be in the range [0..1].
Definition: Task.h:98
Task(Entity &owner, std::string name)
Definition: Task.cpp:19
double progressRate() const
Gets the progress rate.
Definition: Task.h:102
bool isComplete() const
Returns true if the task has completed.
Definition: Task.cpp:33
Definition: Account.cpp:33