-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerEntity.cpp
More file actions
375 lines (326 loc) · 13.2 KB
/
CustomerEntity.cpp
File metadata and controls
375 lines (326 loc) · 13.2 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <iostream>
#include "CustomerEntity.h"
// Default Constructor
CustomerEntity::CustomerEntity()
: fID(""),
fPosition({0.0f, 0.0f}), // default values (will be overriden by the child classes)
fMaxHappiness(1000), // default values (will be overriden by the child classes)
fCurrentHappiness(200), // default values (will be overriden by the child classes)
fReputationBoost(50), // default values (will be overriden by the child classes)
fReputationAttack(50), // default values (will be overriden by the child classes)
fMessage(""),
seated(false),
waitingForFood(false),
assignedFood(nullptr),
appetizerOrdered(false),
mainCourseOrdered(false),
dessertOrdered(false),
champagneOrdered(false),
seatedClock(),
foodClock(),
expectedFoodType(FoodType::None),
finishedMeal(false),
assignedTable(nullptr),
messagePrinted(false),
boostCount(0),
attackCount(0) {
circle.setRadius(20);
circle.setFillColor(sf::Color::Black); // sample color (will be overriden by the child classes)
circle.setPosition(fPosition.x, fPosition.y); // Initialize circle position
}
// Overloaded Constructor
CustomerEntity::CustomerEntity(std::string id, Position position, float radius, int maxHappiness, int currentHappiness, int reputationBoost, int reputationAttack)
: fID(id),
fPosition(position),
fMaxHappiness(maxHappiness),
fCurrentHappiness(currentHappiness),
fReputationBoost(reputationBoost),
fReputationAttack(reputationAttack),
fMessage(""),
seated(false),
waitingForFood(false),
assignedFood(nullptr),
appetizerOrdered(false),
mainCourseOrdered(false),
dessertOrdered(false),
champagneOrdered(false),
seatedClock(),
foodClock(),
expectedFoodType(FoodType::None),
finishedMeal(false),
assignedTable(nullptr),
messagePrinted(false),
boostCount(0),
attackCount(0) {
circle.setRadius(radius);
circle.setFillColor(sf::Color::Black); // sample color (will be overriden by the child classes)
circle.setPosition(fPosition.x, fPosition.y); // Initialize circle position
}
// Getter for fID
std::string CustomerEntity::getCustomerID() {
return fID;
}
// Setter for fID
void CustomerEntity::setCustomerID(std::string& id) {
fID = id;
}
// Getter for fPosition
Position CustomerEntity::getPosition() {
return fPosition;
}
// Setter for fPosition
void CustomerEntity::setPosition(float x, float y) {
circle.setPosition(x, y);
fPosition = { x, y };
}
// Getter for fMaxHappiness
int CustomerEntity::getMaxHappiness() {
return fMaxHappiness;
}
// Setter for fMaxHappiness
void CustomerEntity::setMaxHappiness(int maxHappiness) {
fMaxHappiness = maxHappiness;
}
// Getter for fCurrentHappiness
int CustomerEntity::getCurrentHappiness() {
return fCurrentHappiness;
}
// Setter for fCurrentHappiness
void CustomerEntity::setCurrentHappiness(int currentHappiness) {
fCurrentHappiness = currentHappiness;
}
// Getter for fReputationBoost
int CustomerEntity::getReputationBoost() {
return fReputationBoost;
}
// Setter for fReputationBoost
void CustomerEntity::setReputationBoost(int reputationBoost) {
fReputationBoost = reputationBoost;
}
// Getter for fReputationAttack
int CustomerEntity::getReputationAttack() {
return fReputationAttack;
}
// Setter for fReputationAttack
void CustomerEntity::setReputationAttack(int reputationAttack) {
fReputationAttack = reputationAttack;
}
// Getter for fMessage
std::string CustomerEntity::getMessage() {
return fMessage;
}
// Setter for fMessage
void CustomerEntity::setMessage(std::string message) {
fMessage = std::move(message);
}
// Getter for seated status
bool CustomerEntity::isSeated() {
return seated;
}
// Setter for seated status
void CustomerEntity::setSeated(bool status) {
seated = status;
if (status) {
seatedClock.restart();
}
}
// Status if Customer has finished their meal
bool CustomerEntity::hasFinishedMeal() {
return finishedMeal;
}
// Method to Draw the Customer
void CustomerEntity::draw(sf::RenderWindow& window) {
window.draw(circle);
}
// Method for Click Handling
bool CustomerEntity::isClicked(float mouseX, float mouseY) {
sf::FloatRect bounds = circle.getGlobalBounds();
return bounds.contains(mouseX, mouseY);
}
// Get the Radius of the Customer
// to position the selected Customer's location to the middle of the selected table upon seated
float CustomerEntity::getRadius() const {
return circle.getRadius();
}
// Method to Boost Player's Reputation
void CustomerEntity::boostPlayerReputation() {
Player* player = Player::getInstance(); // Get the singleton instance of Player
if (player) {
int currentReputation = player->getPlayerReputation();
player->setPlayerReputation(currentReputation + fReputationBoost); // Increase reputation
boostCount++;
std::cout << "Customer " << fID << " boosted player reputation by " << fReputationBoost << std::endl;
std::cout << "\n";
}
else {
std::cerr << "Error: Unable to access Player instance.\n" << std::endl;
}
}
// Method to Attack Player's Reputation
void CustomerEntity::attackPlayerReputation() {
Player* player = Player::getInstance(); // Get the singleton instance of Player
if (player) {
int currentReputation = player->getPlayerReputation();
player->setPlayerReputation(currentReputation - fReputationAttack); // Decrease reputation
attackCount++;
std::cout << "Customer " << fID << " attacked player reputation by " << fReputationAttack << std::endl;
std::cout << "\n";
}
else {
std::cerr << "Error: Unable to access Player instance.\n" << std::endl;
}
}
// This method assigns the specified table to the customer,
// tracking which table they are seated at
void CustomerEntity::seat(Table* table) {
assignedTable = table; // Assign the given table to the customer's assignedTable field pointer
}
// Method to order an appetizer
void CustomerEntity::orderAppetizer() {
std::cout << fID << " has ordered an appetizer!\n" << std::endl;
waitingForFood = true;
appetizerOrdered = true; // Appetizer is ordered
expectedFoodType = FoodType::Appetizer; // Set expectedFoodType to be Appetizer
foodClock.restart(); // Start the timer for the appetizer delivery
}
// Method to order a main course
void CustomerEntity::orderMainCourse() {
std::cout << fID << " has ordered the main course!\n" << std::endl;
waitingForFood = true;
mainCourseOrdered = true; // Main Course is ordered
expectedFoodType = FoodType::MainCourse; // Set expectedFoodType to be Main Course
foodClock.restart(); // Start the timer for the main course delivery
}
// Method to order a dessert
void CustomerEntity::orderDessert() {
std::cout << fID << " has ordered dessert!\n" << std::endl;
waitingForFood = true;
dessertOrdered = true; // Dessert is ordered
expectedFoodType = FoodType::Dessert; // Set expectedFoodType to be Dessert
foodClock.restart(); // Start the timer for the dessert delivery
}
// Method to order a champagne
void CustomerEntity::orderChampagne() {
std::cout << fID << " has ordered champagne!\n" << std::endl;
waitingForFood = true;
champagneOrdered = true; // Champagne is ordered
expectedFoodType = FoodType::Champagne; // Set expectedFoodType to be Champagne
foodClock.restart(); // Start the timer for the champagne delivery
}
// Method to call after customer is done with all the services
void CustomerEntity::finishMeal() {
std::cout << fID << " has finished their meal!\n" << std::endl;
finishedMeal = true; // Mark the meal as finished
// Reset the table's availability
if (assignedTable) {
assignedTable->setAvailability(true); // Set table as available
}
else {
std::cout << "Pointer is null " << std::endl;
}
// Clear the table
seated = false; // Mark the customer as not seated
assignedFood = nullptr; // Clear the assigned food
setPosition(-1000, -1000); // Move the customer off-screen
}
// Method that returns a string with current fields of the Customer
std::string CustomerEntity::PrintStat() {
// Create an ostringstream object to accumulate the formatted data.
// Append each field of the CustomerEntity object to the string stream.
std::ostringstream ss;
ss << "Customer ID: " << fID << "\n"
<< "Position: (" << fPosition.x << ", " << fPosition.y << ")\n"
<< "Max Happiness: " << fMaxHappiness << "\n"
<< "Current Happiness: " << fCurrentHappiness << "\n"
<< "Reputation Boost Threshold: " << fReputationBoost << "\n"
<< "Reputation Attack Threshold: " << fReputationAttack << "\n"
<< "Message: " << fMessage << "\n";
// Convert the ostringstream content to a string and return it.
return ss.str();
}
// Method to get the global bounds of the customer
sf::FloatRect CustomerEntity::getGlobalBounds() const {
return circle.getGlobalBounds();
}
// Method to reset the message printed status
void CustomerEntity::resetMessageStatus() {
messagePrinted = false;
}
// Getter for messagePrinted status
bool CustomerEntity::isMessagePrinted() const {
return messagePrinted;
}
// Setter for messagePrinted status
void CustomerEntity::setMessagePrinted(bool status) {
messagePrinted = status;
}
// Getter for boostCount
int CustomerEntity::getBoostCount() const {
return boostCount;
}
// Getter for attackCount
int CustomerEntity::getAttackCount() const {
return attackCount;
}
// Friend operator overload for insertion | Output stream
// This operator is used to print the contents of a CustomerEntity object to an output stream (cout).
// It utilizes the PrintStat() method of CustomerEntity to provide a formatted, readable output of the object's data.
std::ostream& operator<<(std::ostream& aOstream, CustomerEntity& aCustomerEntity) {
// Call the PrintStat() method of the CustomerEntity object.
// This method returns a string or formatted data that represents the CustomerEntity's current stats.
// The returned value is inserted into the output stream (aOstream).
aOstream << aCustomerEntity.PrintStat();
// Return the updated output stream, allowing chaining of << operators.
return aOstream;
}
// Friend operator overload for extraction | Input stream
// This operator is used to parse and process input commands from an input stream, modifying the state of a CustomerEntity object based on the commands.
// It reads the `fMessage` field from the CustomerEntity object, contain <command> <value>
// The function processes these commands to update the customer's happiness and reputation accordingly.
std::istream &operator>>(std::istream &aIstream, CustomerEntity &aCustomerEntity) {
std::string message = aCustomerEntity.getMessage();
if (!message.empty()) {
std::istringstream messageStream(message); // Create a string stream to parse the message
std::string command; // Command extracted from the stream
int value; // Value associated with the command
// Extract command and value from the message
while (messageStream >> command) { // Extract command from the stream
// Attempt to read the next part of the stream as an integer value
if (!(messageStream >> value)) {
std::cerr << "Error parsing value for command: " << command << std::endl; // Error if value parsing fails
std::cout << "\n";
return aIstream; // Return the stream in case of parsing error
}
// Check if the value is negative
if (value < 0) {
std::cerr << "Error: Value cannot be negative for command: " << command << std::endl; // Error if value is negative
std::cout << "\n";
return aIstream; // Return the stream in case of invalid value
}
// Process commands based on the extracted command string
if (command == "IncreaseCurrentHappiness") {
aCustomerEntity.setCurrentHappiness(aCustomerEntity.getCurrentHappiness() + value); // Increase customer's happiness
std::cout << "Increased " << aCustomerEntity.getCustomerID() << "'s happiness by " << value << std::endl;
std::cout << "\n";
aCustomerEntity.boostPlayerReputation(); // Update player's reputation
}
else if (command == "DecreaseCurrentHappiness") {
aCustomerEntity.setCurrentHappiness(aCustomerEntity.getCurrentHappiness() - value); // Decrease customer's happiness
std::cout << "Decreased " << aCustomerEntity.getCustomerID() << "'s happiness by " << value << std::endl;
std::cout << "\n";
aCustomerEntity.attackPlayerReputation(); // Update player's reputation
}
else {
std::cerr << "Unknown command: " << command << std::endl; // Error for unknown commands
std::cout << "\n";
}
}
}
else {
std::cerr << "Message invalid or contains no commands\n" << std::endl; // Error if the message is empty or invalid
}
return aIstream;
}
// Destructor
CustomerEntity::~CustomerEntity() {
}