Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
logs.c
Go to the documentation of this file.
1// Copyright (C) 2025 Giulio Salvi, Jacopo Paradisi
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
20
21#include "tui.h"
22#include "ansi.h"
23#include "logs.h"
24#include "ansi_const.h"
25
27 LogsConfiguration logsConfiguration;
28 struct tm* ltm = localtime(&(time_t){time(NULL)});
29
30 int writtenChars = snprintf(logsConfiguration.fileName, fileNameBufferSize, "logs/%02d-%02d-%d_%02d,%02d,%02d.log",
31 ltm->tm_mday, ltm->tm_mon + 1, ltm->tm_year + 1900,
32 ltm->tm_hour, ltm->tm_min, ltm->tm_sec
33 );
34
35 if(writtenChars != fileNameBufferSize - 1) {
36 logsConfiguration.useConsole = true;
37 printfgr("#b##%d#Failed to create a log file, the console is going to be used.#r#\n", FgBrightRed);
38 return logsConfiguration;
39 }
40
41 FILE* logsFile = fopen(logsConfiguration.fileName, "a+");
42 if(logsFile == NULL) {
43 logsConfiguration.useConsole = true;
44 printfgr("#b##%d#Failed to create a log file, the console is going to be used.#r#\n", FgBrightRed);
45 } else {
46 fclose(logsFile);
47 logsConfiguration.useConsole = false;
48 }
49
50 return logsConfiguration;
51}
52
53void printGameConfiguration(GameConfiguration configuration, LogsConfiguration logsConfiguration, bool newLine) {
54 FILE* outStream = !logsConfiguration.useConsole ? fopen(logsConfiguration.fileName, "a+") : stdout;
55
56 fprintf(outStream, "Allow cards with the same rank: ");
57 if(configuration.allowSameRank)
58 fprintf(outStream, "true");
59 else
60 fprintf(outStream, "false");
61
62 fprintf(outStream, ", allow cards with the same suit: ");
63 if(configuration.allowSameSuit)
64 fprintf(outStream, "true");
65 else
66 fprintf(outStream, "false");
67
68 fprintf(outStream, ", default LPs on the playing field: %d, default players' LPs: %d.\n", configuration.defaultLPsOnField, configuration.defaultPlayersLPs);
69
70 fprintf(outStream, !logsConfiguration.useConsole || newLine ? "\n" : "");
71
72 if(!logsConfiguration.useConsole)
73 fclose(outStream);
74}
75
76void printPlayers(Game game, bool newLine) {
77 for(int i = 0; i < game.playersCounter; i++)
78 printPlayer(*game.players[i], game.logsConfiguration, i == game.playersCounter - 1 ? newLine : true);
79}
80
81void printPlayer(Player player, LogsConfiguration logsConfiguration, bool newLine) {
82 if(logsConfiguration.useConsole) {
83 printfgr("Player #b#%d#r# with #b#%d LPs#r# has cards #b#", player.id, player.lifePoints);
84
85 printCard(player.facedUpCard, logsConfiguration, false);
86 printgr("#r# faced up and #b#");
87 printCard(player.facedDownCard, logsConfiguration, false);
88 printgr("#r# faced down.");
89
90 if(newLine)
91 printgr("\n");
92 } else {
93 FILE* logFile = fopen(logsConfiguration.fileName, "a+");
94
95 fprintf(logFile, "Player %d with %d LPs has cards ", player.id, player.lifePoints);
96 fclose(logFile);
97
98 printCard(player.facedUpCard, logsConfiguration, false);
99
100 logFile = fopen(logsConfiguration.fileName, "a+");
101 fprintf(logFile, " faced up and ");
102 fclose(logFile);
103
104 printCard(player.facedDownCard, logsConfiguration, false);
105
106 logFile = fopen(logsConfiguration.fileName, "a+");
107 fprintf(logFile, " faced down.\n%s", newLine ? "\n" : "");
108 fclose(logFile);
109 }
110}
111
112void printCard(Card card, LogsConfiguration logsConfiguration, bool newLine) {
113 FILE* outStream = !logsConfiguration.useConsole ? fopen(logsConfiguration.fileName, "a+") : stdout;
114
115 switch(card.suit) {
116 case Clubs:
117 fprintf(outStream, "Clubs");
118 break;
119 case Spades:
120 fprintf(outStream, "Spades");
121 break;
122 case Diamonds:
123 fprintf(outStream, "Diamonds");
124 break;
125 case Hearts:
126 fprintf(outStream, "Hearts");
127 break;
128 }
129
130 fprintf(outStream, " ");
131
132 switch(card.rank) {
133 case Ace:
134 fprintf(outStream, "Ace");
135 break;
136 case Two:
137 fprintf(outStream, "Two");
138 break;
139 case Three:
140 fprintf(outStream, "Three");
141 break;
142 case Four:
143 fprintf(outStream, "Four");
144 break;
145 case Five:
146 fprintf(outStream, "Five");
147 break;
148 case Six:
149 fprintf(outStream, "Six");
150 break;
151 case Seven:
152 fprintf(outStream, "Seven");
153 break;
154 case Jack:
155 fprintf(outStream, "Jack");
156 break;
157 case Queen:
158 fprintf(outStream, "Queen");
159 break;
160 case King:
161 fprintf(outStream, "King");
162 break;
163 }
164
165 fprintf(outStream, newLine ? "\n" : "");
166
167 fflush(outStream);
168 if(!logsConfiguration.useConsole)
169 fclose(outStream);
170}
171
172void printDeck(Card* deck, LogsConfiguration logsConfiguration, bool newLine) {
173 for(int i = 0; i < 40; i++)
174 printCard(deck[i], logsConfiguration, i == 39 ? newLine : true);
175}
176
177void printPageData(PageData pages[], LogsConfiguration logsConfiguration, int totalPages, bool newLine) {
178 FILE* outStream = !logsConfiguration.useConsole ? fopen(logsConfiguration.fileName, "a+") : stdout;
179
180 for (int i = 0; i < totalPages; i++) {
181 fprintf(outStream, "Page %d:\n", i + 1);
182 fprintf(outStream, " Rows: %d, Players Per Row: %d, Total Players: %d\n",
183 pages[i].playerRows, pages[i].playerPerRow, pages[i].playerCount);
184
185 for (int j = 0; j < pages[i].playerCount; j++) {
186 fprintf(outStream, " Player ID: %d, Life Points: %d\n",
187 pages[i].players[j]->id,
188 pages[i].players[j]->lifePoints);
189 }
190 fprintf(outStream, "\n");
191 }
192
193 fprintf(outStream, !logsConfiguration.useConsole || newLine ? "\n" : "");
194
195 fflush(outStream);
196 if(!logsConfiguration.useConsole)
197 fclose(outStream);
198}
Implemented following https://en.wikipedia.org/wiki/ANSI_escape_code.
void printgr(const char *text)
Prints a text with the graphic rendition specified with custom graphic rendition format specifiers.
Definition ansi.c:127
void printfgr(char *text,...)
Prints text as printgr but handles standard C format specifiers %, d, i, u, x, X, f,...
Definition ansi.c:222
Declaration of constant values for ANSI module.
#define FgBrightRed
ANSI standard bright red foreground color.
Definition ansi_const.h:42
void printGameConfiguration(GameConfiguration configuration, LogsConfiguration logsConfiguration, bool newLine)
Prints user-friendly game configuration settings.
Definition logs.c:53
void printPlayer(Player player, LogsConfiguration logsConfiguration, bool newLine)
Prints user-friendly player's info.
Definition logs.c:81
void printPlayers(Game game, bool newLine)
Prints user-friendly the players' infos.
Definition logs.c:76
void printDeck(Card *deck, LogsConfiguration logsConfiguration, bool newLine)
Prints user-friendly the cards deck.
Definition logs.c:172
void printPageData(PageData pages[], LogsConfiguration logsConfiguration, int totalPages, bool newLine)
Prints user-friendly the pages' infos.
Definition logs.c:177
void printCard(Card card, LogsConfiguration logsConfiguration, bool newLine)
Prints user-friendly the card rank and suit.
Definition logs.c:112
LogsConfiguration prepareLogs()
Generates the logger configuration for the current session. Tries to generate the log file.
Definition logs.c:26
Definition of logger functions.
#define fileNameBufferSize
The length of the string "logs/dd-MM-yyyy_hh,mm,ss.log", that is the pattern of the filename of every...
#define Diamonds
Integer const to represent suit diamonds.
Definition main.h:51
#define Five
Integer const to represent rank five.
Definition main.h:34
#define Spades
Integer const to represent suit spades.
Definition main.h:49
#define Seven
Integer const to represent rank seven.
Definition main.h:38
#define Three
Integer const to represent rank three.
Definition main.h:30
#define Jack
Integer const to represent rank jack.
Definition main.h:40
#define Four
Integer const to represent rank four.
Definition main.h:32
#define Ace
Integer const to represent rank ace.
Definition main.h:26
#define Clubs
Integer const to represent suit clubs.
Definition main.h:47
#define Hearts
Integer const to represent suit hearts.
Definition main.h:53
#define Six
Integer const to represent rank six.
Definition main.h:36
#define Queen
Integer const to represent rank queen.
Definition main.h:42
#define King
Integer const to represent rank king.
Definition main.h:44
#define Two
Integer const to represent rank two.
Definition main.h:28
Defines a structure for the cards.
Definition main.h:58
unsigned int rank
The rank of the card.
Definition main.h:62
unsigned int suit
The suit of the card.
Definition main.h:60
The game configuration structure.
bool allowSameRank
Determines if a player can have cards with the same rank. It increases randomness if set to false.
int defaultLPsOnField
The default amount of LPs on the playing field at the beginning of a match.
int defaultPlayersLPs
The default amount of LPs that every player have at the beginning of a match.
bool allowSameSuit
Determines if a player can have cards with the same suit It increases randomness if set to false.
Defines a structure for the game.
Definition main.h:80
unsigned int playersCounter
The amount of players currently alive.
Definition main.h:86
Player ** players
The currently alive players vector.
Definition main.h:90
LogsConfiguration logsConfiguration
The configuration for the logs, if the game has to be verbose.
Definition main.h:84
The configuration for the logger.
char fileName[fileNameBufferSize]
The filename path to the log file used in a session. It can be a valid path or an empty string if the...
bool useConsole
Flag for the logger to determine if it has to use the console or the log file.
Struct to represent the layout and data of a page containing player information.
Definition tui.h:45
int playerCount
The total number of players on the page.
Definition tui.h:51
Defines a structure for the player's info.
Definition main.h:66
Card facedDownCard
The faced down card.
Definition main.h:76
unsigned int lifePoints
The current player LPs.
Definition main.h:70
unsigned int id
The player ID.
Definition main.h:68
Card facedUpCard
The faced up card.
Definition main.h:74
Terminal User Interface (TUI) implementation for card game display.