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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 |
#include "gfCSlotSim.h"
#include <algorithm>
gfCSlotSim::gfCSlotSim(const fsStr& pName, const fsStr& pSlotType, fsS32 pServiceTimeMs, fsS32 pMaxQueueLength):
mName(pName),
mSlotType(pSlotType),
mState(eSlotSimState::eLOCKED),
mStaffed(false),
mServiceTimeMs(pServiceTimeMs),
mMaxQueueLength(pMaxQueueLength),
mServingGuestName(""),
mServiceRemainingMs(0),
mServicesBeforeBreak(0),
mOpenMsBeforeBreak(0),
mServicesSinceRepair(0),
mOpenMsSinceRepair(0),
mRepairRemainingMs(0)
{
}
void gfCSlotSim::wearLimitsSet(fsS32 pServicesBeforeBreak, fsS32 pOpenMsBeforeBreak)
{
mServicesBeforeBreak = pServicesBeforeBreak;
mOpenMsBeforeBreak = pOpenMsBeforeBreak;
}
void gfCSlotSim::wearCheck()
{
if (eSlotSimState::eOPEN != mState)
{
return;
}
const fsBool wornByUse = mServicesBeforeBreak > 0 && mServicesSinceRepair >= mServicesBeforeBreak;
const fsBool wornByAge = mOpenMsBeforeBreak > 0 && mOpenMsSinceRepair >= mOpenMsBeforeBreak;
if (wornByUse || wornByAge)
{
mState = eSlotSimState::eBROKEN;
}
}
fsBool gfCSlotSim::repairBegin(fsS32 pRepairTimeMs)
{
// Broken (fix) or open (preventive) only, and not already being repaired.
if ((eSlotSimState::eBROKEN != mState && eSlotSimState::eOPEN != mState)
|| repairingGet() || pRepairTimeMs <= 0)
{
return false;
}
mRepairRemainingMs = pRepairTimeMs;
return true;
}
std::vector<fsStr> gfCSlotSim::occupantsEjectAll()
{
std::vector<fsStr> occupants;
if (!mServingGuestName.emptyGet())
{
occupants.push_back(mServingGuestName);
mServingGuestName = "";
mServiceRemainingMs = 0;
}
for (const fsStr& queued : mQueue)
{
occupants.push_back(queued);<--- Consider using std::copy algorithm instead of a raw loop.
}
mQueue.clear();
return occupants;
}
fsBool gfCSlotSim::hasRoomGet() const
{
if (eSlotSimState::eOPEN != mState)
{
return false;
}
// Mirrors queueJoin's acceptance test: serving position or queue room.
return mServingGuestName.emptyGet() || static_cast<fsS32>(mQueue.size()) < mMaxQueueLength;
}
fsS32 gfCSlotSim::estimatedWaitMsGet() const
{
const fsS32 currentService = mServingGuestName.emptyGet() ? 0 : mServiceRemainingMs;
return currentService + static_cast<fsS32>(mQueue.size()) * mServiceTimeMs;
}
fsBool gfCSlotSim::queueingGet(const fsStr& pGuestName) const
{
return std::end(mQueue) != std::find(std::begin(mQueue), std::end(mQueue), pGuestName);
}
fsBool gfCSlotSim::queueLeave(const fsStr& pGuestName)
{
const std::vector<fsStr>::iterator queued = std::find(mQueue.begin(), mQueue.end(), pGuestName);
if (queued == mQueue.end())
{
return false;
}
mQueue.erase(queued);
return true;
}
fsBool gfCSlotSim::queueJoin(const fsStr& pGuestName)
{
if (eSlotSimState::eOPEN != mState)
{
return false;
}
// Only a staffed slot takes someone into the serving position - an unstaffed
// one never progresses service, so a guest put there would be frozen and
// block the queue. Unstaffed, they queue and keep burning patience instead.
if (mStaffed && mServingGuestName.emptyGet())
{
mServingGuestName = pGuestName;
mServiceRemainingMs = mServiceTimeMs;
return true;
}
if (static_cast<fsS32>(mQueue.size()) >= mMaxQueueLength)
{
return false;
}
mQueue.push_back(pGuestName);
return true;
}
void gfCSlotSim::serveNextBegin()
{
mServingGuestName = mQueue.front();
mQueue.erase(mQueue.begin());
mServiceRemainingMs = mServiceTimeMs;
}
fsStr gfCSlotSim::servingSuspend()
{
if (mServingGuestName.emptyGet())
{
return fsStr("");
}
const fsStr suspended = mServingGuestName;
// Back to the head of the queue - may momentarily exceed mMaxQueueLength,
// which is fine as they were already accepted.
mQueue.insert(mQueue.begin(), suspended);
mServingGuestName = "";
mServiceRemainingMs = 0;
return suspended;
}
fsStr gfCSlotSim::tick(fsS32 pDeltaMs)
{
// A repair (broken fix or preventive) runs regardless of staffing; wear is
// frozen while it's underway and reset when it finishes.
const fsBool repairing = mRepairRemainingMs > 0;
if (repairing)
{
mRepairRemainingMs -= pDeltaMs;
if (mRepairRemainingMs <= 0)
{
mRepairRemainingMs = 0;
mState = eSlotSimState::eOPEN;
mServicesSinceRepair = 0;
mOpenMsSinceRepair = 0;
}
}
// Broken and not yet fixed - serves nobody.
if (eSlotSimState::eBROKEN == mState)
{
return fsStr("");
}
if (!repairing)
{
// Time-based wear runs whenever open, busy or not.
mOpenMsSinceRepair += pDeltaMs;
wearCheck();
if (eSlotSimState::eBROKEN == mState)
{
return fsStr("");
}
}
if (eSlotSimState::eOPEN != mState || !mStaffed)
{
return fsStr("");
}
// Take the next waiting guest whenever the serving position is free - staff
// may have arrived after the queue formed.
if (mServingGuestName.emptyGet() && !mQueue.empty())
{
serveNextBegin();
}
if (mServingGuestName.emptyGet())
{
return fsStr("");
}
mServiceRemainingMs -= pDeltaMs;
if (mServiceRemainingMs > 0)
{
return fsStr("");
}
const fsStr completedGuestName = mServingGuestName;
mServingGuestName = "";
// Usage wear, on completed services - checked before pulling the next guest
// so a just-worn-out slot doesn't start serving someone it can't finish.
if (!repairing)
{
++mServicesSinceRepair;
wearCheck();
}
if (eSlotSimState::eOPEN == mState && !mQueue.empty())
{
serveNextBegin();
}
return completedGuestName;
}
|