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