RyanHub - file viewer
filename: src/player/player.c
branch: main
back to repo
// player.c

#include <stdio.h>
#include "player.h"

void queue_init(EventQueue* q) {
    //printf("\ninitialized queue\n");
    q->head = 0;
    q->tail = 0;
    MUTEX_INIT(&q->lock);
}

int queue_push(EventQueue* q, Event e) {
    //printf("pushed '%c' to queue\n", e.ch);
    MUTEX_LOCK(&q->lock);
    int next = (q->head + 1) % QUEUE_SIZE;
    if (next == q->tail) {
        MUTEX_UNLOCK(&q->lock);
        return 0;
    }
    q->events[q->head] = e;
    q->head = next;
    MUTEX_UNLOCK(&q->lock);
    return 1;
}

int queue_pop(EventQueue* q, Event* e_out) {
    //printf("\npopped from queue\n");
    MUTEX_LOCK(&q->lock);
    if (q->head == q->tail) {
        MUTEX_UNLOCK(&q->lock);
        return 0;
    }
    *e_out = q->events[q->tail];
    q->tail = (q->tail + 1) % QUEUE_SIZE;
    MUTEX_UNLOCK(&q->lock);
    return 1;
}

void queue_destroy(EventQueue* q) {
    //printf("\nqueue destroyed\n");
    MUTEX_DESTROY(&q->lock);
}

void print_event(int player_id, const Event* ev) {
    switch (ev->type) {
    case EVENT_CHAR:
        printf("Player %d sent char: '%c'\n", player_id, ev->ch);
        break;
    case EVENT_BUTTON:
        //printf("Player %d button: %c %s\n", player_id,
            //ev->button.button,
            //ev->button.pressed ? "pressed" : "released");
        break;
    case EVENT_JOYSTICK:
        //printf("Player %d joystick: x=%.2f y=%.2f\n", player_id,
            //ev->joystick.x, ev->joystick.y);
        break;
    case EVENT_TEXT:
        printf("Player %d text: \"%s\"\n", player_id,
            ev->typed.text);
        break;
    case EVENT_COMMAND:
        printf("Player %d command: \"%s\"\n", player_id,
            ev->command.name);
        break;
    default:
        printf("Player %d unknown event type\n", player_id);
        break;
    }
}

Event parse_event(char msg[128]) {

    Event e = { 0 };

    if (strstr(msg, "\"type\":\"button\"")) {
        char btn = 0;
        int pressed = 0;
        if (sscanf_s(msg, "{\"type\":\"button\",\"button\":\"%c\",\"pressed\":%d", &btn, 1, &pressed) == 2) {
            e.type = EVENT_BUTTON;
            e.button.button = btn;
            e.button.pressed = pressed;
        }
    }
    else if (strstr(msg, "\"type\":\"joystick\"")) {
        float x = 0, y = 0;
        if (sscanf_s(msg, "{\"type\":\"joystick\",\"x\":%f,\"y\":%f", &x, &y) == 2) {
            e.type = EVENT_JOYSTICK;
            e.joystick.x = x;
            e.joystick.y = y;
        }
        else if (strstr(msg, "\"type\":\"joystick\"")) {
            const char* x_pos = strstr(msg, "\"x\":");
            const char* y_pos = strstr(msg, "\"y\":");
            if (x_pos && y_pos) {
                float x = 0, y = 0;
                sscanf_s(x_pos, "\"x\":%f", &x);
                sscanf_s(y_pos, "\"y\":%f", &y);
                e.type = EVENT_JOYSTICK;
                e.joystick.x = x;
                e.joystick.y = y;
            }
        }

    }
    else if (strstr(msg, "\"type\":\"text\"")) {
        char text[64] = { 0 };
        const char* val = strstr(msg, "\"value\":\"");
        if (val) {
            val += 9;
            int i = 0;
            while (*val && *val != '"' && i < (int)sizeof(e.typed.text) - 1)
                e.typed.text[i++] = *val++;
            e.typed.text[i] = '\0';
            e.type = EVENT_TEXT;
        }
    }
    else if (strstr(msg, "\"type\":\"command\"")) {
        char name[32] = { 0 };
        const char* val = strstr(msg, "\"name\":\"");
        if (val) {
            val += 8;
            int i = 0;
            while (*val && *val != '"' && i < (int)sizeof(e.command.name) - 1)
                e.command.name[i++] = *val++;
            e.command.name[i] = '\0';
            e.type = EVENT_COMMAND;
        }
    }
    else {
        // fallback to char
        e.type = EVENT_CHAR;
        e.ch = msg[0];
    }
    return e;
}