-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.pl
More file actions
206 lines (163 loc) · 6.33 KB
/
database.pl
File metadata and controls
206 lines (163 loc) · 6.33 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CWT-Prolog Database API %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- module(database, [
attach_db/1,
login/2,
logout/2,
ping/2,
create_game/3,
join_game/4,
resign_game/4
]).
:- use_module(library(persistency)).
:- persistent
user(name:string).
:- persistent
game(game:string, host:string, limit:compound, layout:list(atom)).
:- persistent
player(name:string, game:string, pos:compound, status:atom).
%--------------------------------------------------------------------------------%
% Predicates below are to be considered thread-safe *only if marked as such*.
%% attach_db(+File:atom) is det.
attach_db(File) :-
db_attach(File, []).
%% login(+User:user, -Response:string) is det.
%
% A user has attempted to login via login USERNAME. The status of the login will
% be unified with the appropriate Response. (thread-safe)
login(user(User), Response) :-
with_mutex(user_db, add_user(User, Response)).
%% logout(+User:user, -Response:string) is det.
%
% A user has attempted to logout via logout USERNAME. The status of the logout
% will be unified with the appropriate Response. If the user already exists then
% response should be be success and User will be retracted from the database.
% (thread-safe)
logout(user(User), Response) :-
remove_user(User, Response).
%% ping(+User:user, -Response:string) is det.
%
% ping USERNAME
% This updates a user token in the database, useful for keeping a user logged in.
% (thread-safe)
ping(user(User), Response) :-
( current_user(User)
-> response(success, Response)
; response(failure, Response)
).
%% create_game(+Game:game, +Pos:position, -Response:string) is det.
%
% creategame USERNAME:POSITION:GAMENAME:PLAYERLIMIT:TEAMLAYOUT
% Create a game along with the host player for the game.
% Pos = The player army faction position in the game (the player order)
% Gamename = Title of the game
% Playerlimit = Total amount of players that can be in the game
% Teamlayout = Layout of the teams {"AB" would mean p1 is Team A and p2 is Team B.
% (thread-safe)
create_game(game(User, Title, Limit, Layout), Pos, Response) :-
add_game(User, Pos, Title, Limit, Layout, Response).
%% join_game(+User:user, +Pos:position, +Game:string,
%% -Response:string) is det.
%
% joingame USERNAME:POSITION:GAMENAME
% This allows a player to join an already created game.
% (thread-safe)
join_game(user(User), Pos, Game, Response) :-
join_user(User, Pos, Game, Response).
%% resign_game(+User:user, +Game:string, +Pos:position, -Response:string) is det.
%
% leavegame USERNAME:GAMENAME
% In an inactive game, it'll remove the player from the list of players.
% In an active game, it'll change a player to inactive, making him/her unable to
% take any turns.
% (thread-safe)
resign_game(user(User), Game, Pos, Response) :-
remove_player(User, Game, Pos, Response).
%% active_game(+Game:string) is semidet.
%
% Active game iff more than one team is present in the game.
active_game(Game) :-
game(Game, _, _, Layout),
findall(Team, active_game_player(Game, Layout, Team), [H|Teams]),
\+ maplist(call(=, H), Teams).
active_game_player(Game, Layout, Team) :-
player(_, Game, pos(Pos), active),
nth1(Pos, Layout, Team).
%--------------------------------------------------------------------------------%
% Reads
%--------------------------------------------------------------------------------%
%% current_user(+User:string) is semidet.
current_user(User) :-
user(User).
%% current_game(+Game:string) is semidet.
current_game(Game) :-
game(Game, _, _, _).
%--------------------------------------------------------------------------------%
% Writes
%--------------------------------------------------------------------------------%
%% add_user(+User:string, -Response:string) is det.
add_user(User, Response) :-
( current_user(User)
-> response(failure, Response)
; assert_user(User),
response(success, Response)
).
%% remove_user(+User:string, -Response:string) is det.
remove_user(User, Response) :-
( retract_user(User)
-> response(success, Response)
; response(failure, Response)
).
%% add_game(+User:string, +Pos:position, +Game:string, +Limit:limit,
%% +Layout:list(atom), -Response:string) is det.
add_game(User, Pos, Game, Limit, Layout, Response) :-
( current_user(User),
with_mutex(game_db, add_game_(User, Pos, Game, Limit, Layout))
-> response(success, Response)
; response(failure, Response)
).
add_game_(User, Pos, Game, limit(Limit), Layout) :-
\+ current_game(Game),
length(Layout, Limit),
assert_game(Game, User, limit(Limit), Layout),
assert_player(User, Game, Pos, active).
%% remove_player(+User:string, +Game:string, Pos:position, -Response:string) is det.
remove_player(User, Game, Pos, Response) :-
with_mutex(game_db, remove_player_(User, Game, Pos, Response)).
remove_player_(User, Game, Pos, Response) :-
( player(User, Game, Pos, active),
active_game(Game)
-> retract_player(User, Game, Pos, active),
assert_player(User, Game, Pos, inactive)
; retract_player(User, Game, Pos, active)
),
response(success, Response),
!.
remove_player_(_, _, _, Response) :-
response(failure, Response).
%% join_user(+User:string, +Pos:position, +Game:string, -Response:string) is det.
join_user(User, Pos, Game, Response) :-
( current_user(User),
with_mutex(game_db, join_user_(User, Pos, Game))
-> response(success, Response)
; response(failure, Response)
).
join_user_(User, pos(Slot), GameName) :-
% Game already exists
game(GameName, _, limit(Limit), _),
% Player isn't already a part of the game
\+ player(User, GameName, _, _),
% Grab all positions and the total number of active players
aggregate_all(bag(P)-count, player(_, GameName, P, active), Ps-Players),
\+ member(pos(Slot), Ps),
% Adding User or Pos shouldn't break the player limit
Slot =< Limit,
Players < Limit,
assert_player(User, GameName, pos(Slot), active).
%--------------------------------------------------------------------------------%
% Responses
%--------------------------------------------------------------------------------%
%% response(+Status:atom, -Response:string) is det.
response(success, "{ status:ok }").
response(failure, "{ status:fail }").