Skip to content

Commit f32ee0f

Browse files
Merge pull request #29 from usermicrodevices/develop
add SQLProvider, move sql queries to separated files
2 parents b446784 + e90be72 commit f32ee0f

17 files changed

Lines changed: 1239 additions & 1662 deletions

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ if(USE_SQLITE)
185185
target_compile_definitions(gameserver PRIVATE USE_SQLITE=1)
186186
endif()
187187

188+
add_definitions(-DUSE_POSTGRESQL=1)
189+
add_definitions(-DUSE_SPDLOG=1)
190+
188191
# Link libraries
189192
target_link_libraries(gameserver PRIVATE
190193
${OPENGL_LIBRARIES}
@@ -218,4 +221,4 @@ target_link_options(gameserver PRIVATE -Wl,-Bsymbolic)
218221
# Installation
219222
install(TARGETS gameserver DESTINATION bin)
220223
install(DIRECTORY config/ DESTINATION config)
221-
install(DIRECTORY scripts/ DESTINATION scripts)
224+
install(DIRECTORY scripts/ DESTINATION scripts)

build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ echo "Building with Citus: $USE_CITUS, SQLite: $USE_SQLITE"
6464
rm -f CMakeCache.txt Makefile cmake_install.cmake
6565
rm -rf CMakeFiles
6666

67-
# Create build directory and copy config
67+
# Create build directory and copy related folders
6868
mkdir -p build
6969
rsync -a --delete config/ build/config/
70+
rsync -a --delete dbschema/ build/dbschema/
7071
cd build
7172

7273
# Run CMake
@@ -87,4 +88,4 @@ fi
8788

8889
# create default database user (commented out by default)
8990
#sudo -u postgres psql -c "DROP USER IF EXISTS gameuser;"
90-
#sudo -u postgres psql -c "CREATE USER gameuser WITH PASSWORD 'password' SUPERUSER;"
91+
#sudo -u postgres psql -c "CREATE USER gameuser WITH PASSWORD 'password' SUPERUSER;"

dbschema/citus.sql

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
-- [create_distributed_table_players]
2+
SELECT create_distributed_table('players', 'id');
3+
4+
-- [create_distributed_table_player_inventory]
5+
SELECT create_distributed_table('player_inventory', 'player_id');
6+
7+
-- [create_distributed_table_player_quests]
8+
SELECT create_distributed_table('player_quests', 'player_id');
9+
10+
-- [create_distributed_table_world_chunks]
11+
SELECT create_distributed_table('world_chunks', 'chunk_x');
12+
13+
-- [create_distributed_table_npcs]
14+
SELECT create_distributed_table('npcs', 'id');
15+
16+
-- [create_reference_table_loot_tables]
17+
SELECT create_reference_table('loot_tables');
18+
19+
-- [add_worker_node]
20+
SELECT citus_add_node($1, $2);
21+
22+
-- [remove_worker_node]
23+
SELECT citus_remove_node($1);
24+
25+
-- [get_worker_nodes]
26+
SELECT nodeid, nodename, nodeport, noderole, isactive FROM pg_dist_node ORDER BY nodeid;
27+
28+
-- [get_shard_placements]
29+
SELECT shardid, nodename, nodeport, placementid
30+
FROM pg_dist_placement p
31+
JOIN pg_dist_node n ON p.groupid = n.groupid
32+
ORDER BY shardid, placementid;
33+
34+
-- [rebalance_shards]
35+
SELECT rebalance_table_shards();
36+
37+
-- [move_shard]
38+
SELECT citus_move_shard_placement($1, $2, $3);
39+
40+
-- [isolate_shard]
41+
UPDATE pg_dist_placement SET shardstate = 3 WHERE shardid = $1;
42+
43+
-- [get_shard_statistics]
44+
SELECT shardid,
45+
COUNT(*) as replica_count,
46+
SUM(CASE WHEN shardstate = 1 THEN 1 ELSE 0 END) as active_replicas,
47+
SUM(CASE WHEN shardstate = 3 THEN 1 ELSE 0 END) as isolated_replicas
48+
FROM pg_dist_placement
49+
GROUP BY shardid
50+
ORDER BY shardid;
51+
52+
-- [enable_citus_extension]
53+
CREATE EXTENSION IF NOT EXISTS citus;
54+
55+
-- [check_citus_extension]
56+
SELECT EXISTS(SELECT 1 FROM pg_extension WHERE extname = 'citus');
57+
58+
-- [get_worker_node_stats]
59+
SELECT nodename, nodeport,
60+
COUNT(DISTINCT shardid) as shard_count,
61+
SUM(shardsize) as total_size_bytes
62+
FROM pg_dist_placement p
63+
JOIN pg_dist_node n ON p.groupid = n.groupid
64+
GROUP BY nodename, nodeport
65+
ORDER BY nodename, nodeport;
66+
67+
-- [create_distributed_table]
68+
SELECT create_distributed_table($1, $2);
69+
70+
-- [create_reference_table]
71+
SELECT create_reference_table($1);
72+
73+
-- [create_distributed_function]
74+
SELECT create_distributed_function($1);
75+
76+
-- [get_query_stats]
77+
SELECT query, calls, total_time, mean_time, rows
78+
FROM pg_stat_statements
79+
ORDER BY total_time DESC
80+
LIMIT 20;
81+
82+
-- [get_cluster_stats]
83+
SELECT
84+
(SELECT COUNT(*) FROM pg_dist_node WHERE noderole = 'primary') as primary_nodes,
85+
(SELECT COUNT(*) FROM pg_dist_node WHERE noderole = 'secondary') as secondary_nodes,
86+
(SELECT COUNT(DISTINCT shardid) FROM pg_dist_placement) as total_shards,
87+
(SELECT COUNT(*) FROM pg_dist_placement WHERE shardstate = 1) as active_placements,
88+
(SELECT COUNT(*) FROM pg_dist_placement WHERE shardstate = 3) as isolated_placements,
89+
(SELECT SUM(shardsize) FROM pg_dist_placement) as total_data_size_bytes;
90+
91+
-- [get_shard_query_stats]
92+
SELECT shardid, query, calls, total_time
93+
FROM citus_stat_statements
94+
WHERE shardid = $1
95+
ORDER BY total_time DESC
96+
LIMIT 50;
97+
98+
-- [replicate_reference_tables]
99+
SELECT citus_replicate_reference_tables();

dbschema/postgres.sql

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
-- [create_table_players]
2+
CREATE TABLE IF NOT EXISTS players (
3+
id BIGINT PRIMARY KEY,
4+
data JSONB NOT NULL,
5+
position_x REAL DEFAULT 0,
6+
position_y REAL DEFAULT 0,
7+
position_z REAL DEFAULT 0,
8+
level INTEGER DEFAULT 1,
9+
experience REAL DEFAULT 0,
10+
health INTEGER DEFAULT 100,
11+
max_health INTEGER DEFAULT 100,
12+
mana INTEGER DEFAULT 50,
13+
max_mana INTEGER DEFAULT 50,
14+
currency_gold INTEGER DEFAULT 0,
15+
currency_gems INTEGER DEFAULT 0,
16+
total_playtime INTEGER DEFAULT 0,
17+
created_at TIMESTAMPTZ DEFAULT NOW(),
18+
updated_at TIMESTAMPTZ DEFAULT NOW()
19+
);
20+
21+
-- [create_table_game_state]
22+
CREATE TABLE IF NOT EXISTS game_state (
23+
key VARCHAR(64) PRIMARY KEY,
24+
value JSONB NOT NULL,
25+
updated_at TIMESTAMPTZ DEFAULT NOW()
26+
);
27+
28+
-- [create_table_world_chunks]
29+
CREATE TABLE IF NOT EXISTS world_chunks (
30+
chunk_x INTEGER NOT NULL,
31+
chunk_z INTEGER NOT NULL,
32+
biome INTEGER NOT NULL,
33+
data JSONB NOT NULL,
34+
last_updated TIMESTAMPTZ DEFAULT NOW(),
35+
PRIMARY KEY (chunk_x, chunk_z)
36+
);
37+
38+
-- [create_table_player_inventory]
39+
CREATE TABLE IF NOT EXISTS player_inventory (
40+
player_id BIGINT PRIMARY KEY REFERENCES players(id) ON DELETE CASCADE,
41+
data JSONB NOT NULL,
42+
last_updated TIMESTAMPTZ DEFAULT NOW()
43+
);
44+
45+
-- [create_table_player_quests]
46+
CREATE TABLE IF NOT EXISTS player_quests (
47+
player_id BIGINT REFERENCES players(id) ON DELETE CASCADE,
48+
quest_id VARCHAR(64) NOT NULL,
49+
progress JSONB NOT NULL,
50+
last_updated TIMESTAMPTZ DEFAULT NOW(),
51+
PRIMARY KEY (player_id, quest_id)
52+
);
53+
54+
-- [create_table_npcs]
55+
CREATE TABLE IF NOT EXISTS npcs (
56+
id BIGINT PRIMARY KEY,
57+
type INTEGER NOT NULL,
58+
position JSONB NOT NULL,
59+
level INTEGER NOT NULL DEFAULT 1,
60+
data JSONB NOT NULL,
61+
created_at TIMESTAMPTZ DEFAULT NOW(),
62+
updated_at TIMESTAMPTZ DEFAULT NOW()
63+
);
64+
65+
-- [create_table_loot_tables]
66+
CREATE TABLE IF NOT EXISTS loot_tables (
67+
table_id VARCHAR(64) PRIMARY KEY,
68+
name VARCHAR(128) NOT NULL,
69+
data JSONB NOT NULL,
70+
created_at TIMESTAMPTZ DEFAULT NOW()
71+
);
72+
73+
-- [create_table_schema_migrations]
74+
CREATE TABLE IF NOT EXISTS schema_migrations (
75+
version INTEGER PRIMARY KEY,
76+
name VARCHAR(255) NOT NULL,
77+
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
78+
checksum VARCHAR(64)
79+
);
80+
81+
-- [save_player_data]
82+
INSERT INTO players (id, data, updated_at) VALUES ($1, $2, NOW())
83+
ON CONFLICT (id) DO UPDATE SET data = EXCLUDED.data, updated_at = NOW();
84+
85+
-- [load_player_data]
86+
SELECT data FROM players WHERE id = $1;
87+
88+
-- [update_player_position]
89+
UPDATE players SET position_x = $1, position_y = $2, position_z = $3, updated_at = NOW() WHERE id = $4;
90+
91+
-- [player_exists]
92+
SELECT EXISTS(SELECT 1 FROM players WHERE id = $1);
93+
94+
-- [get_player_stats]
95+
SELECT level, experience, health, max_health, mana, max_mana, currency_gold, currency_gems, total_playtime FROM players WHERE id = $1;
96+
97+
-- [get_player]
98+
SELECT * FROM players WHERE id = $1;
99+
100+
-- [save_game_state]
101+
INSERT INTO game_state (key, value, updated_at) VALUES ($1, $2, NOW())
102+
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW();
103+
104+
-- [load_game_state]
105+
SELECT value FROM game_state WHERE key = $1;
106+
107+
-- [delete_game_state]
108+
DELETE FROM game_state WHERE key = $1;
109+
110+
-- [list_game_states]
111+
SELECT key FROM game_state ORDER BY key;
112+
113+
-- [save_chunk_data]
114+
INSERT INTO world_chunks (chunk_x, chunk_z, biome, data, last_updated) VALUES ($1, $2, $3, $4, NOW())
115+
ON CONFLICT (chunk_x, chunk_z) DO UPDATE SET biome = EXCLUDED.biome, data = EXCLUDED.data, last_updated = NOW();
116+
117+
-- [load_chunk_data]
118+
SELECT data FROM world_chunks WHERE chunk_x = $1 AND chunk_z = $2;
119+
120+
-- [delete_chunk_data]
121+
DELETE FROM world_chunks WHERE chunk_x = $1 AND chunk_z = $2;
122+
123+
-- [list_chunks_in_range]
124+
SELECT chunk_x, chunk_z FROM world_chunks WHERE chunk_x BETWEEN $1 AND $2 AND chunk_z BETWEEN $3 AND $4;
125+
126+
-- [save_inventory]
127+
INSERT INTO player_inventory (player_id, data, last_updated) VALUES ($1, $2, NOW())
128+
ON CONFLICT (player_id) DO UPDATE SET data = EXCLUDED.data, last_updated = NOW();
129+
130+
-- [load_inventory]
131+
SELECT data FROM player_inventory WHERE player_id = $1;
132+
133+
-- [save_quest_progress]
134+
INSERT INTO player_quests (player_id, quest_id, progress, last_updated) VALUES ($1, $2, $3, NOW())
135+
ON CONFLICT (player_id, quest_id) DO UPDATE SET progress = EXCLUDED.progress, last_updated = NOW();
136+
137+
-- [load_quest_progress]
138+
SELECT progress FROM player_quests WHERE player_id = $1 AND quest_id = $2;
139+
140+
-- [list_active_quests]
141+
SELECT quest_id FROM player_quests WHERE player_id = $1 ORDER BY quest_id;
142+
143+
-- [begin_transaction]
144+
BEGIN;
145+
146+
-- [commit_transaction]
147+
COMMIT;
148+
149+
-- [rollback_transaction]
150+
ROLLBACK;
151+
152+
-- [migration_current_version]
153+
SELECT MAX(version) as current_version FROM schema_migrations;
154+
155+
-- [delete_migration]
156+
DELETE FROM schema_migrations WHERE version = $1;
157+
158+
-- [enable_pg_stat_statements]
159+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
160+
161+
-- [disable_pg_stat_statements]
162+
DROP EXTENSION IF EXISTS pg_stat_statements;

0 commit comments

Comments
 (0)