Coverage report for server


src/
File: src/init.c
Date: 2024-06-25 10:57:05
Lines:
0/55
0.0%
Functions:
0/6
0.0%
Branches:
0/24
0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** zappy
4 ** File description:
5 ** init.c
6 */
7
8 #include "server.h"
9 #include <string.h>
10 #include <stdio.h>
11
12 static struct sockaddr_in init_socket_address(size_t port)
13 {
14 struct sockaddr_in info;
15
16 memset(&info, 0, sizeof(info));
17 info.sin_family = AF_INET;
18 info.sin_port = htons(port);
19 info.sin_addr.s_addr = htonl(INADDR_ANY);
20 return info;
21 }
22
23 static bool init_teams(server_t *server)
24 {
25 char **teams = server->proprieties.names;
26 int nb_clients = server->proprieties.nb_clients;
27 int width = server->proprieties.width;
28 int height = server->proprieties.height;
29 team_list_t *new_team;
30
31 for (size_t i = 0; teams[i] != NULL; i++) {
32 new_team = malloc(sizeof(team_list_t));
33 if (new_team == NULL)
34 return false;
35 new_team->team = init_team(teams[i], nb_clients, width, height);
36 if (new_team->team == NULL)
37 return false;
38 TAILQ_INSERT_TAIL(&server->teams, new_team, entries);
39 }
40 return true;
41 }
42
43 static tile_t *init_map(int width, int height)
44 {
45 tile_t *map = calloc(height * width, sizeof(tile_t));
46
47 if (map == NULL)
48 return NULL;
49 for (int i = 0; i < height * width; i++) {
50 map[i].num_objects = 0;
51 map[i].objects = NULL;
52 }
53 return map;
54 }
55
56 static void add_object(int obj, int obj_enum, server_t *server)
57 {
58 int x = server->proprieties.width;
59 int y = server->proprieties.height;
60
61 for (int i = obj; i >= 0; i--)
62 add_element_to_map(server, rand_p(x), rand_p(y), obj_enum);
63 }
64
65 static void init_meteor(server_t *server)
66 {
67 info_map_t objs = server->proprieties.max_map;
68
69 add_object(objs.food, FOOD, server);
70 add_object(objs.linemate, LINEMATE, server);
71 add_object(objs.deraumere, DERAUMERE, server);
72 add_object(objs.sibur, SIBUR, server);
73 add_object(objs.mendiane, MENDIANE, server);
74 add_object(objs.phiras, PHIRAS, server);
75 add_object(objs.thystame, THYSTAME, server);
76 clock_gettime(CLOCK_REALTIME, &server->meteor_last_time);
77 }
78
79 bool init_server(server_t *server, const char **args)
80 {
81 if (init_flags(&server->proprieties, args) == false)
82 return false;
83 TAILQ_INIT(&server->teams);
84 if (init_teams(server) == false)
85 return false;
86 TAILQ_INIT(&server->clients);
87 print_teams_infos(&server->teams);
88 server->info = init_socket_address((size_t)server->proprieties.port);
89 server->fd = socket(AF_INET, SOCK_STREAM, 0);
90 server->addrlen = sizeof(struct sockaddr_in);
91 if (server->fd < 0)
92 return false;
93 FD_ZERO(&server->current_sockets);
94 FD_SET(server->fd, &server->current_sockets);
95 server->timeout.tv_sec = 0;
96 server->timeout.tv_usec = 0;
97 server->map = init_map(server->proprieties.width,
98 server->proprieties.height);
99 init_meteor(server);
100 return true;
101 }
102