Quellcode durchsuchen

Ajout compétitif et restructuration API

Brendan Abolivier vor 9 Jahren
Ursprung
Commit
a560ce01f5
5 geänderte Dateien mit 52 neuen und 43 gelöschten Zeilen
  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 Datei anzeigen

@@ -0,0 +1,37 @@
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 Datei anzeigen

@@ -1,39 +1,7 @@
1 1
 <?php
2 2
 
3
-require_once('model/classes.php');
4
-
5 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 Datei anzeigen

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

+ 8
- 5
web/js/multi.js Datei anzeigen

@@ -12,6 +12,7 @@ var disconnect = true;
12 12
 var scoreAdversaire = 0;
13 13
 var pseudo ='';
14 14
 var reponseUser = -1, bonneReponse;
15
+var againstLoser = false;
15 16
 
16 17
 function init() {
17 18
 
@@ -55,8 +56,9 @@ var setEventHandlers = function() {
55 56
   socket.on("questions", play);
56 57
   socket.on("lolheded", endGame);
57 58
   socket.on("end", onEnd);
58
-  socket.on("qpass", function() {
59
+  socket.on("qpass", function(gotAPoint) {
59 60
     reponseUser = -1;
61
+    againstLoser = gotAPoint;
60 62
     checkAnswer();
61 63
   })
62 64
 };
@@ -128,11 +130,11 @@ function quest(id) {
128 130
   $("#question-count").html("Question "+currentQuestion+"/"+nbQuestions);
129 131
   if(canClick) {
130 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 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 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,8 +144,9 @@ function checkAnswer() {
142 144
   $("#rep2").off('click');
143 145
   $("#both").off('click');
144 146
   stopTimer();
145
-  if(reponseUser == bonneReponse) {
147
+  if(reponseUser == bonneReponse || againstLoser) {
146 148
     score += secRestantes+1;
149
+    againstLoser = false;
147 150
   }
148 151
   if(score > 1) {
149 152
     $("#score").html("Score : "+score+" miams");

+ 3
- 3
web/multi/server/server.js Datei anzeigen

@@ -90,12 +90,12 @@ function onSocketConnection(client) {
90 90
       });
91 91
     });
92 92
 
93
-    client.on('nextQuestion', function() {
93
+    client.on('nextQuestion', function(isRight) {
94 94
       games.forEach(function(row) {
95 95
         if(row.joueur1.socket.id === client.id) {
96
-          row.joueur2.socket.emit('qpass');
96
+          row.joueur2.socket.emit('qpass', !isRight);
97 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
     })