-
Notifications
You must be signed in to change notification settings - Fork 7
/
battleships.sql
54 lines (45 loc) · 1.48 KB
/
battleships.sql
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
create or replace procedure screen_loop()
language plpgsql
as $$
declare
keyboard_session_id text;
game_id text;
txt text;
player text;
begin
select keyboard_init() into keyboard_session_id;
raise info '';
raise info 'new game; -- create new game server';
raise info 'connect <game-id>; -- connect to existing game server';
loop
select keyboard_read(keyboard_session_id) into txt;
if txt = 'new game;' then
raise info 'Starting new game...';
select game_create() into game_id;
commit;
raise info 'Game ID: %', game_id;
raise info '';
raise info 'Waiting for opponent...';
call game_wait_for_opponent(game_id);
select 'a' into player;
exit;
end if;
if substr(txt, 0, 8) = 'connect' then
select substr(txt, 9, 10) into game_id;
raise info 'Connecting to %...', game_id;
call game_connect(game_id);
raise info 'Connected!';
select 'b' into player;
exit;
end if;
end loop;
raise info 'Placing ships phase...';
raise info '';
call game_place_ships_loop(keyboard_session_id, game_id, player);
call game_wait_for_ready(game_id);
raise info 'All players are ready!';
raise info 'Battle begins in several seconds...!';
perform pg_sleep(2);
call game_battlefield_loop(keyboard_session_id, game_id, player);
end
$$;