Skip to content

Commit 1ccb581

Browse files
authored
[fix] Local record creation (#31)
2 parents 9bf583a + 30e628d commit 1ccb581

File tree

17 files changed

+115
-82
lines changed

17 files changed

+115
-82
lines changed

src/api/main.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,35 @@ unsigned char postAPI(const Configuration* config, List* players) {
8787
return error;
8888
}
8989

90-
unsigned char createLocalRecord(const Configuration* config, List* players, char* localFilePath) {
90+
unsigned char createLocalRecord(Configuration* config, List* players) {
9191
unsigned char error;
9292

9393
FILE* file;
9494

9595
// Chars per column
9696
const int indexCol = 2;
9797
const int nameCol = PLAYER_NAME_LENGTH;
98-
const int pointsCol = 6;
99-
const int columns = 3;
98+
const int gamesWonCol = sizeof("Games won") - 1;
99+
const int lostGamesCol = sizeof("Lost games") - 1;
100+
const int tiedGamesCol = sizeof("Tied games") - 1;
101+
const int pointsCol = sizeof("Points") - 1;
102+
const int columns = 6;
100103
const int separatorsBetweenCols = columns - 1;
101104

102-
const int lineLength = (indexCol + nameCol + pointsCol) + (columns * 2) + separatorsBetweenCols;
105+
const int lineLength =
106+
(indexCol + nameCol + gamesWonCol + lostGamesCol + tiedGamesCol + pointsCol) +
107+
(columns * 2) + separatorsBetweenCols;
103108

104109
int i;
105110
Player player;
106111
size_t playersLength;
107112

108-
error = concatCurrentTime(localFilePath);
113+
error = concatCurrentTime(config->localRecordPath);
109114
if (error) return 1;
110115

111-
strcat(localFilePath, ".txt");
116+
strcat(config->localRecordPath, ".txt");
112117

113-
file = fopen(localFilePath, "wt");
118+
file = fopen(config->localRecordPath, "wt");
114119
if (file == NULL) return 1;
115120

116121
// Header - Team name
@@ -122,7 +127,8 @@ unsigned char createLocalRecord(const Configuration* config, List* players, char
122127
fprintf(file, "+\n");
123128

124129
// Table - Titles
125-
fprintf(file, "| N° | %-*s | Points |\n", nameCol, "Player name");
130+
fprintf(file, "| N° | %-*s | Games won | Lost games | Tied games | Points |\n", nameCol,
131+
"Player name");
126132

127133
// Table - Separator line
128134
fputc('|', file);
@@ -143,8 +149,9 @@ unsigned char createLocalRecord(const Configuration* config, List* players, char
143149
error = getElement(players, &player, sizeof(player), i);
144150
if (error) break;
145151

146-
fprintf(file, "| %0*d | %-*s | %0*d |\n", indexCol, i + 1, nameCol, player.name, pointsCol,
147-
player.points);
152+
fprintf(file, "| %0*d | %-*s | %0*d | %0*d | %0*d | %0*d |\n", indexCol, i + 1, nameCol,
153+
player.name, gamesWonCol, player.gamesWons, lostGamesCol, player.lostGames,
154+
tiedGamesCol, player.tiedGames, pointsCol, player.points);
148155
};
149156

150157
// Table - End line

src/api/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ unsigned char getAPI(const char* endpoint, List* players);
1010

1111
unsigned char postAPI(const Configuration* config, List* players);
1212

13-
unsigned char createLocalRecord(const Configuration* config, List* players, char* localFilePath);
13+
unsigned char createLocalRecord(Configuration* config, List* players);
1414

1515
#endif // SRC__API_H_INCLUDED

src/configuration/functions.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
#include "./structs.h"
77

88
unsigned char getConfiguration(Configuration* config, const char* path) {
9+
size_t fieldsRead = 0;
910
char line[CONFIGURATION_LINE_LENGTH];
1011

12+
char apiURL[API_URL_LENGTH];
13+
char localRecordPath[LOCAL_RECORD_PATH_LENGTH];
14+
char teamName[TEAM_NAME_LENGTH];
15+
int gamesPerPlayer;
16+
1117
char* fieldStart;
1218
char* fieldEnd;
1319

@@ -24,23 +30,29 @@ unsigned char getConfiguration(Configuration* config, const char* path) {
2430

2531
*fieldEnd = '\0';
2632
if (strlen(fieldStart) == 0) return 1;
27-
strcpy(config->apiURL, fieldStart);
33+
strcpy(apiURL, fieldStart);
2834

2935
fieldStart = fieldEnd + 1;
3036
fieldEnd = strchr(fieldStart, ' ');
3137
if (fieldEnd == NULL) return 1;
3238

3339
fieldStart = fieldEnd + 1;
3440
lineBreak = strrchr(fieldStart, '\n');
35-
if (lineBreak != NULL) {
36-
*lineBreak = '\0';
37-
};
41+
if (lineBreak != NULL) *lineBreak = '\0';
3842

39-
strcpy(config->teamName, fieldStart);
43+
strcpy(teamName, fieldStart);
4044

4145
if (fgets(line, sizeof(line), file) == NULL) return 1;
4246

43-
sscanf(line, "%d", &config->gamesPerPlayer);
47+
fieldsRead = sscanf(line, "%d", &gamesPerPlayer);
48+
if (fieldsRead != 1) return 1;
49+
50+
strcpy(localRecordPath, LOCAL_RECORD_PATH);
51+
52+
strcpy(config->apiURL, apiURL);
53+
strcpy(config->teamName, teamName);
54+
config->gamesPerPlayer = gamesPerPlayer;
55+
strcpy(config->localRecordPath, LOCAL_RECORD_PATH);
4456

4557
return 0;
4658
}

src/configuration/logs/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ void reprConfiguration(Configuration* config) {
99
};
1010

1111
printf("Configuration 0x%p {\n", (void*)&config);
12-
printf(" char apiURL[MAXIMUM_API_URL_LENGTH] = \"%s\",\n", config->apiURL);
13-
printf(" char teamName[TEAM_NAME_LENGTH] = \"%s\",\n", config->teamName);
12+
printf(" char apiURL[%d] = \"%s\",\n", API_URL_LENGTH, config->apiURL);
13+
printf(" char localRecordPath[%d] = \"%s\",\n", (int)LOCAL_RECORD_PATH_LENGTH, config->teamName);
14+
printf(" char teamName[%d] = \"%s\",\n", (int)TEAM_NAME_LENGTH, config->teamName);
1415
printf(" int gamesPerPlayer = %d,\n", config->gamesPerPlayer);
1516
printf("};");
1617
}

src/configuration/macros.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
#ifndef SRC__CONFIG_MACRO_H_INCLUDED
22
#define SRC__CONFIG_MACRO_H_INCLUDED
33

4-
#define MAXIMUM_API_URL_LENGTH 256
4+
#define CONFIGURATION_PATH "./statics/configuration.txt"
5+
6+
#define API_URL_LENGTH 256
7+
8+
#define LOCAL_RECORD_PATH "./statics/local-storage/informe-juego_"
9+
10+
#define LOCAL_RECORD_PATH_LENGTH \
11+
(sizeof(LOCAL_RECORD_PATH) + sizeof("YYYY-MM-DD-HH-mm") + sizeof(".txt") + 1)
12+
13+
#define TEAM_NAME_LENGTH 32
514

615
#define CONFIGURATION_LINE_LENGTH 256
716

src/configuration/structs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "./macros.h"
77

88
typedef struct {
9-
char apiURL[MAXIMUM_API_URL_LENGTH];
9+
char apiURL[API_URL_LENGTH];
10+
char localRecordPath[LOCAL_RECORD_PATH_LENGTH];
1011
char teamName[TEAM_NAME_LENGTH];
1112
int gamesPerPlayer;
1213
} Configuration;

src/macros.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
#ifndef SRC_MACROS_H_INCLUDED
22
#define SRC_MACROS_H_INCLUDED
33

4-
/* --------------------------------- Common --------------------------------- */
5-
64
#define DEVELOPMENT_TEAM "SQUASH"
75

8-
#define TEAM_NAME_LENGTH 32
9-
10-
#define CONFIGURATION_PATH "./statics/configuration.txt"
11-
12-
#define LOCAL_FILE_PATH "./statics/local-storage/informe-juego_"
13-
14-
#define LOCAL_FILE_PATH_LENGTH \
15-
(sizeof(LOCAL_FILE_PATH) + sizeof("YYYY-MM-DD-HH-mm") + sizeof(".txt") + 1)
16-
17-
/* --------------------------------- Player --------------------------------- */
6+
#define DEVELOPMENT_TEAM_LENGTH 16
187

198
#define PLAYER_NAME_LENGTH 64
209

21-
#define PLAYER_LAST_GAME_PLAYED_LENGTH 19
10+
#define PLAYER_LAST_GAME_PLAYED_LENGTH 20
2211

2312
#endif // SRC_MACROS_H_INCLUDED

src/main.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
int main(const int argsLength, char* args[]) {
1414
unsigned char error;
1515

16-
Team team;
1716
Configuration config;
18-
char localFilePath[LOCAL_FILE_PATH_LENGTH] = LOCAL_FILE_PATH;
1917

2018
int userInput;
2119

@@ -25,8 +23,6 @@ int main(const int argsLength, char* args[]) {
2523
return 1;
2624
};
2725

28-
strcpy(team.name, config.teamName);
29-
3026
printf("> %s team - A Tic-Tac-Toe game developed with C...", DEVELOPMENT_TEAM);
3127

3228
printf("\n\n> Available options:\n\n%s%s%s", " 1 - Play Tic-Tac-Toe.\n", " 2 - Show ranking.\n",
@@ -39,8 +35,8 @@ int main(const int argsLength, char* args[]) {
3935
while (userInput != 0) {
4036
switch (userInput) {
4137
case 1:
42-
printf("\n> Team %s...\n\n", team.name);
43-
playTicTacToe(&config, localFilePath);
38+
printf("\n> Team %s...\n\n", config.teamName);
39+
playTicTacToe(&config);
4440
break;
4541
case 2:
4642
showRanking(&config);

src/play/main.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#include <stdio.h>
44

55
#include "../../libs/main.h"
6+
#include "../../utilities.h"
67
#include "../api/main.h"
78
#include "../configuration/main.h"
89
#include "../structs.h"
910
#include "./tateti/main.h"
1011
#include "./utilities.h"
1112

12-
unsigned char playTicTacToe(const Configuration* config, char* localFilePath) {
13+
unsigned char playTicTacToe(Configuration* config) {
1314
List players;
1415
List playersAfterMatch;
1516

@@ -20,11 +21,6 @@ unsigned char playTicTacToe(const Configuration* config, char* localFilePath) {
2021
newList(&players);
2122
newList(&playersAfterMatch);
2223

23-
if (!requestPlayerNames(&players)) {
24-
printf("> Error! An error occurred on get player names.\n\n");
25-
return 0;
26-
};
27-
2824
randomSort(&players);
2925

3026
while (popElement(&players, &player, sizeof(player))) {
@@ -43,13 +39,11 @@ unsigned char playTicTacToe(const Configuration* config, char* localFilePath) {
4339

4440
printf("> Your final score is %d.\n\n", player.points);
4541

46-
printf("> ");
47-
system("pause");
48-
puts("");
49-
5042
if (!pushElement(&playersAfterMatch, &player, sizeof(player))) return 0;
5143
}
5244

45+
selectionSort(&playersAfterMatch, &cmpPlayersAscPoints);
46+
5347
if (postAPI(config, &playersAfterMatch)) {
5448
puts("> Error! An error occurred on post to the API.\n\n");
5549

@@ -61,7 +55,7 @@ unsigned char playTicTacToe(const Configuration* config, char* localFilePath) {
6155

6256
puts("> The game statistics was sent to the API.\n");
6357

64-
if (createLocalRecord(config, &playersAfterMatch, localFilePath)) {
58+
if (createLocalRecord(config, &playersAfterMatch)) {
6559
puts("> Error! An error occurred on create local record.\n\n");
6660

6761
destroyList(&players);

src/play/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
#include "../configuration/main.h"
55

6-
unsigned char playTicTacToe(const Configuration* config, char* localFilePath);
6+
unsigned char playTicTacToe(Configuration* config);
77

88
#endif // SRC__PLAY_H_INCLUDED

src/play/structs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SRC__PLAY_STRUCTS_H_INCLUDED
2+
#define SRC__PLAY_STRUCTS_H_INCLUDED
3+
4+
#include "../../libs/main.h"
5+
#include "../configuration/structs.h"
6+
#include "../structs.h"
7+
8+
typedef struct {
9+
Player winner;
10+
Player loser;
11+
unsigned char result;
12+
Configuration* configuration;
13+
} Game;
14+
15+
typedef struct {
16+
List* players;
17+
List* games;
18+
} TicTacToeGame;
19+
20+
#endif // SRC__PLAY_STRUCTS_H_INCLUDED

0 commit comments

Comments
 (0)