aboutsummaryrefslogtreecommitdiff
path: root/client/acronc/client.c
blob: d64f96fc5b17158a66c9534bc0ad3a85f37a77c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Created by yuuta on 7/24/22.
 */

#include "client.h"
#include "helpers.h"

#include <libac.h>
#include <stdio.h>

static void handle_event(const ac_event_t *event) {
    switch (event->type) {
        case AC_EVENT_LAGGING: {
            ac_event_lagging_t *o = (ac_event_lagging_t *) event;
            printf("Server lagging: running %ld milliseconds (%ld ticks) behind.\n",
                   o->ms,
                   o->ticks);
            break;
        }
        case AC_EVENT_ENTITY_DEATH: {
            ac_event_entity_death_t *o = (ac_event_entity_death_t *) event;
            printf("Entity '%s' died at %s(%.2f, %.2f, %.2f): %s.\n",
                   o->entity.name,
                   world_name(o->entity.world),
                   o->entity.pos.x,
                   o->entity.pos.y,
                   o->entity.pos.z,
                   o->message);
            break;
        }
        case AC_EVENT_PLAYER_MESSAGE: {
            ac_event_player_message_t *o = (ac_event_player_message_t *) event;
            printf("Player '%s' said at %s(%.2f, %.2f, %.2f): %s.\n",
                   o->player.name,
                   world_name(o->player.world),
                   o->player.pos.x,
                   o->player.pos.y,
                   o->player.pos.z,
                   o->text);
            break;
        }
        case AC_EVENT_PLAYER_DISCONNECT: {
            ac_event_player_disconnect_t *o = (ac_event_player_disconnect_t *) event;
            printf("Player '%s' disconnected at %s(%.2f, %.2f, %.2f): %s.\n",
                   o->player.name,
                   world_name(o->player.world),
                   o->player.pos.x,
                   o->player.pos.y,
                   o->player.pos.z,
                   o->reason);
            break;
        }
        case AC_EVENT_PLAYER_JOIN: {
            ac_event_player_join_t *o = (ac_event_player_join_t *) event;
            printf("Player '%s' joined at %s(%.2f, %.2f, %.2f).\n",
                   o->player.name,
                   world_name(o->player.world),
                   o->player.pos.x,
                   o->player.pos.y,
                   o->player.pos.z);
            break;
        }
        default: {
            printf("Received an unrecognized event of type '%u'.\n",
                   event->type);
        }
    }
}

static void handle_response(const ac_response_t *response) {
    switch (response->type) {
        case AC_RESPONSE_OK: {
            ac_response_ok_t *o = (ac_response_ok_t *) response;
#ifdef DEBUG
            printf("Request %d OK.\n",
                   o->id);
#endif
            break;
        }
        case AC_RESPONSE_ERROR: {
            ac_response_error_t *o = (ac_response_error_t *) response;
            printf("Request %d failed: %s (%d).\n",
                   o->id,
                   o->message,
                   o->code);
            break;
        }
        case AC_RESPONSE_CMD_OUT: {
            ac_response_cmd_out_t *o = (ac_response_cmd_out_t *) response;
#ifdef DEBUG
            printf("Request %d output by %s: %s.\n",
                   o->id,
                   o->sender,
                   o->out);
#else
            printf("%s\n",
                   o->out);
#endif
            break;
        }
        case AC_RESPONSE_CMD_RESULT: {
            ac_response_cmd_result_t *o = (ac_response_cmd_result_t *) response;
#ifdef DEBUG
            printf("Request %d is done: %s (%d).\n",
                   o->id,
                   o->success ? "Success" : "Failed",
                   o->result);
#endif
            break;
        }
        default: {
            printf("Received an unrecognized response of type '%u'.\n",
                   response->type);
        }
    }
}

int handle_object(ac_obj_t *obj) {
    if (AC_IS_RESPONSE(obj->type)) handle_response((ac_response_t *) obj);
    else handle_event((ac_event_t *) obj);
    ac_object_free(obj);
    return 0;
}