Skip to content

Commit

Permalink
Networking pt. 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrej123456789 committed Nov 25, 2022
1 parent e1ba4cb commit 9113055
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ In short, this game mostly uses official Uno rules which you can find on link ab
- [x] Basic AI (TODO: get rid of cards guesses)
- [x] Colors
- [x] Gameplay (most of gameplay)
- [ ] Networking
- [x] Networking (TODO: implement it fully)
- [ ] Nice graphics
- [x] Settings (in .json files)
- [ ] Windows port (Win32 API)
Expand Down Expand Up @@ -60,7 +60,7 @@ will load default config file) type in your terminal (emulator) `make run`.
```
[
{
"network_sequence": {[0 - disabled, 1 - enabled] per player},
"network_sequence": {[0 - disabled, 1 - enabled] per player} // also see client.c on how to properly connect client to server,
"ip": "127.0.0.1", // example
"port": 5956 // example
}
Expand Down
Binary file modified client/client
Binary file not shown.
24 changes: 22 additions & 2 deletions client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdbool.h>
#include <ctype.h>

const char* logo_row1 = ".----------------. .-----------------. .----------------. .----------------. .----------------. \n";
const char* logo_row2 = "| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |\n";
Expand Down Expand Up @@ -57,6 +59,14 @@ void str_trim_lf(char *arr, int length)
}
}

/* Checks does one string starts with another string */
bool StartsWith(const char *a, const char *b)
{
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}


/* Catches Ctrl-C and exits; actually setting global variable named flag to 1 */
void catch_ctrl_c_and_exit(int sig)
{
Expand Down Expand Up @@ -125,15 +135,25 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}

char *ip = "127.0.0.1";
char ip[64];
int port = atoi(argv[1]);

signal(SIGINT, catch_ctrl_c_and_exit);

printf("Please enter your name: ");
printf("Please IP address: ");
fgets(ip, 64, stdin);
str_trim_lf(ip, strlen(ip));

printf("Please enter your name (Player + space + position in network sequence): ");
fgets(name, 32, stdin);
str_trim_lf(name, strlen(name));

if (!StartsWith(name, "Player "))
{
printf("Your name cannot be accepted!\n");
return EXIT_SUCCESS;
}

if (strlen(name) > 32 || strlen(name) < 2)
{
printf("Name must be less than 30 and more than 2 characters.\n");
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -Wall -Wextra -Werror -pedantic -g -ljson-c
CFLAGS = -Wall -Wextra -Werror -pedantic -g -lm -ljson-c

OBJDIR = objects
OBJS = $(addprefix $(OBJDIR)/, $(patsubst %.c, %.o, $(wildcard *.c)))
Expand Down
Binary file modified src/executable/uno
Binary file not shown.
11 changes: 3 additions & 8 deletions src/gameplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1089,12 +1089,12 @@ void PointsManager(Player player[], Settings* settings, Points* points, int play

/**
* Entry point for gameplay mechanics, calls all other functions in gameplay.h and gameplay.c.
* @param runtime - struct for holding information during the game, points to runtime_T
* @param settings - struct which contains information about settings, points to setting_T
* @param points - struct for holding information about points, points to points_T
* @param theme - struct for holding graphics (theme releated stuff mostly) informations during runtime, points to theme_t
* @param network - struct which holds all informations on network, points to network_T
*/
void Gameplay(Settings* settings, Points* points, Theme* theme)
void Gameplay(Runtime* runtime, Settings* settings, Points* points, Theme* theme)
{
char tmp_input[20];
time_t t;
Expand All @@ -1103,11 +1103,6 @@ void Gameplay(Settings* settings, Points* points, Theme* theme)

Player player[players];
Stacking* stacking = malloc(sizeof(Stacking));
Runtime* runtime = malloc(sizeof(Runtime));

runtime->current_card_id = 0;
runtime->player_turn = 0;
runtime->isPositive = true;

srand((unsigned) time(&t));
cvector_vector_type(Cards) cards = GenerateDeck(runtime, settings);
Expand Down Expand Up @@ -1239,7 +1234,7 @@ void Gameplay(Settings* settings, Points* points, Theme* theme)

else if (runtime->player_turn != 0 && settings->network_sequence[runtime->player_turn] == '1')
{
printf("Network is WIP (Work in progress)!!\n\n");
printf("Network is not yet implemented fully!!\n\n");
NextPlayer(runtime, settings, false);
goto again;
}
Expand Down
3 changes: 2 additions & 1 deletion src/include/gameplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ void PointsManager(Player player[], Settings* settings, Points* points, int play

/**
* Entry point for gameplay mechanics, calls all other functions in gameplay.h and gameplay.c.
* @param runtime - struct for holding information during the game, points to runtime_T
* @param settings - struct which contains information about settings, points to setting_T
* @param points - struct for holding information about points, points to points_T
* @param theme - struct for holding graphics (theme releated stuff mostly) informations during runtime, points to theme_T
* @param network - struct which holds all informations on network, points to network_T
*/
void Gameplay(Settings* settings, Points* points, Theme* theme);
void Gameplay(Runtime* runtime, Settings* settings, Points* points, Theme* theme);
2 changes: 2 additions & 0 deletions src/include/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ typedef struct network_T

/**
* Struct which holds all information which will be passed to server when server is created
* @param runtime - struct for holding information during the game, points to runtime_T
* @param network - struct which holds all informations on network, points to network_T
*/
typedef struct {
Runtime* runtime;
Network* network;
} Arg;
9 changes: 8 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ int main(int argc, const char** argv)
free(temp);
}

Runtime* runtime = malloc(sizeof(Runtime));

runtime->current_card_id = 0;
runtime->player_turn = 0;
runtime->isPositive = true;

Settings *settings = malloc(sizeof(Settings));
Points *points = malloc(sizeof(Points));
Theme *theme = malloc(sizeof(Theme));
Expand All @@ -105,11 +111,12 @@ int main(int argc, const char** argv)
{
Arg *arg = malloc(sizeof(Arg));

arg->runtime = runtime;
arg->network = network;
RunServer(arg);
}

Gameplay(settings, points, theme);
Gameplay(runtime, settings, points, theme);

/* Frees structs */
free(settings);
Expand Down
24 changes: 21 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "include/server.h"

#include <math.h>
#include <stdarg.h>

static _Atomic unsigned int cli_count = 0;
Expand All @@ -19,6 +20,7 @@ static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct
{
client_t* cli;
Runtime* runtime;
Network* network;
} Arg2;

Expand Down Expand Up @@ -225,15 +227,29 @@ void *handle_client(void *arg)
str_trim_lf(buff_out, strlen(buff_out));
printf("%s\n", buff_out);

const char* sub = malloc(sizeof(char) * 1024);
const char* sub = malloc(sizeof(char) + strlen(cli->name) + 3);
strcat(sub, cli->name);
strcat(sub, ": ");

const char* buffer = malloc(sizeof(char) * 1024);
const char* buffer = malloc(strlen(strremove(buff_out, sub)));
strcpy(buffer, strremove(buff_out, sub));

printf("%s\n", buffer);
free(sub);

int digits = (actual_arg_2->runtime->player_turn == 0) ? 1 : (log10(actual_arg_2->runtime->player_turn) + 1);
printf("Digits: %d\n", digits);

if (strrchr(cli->name, '\0') - digits == actual_arg_2->runtime->player_turn)
{
printf("Your turn!\n");
}

else
{
printf("Not your turn!\n%d | %d\n", actual_arg_2->runtime->player_turn, strrchr(cli->name, '\0') - digits);
}

free(buffer);
}
}
Expand Down Expand Up @@ -333,7 +349,9 @@ void* StartServer(void* arg)

Arg2* arg2 = malloc(sizeof(Arg2));
arg2->cli = cli;
arg2->network;
arg2->network = actual_arg->network;

arg2->runtime = actual_arg->runtime;

/* Add client to the queue and fork thread */
queue_add(cli);
Expand Down
4 changes: 2 additions & 2 deletions src/settings/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"points_path": "settings/.points.txt",
"debug_mode": 0,
"colors": 1,
"players": 3,
"players": 2,
"special": [{
"swap_card": 1,
"stacking": 1,
"seven_o": 0
}],
"ai_sequence": "00",
"network": [{
"network_sequence": "01",
"network_sequence": "00",
"ip": "127.0.0.1",
"port": 5956
}]
Expand Down

0 comments on commit 9113055

Please sign in to comment.