/** * Handles a request to perform given move on the board. It also checks whether * the game has ended and then terminates the thread. * @param[in] client_fd File descriptor of a client that is currently served. * @param[in] request Pointer to a structure containing move coordinates details. * @param[in] game Pointer to a game structure that is currently played. * \sa request_s game_s */ void thread_handle_make_move_request(int client_fd, request_s *request, game_s *game) { int lost, turn, validate_game = 0; char *result = NULL; response_s response; move_s move; response.type = MSG_MAKE_MOVE_RSP; if (game->current_player != client_fd) { response.error = MSG_RSP_ERROR_WRONG_TURN; send_response_message(client_fd, &response); return; } result = strtok(request->payload, PAYLOAD_DELIM); move.x = atoi(result) - 1; result = strtok(NULL, PAYLOAD_DELIM); move.y = atoi(result) - 1; get_pawn(client_fd, game, &move.pawn); validate_game = make_move(game->board, &move, &game->free); if (validate_game == -1) { response.error = MSG_RSP_ERROR_WRONG_MOVE; send_response_message(client_fd, &response); return; } else if (validate_game == 1) { response_s response_lst; response_lst.type = MSG_PRINT_LOST_RSP; response_lst.error = MSG_RSP_ERROR_NONE; response.type = MSG_PRINT_WIN_RSP; response.error = MSG_RSP_ERROR_NONE; send_broadcast_win_message(client_fd); send_response_message(client_fd, &response); if ((lost = check_current_player(client_fd)) != -1) { send_response_message(lost, &response_lst); } play = 0; thwork = 0; return; } else if (validate_game == 2) { send_broadcast_draw_message(); play = 0; thwork = 0; return; } if ((turn = check_current_player(client_fd)) != -1) { game->current_player = turn; } response.error = MSG_RSP_ERROR_NONE; send_response_message(client_fd, &response); send_broadcast_message(); }
int test_send_broadcast_message() { printf("input broadcast message:\n"); char msg[100] = {}; scanf("%s", msg); printf("input broadcast data:\n"); char data[1024] = {}; scanf("%s", data); PRINTF_GREEN("testing send_broadcast_message\n"); if (send_broadcast_message(msg, data) < 0) PRINTF_RED("send_broadcast_message error, test failed\n"); return 0; }