-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentSyncMessageConsumer.cc
101 lines (86 loc) · 3.35 KB
/
ComponentSyncMessageConsumer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "ComponentSyncMessageConsumer.hh"
#include "SystemProcessorUtils.hh"
namespace bsgo {
ComponentSyncMessageConsumer::ComponentSyncMessageConsumer(SystemServiceShPtr systemService,
SystemProcessorMap systemProcessors,
IMessageQueue *const messageQueue)
: AbstractMessageConsumer("component", {MessageType::COMPONENT_SYNC})
, m_systemService(std::move(systemService))
, m_systemProcessors(std::move(systemProcessors))
, m_messageQueue(messageQueue)
{
addModule("sync");
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 ComponentSyncMessageConsumer::onMessageReceived(const IMessage &message)
{
const auto &componentSync = message.as<ComponentSyncMessage>();
std::optional<Uuid> systemDbId{};
switch (componentSync.getEntityKind())
{
case EntityKind::SHIP:
systemDbId = determineSystemForShip(componentSync.getEntityDbId());
break;
case EntityKind::ASTEROID:
systemDbId = determineSystemForAsteroid(componentSync.getEntityDbId());
break;
case EntityKind::OUTPOST:
systemDbId = determineSystemForOutpost(componentSync.getEntityDbId());
break;
default:
error("Unsupported kind of entity to sync " + str(componentSync.getEntityKind()));
break;
}
if (!systemDbId)
{
error("Failed to determine system for entity " + str(componentSync.getEntityDbId())
+ " with kind " + str(componentSync.getEntityKind()));
}
auto out = message.clone();
out->as<ComponentSyncMessage>().setSystemDbId(*systemDbId);
m_messageQueue->pushMessage(std::move(out));
}
auto ComponentSyncMessageConsumer::determineSystemForShip(const Uuid shipDbId) const -> Uuid
{
const auto [systemDbId, _] = findSystemAndProcessorFromShip(shipDbId,
*m_systemService,
m_systemProcessors);
if (!systemDbId)
{
error("Failed to process ship component synced message for " + str(shipDbId),
"No system for ship");
}
return *systemDbId;
}
auto ComponentSyncMessageConsumer::determineSystemForAsteroid(const Uuid asteroidDbId) const -> Uuid
{
const auto [systemDbId, _] = findSystemAndProcessorFromAsteroid(asteroidDbId,
*m_systemService,
m_systemProcessors);
if (!systemDbId)
{
error("Failed to process asteroid component synced message for " + str(asteroidDbId),
"No system for asteroid");
}
return *systemDbId;
}
auto ComponentSyncMessageConsumer::determineSystemForOutpost(const Uuid outpostDbId) const -> Uuid
{
const auto [systemDbId, _] = findSystemAndProcessorFromOutpost(outpostDbId,
*m_systemService,
m_systemProcessors);
if (!systemDbId)
{
error("Failed to process outpost component synced message for " + str(outpostDbId),
"No system for outpost");
}
return *systemDbId;
}
} // namespace bsgo