From 49dc1aecd1783a6ddc5de5cad0ad746941edab75 Mon Sep 17 00:00:00 2001 From: Damianson Pochette Date: Thu, 11 Jun 2026 15:29:17 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B4=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/logger.h | 8 ++++++++ logs/game.log | 5 +++++ src/game.cpp | 18 +++++++++++++++++- src/logger.cpp | 22 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 include/logger.h create mode 100644 logs/game.log create mode 100644 src/logger.cpp diff --git a/include/logger.h b/include/logger.h new file mode 100644 index 0000000..320c35d --- /dev/null +++ b/include/logger.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +class Logger { +public: + static void log(const std::string& message); +}; \ No newline at end of file diff --git a/logs/game.log b/logs/game.log new file mode 100644 index 0000000..a94b967 --- /dev/null +++ b/logs/game.log @@ -0,0 +1,5 @@ +[2026-06-11 15:28:07] Game started +[2026-06-11 15:28:09] New game started. Player name: Zero +[2026-06-11 15:28:12] Player opened chest in location: Entrance Hall +[2026-06-11 15:28:12] Player received item ID: 101 +[2026-06-11 15:28:12] Player received item ID: 101 diff --git a/src/game.cpp b/src/game.cpp index f931118..255cb15 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,10 +1,12 @@ #include "../include/game.h" #include "../include/save_manager.h" +#include "../include/logger.h" #include #include #include void Game::run() { + Logger::log("Game started"); loadItems(); loadLocations(); showMainMenu(); @@ -13,6 +15,8 @@ void Game::run() { render(); processCommand(); } + + Logger::log("Game closed"); } void Game::loadLocations() { @@ -69,11 +73,13 @@ void Game::showMainMenu() { } std::cout << ">>> Created new character " << player.name << "!\n"; + Logger::log("New game started. Player name: " + player.name); break; } else if (str == "2") { if (SaveManager::loadGame(player, "saves/save.txt")) { std::cout << ">>> Game loaded.\n"; + Logger::log("New game started. Player name: " + player.name); break; } } @@ -149,6 +155,7 @@ void Game::processCommand() { else if (command == "save") { if (SaveManager::saveGame(player, "saves/save.txt")) { std::cout << "Game saved.\n"; + Logger::log("Game saved"); } } else if (command == "load") { @@ -233,6 +240,7 @@ void Game::handleTile(char tile) { if (newLocation != nullptr) { player.position = newLocation->startPosition; std::cout << "You entered: " << newLocation->title << "\n"; + Logger::log("Player entered location: " + newLocation->title); } } } @@ -252,9 +260,10 @@ bool Game::solveRiddle(Location& location) { if (answer == location.riddle.answer) { std::cout << "Correct.\n"; location.riddle.isSolved = true; + Logger::log("Riddle solved in location: " + location.title); return true; } - + Logger::log("Wrong riddle answer in location: " + location.title); return false; } @@ -318,10 +327,12 @@ void Game::openChest(Location& location) { } std::cout << "You opened the chest.\n"; + Logger::log("Player opened chest in location: " + location.title); for (int itemId : chest.itemIds) { player.inventory.addItem(itemId); std::cout << "Received item ID: " << itemId << "\n"; + Logger::log("Player received item ID: " + std::to_string(itemId)); } chest.opened = true; @@ -363,6 +374,7 @@ void Game::applyDialogResult(NPC& npc, const DialogOption& option) { void Game::startCombat(NPC& npc) { std::cout << "\nCombat started with " << npc.name << "!\n"; + Logger::log("Combat started with NPC: " + npc.name); while (player.hp > 0 && npc.hp > 0) { std::cout << "\nYour HP: " << player.hp << "\n"; @@ -385,6 +397,7 @@ void Game::startCombat(NPC& npc) { if (npc.hp <= 0) { npc.defeated = true; std::cout << "You defeated " << npc.name << ".\n"; + Logger::log("NPC defeated: " + npc.name); return; } } @@ -393,6 +406,7 @@ void Game::startCombat(NPC& npc) { } else if (choice == 3) { std::cout << "You ran away.\n"; + Logger::log("Player escaped from combat with: " + npc.name); return; } else { @@ -405,6 +419,7 @@ void Game::startCombat(NPC& npc) { if (player.hp <= 0) { std::cout << "You died.\n"; + Logger::log("Player died in combat with: " + npc.name); isRunning = false; return; } @@ -437,6 +452,7 @@ void Game::interactWithNPC(Location& location) { int choice; std::cin >> choice; + Logger::log("Player selected dialog option with NPC: " + npc.name); if (choice < 1 || choice > npc.options.size()) { std::cout << "Invalid dialog option.\n"; diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..d57fb4b --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,22 @@ +#include "../include/logger.h" + +#include +#include +#include + +void Logger::log(const std::string& message) { + std::ofstream file("logs/game.log", std::ios::app); + + if (!file.is_open()) { + return; + } + + std::time_t now = std::time(nullptr); + std::tm* localTime = std::localtime(&now); + + file << "[" + << std::put_time(localTime, "%Y-%m-%d %H:%M:%S") + << "] " + << message + << "\n"; +} \ No newline at end of file