-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpachete.sql
162 lines (137 loc) · 5.98 KB
/
pachete.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
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
--=============================================================================--
--- PACHET PENTRU GESTIONAREA DATELOR DIN BAZA DE DATE IN BOTUL DE PE DISCORD ---
CREATE OR REPLACE PACKAGE discord_bot AS
function getAllUsers return varchar2;
function getTopUsers return varchar2;
function getUserMaxScore(p_username varchar2) return integer;
function checkIfUserExists(p_username varchar2) return integer;
END discord_bot;
/
CREATE OR REPLACE PACKAGE BODY discord_bot AS
FUNCTION getAllUsers return varchar2 is
cursor jucator is select * from useri order by username;
v_linie_user jucator%rowtype;
v_text_returnat varchar2(1000);
v_index_linie_returnata integer := 1;
begin
open jucator;
loop
fetch jucator into v_linie_user;
exit when jucator%notfound;
v_text_returnat := v_text_returnat || v_index_linie_returnata || ' - ' || v_linie_user.username || chr(10);
v_index_linie_returnata := v_index_linie_returnata + 1;
end loop;
return v_text_returnat;
end;
function getUserMaxScore(p_username varchar2) return integer is
v_scor_maxim scoruri.scor%type := 0;
v_userul_exista integer := 0;
v_userul_are_scor integer := 0;
begin
select count(*) into v_userul_exista from useri where USERNAME = p_username;
if v_userul_exista != 0 then
select count(*) into v_userul_are_scor from scoruri where ID_USER = (select id from useri where USERNAME = p_username);
if v_userul_are_scor != 0 then
select max(scor) into v_scor_maxim
from scoruri where id_user =
(select id from useri where username = p_username);
end if;
end if;
return v_scor_maxim;
end;
FUNCTION getTopUsers return varchar2 is
cursor jucator is select * from useri order by username;
v_linie_user jucator%rowtype;
v_scor_maxim_jucator integer;
v_scor_loc1 scoruri.scor%type := 9999;
v_scor_loc2 scoruri.scor%type := 9999;
v_scor_loc3 scoruri.scor%type := 9999;
v_username_loc1 useri.username%type := '';
v_username_loc2 useri.username%type := '';
v_username_loc3 useri.username%type := '';
v_text_returnat varchar2(1000);
begin
open jucator;
loop
fetch jucator into v_linie_user;
v_scor_maxim_jucator := getUserMaxScore(v_linie_user.USERNAME);
if v_scor_maxim_jucator < v_scor_loc1 then
v_scor_loc3 := v_scor_loc2;
v_scor_loc2 := v_scor_loc1;
v_scor_loc1 := v_scor_maxim_jucator;
v_username_loc3 := v_username_loc2;
v_username_loc2 := v_username_loc1;
v_username_loc1 := v_linie_user.USERNAME;
else
if v_scor_maxim_jucator < v_scor_loc2 then
v_scor_loc3 := v_scor_loc2;
v_scor_loc2 := v_scor_maxim_jucator;
v_username_loc3 := v_username_loc2;
v_username_loc2 := v_linie_user.USERNAME;
else
if v_scor_maxim_jucator < v_scor_loc3 then
v_scor_loc3 := v_scor_maxim_jucator;
v_username_loc3 := v_linie_user.USERNAME;
end if;
end if;
end if;
exit when jucator%notfound;
end loop;
if v_scor_loc1 = 9999 then
v_scor_loc1 := 0;
end if;
if v_scor_loc2 = 9999 then
v_scor_loc2 := 0;
end if;
if v_scor_loc3 = 9999 then
v_scor_loc3 := 0;
end if;
v_text_returnat := v_text_returnat || '1. **' || v_username_loc1 || ' ' || v_scor_loc1 || '**' || chr(10);
v_text_returnat := v_text_returnat || '2. **' || v_username_loc2 || ' ' || v_scor_loc2 || '**' || chr(10);
v_text_returnat := v_text_returnat || '3. **' || v_username_loc3 || ' ' || v_scor_loc3 || '**';
return v_text_returnat;
end;
function checkIfUserExists(p_username varchar2) return integer is
v_exista integer := 0;
begin
select count(*) into v_exista from useri where USERNAME = p_username;
return v_exista;
end;
END discord_bot;
/
--=======================================================--
--- PACHET PENTRU GESTIONAREA DATELOR PRIMTIE DE LA JOC ---
CREATE OR REPLACE PACKAGE game_pa AS
procedure insertNewUser(p_username IN varchar2);
procedure insertUserScore(p_username IN varchar2, p_scor IN integer);
procedure insertNewLoggedUser(p_username IN varchar2);
function checkIfUserIsLogged(p_username IN varchar2) return integer;
END game_pa;
/
CREATE OR REPLACE PACKAGE BODY game_pa AS
procedure insertNewUser(p_username IN varchar2) is
begin
insert into useri values(1, p_username);
end;
procedure insertUserScore(p_username IN varchar2, p_scor IN integer) is
v_username_id useri.id%type;
begin
select id into v_username_id from useri where USERNAME = p_username;
insert into scoruri values (1, v_username_id, p_scor, sysdate);
end;
procedure insertNewLoggedUser(p_username IN varchar2) is
v_username_id useri.id%type;
begin
select id into v_username_id from useri where USERNAME = p_username;
insert into USERI_LOGGATI values (1, v_username_id);
end;
function checkIfUserIsLogged(p_username IN varchar2) return integer is
v_esteLogat integer := 0;
v_username_id useri.id%type;
begin
select id into v_username_id from useri where USERNAME = p_username;
select count(*) into v_esteLogat from USERI_LOGGATI where ID_USER = v_username_id;
return v_esteLogat;
end;
END game_pa;
/