Browse Source

Ajout compétitif et restructuration API

Brendan Abolivier 9 years ago
parent
commit
a560ce01f5
5 changed files with 52 additions and 43 deletions
  1. 37
    0
      web/api/controller/routes.php
  2. 3
    35
      web/api/index.php
  3. 1
    0
      web/js/game.js
  4. 8
    5
      web/js/multi.js
  5. 3
    3
      web/multi/server/server.js

+ 37
- 0
web/api/controller/routes.php View File

1
+<?php
2
+
3
+require_once('model/classes.php');
4
+
5
+try {
6
+	if(!empty($_GET['page'])) {
7
+		if($_GET['page'] == "palmares") {
8
+			if(include("controller/palmares.php")) {
9
+        $api = $scArray;
10
+				$api["status"] = 1;
11
+			} else {
12
+				$api = array(
13
+					"status" => 0,
14
+					"source" => "root",
15
+					"message" => "Erreur d'inclusion"
16
+				);
17
+			}
18
+		}
19
+	} else {
20
+		if(include("controller/questions.php")) {
21
+			$api = $categories;
22
+      $api["status"] = 1;
23
+		} else {
24
+			$api = array(
25
+				"status" => 0,
26
+				"source" => "root",
27
+				"message" => "Erreur d'inclusion"
28
+			);
29
+		}
30
+	}
31
+} catch(Exception $e) {
32
+	$api = array(
33
+		"status" => 0,
34
+		"source" => $e->getTrace()[0]["class"],
35
+		"message" => $e->getMessage()
36
+	);
37
+}

+ 3
- 35
web/api/index.php View File

1
 <?php
1
 <?php
2
 
2
 
3
-require_once('model/classes.php');
4
-
5
 header("Content-Type:application/json");
3
 header("Content-Type:application/json");
6
 
4
 
7
-try {
8
-	if(!empty($_GET['page'])) {
9
-		if($_GET['page'] == "palmares") {
10
-			if(include("controller/palmares.php")) {
11
-				$scArray["status"] = 1;
12
-				echo json_encode($scArray);
13
-			} else {
14
-				echo json_encode(array(
15
-					"status" => 0,
16
-					"source" => "root",
17
-					"message" => "Erreur d'inclusion"
18
-				));
19
-			}
20
-		}
21
-	} else {
22
-		if(include("controller/questions.php")) {
23
-			$categories["status"] = 1;
24
-			echo json_encode($categories);
25
-		} else {
26
-			echo json_encode(array(
27
-				"status" => 0,
28
-				"source" => "root",
29
-				"message" => "Erreur d'inclusion"
30
-			));
31
-		}
32
-	}
33
-} catch(Exception $e) {
34
-	echo json_encode(array(
35
-		"status" => 0,
36
-		"source" => $e->getTrace()[0]["class"],
37
-		"message" => $e->getMessage()
38
-	));
39
-}
5
+include("controller/routes.php");
6
+
7
+echo json_encode($api);

+ 1
- 0
web/js/game.js View File

13
 
13
 
14
 function play() {
14
 function play() {
15
   apiReq();
15
   apiReq();
16
+  console.log(json);
16
   $("#play").remove();
17
   $("#play").remove();
17
   if(json.status != 1) {
18
   if(json.status != 1) {
18
     var message;
19
     var message;

+ 8
- 5
web/js/multi.js View File

12
 var scoreAdversaire = 0;
12
 var scoreAdversaire = 0;
13
 var pseudo ='';
13
 var pseudo ='';
14
 var reponseUser = -1, bonneReponse;
14
 var reponseUser = -1, bonneReponse;
15
+var againstLoser = false;
15
 
16
 
16
 function init() {
17
 function init() {
17
 
18
 
55
   socket.on("questions", play);
56
   socket.on("questions", play);
56
   socket.on("lolheded", endGame);
57
   socket.on("lolheded", endGame);
57
   socket.on("end", onEnd);
58
   socket.on("end", onEnd);
58
-  socket.on("qpass", function() {
59
+  socket.on("qpass", function(gotAPoint) {
59
     reponseUser = -1;
60
     reponseUser = -1;
61
+    againstLoser = gotAPoint;
60
     checkAnswer();
62
     checkAnswer();
61
   })
63
   })
62
 };
64
 };
128
   $("#question-count").html("Question "+currentQuestion+"/"+nbQuestions);
130
   $("#question-count").html("Question "+currentQuestion+"/"+nbQuestions);
129
   if(canClick) {
131
   if(canClick) {
130
     $("#rep1").off('click');
132
     $("#rep1").off('click');
131
-    $("#rep1").one("click", function() { reponseUser = 1; checkAnswer(); socket.emit('nextQuestion'); });
133
+    $("#rep1").one("click", function() { reponseUser = 1; checkAnswer(); socket.emit('nextQuestion', reponseUser == bonneReponse); });
132
     $("#rep2").off('click');
134
     $("#rep2").off('click');
133
-    $("#rep2").one("click", function() { reponseUser = 2; checkAnswer(); socket.emit('nextQuestion'); });
135
+    $("#rep2").one("click", function() { reponseUser = 2; checkAnswer(); socket.emit('nextQuestion', reponseUser == bonneReponse); });
134
     $("#both").off('click');
136
     $("#both").off('click');
135
-    $("#both").one("click", function() { reponseUser = 0; checkAnswer(); socket.emit('nextQuestion'); });
137
+    $("#both").one("click", function() { reponseUser = 0; checkAnswer(); socket.emit('nextQuestion', reponseUser == bonneReponse); });
136
   }
138
   }
137
 }
139
 }
138
 
140
 
142
   $("#rep2").off('click');
144
   $("#rep2").off('click');
143
   $("#both").off('click');
145
   $("#both").off('click');
144
   stopTimer();
146
   stopTimer();
145
-  if(reponseUser == bonneReponse) {
147
+  if(reponseUser == bonneReponse || againstLoser) {
146
     score += secRestantes+1;
148
     score += secRestantes+1;
149
+    againstLoser = false;
147
   }
150
   }
148
   if(score > 1) {
151
   if(score > 1) {
149
     $("#score").html("Score : "+score+" miams");
152
     $("#score").html("Score : "+score+" miams");

+ 3
- 3
web/multi/server/server.js View File

90
       });
90
       });
91
     });
91
     });
92
 
92
 
93
-    client.on('nextQuestion', function() {
93
+    client.on('nextQuestion', function(isRight) {
94
       games.forEach(function(row) {
94
       games.forEach(function(row) {
95
         if(row.joueur1.socket.id === client.id) {
95
         if(row.joueur1.socket.id === client.id) {
96
-          row.joueur2.socket.emit('qpass');
96
+          row.joueur2.socket.emit('qpass', !isRight);
97
         } else if(row.joueur2.socket.id === client.id) {
97
         } else if(row.joueur2.socket.id === client.id) {
98
-          row.joueur1.socket.emit('qpass');
98
+          row.joueur1.socket.emit('qpass', !isRight);
99
         }
99
         }
100
       });
100
       });
101
     })
101
     })