aboutsummaryrefslogtreecommitdiff
path: root/client/libacron/acronc/main.c
blob: 282f6e3d0c7f070e9c113c78a5e10990fdc5c99f (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Created by yuuta on 7/20/22.
 */

#include "libac.h"

#include <stdio.h>
#include <unistd.h>

static const char *world_name(const enum ac_world world) {
    switch (world) {
        case overworld:
            return "overworld";
        case nether:
            return "nether";
        case end:
            return "end";
        default:
            return "unknown world";
    }
}

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 %lu milliseconds (%lu 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;
            printf("Request %d OK.\n",
                   o->id);
            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;
            printf("Request %d output by %s: %s.\n",
                   o->id,
                   o->sender,
                   o->out);
            break;
        }
        case AC_RESPONSE_CMD_RESULT: {
            ac_response_cmd_result_t *o = (ac_response_cmd_result_t *) response;
            printf("Request %d is done: %s (%d).\n",
                   o->id,
                   o->success ? "Success" : "Failed",
                   o->result);
            break;
        }
        default: {
            printf("Received an unrecognized response of type '%u'.\n",
                   response->type);
        }
    }
}

int main(int argc, char **argv) {
    int r;
    libac_config_t config = {
#ifdef DEBUG
            .out = stderr,
#else
            .out = NULL,
#endif
            .tok = NULL
    };
    if ((r = ac_init(&config))) return r;
    void *connection;
    ac_connection_parameters_t parameters = {
            .url = "ws://localhost:25575/ws?id=1&token=123"
    };
    if ((r = ac_connect(parameters, &connection))) {
        ac_free();
        return r;
    }
    ac_obj_t *obj;
    ac_config_t conf = {
            .name = "acronc"
    };
    ac_request_cmd_t req = {
            .type = AC_REQUEST_CMD,
            .id = 100,
            .config = &conf,
            .cmd = "say Hi"
    };
    if ((r = ac_request(connection, (ac_request_t *) &req))) {
        ac_disconnect(connection);
        ac_free();
        return r;
    }
    while (!(r = ac_receive(connection, &obj))) {
        if (!obj) {
            continue;
        }
        if (AC_IS_EVENT(obj->type)) {
            handle_event((ac_event_t *) obj);
        } else if (AC_IS_RESPONSE(obj->type)) {
            handle_response((ac_response_t *) obj);
        }
        ac_object_free(obj);
    }
    ac_disconnect(connection);
    ac_free();
    return r;
}