generated from UnBParadigmas2021-2/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.pl
executable file
·76 lines (64 loc) · 2.19 KB
/
server.pl
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
#!/usr/bin/swipl
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_cors)).
% Serve the page
:- use_module(library(http/http_server)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_path)).
% URL handlers.
:- set_setting(http:cors, [*]).
:- http_handler(root(.), handle_request, [methods([post])]).
:- http_handler(root(start), handle_start_request, [methods([get])]).
:- http_handler(root(verify), handle_verify_request, [post]).
% Game and its assets
:- http_handler(root(game), http_reply_file('front/index.html', []), [prefix]).
:- http_handler('/css/style.css', http_reply_file('front/css/style.css', []), []).
:- http_handler('/js/script.js', http_reply_file('front/js/script.js', []), []).
verify(_{guess:Guess}, _{status:Status,data:Data}) :-
selected_word(Word),
consult('wordle'),
atom_string(Atom_Guess, Guess),
wordle(Atom_Guess, Word, Status, Colors),
guesses(Guesses),
append(Guesses, [_{guess:Guess,colors:Colors}], NewGuesses),
retractall(guesses(_)),
assertz(guesses(NewGuesses)),
guesses(Data).
handle_verify_request(Request) :-
cors_enable(Request,
[ methods([get,post,delete])
]),
http_read_json_dict(Request, Query),
verify(Query, Answer),
reply_json_dict(Answer).
start(Word) :-
consult('word_utils'),
get_random_word(Selected_Word),
retractall(guesses(_)),
assertz(guesses([])),
retractall(selected_word(_)),
assertz(selected_word(Selected_Word)),
selected_word(Word).
handle_start_request(Request) :-
cors_enable(Request,
[ methods([get,post,delete])
]),
start(Word),
reply_json_dict(Word).
% Calculates a + b.
solve(_{a:X, b:Y}, _{answer:N}) :-
number(X),
number(Y),
N is X + Y.
handle_request(Request) :-
cors_enable(Request,
[ methods([get,post,delete])
]),
http_read_json_dict(Request, Query),
solve(Query, Solution),
reply_json_dict(Solution).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
:- initialization(server(8000)).