-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityRemovedMessageConsumer.cc
86 lines (74 loc) · 3.05 KB
/
EntityRemovedMessageConsumer.cc
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
#include "EntityRemovedMessageConsumer.hh"
#include "SystemProcessorUtils.hh"
namespace bsgo {
EntityRemovedMessageConsumer::EntityRemovedMessageConsumer(SystemServiceShPtr systemService,
SystemProcessorMap systemProcessors,
IMessageQueue *const messageQueue)
: AbstractMessageConsumer("entity", {MessageType::ENTITY_REMOVED})
, m_systemService(std::move(systemService))
, m_systemProcessors(std::move(systemProcessors))
, m_messageQueue(messageQueue)
{
addModule("removed");
if (nullptr == m_messageQueue)
{
throw std::invalid_argument("Expected non null message queue");
}
if (nullptr == m_systemService)
{
throw std::invalid_argument("Expected non null system service");
}
}
void EntityRemovedMessageConsumer::onMessageReceived(const IMessage &message)
{
const auto &removed = message.as<EntityRemovedMessage>();
switch (removed.getEntityKind())
{
case EntityKind::SHIP:
handleShipEntityRemoved(removed.getEntityDbId(), removed.isDead());
return;
case EntityKind::ASTEROID:
handleAsteroidEntityRemoved(removed.getEntityDbId(), removed.isDead());
return;
default:
error("Unsupported kind of entity to remove " + str(removed.getEntityKind()));
break;
}
}
void EntityRemovedMessageConsumer::handleShipEntityRemoved(const Uuid shipDbId,
const bool dead) const
{
const auto [systemDbId, maybeProcessor] = findSystemAndProcessorFromShip(shipDbId,
*m_systemService,
m_systemProcessors);
if (!systemDbId || !maybeProcessor)
{
warn("Failed to process ship removed message for " + str(shipDbId), "No system for ship");
return;
}
if (dead && !m_systemService->trySendPlayerShipBackToOutpost(shipDbId))
{
warn("Failed to process ship removed message for " + str(shipDbId));
return;
}
const auto &processor = *maybeProcessor;
processor->pushMessage(
std::make_unique<EntityRemovedMessage>(shipDbId, EntityKind::SHIP, dead, *systemDbId));
}
void EntityRemovedMessageConsumer::handleAsteroidEntityRemoved(const Uuid asteroidDbId,
const bool dead) const
{
const auto [systemDbId, maybeProcessor] = findSystemAndProcessorFromAsteroid(asteroidDbId,
*m_systemService,
m_systemProcessors);
if (!systemDbId || !maybeProcessor)
{
warn("Failed to process asteroid removed message for " + str(asteroidDbId),
"No system for asteroid");
return;
}
const auto &processor = *maybeProcessor;
processor->pushMessage(
std::make_unique<EntityRemovedMessage>(asteroidDbId, EntityKind::ASTEROID, dead, *systemDbId));
}
} // namespace bsgo