-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.sql
More file actions
143 lines (112 loc) · 4.72 KB
/
Setup.sql
File metadata and controls
143 lines (112 loc) · 4.72 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
create table cards(id serial primary key, value varchar(2), suit varchar(1));
create table shuffled_deck(id int primary key references cards(id));
create table hands(id bigserial primary key, seat_no int, card_1 varchar(3), card_2 varchar(3), hand_no int);
create table community_cards(hand_no int, burn_1 varchar(3), card_1 varchar(3), card_2 varchar(3),
card_3 varchar(3), burn_2 varchar(3), card_4 varchar(3), burn_3 varchar(3), card_5 varchar(3));
CREATE or replace FUNCTION shuffle() RETURNS void AS $$
DECLARE
count int := 1;
card int := 1;
exists int := 0;
BEGIN
alter sequence shuffled_deck_place_in_deck_seq restart with 1;
truncate table shuffled_deck;
while count <= 52
loop
card = floor(random() *(52-1+1)+1);
exists = count(*) from shuffled_deck where id=card;
if exists = 0 then
insert into shuffled_deck(id) values(card);
count := count + 1;
end if;
end loop;
END;
$$ LANGUAGE plpgsql;
/*
jp_poker=# insert into cards(value, suit) values
jp_poker-# ('A','D'),('K','D'),('Q','D'),('J','D'),(10,'D'),(9,'D'),(8,'D'),(7,'D'),(6,'D'),(5,'D'),(4,'D'),(3,'D'),(2,'D');
INSERT 0 13
jp_poker=# insert into cards(value, suit) select value, 'S' from cards where suit='D';
INSERT 0 13
jp_poker=# insert into cards(value, suit) select value, 'C' from cards where suit='D';
INSERT 0 13
jp_poker=# insert into cards(value, suit) select value, 'H' from cards where suit='D';
INSERT 0 13
*/
CREATE or replace FUNCTION deal(players int) RETURNS void AS $$
DECLARE
count int := 1;
hand_num int := 0;
seat_no int := 1;
community_id int := 0;
card varchar(3) := 'XYZ';
BEGIN
hand_num = coalesce(max(hands.hand_no),0)+1 from hands;
-- deal out the first card
while seat_no <= players loop
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
insert into hands(seat_no, card_1, hand_no)
values(seat_no,card,hand_num);
count := count + 1;
seat_no := seat_no + 1;
end loop;
--reset the seat posistion
seat_no := 1;
-- deal out the second card
while seat_no <= players loop
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
insert into hands(seat_no, card_2, hand_no)
values(seat_no,card,hand_num);
count := count + 1;
seat_no := seat_no + 1;
end loop;
-- deal the community cards
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
insert into community_cards(hand_no,burn_1)
values(hand_num, card);
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set card_1 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set card_2 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set card_3 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set burn_2 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set card_4 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set burn_3 = card where community_cards.hand_no = hand_num;
-- goto next card
count := count + 1;
card := value || suit from cards join shuffled_deck on
shuffled_deck.id=cards.id where place_in_deck=count;
update community_cards set card_5 = card where community_cards.hand_no = hand_num;
END;
$$ LANGUAGE plpgsql;
CREATE or replace function play_game(players int) RETURNS void AS $$
BEGIN
perform shuffle();
perform deal(players);
END;
$$ LANGUAGE plpgsql;