Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/frontend/StarContainerInteractor.cpp
blob: 47a9de2ff29fa6bf4571388e803073e1f0ab9510 (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
#include "StarContainerInteractor.hpp"

namespace Star {

void ContainerInteractor::openContainer(ContainerEntityPtr containerEntity) {
  if (m_openContainer && m_openContainer->inWorld())
    m_openContainer->containerClose();
  m_openContainer = std::move(containerEntity);
  if (m_openContainer) {
    starAssert(m_openContainer->inWorld());
    m_openContainer->containerOpen();
  }
}

void ContainerInteractor::closeContainer() {
  openContainer({});
}

bool ContainerInteractor::containerOpen() const {
  return openContainerId() != NullEntityId;
}

EntityId ContainerInteractor::openContainerId() const {
  if (m_openContainer && !m_openContainer->inWorld())
    m_openContainer = {};

  if (m_openContainer)
    return m_openContainer->entityId();

  return NullEntityId;
}

ContainerEntityPtr const& ContainerInteractor::openContainer() const {
  if (m_openContainer && !m_openContainer->inWorld())
    m_openContainer = {};

  if (!m_openContainer)
    throw StarException("ContainerInteractor has no open container");

  return m_openContainer;
}

List<ContainerResult> ContainerInteractor::pullContainerResults() {
  List<List<ItemPtr>> results;
  eraseWhere(m_pendingResults, [&results](auto& promise) {
      if (auto res = promise.result())
        results.append(res.take());
      return promise.finished();
    });
  return results;
}

void ContainerInteractor::swapInContainer(size_t slot, ItemPtr const& items) {
  m_pendingResults.append(openContainer()->swapItems(slot, items).wrap(resultFromItem));
}

void ContainerInteractor::addToContainer(ItemPtr const& items) {
  m_pendingResults.append(openContainer()->addItems(items).wrap(resultFromItem));
}

void ContainerInteractor::takeFromContainerSlot(size_t slot, size_t count) {
  m_pendingResults.append(openContainer()->takeItems(slot, count).wrap(resultFromItem));
}

void ContainerInteractor::applyAugmentInContainer(size_t slot, ItemPtr const& augment) {
  m_pendingResults.append(openContainer()->applyAugment(slot, augment).wrap(resultFromItem));
}

void ContainerInteractor::startCraftingInContainer() {
  openContainer()->startCrafting();
}

void ContainerInteractor::stopCraftingInContainer() {
  openContainer()->stopCrafting();
}

void ContainerInteractor::burnContainer() {
  openContainer()->burnContainerContents();
}

void ContainerInteractor::clearContainer() {
  m_pendingResults.append(openContainer()->clearContainer());
}

ContainerResult ContainerInteractor::resultFromItem(ItemPtr const& item) {
  if (item)
    return {item};
  else
    return {};
}

}