forked from SufalBasak/TypeSpeakPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_ranked_tables.sql
More file actions
54 lines (44 loc) · 2.11 KB
/
Copy pathcreate_ranked_tables.sql
File metadata and controls
54 lines (44 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Add ELO Rating to Profiles (if it doesn't exist)
-- Note: Assuming 'profiles' table exists and mirrors auth.users.
-- If you don't have a 'profiles' table yet, this creates a basic one or adds columns to auth.users metadata wrapper.
-- Since we often can't modify auth.users directly, we'll create/update a public.profiles table.
create table if not exists public.profiles (
id uuid references auth.users(id) on delete cascade primary key,
username text,
avatar_url text,
elo_rating int default 1000,
ranked_matches_played int default 0,
updated_at timestamp with time zone
);
-- Enable RLS on profiles
alter table public.profiles enable row level security;
create policy "Public profiles are viewable by everyone."
on public.profiles for select
using ( true );
create policy "Users can insert their own profile."
on public.profiles for insert
with check ( auth.uid() = id );
create policy "Users can update own profile."
on public.profiles for update
using ( auth.uid() = id );
-- Create Ranked Matches Table
create table public.ranked_matches (
id uuid default gen_random_uuid() primary key,
player1_id uuid references auth.users(id) not null,
player2_id uuid references auth.users(id), -- Can be null if it's a Bot/Ghost which we might handle differently, but usually we want a record. For Ghost, maybe we store the Ghost ID or null.
winner_id uuid references auth.users(id),
elo_change int default 0,
created_at timestamp with time zone default timezone('utc'::text, now()) not null
);
-- RLS for Matches
alter table public.ranked_matches enable row level security;
create policy "Anyone can read matches"
on public.ranked_matches for select
using ( true );
create policy "Server/Functions can insert matches"
on public.ranked_matches for insert
with check ( true ); -- Ideally locked down to service role, but for client-side demo:
-- Allow authenticated users to insert match results (In production, move ELO logic to Database Function/Edge Function to prevent cheating)
create policy "Users can insert matches"
on public.ranked_matches for insert
with check ( auth.role() = 'authenticated' );