Добавлена система логирования действий
This commit is contained in:
8
include/logger.h
Normal file
8
include/logger.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
public:
|
||||||
|
static void log(const std::string& message);
|
||||||
|
};
|
||||||
5
logs/game.log
Normal file
5
logs/game.log
Normal file
@@ -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
|
||||||
18
src/game.cpp
18
src/game.cpp
@@ -1,10 +1,12 @@
|
|||||||
#include "../include/game.h"
|
#include "../include/game.h"
|
||||||
#include "../include/save_manager.h"
|
#include "../include/save_manager.h"
|
||||||
|
#include "../include/logger.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
void Game::run() {
|
void Game::run() {
|
||||||
|
Logger::log("Game started");
|
||||||
loadItems();
|
loadItems();
|
||||||
loadLocations();
|
loadLocations();
|
||||||
showMainMenu();
|
showMainMenu();
|
||||||
@@ -13,6 +15,8 @@ void Game::run() {
|
|||||||
render();
|
render();
|
||||||
processCommand();
|
processCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Logger::log("Game closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::loadLocations() {
|
void Game::loadLocations() {
|
||||||
@@ -69,11 +73,13 @@ void Game::showMainMenu() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << ">>> Created new character " << player.name << "!\n";
|
std::cout << ">>> Created new character " << player.name << "!\n";
|
||||||
|
Logger::log("New game started. Player name: " + player.name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (str == "2") {
|
else if (str == "2") {
|
||||||
if (SaveManager::loadGame(player, "saves/save.txt")) {
|
if (SaveManager::loadGame(player, "saves/save.txt")) {
|
||||||
std::cout << ">>> Game loaded.\n";
|
std::cout << ">>> Game loaded.\n";
|
||||||
|
Logger::log("New game started. Player name: " + player.name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,6 +155,7 @@ void Game::processCommand() {
|
|||||||
else if (command == "save") {
|
else if (command == "save") {
|
||||||
if (SaveManager::saveGame(player, "saves/save.txt")) {
|
if (SaveManager::saveGame(player, "saves/save.txt")) {
|
||||||
std::cout << "Game saved.\n";
|
std::cout << "Game saved.\n";
|
||||||
|
Logger::log("Game saved");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (command == "load") {
|
else if (command == "load") {
|
||||||
@@ -233,6 +240,7 @@ void Game::handleTile(char tile) {
|
|||||||
if (newLocation != nullptr) {
|
if (newLocation != nullptr) {
|
||||||
player.position = newLocation->startPosition;
|
player.position = newLocation->startPosition;
|
||||||
std::cout << "You entered: " << newLocation->title << "\n";
|
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) {
|
if (answer == location.riddle.answer) {
|
||||||
std::cout << "Correct.\n";
|
std::cout << "Correct.\n";
|
||||||
location.riddle.isSolved = true;
|
location.riddle.isSolved = true;
|
||||||
|
Logger::log("Riddle solved in location: " + location.title);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Logger::log("Wrong riddle answer in location: " + location.title);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,10 +327,12 @@ void Game::openChest(Location& location) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "You opened the chest.\n";
|
std::cout << "You opened the chest.\n";
|
||||||
|
Logger::log("Player opened chest in location: " + location.title);
|
||||||
|
|
||||||
for (int itemId : chest.itemIds) {
|
for (int itemId : chest.itemIds) {
|
||||||
player.inventory.addItem(itemId);
|
player.inventory.addItem(itemId);
|
||||||
std::cout << "Received item ID: " << itemId << "\n";
|
std::cout << "Received item ID: " << itemId << "\n";
|
||||||
|
Logger::log("Player received item ID: " + std::to_string(itemId));
|
||||||
}
|
}
|
||||||
|
|
||||||
chest.opened = true;
|
chest.opened = true;
|
||||||
@@ -363,6 +374,7 @@ void Game::applyDialogResult(NPC& npc, const DialogOption& option) {
|
|||||||
|
|
||||||
void Game::startCombat(NPC& npc) {
|
void Game::startCombat(NPC& npc) {
|
||||||
std::cout << "\nCombat started with " << npc.name << "!\n";
|
std::cout << "\nCombat started with " << npc.name << "!\n";
|
||||||
|
Logger::log("Combat started with NPC: " + npc.name);
|
||||||
|
|
||||||
while (player.hp > 0 && npc.hp > 0) {
|
while (player.hp > 0 && npc.hp > 0) {
|
||||||
std::cout << "\nYour HP: " << player.hp << "\n";
|
std::cout << "\nYour HP: " << player.hp << "\n";
|
||||||
@@ -385,6 +397,7 @@ void Game::startCombat(NPC& npc) {
|
|||||||
if (npc.hp <= 0) {
|
if (npc.hp <= 0) {
|
||||||
npc.defeated = true;
|
npc.defeated = true;
|
||||||
std::cout << "You defeated " << npc.name << ".\n";
|
std::cout << "You defeated " << npc.name << ".\n";
|
||||||
|
Logger::log("NPC defeated: " + npc.name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -393,6 +406,7 @@ void Game::startCombat(NPC& npc) {
|
|||||||
}
|
}
|
||||||
else if (choice == 3) {
|
else if (choice == 3) {
|
||||||
std::cout << "You ran away.\n";
|
std::cout << "You ran away.\n";
|
||||||
|
Logger::log("Player escaped from combat with: " + npc.name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -405,6 +419,7 @@ void Game::startCombat(NPC& npc) {
|
|||||||
|
|
||||||
if (player.hp <= 0) {
|
if (player.hp <= 0) {
|
||||||
std::cout << "You died.\n";
|
std::cout << "You died.\n";
|
||||||
|
Logger::log("Player died in combat with: " + npc.name);
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -437,6 +452,7 @@ void Game::interactWithNPC(Location& location) {
|
|||||||
|
|
||||||
int choice;
|
int choice;
|
||||||
std::cin >> choice;
|
std::cin >> choice;
|
||||||
|
Logger::log("Player selected dialog option with NPC: " + npc.name);
|
||||||
|
|
||||||
if (choice < 1 || choice > npc.options.size()) {
|
if (choice < 1 || choice > npc.options.size()) {
|
||||||
std::cout << "Invalid dialog option.\n";
|
std::cout << "Invalid dialog option.\n";
|
||||||
|
|||||||
22
src/logger.cpp
Normal file
22
src/logger.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "../include/logger.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user