From d3f3855e2b8bda9ddc402470153dbfb2b2bb9ed0 Mon Sep 17 00:00:00 2001 From: Martijn Braam Date: Sat, 17 Oct 2020 00:11:52 +0200 Subject: [PATCH] Initial commit --- .gitignore | 99 +++++++++ main.c | 613 ++++++++++++++++++++++++++++++++++++++++++++++++++++ meson.build | 5 + 3 files changed, 717 insertions(+) create mode 100644 .gitignore create mode 100644 main.c create mode 100644 meson.build diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b55abb --- /dev/null +++ b/.gitignore @@ -0,0 +1,99 @@ +# Created by .ignore support plugin (hsz.mobi) +### C template +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### C template +# Prerequisites + +# Object files + +# Linker output + +# Precompiled Headers + +# Libraries + +# Shared objects (inc. Windows DLLs) + +# Executables + +# Debug files + +# Kernel Module Compile Results + +### CMake template +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +/.idea +/cmake-build-debug +/cmake-build-release +*~ + +/.ninja_deps +/.ninja_log +/build.ninja +/meson-info +/meson-private +/builddir +/build diff --git a/main.c b/main.c new file mode 100644 index 0000000..2ebf1d9 --- /dev/null +++ b/main.c @@ -0,0 +1,613 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NUM_PLAYERS 10 + +enum game_stage { + STAGE_LOBBY, + STAGE_PLAYING, + STAGE_DISCUSS, +}; + +enum player_stage { + PLAYER_STAGE_NAME, + PLAYER_STAGE_LOBBY, + PLAYER_STAGE_MAIN, + PLAYER_STAGE_DISCUSS, +}; + +enum player_location { + LOC_CAFETERIA, + LOC_REACTOR, + LOC_UPPER_ENGINE, + LOC_LOWER_ENGINE, + LOC_SECURITY, + LOC_MEDBAY, + LOC_ELECTRICAL, + LOC_STORAGE, + LOC_ADMIN, + LOC_COMMUNICATIONS, + LOC_O2, + LOC_WEAPONS, + LOC_SHIELDS, + LOC_NAVIGATION, +}; + +struct player { + int fd; + struct sockaddr_in *addr; + enum player_stage stage; + char name[20]; + int is_admin; + int is_imposter; + enum player_location location; + int in_vent; + int is_alive; + int has_cooldown; +}; + +struct gamestate { + enum game_stage stage; + int has_admin; + int players; + int is_reactor_meltdown; +}; + +struct gamestate state; +struct player *players; + + +void +broadcast(char* message, int notfd) +{ + char buf[1024]; + int pid; + + printf("*> %s\n", message); + + sprintf(buf, "%s\n", message); + + for (pid = 0; pid < NUM_PLAYERS; pid++) { + if (players[pid].fd == -1) + continue; + if (players[pid].fd == notfd) + continue; + + write(players[pid].fd, buf, strlen(buf)); + } +} + +int +startswith(const char *str, const char *prefix) +{ + return strncmp(prefix, str, strlen(prefix)) == 0; +} + +void +player_move(int pid, enum player_location location) +{ + char buf[100]; + printf("Moving player %d to %d\n", pid, location); + players[pid].location = location; + players[pid].has_cooldown = 0; + + // body detection + for(int i=0; i 0) { + continue; + } + // Bonus points for goto usage + goto found_spot; + } + sprintf(buf, "There are no spots available, goodbye!\n"); + write(fd, buf, strlen(buf)); + close(fd); + return; + +found_spot: + printf("Assigned player to spot %d\n", i); + players[i].fd = fd; + players[i].addr = malloc(sizeof(addr)); + if (!state.has_admin) { + state.has_admin = 1; + players[i].is_admin = 1; + } + players[i].stage = PLAYER_STAGE_NAME; + sprintf(buf, "Welcome player %d!\n\nEnter your name:\n> ", i); + write(fd, buf, strlen(buf)); +} + +int +main() +{ + int listen_fd; + int new_fd; + int client_size; + struct sockaddr_in listen_addr, client_addr; + int port = 1234; + int i; + fd_set rfds, afds; + + players = (struct player*)malloc(sizeof(struct player) * NUM_PLAYERS); + + for (i = 0; i < NUM_PLAYERS; i++) { + players[i].fd = -1; + }; + srand(time(NULL)); + + listen_fd = socket(AF_INET, SOCK_STREAM, 0); + + listen_addr.sin_family = AF_INET; + listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); + listen_addr.sin_port = htons(port); + + if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { + printf("bind failed\n"); + return -1; + } + + listen(listen_fd, 5); + printf("Listening on :%d\n", port); + + state.stage = STAGE_LOBBY; + + FD_ZERO(&afds); + FD_SET(listen_fd, &afds); + + while (1) { + rfds = afds; + if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0) { + printf("select oops\n"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < FD_SETSIZE; ++i) { + if (FD_ISSET (i, &rfds)) { + if (i == listen_fd) { + printf("welcome client!\n"); + client_size = sizeof(client_addr); + new_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_size); + if (new_fd < 0) { + printf("new client oops\n"); + exit(EXIT_FAILURE); + } + + printf("New connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); + FD_SET(new_fd, &afds); + welcome_player(new_fd, client_addr); + } else { + if(handle_input(i) < 0) { + close(i); + FD_CLR(i, &afds); + } + } + } + } + } + return 0; +} diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..2fc0601 --- /dev/null +++ b/meson.build @@ -0,0 +1,5 @@ +project('among-oss', 'c') +cc = meson.get_compiler('c') +libm = cc.find_library('m', required: false) + +executable('among-oss', 'main.c', install : true)