-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.cpp
269 lines (229 loc) · 5.2 KB
/
factory.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "factory.h"
#include "storage.h"
#include <QDebug>
size_t PRICE_FOR_WORKER = 30;
Factory::Factory(Storage* storage, std::size_t money, float rating, std::size_t level, std::size_t factory_experience,
std::size_t level_experience, std::size_t current_order_id,
std::vector<Worker*> workers,
std::map<std::size_t, Order*> orders)
{
workers_ = workers;
orders_ = orders;
storage_.reset(storage);
money_ = money;
rating_ = rating;
level_ = level;
factory_experience_ = factory_experience;
level_experience_ = level_experience;
current_order_id_ = current_order_id;
}
// TODO create destructor to delete workers
void Factory::add_worker(Worker *worker)
{
workers_.push_back(worker);
}
void Factory::remove_worker()
{
workers_.pop_back();
}
void Factory::set_workers(std::vector<Worker *> workers)
{
workers_ = workers;
}
void Factory::add_order(Order* order)
{
order->set_status(RECEIVED);
orders_.insert(std::pair<std::size_t, Order*>(current_order_id_, order));
emit orderCreated(current_order_id_);
current_order_id_++;
}
void Factory::remove_order(std::size_t order_id)
{
orders_.erase(order_id);
emit orderRemoved(order_id);
}
void Factory::set_orders(std::map<std::size_t, Order*> orders)
{
orders_ = orders;
}
bool Factory::buy_part(std::string part, Store store)
{
if (store.find(part) != store.end() && money_ >= store[part])
{
money_ -= store[part];
emit this->moneyChanged(money_);
storage_->add_material(part);
return true;
}
else
{
return false;
}
}
void Factory::add_money(std::size_t additional_money)
{
money_ += additional_money;
emit moneyChanged(money_);
}
void Factory::buyWork()
{
money_ -= PRICE_FOR_WORKER;
workers_.push_back(new Worker(storage_.get()));
}
std::string Factory::takePart(std::string regexp)
{
if(storage_->get_material(regexp)) {
return regexp;
}
else {
return "";
}
}
void Factory::levelUp()
{
level_up();
}
bool Factory::buyParts(std::string regexp, std::size_t quantity, Store store)
{
std::size_t price = store[regexp];
if (store.find(regexp) != store.end() && money_ >= quantity * price )
{
for(size_t i = 0; i < quantity; i++)
{
money_ -= price;
storage_->add_material(regexp);
}
return true;
}
else
{
return false;
}
}
void Factory::set_money(std::size_t money)
{
money_ = money;
emit moneyChanged(money_);
}
void Factory::rating_increase(float increase)
{
rating_ += increase;
if (rating_ >= 100) {
this->level_up();
rating_ = 100 - rating_;
}
emit ratingChanged(rating_);
}
void Factory::rating_decrease(float decrease)
{
rating_ = rating_ - decrease;
if (rating_ < 0) {
rating_ = 0;
}
emit ratingChanged(rating_);
}
void Factory::set_rating(float new_rating)
{
rating_ = new_rating;
emit ratingChanged(rating_);
}
void Factory::set_current_order_id(size_t id)
{
current_order_id_ = id;
}
void Factory::experience_increase(std::size_t experience)
{
factory_experience_ += experience;
}
void Factory::level_up()
{
emit ratingChanged(100);
if(level_ < 10) {
level_++;
level_experience_ *= 2;
emit levelChanged(level_);
}
}
void Factory::create_order(std::vector<std::string> words, Client& client, std::size_t price, std::size_t experience,
std::string solution = "")
{
Order* new_order = new Order(words, SENT, price, experience, client, solution);
add_order(new_order);
}
Factory::~Factory()
{
for (Worker* worker : workers_)
{
delete worker;
}
}
// get methods:
std::vector<Worker*> Factory::get_workers()
{
return workers_;
}
std::map<std::size_t, Order*> Factory::get_orders()
{
return orders_;
}
std::size_t Factory::get_money()
{
return money_;
}
float Factory::get_rating()
{
return rating_;
}
std::size_t Factory::get_level()
{
return level_;
}
std::size_t Factory::get_factory_experience()
{
return factory_experience_;
}
std::size_t Factory::get_level_experience()
{
return level_experience_;
}
std::size_t Factory::get_current_id()
{
return current_order_id_;
}
// public slots
void Factory::workOnOrder(size_t order_id)
{
if(orders_.find(order_id) != orders_.end())
{
orders_[order_id]->set_status(IN_PROGRESS);
}
}
bool Factory::validateRegExp(std::string expression, std::vector<std::string> words)
{
QString qexpression(expression.c_str());
QRegExp r(qexpression);
if(!r.isValid())
{
return false;
}
for(std::string word : words)
{
QString qword(word.c_str());
if(!r.exactMatch(qword))
{
return false;
}
}
return true;
}
Order* Factory::get_order_in_progress() {
return order_in_progress_;
}
int Factory::take_order() {
if(orders_.size() > 0) {
Order* order = orders_.begin()->second;
order_in_progress_ = order;
return orders_.begin()->first;
}
return -1;
}