Implemented interactions with storage class

This commit is contained in:
Andrew nuark G 2020-12-15 21:24:48 +07:00
parent 7d60d579b6
commit 6d4383273b
2 changed files with 40 additions and 0 deletions

View file

@ -20,6 +20,42 @@ const QVector<cargo_entity> storage_entity::cargo() {
return this->_cargo; return this->_cargo;
} }
void storage_entity::add_cargo(cargo_entity object, bool &success) {
success = ((int)this->_capacity - (int)object.volume()) > 0;
if (success) {
this->_cargo.push_back(object);
this->_capacity -= object.volume();
}
}
cargo_entity storage_entity::get_cargo(unsigned long long oid, bool &found) {
cargo_entity ent;
found = false;
auto vit = this->_cargo.begin();
for (; vit != this->_cargo.end(); vit++) {
if ((*vit).id() == oid) {
ent = *vit;
found = true;
break;
}
}
return ent;
}
void storage_entity::withdraw_cargo(unsigned long long oid, bool &success) {
success = false;
auto vit = this->_cargo.begin();
for (; vit != this->_cargo.end(); vit++) {
if ((*vit).id() == oid) {
this->_capacity += (*vit).volume();
this->_cargo.erase(vit);
success = true;
break;
}
}
}
void storage_entity::serialize(QDataStream &output) { void storage_entity::serialize(QDataStream &output) {
output << this->_id << this->_capacity << this->_cargo.size(); output << this->_id << this->_capacity << this->_cargo.size();
for (auto item : this->_cargo) { for (auto item : this->_cargo) {

View file

@ -24,6 +24,10 @@ public:
unsigned int capacity(); unsigned int capacity();
const QVector<cargo_entity> cargo(); const QVector<cargo_entity> cargo();
void add_cargo(cargo_entity object, bool &success);
cargo_entity get_cargo(unsigned long long oid, bool &found);
void withdraw_cargo(unsigned long long oid, bool &success);
void serialize(QDataStream &output); void serialize(QDataStream &output);
void deserialize(QDataStream &input); void deserialize(QDataStream &input);
}; };