]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/server_commands.c
Generate spawn hut
[dragonblocks_alpha.git] / src / server / server_commands.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "day.h"
4 #include "server/database.h"
5 #include "server/server.h"
6 #include "server/server_map.h"
7 #include "perlin.h"
8 #include "util.h"
9
10 // command callbacks
11
12 // disconnect client without sending a packet (client won't recieve it)
13 static bool disconnect_handler(Client *client, bool good)
14 {
15         if (good)
16                 server_disconnect_client(client, DISCO_NO_SEND, NULL);
17         return true;
18 }
19
20 // insert client into player list and update state (if checks pass)
21 static bool auth_handler(Client *client, bool good)
22 {
23         char *name = read_string(client->fd, PLAYER_NAME_MAX);
24
25         if (! name)
26                 return false;
27
28         if (! good) {
29                 free(name);
30                 return true;
31         }
32
33         pthread_rwlock_wrlock(&client->server->players_rwlck);
34         u8 success = list_put(&client->server->players, name, client);
35
36         if (success) {
37                 client->name = name;
38                 client->state = CS_ACTIVE;
39
40                 if (! database_load_player(client->name, &client->pos)) {
41                         client->pos = (v3f64) {0.0, server_map.spawn_height + 0.5, 0.0};
42
43                         database_create_player(client->name, client->pos);
44                 }
45         } else {
46                 free(name);
47         }
48
49         pthread_mutex_lock(&client->mtx);
50         bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
51         if (ret && success)
52                 ret = ret
53                         && write_u32(client->fd, CC_INFO) && write_u32(client->fd, client->server->config.simulation_distance) && write_s32(client->fd, seed)
54                         && write_u32(client->fd, CC_SETPOS) && write_v3f64(client->fd, client->pos)
55                         && write_u32(client->fd, CC_TIMEOFDAY) && write_u64(client->fd, (u64) get_time_of_day());
56         pthread_mutex_unlock(&client->mtx);
57
58         pthread_rwlock_unlock(&client->server->players_rwlck);
59
60         printf("Authentication %s: %s -> %s\n", success ? "success" : "failure", client->address, name);
61
62         return ret;
63 }
64
65 // set a node on the map
66 static bool setnode_handler(Client *client, bool good)
67 {
68         v3s32 pos;
69
70         if (! read_v3s32(client->fd, &pos))
71                 return false;
72
73         MapNode node;
74
75         if (! map_deserialize_node(client->fd, &node))
76                 return false;
77
78         if (good)
79                 map_set_node(server_map.map, pos, node, false, NULL);
80
81         return true;
82 }
83
84 // update player's position
85 static bool pos_handler(Client *client, bool good)
86 {
87         v3f64 pos;
88
89         if (! read_v3f64(client->fd, &pos))
90                 return false;
91
92         if (good) {
93                 client->pos = pos;
94                 database_update_player_pos(client->name, client->pos);
95         }
96
97         return true;
98 }
99
100 // tell server map manager client requested the block
101 static bool request_block_handler(Client *client, bool good)
102 {
103         v3s32 pos;
104
105         if (! read_v3s32(client->fd, &pos))
106                 return false;
107
108         if (good)
109                 server_map_requested_block(client, pos);
110
111         return true;
112 }
113
114 // declared in network.h
115 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
116         {0},
117         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
118         {&auth_handler, "AUTH", CS_CREATED},
119         {&setnode_handler, "SETNODE", CS_ACTIVE},
120         {&pos_handler, "POS", CS_ACTIVE},
121         {&request_block_handler, "REQUEST_BLOCK", CS_ACTIVE},
122 };