From 384e4b144419a8a61cf878ac0fe1daae086100c8 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Wed, 9 Aug 2023 16:58:31 +0200 Subject: [PATCH] Changed method for getting random location. --- src/database/models/location.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/database/models/location.rs b/src/database/models/location.rs index fe9328f..c29045a 100644 --- a/src/database/models/location.rs +++ b/src/database/models/location.rs @@ -17,7 +17,14 @@ impl Location { ) -> Result { sqlx::query_as!( Location, - "SELECT id, map, has_entities FROM locations WHERE has_entities = $1 ORDER BY RANDOM() LIMIT 1", + " + SELECT id, map, has_entities FROM locations + WHERE has_entities = $1 + OFFSET ( + SELECT floor(random() * (SELECT count(id) FROM locations WHERE has_entities = $1))::int + ) + LIMIT 1 + ", has_entities ) .fetch_one(&db.pool)