-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.cpp
47 lines (40 loc) · 949 Bytes
/
storage.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
#include "storage.h"
#include <QDebug>
Storage::Storage(std::map<std::string,int> materials)
{
materials_ = materials;
}
void Storage::add_material(std::string material)
{
materials_mutex_.lock();
if(materials_.find(material) != materials_.end())
{
materials_[material] += 1;
}
else
{
materials_.insert(std::pair<std::string,int>(material,1));
}
materials_mutex_.unlock();
emit this->addedMaterial(material);
}
bool Storage::get_material(std::string material)
{
materials_mutex_.lock();
if(materials_.find(material) != materials_.end() && materials_[material] > 0)
{
materials_[material] -= 1;
materials_mutex_.unlock();
emit this->tookMaterial(material);
return true;
}
else
{
materials_mutex_.unlock();
return false;
}
}
const std::map<std::string,int>& Storage::materials() const
{
return materials_;
}