tt

A terminal-based typing game
git clone git://pcardenasb.com/tt
Log | Files | Refs | README | LICENSE

commit 9a3daf51ba202dbd1fbf06307bc92aa8b3184454
parent 78ee7ff36d84be6f20c379acde918d22eba40c0a
Author: Pablo Cárdenas <pablo.cardenas@imca.edu.pe>
Date:   Sat, 22 Mar 2025 17:13:39 -0500

Added "restart random" from client

Diffstat:
Mtt.h | 2+-
Mttcli.c | 34++++++++++++++++++++++++++++------
Mttsrv.c | 93+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
3 files changed, 87 insertions(+), 42 deletions(-)

diff --git a/tt.h b/tt.h @@ -1,5 +1,5 @@ #pragma once struct state { - unsigned short pos; + unsigned int pos; }; diff --git a/ttcli.c b/ttcli.c @@ -228,6 +228,7 @@ int main(int argc, const char *argv[]) */ char recv_step = 0; char outdated = 0; + int new_quote = -1; char header_type; short header_length; @@ -248,13 +249,15 @@ int main(int argc, const char *argv[]) struct timespec last_input = {0, 0}; struct timespec first_input = {0, 0}; + srand(time(NULL)); + while (1) { fd_set readfds, writefds; FD_ZERO(&readfds); FD_ZERO(&writefds); FD_SET(0, &readfds); FD_SET(sockfd, &readfds); - if (outdated == 1) { + if (outdated || new_quote >= 0) { FD_SET(sockfd, &writefds); } @@ -270,6 +273,8 @@ int main(int argc, const char *argv[]) int c = getch(); if (c == ERR) { + } else if (c == '\n') { + new_quote = rand() % 6357; } else if (c == 0x1b) { c = getch(); if (c == '\b' || c == 0x7f) { @@ -413,11 +418,28 @@ int main(int argc, const char *argv[]) } if (FD_ISSET(sockfd, &writefds)) { - struct state state = { - .pos = pos, - }; - send(sockfd, &state, sizeof state, 0); - outdated = 0; + if (new_quote >= 0) { + struct { + char type; + unsigned int new_quote; + } message = { + .type = 1, + .new_quote = new_quote, + }; + send(sockfd, &message, sizeof message, 0); + new_quote = -1; + outdated = 0; + } else if (outdated) { + struct { + char type; + struct state state; + } message = { + .type = 0, + .state.pos = pos, + }; + send(sockfd, &message, sizeof message, 0); + outdated = 0; + } } print_text( diff --git a/ttsrv.c b/ttsrv.c @@ -7,6 +7,28 @@ #include <sys/socket.h> #include <unistd.h> +int change_quote( + int quote_idx, + struct json_object *quotes, + struct state states[FD_SETSIZE], + int sockfd, + fd_set set) +{ + json_object *quote = json_object_array_get_idx(quotes, quote_idx); + json_object_object_get_ex(quote, "text", &quote); + const char *str_quote = json_object_get_string(quote); + short length = strlen(str_quote); + for (int i = 0; i < FD_SETSIZE; i++) { + states[i].pos = 0; + if (!(i != 0 && i != sockfd && FD_ISSET(i, &set))) { + continue; + } + send(i, "\x00", 1, 0); + send(i, &length, sizeof length, 0); + send(i, str_quote, length, 0); + } +} + int main(int argc, const char *argv[]) { if (argc != 3) { @@ -76,23 +98,8 @@ int main(int argc, const char *argv[]) "0 <= quote_index < %d.\n", num_quotes); } else { - json_object *quote = json_object_array_get_idx( - quotes, quote_idx); - json_object_object_get_ex( - quote, "text", &quote); - const char *str_quote = - json_object_get_string(quote); - short length = strlen(str_quote); - for (int i = 0; i < FD_SETSIZE; i++) { - states[i].pos = 0; - if (!(i != 0 && i != sockfd && - FD_ISSET(i, &set))) { - continue; - } - send(i, "\x00", 1, 0); - send(i, &length, sizeof length, 0); - send(i, str_quote, length, 0); - } + change_quote( + quote_idx, quotes, states, sockfd, set); } } @@ -109,8 +116,13 @@ int main(int argc, const char *argv[]) continue; } - int length = - recv(fd, &states[fd], sizeof(struct state), 0); + struct { + char type; + unsigned int number; + } message; + + int length = recv(fd, &message, 1 + sizeof(int), 0); + if (length == 0) { states[fd].pos = 0; printf("Disconnected\n"); @@ -118,26 +130,37 @@ int main(int argc, const char *argv[]) continue; } - for (int dest = 0; dest < FD_SETSIZE; dest++) { - if (!(dest != 0 && dest != fd && - dest != sockfd && FD_ISSET(dest, &set))) { - continue; - } - struct state buffer[FD_SETSIZE]; - unsigned short buffer_size = 0; - for (int i = 0; i < FD_SETSIZE; i++) { - if (!(i != 0 && i != dest && - i != sockfd && - FD_ISSET(i, &set))) { + if (message.type == 0) { + states[fd].pos = message.number; + for (int dest = 0; dest < FD_SETSIZE; dest++) { + if (!(dest != 0 && dest != fd && + dest != sockfd && + FD_ISSET(dest, &set))) { continue; } - buffer[buffer_size++] = states[i]; + struct state buffer[FD_SETSIZE]; + unsigned short buffer_size = 0; + for (int i = 0; i < FD_SETSIZE; i++) { + if (!(i != 0 && i != dest && + i != sockfd && + FD_ISSET(i, &set))) { + continue; + } + buffer[buffer_size++] = + states[i]; + } + buffer_size *= sizeof(struct state); + + send(dest, "\x01", 1, 0); + send(dest, &buffer_size, 2, 0); + send(dest, buffer, buffer_size, 0); } - buffer_size *= sizeof(struct state); + } - send(dest, "\x01", 1, 0); - send(dest, &buffer_size, 2, 0); - send(dest, buffer, buffer_size, 0); + if (message.type == 1) { + int quote_idx = message.number; + change_quote( + quote_idx, quotes, states, sockfd, set); } } }