From a43a4427d3dcb93e4317291bbd6227bc4130670e Mon Sep 17 00:00:00 2001
From: Tor Andersson <tor@ccxvii.net>
Date: Thu, 14 Mar 2024 20:50:44 +0100
Subject: WIP match making

Use greedy best fit algorithm to partition tickets into matches.
Iterate using random start points to search for the best partitioning.
---
 schema.sql | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

(limited to 'schema.sql')

diff --git a/schema.sql b/schema.sql
index a0a3b02..fd54c9e 100644
--- a/schema.sql
+++ b/schema.sql
@@ -221,6 +221,42 @@ create table if not exists setups (
 	unique (title_id, player_count, scenario)
 );
 
+create table if not exists tickets (
+	ticket_id integer primary key,
+	user_id integer,
+	setup_id integer,
+	pace integer,
+	time real, -- julianday
+	unique (user_id, setup_id, pace)
+);
+
+drop view if exists ticket_rating_view;
+create view ticket_rating_view as
+	select
+		tickets.*,
+		coalesce(rating, 1500) as rating
+	from tickets
+	join setups using(setup_id)
+	left join ratings using(title_id, user_id)
+	where time < julianday('now', '-30 seconds')
+;
+
+drop view if exists matchmaking_view;
+create view matchmaking_view as
+	select
+		setup_id,
+		pace,
+		count(1) / player_count as n,
+		julianday() - min(time) as age
+	from
+		tickets
+		natural join setups
+	group by
+		setup_id, pace
+	having n > 0
+	order by age desc
+;
+
 -- Friend and Block Lists --
 
 create table if not exists contacts (
@@ -659,6 +695,47 @@ begin
 		players.game_id = old.game_id and players.role in ( 'Both', old.active );
 end;
 
+drop trigger if exists trigger_part_check;
+create trigger trigger_part_check before delete on players
+begin
+	select
+		raise(abort, 'Cannot remove players from matches and/or finished games!')
+	where
+		old.user_id > 0 and exists (
+			select 1 from games where games.game_id=old.game_id and ( is_match or status > 1 )
+		)
+	;
+end;
+
+-- Log matchmaking runs and expired tickets
+
+create table if not exists matchmaking_log (
+	time datetime default current_timestamp,
+	setup_id integer,
+	pace integer,
+	age real,
+	score integer,
+	tickets json,
+	matches json
+);
+
+drop view if exists matchmaking_log_view;
+create view matchmaking_log_view as
+	select
+		time, title_id, player_count, scenario, pace, age, score, tickets, matches
+	from
+		matchmaking_log
+		natural join setups
+;
+
+create table if not exists expired_tickets_log (
+	user_id integer,
+	setup_id integer,
+	pace integer,
+	ctime datetime,
+	xtime datetime
+);
+
 -- Trigger to remove game data when filing a game as archived
 
 drop trigger if exists trigger_archive_game;
@@ -705,6 +782,7 @@ begin
 	delete from game_chat where user_id = old.user_id;
 	delete from ratings where user_id = old.user_id;
 	delete from players where user_id = old.user_id and game_id in (select game_id from games where status <= 1);
+	delete from tickets where user_id = old.user_id;
 	update players set user_id = 0 where user_id = old.user_id;
 	update games set owner_id = 0 where owner_id = old.user_id;
 end;
-- 
cgit v1.2.3

