Zappy - Year end project 2
This is a project that Epitech asked us to create in order to allow us to reveiw the notions of the current year.
Loading...
Searching...
No Matches
command_handling.c
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** server
4** File description:
5** command_handling
6*/
7
8#include <unistd.h>
9#include <string.h>
10#include <stdio.h>
11
12#include "utils.h"
13#include "command_parse.h"
14#include "server_handler.h"
15#include "client_management.h"
16
17static int search_in_queue(server_handler_t *server, const int fd)
18{
19 for (int i = 0; i < MAX_CLIENT; i++) {
20 if (server->fd_queue[i] == fd) {
21 return (fd);
22 }
23 }
24 return (-1);
25}
26
27static void remove_fd_from_queue(server_handler_t *server, const int fd)
28{
29 for (int i = 0; i < MAX_CLIENT; i++) {
30 if (server->fd_queue[i] == fd) {
31 server->fd_queue[i] = UNKNOWN;
32 return;
33 }
34 }
35}
36
37static void set_gui_client(server_handler_t *server, const int fd)
38{
39 for (int i = 0; i < MAX_CLIENT; i++) {
40 if (server->game_data.clients[i].client_num == UNKNOWN) {
41 server->game_data.clients[i].fd = fd;
42 server->game_data.clients[i].team_name = strdup(GUI_TEAM_NAME);
43 remove_fd_from_queue(server, fd);
44 return;
45 }
46 }
47}
48
49static int search_team_name(server_handler_t *server,
50 const char buffer[MAX_BUFFER_SIZE])
51{
52 bool is_found = false;
53
54 for (int i = 0; server->game_data.teams[i].team_name != NULL; i++) {
55 if (strncmp(buffer, server->game_data.teams[i].team_name,
56 strlen(server->game_data.teams[i].team_name) - 1) == 0) {
57 is_found = true;
58 break;
59 }
60 }
61 if (is_found == false) {
62 return (-1);
63 }
64 return (0);
65}
66
67static void set_ai_client(server_handler_t *server,
68 const char buffer[MAX_BUFFER_SIZE], const int fd)
69{
70 char str[MAX_BUFFER_SIZE];
71
72 for (int i = 0; i < MAX_CLIENT; i++) {
73 if (strncmp(server->game_data.clients[i].team_name, buffer,
74 strlen(buffer) - 1) == 0 &&
75 server->game_data.clients[i].fd == UNKNOWN) {
76 server->game_data.clients[i].fd = fd;
77 server->game_data.clients[i].is_connected = true;
78 memset(str, '\0', MAX_BUFFER_SIZE);
79 sprintf(str, "%d\n", server->game_data.clients[i].client_num);
80 write_to_client(fd, str);
81 memset(str, '\0', MAX_BUFFER_SIZE);
82 sprintf(str, "%d %d\n", server->game_data.map_size[0],
83 server->game_data.map_size[1]);
84 write_to_client(fd, str);
85 remove_fd_from_queue(server, fd);
86 break;
87 }
88 }
89}
90
91static void launch_command(server_handler_t *server,
92 const char buffer[MAX_BUFFER_SIZE], const bool is_in_queue, const int idx)
93{
94 if (is_in_queue == true) {
95 if (strncmp(buffer, GUI_TEAM_NAME, strlen(GUI_TEAM_NAME)) == 0) {
96 set_gui_client(server, idx);
97 return;
98 }
99 if (search_team_name(server, buffer) == 0) {
100 set_ai_client(server, buffer, idx);
101 return;
102 }
103 write_to_client(idx, "ko\n");
104 return;
105 }
106}
107
108void command_handling(server_handler_t *server, const int fd)
109{
110 char buffer[MAX_BUFFER_SIZE];
111 ssize_t read_bytes = 0;
112 int idx = get_client(server->game_data.clients, fd);
113 bool is_in_queue = false;
114
115 if (idx == -1) {
116 idx = search_in_queue(server, fd);
117 if (idx == -1)
118 return;
119 is_in_queue = true;
120 }
121 memset(buffer, '\0', MAX_BUFFER_SIZE);
122 read_bytes = read(fd, buffer, sizeof(buffer));
123 if (read_bytes <= 0) {
124 FD_CLR(fd, &server->current_fd);
125 delete_client(server->game_data.clients, fd);
126 printf("The client %d has been deconnected.\n", fd);
127 return;
128 }
129 launch_command(server, buffer, is_in_queue, idx);
130}
int delete_client(cli_t client[MAX_CLIENT], const int fd)
The function to delete a specific client from the cli_t structure by the fd.
int get_client(cli_t client[MAX_CLIENT], const int fd)
The function to get the client index in the structure by the fd.
void command_handling(server_handler_t *server, const int fd)
Handle client message/command.
#define GUI_TEAM_NAME
Definition constants.h:58
#define MAX_CLIENT
Definition constants.h:15
#define UNKNOWN
Definition constants.h:16
#define MAX_BUFFER_SIZE
Definition constants.h:14
int client_num
char * team_name
bool is_connected
cli_t clients[MAX_CLIENT]
A structure to store server general data.
int fd_queue[MAX_CLIENT]
game_data_t game_data
char * team_name
int write_to_client(const int fd, const char *str)
Write any message to a specific file descriptor.
Definition writer.c:17