|
4 | 4 | #include <unistd.h>
|
5 | 5 | #include <arpa/inet.h>
|
6 | 6 |
|
7 |
| -#include "tictactoe.h" |
| 7 | +// #include "tictactoe.h" |
8 | 8 | #include "high_score.h"
|
| 9 | +#include "helper.h" |
9 | 10 |
|
10 | 11 | //void add_winner(int client_fd, enum Player winner) {
|
11 | 12 | // //TODO
|
12 | 13 | //}
|
13 | 14 |
|
14 |
| -#define MAX_BUF_SIZE 0x100 |
15 |
| -char winners_list[MAX_BUF_SIZE]; |
16 |
| -char intro[MAX_BUF_SIZE]; |
17 |
| -char outro[MAX_BUF_SIZE]; |
| 15 | +#define MAX_BUF_SIZE (0x100) |
| 16 | +char winners_list[MAX_BUF_SIZE] = {0}; |
| 17 | +char outro_message[MAX_BUF_SIZE] = {0}; |
18 | 18 |
|
19 |
| -void init_msgs() |
| 19 | +void reset_winner_data() |
20 | 20 | {
|
21 | 21 | memset(winners_list, sizeof(winners_list), 0);
|
22 |
| - memset(intro, sizeof(winners_list), 0); |
23 |
| - memset(outro, sizeof(winners_list), 0); |
24 |
| - |
25 |
| - strcpy(intro, "Special Congratulations to our Winners:\n" ); |
26 |
| - strcpy(outro, "And Better Luck to our other participants:\n" ); |
27 |
| -} |
28 |
| - |
29 |
| -char * handle_read(int client_fd, int * num_read) |
30 |
| -{ |
31 |
| - char * dst_buf = malloc(MAX_BUF_SIZE); |
32 |
| - int bytes_read = recv(client_fd, dst_buf, MAX_BUF_SIZE, 0); |
33 |
| - if (bytes_read == -1) { |
34 |
| - perror("recv from get_str_from_client in len"); |
35 |
| - } |
36 |
| - if (bytes_read == 0) { |
37 |
| - printf("client disconnected in str len in get_str_from_client\n"); |
38 |
| - return false; |
39 |
| - } |
40 |
| - if (bytes_read != sizeof(dst_buf)) { |
41 |
| - printf("client didn't send enough bytes for string length in get_str_from_client\n"); |
42 |
| - return false; |
43 |
| - } |
44 |
| - |
45 |
| - *num_read = bytes_read; |
46 |
| - return dst_buf; |
47 |
| -} |
48 |
| - |
49 |
| -int display_to_user(int client_fd, char * msg_buf) |
50 |
| -{ |
51 |
| - int err = send(client_fd, msg_buf, strlen(msg_buf), 0); |
52 |
| - if (err < 0) { |
53 |
| - perror("send len in respond_str_to_client"); |
54 |
| - } |
55 |
| - err = send(client_fd, msg_buf, strlen(msg_buf), 0); |
56 |
| - if (err < 0) { |
57 |
| - perror("send str in respond_str_to_client"); |
58 |
| - } |
59 |
| - return err; |
| 22 | + memset(outro_message, sizeof(outro_message), 0); |
| 23 | + strcpy(outro_message, "\nBetter Luck to our other participants next time!\n" ); |
60 | 24 | }
|
61 | 25 |
|
62 | 26 | bool handle_add_winner(int client_fd, char* client_str) {
|
63 |
| - char buff[MAX_BUF_SIZE]; |
64 |
| - int num_read = 0; |
65 |
| - char * user_str = handle_read(client_fd, &num_read); |
66 |
| - int bytes_written = snprintf(buff, num_read, "%s\n", user_str); |
67 |
| - free(user_str); |
68 |
| - |
69 |
| - bytes_written = snprintf(winners_list, MAX_BUF_SIZE, "%s", buff); |
70 |
| - return true; |
71 |
| -} |
72 |
| - |
73 |
| -bool handle_set_intro(int client_fd, char* client_str) { |
74 |
| - int num_read = 0; |
75 |
| - char * user_str = handle_read(client_fd, &num_read); |
76 |
| - strncpy(intro, user_str, num_read); |
77 |
| - free(user_str); |
| 27 | + char winner_name[0x80]; |
| 28 | + if (!get_buffer_from_client(client_fd, winner_name, sizeof(winner_name))) { |
| 29 | + printf("handle_add_winner error: get_buffer_from_client failed\n"); |
| 30 | + return false; |
| 31 | + } |
| 32 | + strncpy(winners_list, winner_name, sizeof(winners_list)); |
| 33 | + printf("handle_add_winner success!\n"); |
78 | 34 | return true;
|
79 | 35 | }
|
80 | 36 |
|
81 |
| -bool handle_set_outro(int client_fd, char* client_str) { |
82 |
| - int num_read = 0; |
83 |
| - char * user_str = handle_read(client_fd, &num_read); |
84 |
| - strncpy(intro, user_str, num_read); |
85 |
| - free(user_str); |
| 37 | +bool handle_set_outro_message(int client_fd, char* client_str) { |
| 38 | + if (!get_buffer_from_client(client_fd, outro_message, sizeof(outro_message))) { |
| 39 | + printf("handle_set_outro_message error: get_buffer_from_client failed\n"); |
| 40 | + return false; |
| 41 | + } |
| 42 | + printf("handle_set_outro_message success!\n"); |
86 | 43 | return true;
|
87 | 44 | }
|
88 | 45 |
|
89 | 46 | bool handle_report_winners(int client_fd, char* client_str) {
|
90 |
| - char msg_to_user[MAX_BUF_SIZE * 3]; |
91 |
| - int bytes_written = snprintf(msg_to_user, sizeof(msg_to_user), "%s\n", intro); |
92 |
| - bytes_written = snprintf(msg_to_user + bytes_written, sizeof(msg_to_user), "%s\n", winners_list); |
93 |
| - bytes_written = snprintf(msg_to_user + bytes_written, sizeof(msg_to_user), "%s\n", outro); |
94 |
| - |
95 |
| - display_to_user(client_fd, msg_to_user); |
| 47 | + char msg_to_user[MAX_BUF_SIZE]; |
| 48 | + |
| 49 | + int bytes_written = snprintf(msg_to_user, sizeof(msg_to_user), "**WINNER LIST**\n%s\n*** :) ***", winners_list); |
| 50 | + snprintf(msg_to_user + bytes_written, sizeof(msg_to_user) - bytes_written, "%s", outro_message); |
| 51 | + respond_str_to_client(client_fd, msg_to_user); |
96 | 52 | return true;
|
97 | 53 | }
|
98 |
| - |
99 |
| -//bool handle_get_score(int client_fd, char* client_str) { |
100 |
| -// |
101 |
| -//} |
102 |
| - |
0 commit comments