-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.pl
72 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
% server(8000). pour lancer le server
:- use_module(library('http/thread_httpd')).
:- use_module(library('http/http_dispatch')).
:- use_module(library('http/http_files')).
:- use_module(library('http/http_parameters')).
:- use_module('game').
%lance le server
server(Port) :-
http_server(http_dispatch, [port(Port)]).
%créer un handler pour le http://localhost:port/init. La méthode init est appelé
:- http_handler(root(.), homePage, []).
:- http_handler(root(initPlane), initPlane, []).
:- http_handler(root(next), next, []).
:- http_handler(root(nextPlayer), nextPlayer, []).
:- http_handler(root(definePlayer), definePlayer, []).
%todo
homePage(Request) :-
http_reply_from_files('/Interface/avion.html', [], Request).
initPlane(Request) :-
game:reset,
plane:plane(1, X1, Y1, V1, D1),
plane:plane(2, X2, Y2, V2, D2),
format('Content-type: text/jsonp~n~n'),
format('updatePlane({avion1 : {x : ~w, y : ~w, v : ~w, d : "~w"},
avion2 : {x : ~w, y : ~w, v : ~w, d : "~w"}} ~n)',
[X1, Y1, V1, D1, X2, Y2, V2, D2] ).
next(Request) :-
game:stepHttp,
plane:plane(1, X1, Y1, V1, D1),
plane:plane(2, X2, Y2, V2, D2),
game:actionHttp(1, Action1),
game:actionHttp(2, Action2),
format('Content-type: text/jsonp~n~n'),
format('updatePlane({avion1 : {x : ~w, y : ~w, v : ~w, d : "~w"},
avion2 : {x : ~w, y : ~w, v : ~w, d : "~w"},
move1 : "~w",
move2 : "~w"} ~n)',
[X1, Y1, V1, D1, X2, Y2, V2, D2, Action1, Action2] ).
%TODO player humain
nextPlayer(Request) :-
http_parameters(Request,
[ act1(Act1, []),
act2(Act2, []),
act3(Act3, [])
]
),
game:stepHttpPlayer([Act1, Act2, Act3]),
plane:plane(1, X1, Y1, V1, D1),
plane:plane(2, X2, Y2, V2, D2),
game:actionHttp(1, Action1),
game:actionHttp(2, Action2),
format('Content-type: text/jsonp~n~n'),
format('updatePlane({avion1 : {x : ~w, y : ~w, v : ~w, d : "~w"},
avion2 : {x : ~w, y : ~w, v : ~w, d : "~w"},
move1 : "~w",
move2 : "~w"} ~n)',
[X1, Y1, V1, D1, X2, Y2, V2, D2, Action1, Action2] ).
definePlayer(Request) :-
http_parameters(Request,
[ player(Pl, [integer]),
value(Val, [integer])
]),
selectPlayer(Pl, Val),
format('Content-type: text/jsonp~n~n').