0% found this document useful (0 votes)
21 views4 pages

#Include Iostream

Uploaded by

Abhijeet Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

#Include Iostream

Uploaded by

Abhijeet Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <fstream>
#include <vector>
#include <random>
#include <chrono>
#include <ctime>
#include <string>
#include <unordered_map>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <json/json.h> // JSON library

class EventLogger {
public:
EventLogger(const std::string& sessionID, const std::string& format = "csv")
: sessionID(sessionID), logFormat(format) {
if (logFormat == "csv") {
[Link]("event_log_" + sessionID + ".csv", std::ios::out |
std::ios::app);
if (logFile.is_open()) {
logFile <<
"Timestamp,SessionID,EventType,InputType,BetAmount,SpinResult,RNGValue,Outcome\n";
} else {
std::cerr << "Error opening CSV log file." << std::endl;
}
} else if (logFormat == "json") {
jsonLog["session_id"] = sessionID;
jsonLog["events"] = Json::arrayValue;
}
}

~EventLogger() {
if (logFile.is_open()) [Link]();
if (logFormat == "json") {
std::ofstream jsonFile("event_log_" + sessionID + ".json");
jsonFile << [Link]();
[Link]();
}
}

void logEvent(const std::string& eventType, const std::string& inputType,


int betAmount, int spinResult, int rngValue, const std::string&
outcome) {
std::string timestamp = getTimestamp();
if (logFormat == "csv") {
logFile << timestamp << "," << sessionID << "," << eventType << "," <<
inputType << ","
<< betAmount << "," << spinResult << "," << rngValue << "," <<
outcome << "\n";
[Link]();
} else if (logFormat == "json") {
Json::Value event;
event["timestamp"] = timestamp;
event["event_type"] = eventType;
event["input_type"] = inputType;
event["bet_amount"] = betAmount;
event["spin_result"] = spinResult;
event["rng_value"] = rngValue;
event["outcome"] = outcome;
jsonLog["events"].append(event);
}
}

void logInputAction(const std::string& action) {


std::string timestamp = getTimestamp();
if (logFormat == "csv") {
logFile << timestamp << "," << sessionID <<
",InputAction,N/A,N/A,N/A,N/A," << action << "\n";
[Link]();
} else if (logFormat == "json") {
Json::Value event;
event["timestamp"] = timestamp;
event["event_type"] = "InputAction";
event["action"] = action;
jsonLog["events"].append(event);
}
}

private:
std::ofstream logFile;
std::string sessionID;
std::string logFormat;
Json::Value jsonLog;

std::string getTimestamp() {
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
char buf[20];
std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S",
std::localtime(&now_time));
return std::string(buf);
}
};

class SlotMachineMK7 {
public:
SlotMachineMK7(const std::string& sessionID, int initialBet = 1, const
std::string& logFormat = "csv")
: sessionID(sessionID), betAmount(initialBet), rng(std::random_device{}()),
logger(sessionID, logFormat) {
[Link]("Machine initialized");
std::cout << "Machine initialized with session ID: " << sessionID <<
std::endl;
}

void placeBet(int amount) {


betAmount = amount;
[Link]("BetPlaced", "BetAmount", betAmount, -1, -1, "N/A");
std::cout << "Bet placed: " << betAmount << std::endl;
}

void playerSpin() {
int rngValue = generateRNG();
int spinResult = determineOutcome(rngValue);
std::string outcome = (spinResult > 0) ? "Win" : "Loss";
[Link]("SpinInitiated", "Spin", betAmount, spinResult, rngValue,
outcome);
std::cout << "Spin result: " << spinResult << " (" << outcome << ")" <<
std::endl;
}

void runSession(int numSpins) {


for (int i = 0; i < numSpins; ++i) {
int betAmount;
std::cout << "Enter Bet Amount for Spin " << (i + 1) << ": ";
while (!(std::cin >> betAmount) || betAmount < 1) {
std::[Link](); // clear the error flag
std::[Link](std::numeric_limits<std::streamsize>::max(), '\n');
// discard invalid input
std::cout << "Please enter a valid positive integer for the bet
amount: ";
}

placeBet(betAmount);
playerSpin();
}
}

private:
int betAmount;
std::mt19937 rng;
std::string sessionID;
EventLogger logger;

int generateRNG() {
std::uniform_int_distribution<int> dist(0, 999); // Mock RNG range
int rngValue = dist(rng);
return rngValue;
}

int determineOutcome(int rngValue) {


return (rngValue % 10 == 0) ? rngValue : 0; // Example outcome logic
}
};

int main() {
std::string sessionID;
int initialBet;
std::string logFormat;

// Prompt for session ID, initial bet, and log format


std::cout << "Enter Session ID: ";
std::cin >> sessionID;

std::cout << "Enter Initial Bet Amount: ";


while (!(std::cin >> initialBet) || initialBet < 1) {
std::[Link](); // clear the error flag
std::[Link](std::numeric_limits<std::streamsize>::max(), '\n'); //
discard invalid input
std::cout << "Please enter a valid positive integer for the bet amount: ";
}

std::cout << "Enter Log Format (csv/json): ";


std::cin >> logFormat;

SlotMachineMK7 machine(sessionID, initialBet, logFormat);

int numSpins;
std::cout << "Enter Number of Spins to Run: ";
while (!(std::cin >> numSpins) || numSpins < 1) {
std::[Link](); // clear the error flag
std::[Link](std::numeric_limits<std::streamsize>::max(), '\n'); //
discard invalid input
std::cout << "Please enter a valid positive integer for the number of
spins: ";
}

[Link](numSpins);

return 0;
}

You might also like