-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldLogicRound.cpp
More file actions
169 lines (142 loc) · 5.74 KB
/
WorldLogicRound.cpp
File metadata and controls
169 lines (142 loc) · 5.74 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "WorldLogicRound.h"
#include <EventTemplate.h>
#include <Messages/All.h>
#include <Platform/NumberConverter.h>
#include <PlayerPerks.h>
#include "EventsManipulator.h"
#include "WorldLogic.h"
WorldLogicRound::WorldLogicRound(WorldLogic& world) : _world{world} {}
void WorldLogicRound::DeclareEvent() {
//_world.SetPhase(WorldLogic::RoundPhase::DeclareEvent);
_world.Events().SelectNextEvent();
}
void WorldLogicRound::CollectTax() {
_world.GetPlayers().ForEach([](PlayerLogic& p) {
auto tax = p.GetTax();
p.AddMoney(tax);
p.Perks().Tax().Apply();
});
}
void WorldLogicRound::ThrowDices() {
_world.GetPlayers().ForEach(
[](PlayerLogic& player) { player.ResetDices(); });
_world.GetPlayers().ForEach(
[](PlayerLogic& player) { player.ThrowDices(); });
}
void WorldLogicRound::IdentifyOrder() {
_world.GetPlayers().ForEach(
[](PlayerLogic& player) { player.SortDices(); });
}
void WorldLogicRound::ApplyThrowDicesStagePerks() {
auto evt = _world.EventsDeck().Current();
auto t = GetEventTemplate(evt);
if (t.GetStage() == GlobalEventStage::DiceStage) {
t.GetEffect().Apply(_world);
}
}
void WorldLogicRound::SelectActions() {
_world.GetPlayers().ForEach([](PlayerLogic& player) {
player.BeginSelectAction(
player.GetDice(0), true, [&player](ActionType type) {
INFO(
"Player {} selected for dice {} with value {} action {} "
"({})",
player.GetId(), 0, player.GetDice(0),
ActionToString[(int)type], (int)type);
player.SetAction(0, type);
player.BeginSelectAction(
player.GetDice(1), true, [&player](ActionType type) {
INFO(
"Player {} selected for dice {} with value {} "
"action {} ({})",
player.GetId(), 1, player.GetDice(1),
ActionToString[(int)type], (int)type);
player.SetAction(1, type);
if (player.GetDiceCount() == 3) {
player.BeginSelectAction(
player.GetDice(2), true,
[&player](ActionType type) {
INFO(
"Player {} selected for dice {} with "
"value {} action {} ({})",
player.GetId(), 2, player.GetDice(2),
ActionToString[(int)type], (int)type);
player.SetAction(2, type);
});
}
});
});
});
/*BeginSelectActionsMessage msg{ true };
_world.Notify(msg);*/
}
void WorldLogicRound::OpenActions() {
OpenSelectedActionsMessage msg{};
_world.Notify(msg);
}
bool WorldLogicRound::AllActionsDone() const {
return _world.GetPlayers().All(
[](const PlayerLogic& player) { return player.AreActionsDone(); });
}
ConstWorldLogicRound::ConstWorldLogicRound(const WorldLogic& world)
: _world{world} {}
bool ConstWorldLogicRound::AllActionsDone() const {
return _world.GetPlayers().All([](const PlayerLogic& player) {
return player.AreActionsDone() && player.IsIdle();
});
}
bool ConstWorldLogicRound::AllProgressDone() const {
return _world.GetPlayers().All([](const PlayerLogic& player) {
return player.IsProgressDone() && player.IsIdle();
});
}
bool ConstWorldLogicRound::AllEventEffectsApplied() const {
auto& evt = _world.Events().Current();
if (evt.GetStage() == GlobalEventStage::DiceStage) return true;
return _world.GetPlayers().All(
[](const PlayerLogic& player) { return player.IsIdle(); });
}
void WorldLogicRound::BeginExecuteActions() {
_world.BeginExecuteActions();
_world.GetPlayers().ForEach(
[](PlayerLogic& player) { player.BeginExecuteActions(); });
}
void WorldLogicRound::ExecuteNextAction() {
auto [newAction, playerId, diceCount, actionType] =
_world._actionExecutionState.Next();
if (newAction) {
auto requireValue = As<int>(actionType);
bool canExecute = true;
auto diff = diceCount - requireValue;
if (diff < 0) {
auto population = _world.GetPlayer(playerId).GetPopulation();
if (population + diff >= 0) {
_world.GetPlayer(playerId).RemovePopulation(-diff);
} else {
canExecute = false;
}
}
if (canExecute) {
_world.GetPlayer(playerId).BeginExecuteAction(
diceCount, actionType,
[&world = _world, playerId = playerId]() mutable {
INFO("Player {} ExecuteNextAction", playerId);
world.GetPlayer(playerId).ExecuteNextAction();
});
}
}
}
void WorldLogicRound::BeginProgressStage() { _world.BeginProgressStage(); }
void WorldLogicRound::ExecuteProgressAction() {
_world._progressState.Execute(_world, [](PlayerLogic& player) {
BeginProgressMessage msg{player.GetId()};
player.Notify(msg);
});
}
void WorldLogicRound::EndProgressStage() { _world.EndProgressStage(); }
void WorldLogicRound::ApplyGlobalEvent() {
auto& evt = _world.Events().Current();
if (evt.GetStage() == GlobalEventStage::DiceStage) return;
INFO("Global event: {}", ToString(evt.GetType()));
evt.GetEffect().Apply(_world);
}