forked from DreamTeamBuilding/BomberCo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathihm.pl
113 lines (100 loc) · 3.39 KB
/
ihm.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
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
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/http_files)).
:- use_module(library(http/html_write)).
:- use_module(library(http/html_head)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_parameters)).
:- use_module(bomberCo).
:- multifile http:location/3.
:- dynamic http:location/3.
http:location(files, "/files", []).
:- http_handler(files(.), http_reply_from_files("assets", []), [prefix]).
:- http_handler(root(.), accueil, []).
:- http_handler(root(game), getInfoGame, []).
:- http_handler(root(starting), starting, []).
:- http_handler(root(playMove), playMove, []).
:- http_handler(root(playMoveJoueur), playMoveJoueur, []).
server(Port) :- http_server(http_dispatch, [port(Port)]).
stopServer(Port) :- http_stop_server(Port,[]).
accueil(_) :-
reply_html_page(
[title("BomberCo"),
link([rel="icon", type="img/ico",href="files/favicon.ico"]),
link([rel="stylesheet", type="text/css", href="files/theme.css"])],
[h1("Just look"),
div(id="conteneur","Partie non demarree"),
div(id="conteneur2",""),
script(src="files/jquery.js",""),
script(src="files/ihm_action.js","")]).
starting(Request) :-
http_parameters(Request, [
players(PlayersData,[]),
size(SizeData,[]),
iaPlayer1(IaPlayer1Data, []),
iaPlayer2(IaPlayer2Data, [])
]),
atom_number(PlayersData, Players),
atom_number(SizeData, Size),
atom_number(IaPlayer1Data, IaPlayer1),
atom_number(IaPlayer2Data, IaPlayer2),
lancerPartie(Players, Size, IaPlayer1, IaPlayer2),
reply_json_dict("{\"result\":1}").
playMove(_) :-
(
fin(0)
->
(jouer,reply_json_dict("{\"result\":1}"))
;
reply_json_dict("{\"result\":0}")
)
.
playMoveJoueur(Request) :-
http_parameters(Request, [
action(ActionData,[])
]),
atom_number(ActionData, Action),
(
fin(0)
->
(jouerVraiJoueur(Action),reply_json_dict("{\"result\":1}"))
;
reply_json_dict("{\"result\":0}")
)
.
getInfoGame(_):-
taillePlateau(TP),
nbJoueurs(NBJ),
fin(Fin),
tourActuel(TourActuel),
findall([Y,X],joueursSav(Y,X,-1),JoueursVivants),
findall([Y,X],joueursSav(Y,X,0),JoueursMorts),
findall([X,Y],bombes(X,Y),Bombes),
plateauSav(Plateau),
getStringFromDoubleList(JoueursVivants,StrVivants),
getStringFromDoubleList(JoueursMorts,StrMorts),
getStringFromDoubleList(Bombes,StrBombes),
getStringFromList(Plateau,StrPlateau),
StringTab = [
"{",
"\"taillePlateau\":",TP,
",\"nbJoueurs\":",NBJ,
",\"plateau\" : [",StrPlateau,"]",
",\"joueursVivants\" : [",StrVivants,"]",
",\"joueursMorts\" : [",StrMorts,"]",
",\"bombes\" : [",StrBombes,"]",
",\"fin\":",Fin,
",\"tourActuel\":",TourActuel,
"}"],
getStringFromConcat(StringTab, S),
reply_json_dict(S)
.
getStringFromConcat([],""):-!.
getStringFromConcat([X|Liste], String):-getStringFromConcat(Liste,StringPrec),atom_concat(X,StringPrec,String).
getStringFromList([],""):-!.
getStringFromList([X],S):-atom_concat(X,"",S).
getStringFromList([X|Liste],String):-getStringFromList(Liste,StringPrec),atom_concat(X,",",Virgule),atom_concat(Virgule,StringPrec,String).
getStringFromDoubleList([],""):-!.
getStringFromDoubleList([X],S):-getStringFromList(X,StringX),getStringFromConcat(["[",StringX,"]"],S).
getStringFromDoubleList([X|Liste],String):-getStringFromList(X,StringX),getStringFromDoubleList(Liste,StringPrec),getStringFromConcat(["[",StringX,"]",",",StringPrec], String).