-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrank_hands_function.sql
More file actions
172 lines (156 loc) · 6.02 KB
/
rank_hands_function.sql
File metadata and controls
172 lines (156 loc) · 6.02 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
--
-- Name: rank_hands(integer); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.rank_hands(hand_num integer) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
trips int := 0;
fours int := 0;
pairs int := 0;
no_of_players int := 0;
player int := 1;
suit char(1) := 'E';
card_no int := 1;
min_card_value int := 0;
max_card_value int := 0;
BEGIN
no_of_players := max(seat_no) from hands where hand_no=hand_num;
while player <= no_of_players loop
--look for flush
suit := a from (
select card_2_suit as a from hands where seat_no=player and hand_no=hand_num
union all
select card_1_suit from hands where seat_no=player and hand_no=hand_num
union all
select card_1_suit from community_cards where hand_no=hand_num
union all
select card_2_suit from community_cards where hand_no=hand_num
union all
select card_3_suit from community_cards where hand_no=hand_num
union all
select card_4_suit from community_cards where hand_no=hand_num
union all
select card_5_suit from community_cards where hand_no=hand_num
) x where a != '' group by a having count(*) >=5;
if suit is not null then
-- we have a flush check if straight
update hands set hand_rank = 5 where seat_no=player and hand_no=hand_num;
-- find the min strength and add 1, if you have another, continue
drop table if exists hand;
create temp table hand (hand_suit char(1), card_num int);
insert into hand (hand_suit, card_num)
select suit, card_1_strength from hands
where hand_no=hand_num and seat_no=player and card_1_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_2_strength from hands
where hand_no=hand_num and seat_no=player and card_2_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_1_strength from community_cards
where hand_no=hand_num and card_1_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_2_strength from community_cards
where hand_no=hand_num and card_2_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_3_strength from community_cards
where hand_no=hand_num and card_3_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_4_strength from community_cards
where hand_no=hand_num and card_4_suit=suit;
insert into hand (hand_suit, card_num)
select suit, card_5_strength from community_cards
where hand_no=hand_num and card_5_suit=suit;
min_card_value := min(card_num) from hand where hand_suit=suit;
max_card_value := max(card_num) from hand where hand_suit=suit;
if max_card_value - min_card_value = 4 then
--we have a straigh flush, royal flush wouldn't come in here
update hands set hand_rank = 2 where seat_no=player and hand_no=hand_num;
elsif min_card_value = 1 then
-- check if royal flush
min_card_value := min(card_num) from hand where hand_suit=suit and card_num != 1;
if min_card_value=10 then
--royal flush
update hands set hand_rank = 1 where seat_no=player and hand_no=hand_num;
--if a royal flush, nothing is higher, exit out of the ranking function
end if;
end if;
end if;
--look for straight Ace can be high or low
drop table if exists hand;
create temp table hand (hand_suit char(1), card_num int);
insert into hand (hand_suit, card_num)
select card_1_suit, card_1_strength from hands
where hand_no=hand_num and seat_no=player;
insert into hand (hand_suit, card_num)
select card_2_suit, card_2_strength from hands
where hand_no=hand_num and seat_no=player;
insert into hand (hand_suit, card_num)
select card_1_suit, card_1_strength from community_cards
where hand_no=hand_num;
insert into hand (hand_suit, card_num)
select card_2_suit, card_2_strength from community_cards
where hand_no=hand_num;
insert into hand (hand_suit, card_num)
select card_3_suit, card_3_strength from community_cards
where hand_no=hand_num;
insert into hand (hand_suit, card_num)
select card_4_suit, card_4_strength from community_cards
where hand_no=hand_num;
insert into hand (hand_suit, card_num)
select card_5_suit, card_5_strength from community_cards
where hand_no=hand_num;
--Find the lowest card num, look for one adjacent, repeat twice if none found, kick out
min_card_value := min(card_num) from hand;
--If the min card value is 1, that's an Ace so we need to check for a King
--if min_card_vlaue = 1 then
--setup the checks for pair/four of a kind/trips/full boat
drop table if exists card_counts;
create temp table card_counts (num_of_cards int, card_num int);
insert into card_counts(num_of_cards, card_num)
select count(*) as num_of_cards, card_num
from hand group by card_num having count(*) >= 2;
trips := count(*) from card_counts where num_of_cards=3;
fours := count(*) from card_counts where num_of_cards=4;
pairs := count(*) from card_counts where num_of_cards=2;
--look for a full house
if (trips = 1 and pairs >=1) or trips = 2
then
--we have a full house
update hands set hand_rank = 4 where seat_no=player and hand_no=hand_num;
end if;
--look for four of a kind
if fours = 1
then
update hands set hand_rank = 3 where seat_no=player and hand_no=hand_num;
end if;
--look for three of a kind
if trips >= 1 -- you have 3 of a kind, but don't set rank if it is already set
then
update hands set hand_rank = 7 where seat_no=player and hand_no=hand_num and hand_rank is null;
--look for pairs
end if;
if pairs >= 2 then
update hands set hand_rank = 8 where seat_no=player and hand_no=hand_num and hand_rank is null;
end if;
if pairs = 1 then
update hands set hand_rank = 9 where seat_no=player and hand_no=hand_num and hand_rank is null;
end if;
--determine highest card and suit
update hand set card_num=14 where card_num=1;
update hands set high_card_1 =
(select cast(card_num as varchar(2)) || ' ' || hand_suit from hand join suits
on hand_suit=suit_value
order by card_num desc , suit_rank desc limit 1)
where hand_no=hand_num and seat_no=player;
update hands set high_card_2 =
(select cast(card_num as varchar(2)) || ' ' || hand_suit from hand join suits
on hand_suit=suit_value
order by card_num desc, suit_rank desc offset 1 limit 1)
where hand_no=hand_num and seat_no=player;
player := player + 1;
drop table hand;
drop table card_counts;
end loop;
END;
$$;
ALTER FUNCTION public.rank_hands(hand_num integer) OWNER TO postgres;