Browse Source

Support du fichier de configuration (params.cfg)

Brendan Abolivier 9 years ago
parent
commit
eabe2f5a26
7 changed files with 55 additions and 31 deletions
  1. 7
    5
      web/api/model/class.connector.php
  2. 2
    1
      web/index.htm
  3. 8
    6
      web/js/game.js
  4. 14
    9
      web/js/multi.js
  5. 9
    10
      web/multi.php
  6. 10
    0
      web/multi/server/server.js
  7. 5
    0
      web/params.cfg

+ 7
- 5
web/api/model/class.connector.php View File

5
 	private $bdd;
5
 	private $bdd;
6
 
6
 
7
 	function __construct() {
7
 	function __construct() {
8
-		$host = "localhost";
9
-		$db = "burgerquizz";
10
-		$user = "alain";
11
-		$pass = "chabat";
8
+		$params = file_get_contents("../params.cfg");
9
+		preg_match_all('/db_(.+)\: (.+)/', $params, $matches);
10
+		$dbconnect = array();
11
+		for($i = 0; $i < sizeof($matches[0]); $i++) {
12
+			$dbconnect[$matches[1][$i]] = $matches[2][$i];
13
+		}
12
 
14
 
13
-		$this->bdd = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
15
+		$this->bdd = new PDO("mysql:host=".$dbconnect["host"].";dbname=".$dbconnect["dbname"], $dbconnect["user"], $dbconnect["pass"]);
14
 		$this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
16
 		$this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
15
 	}
17
 	}
16
 
18
 

+ 2
- 1
web/index.htm View File

16
   </head>
16
   </head>
17
   <body>
17
   <body>
18
     <div id="game"></div>
18
     <div id="game"></div>
19
-    <input type="submit" id="play" value="Play" /><br />
19
+    <input type="submit" id="play" value="Solo (un joueur)" /><br />
20
+    <form action="multi.php"><input type="submit" id="multi" value="Multi (deux joueurs)" /></form>
20
     <script type="text/javascript">
21
     <script type="text/javascript">
21
       $("#play").click(function() {
22
       $("#play").click(function() {
22
         play();
23
         play();

+ 8
- 6
web/js/game.js View File

114
 
114
 
115
 function play() {
115
 function play() {
116
   $("#play").remove();
116
   $("#play").remove();
117
+  $("#multi").remove();
117
   apiReq();
118
   apiReq();
118
   loadCat(id_cat);
119
   loadCat(id_cat);
119
 }
120
 }
144
 }
145
 }
145
 
146
 
146
 function scoreConfirm() {
147
 function scoreConfirm() {
148
+  console.log("qsfd");
147
   addScore($("#login").val(), score);
149
   addScore($("#login").val(), score);
148
   var message = json.message;
150
   var message = json.message;
149
   $("#registerScore").fadeOut();
151
   $("#registerScore").fadeOut();
162
         +"Essayez avec un autre pseudonyme : <input type=\"text\" id=\"login\" placeholder=\"Nom ou pseudonyme\" />"
164
         +"Essayez avec un autre pseudonyme : <input type=\"text\" id=\"login\" placeholder=\"Nom ou pseudonyme\" />"
163
         +"<input type=\"submit\" id=\"sendScore\" value=\"Valider\" /><br />"
165
         +"<input type=\"submit\" id=\"sendScore\" value=\"Valider\" /><br />"
164
         +"<a href=\"palmares.htm\">Voir les meilleurs scores</a>");
166
         +"<a href=\"palmares.htm\">Voir les meilleurs scores</a>");
167
+        $("#sendScore").on('click', scoreConfirm);
168
+        $("#login").on('keypress', function(event) {
169
+          if(event.which == 13) {
170
+            scoreConfirm();
171
+          }
172
+        });
165
       }, 400);
173
       }, 400);
166
-      $("#sendScore").on('click', scoreConfirm);
167
-      $("#login").on('keypress', function(event) {
168
-        if(event.which == 13) {
169
-          scoreConfirm();
170
-        }
171
-      });
172
     } else {
174
     } else {
173
       window.setTimeout(function() {
175
       window.setTimeout(function() {
174
         $("#registerScore").addClass("error");
176
         $("#registerScore").addClass("error");

web/multi/client/js/game.js → web/js/multi.js View File

11
 var disconnect = true;
11
 var disconnect = true;
12
 var scoreAdversaire = 0;
12
 var scoreAdversaire = 0;
13
 var pseudo ='';
13
 var pseudo ='';
14
+var reponseUser = -1, bonneReponse;
14
 
15
 
15
 function init() {
16
 function init() {
16
 
17
 
18
+    var hostname = $('script')[1]['src'].match(/http:\/\/(.+)\:/)[1];
19
+
17
     // Connexion à socket.io
20
     // Connexion à socket.io
18
-    socket = io.connect('http://172.17.7.66:8000');
21
+    socket = io.connect('http://'+hostname+':8000');
19
 
22
 
20
     // Gestion des evenements
23
     // Gestion des evenements
21
     setEventHandlers();
24
     setEventHandlers();
22
 
25
 
23
     // On demande le pseudo a l'utilisateur, on l'envoie au serveur et on l'affiche dans le titre
26
     // On demande le pseudo a l'utilisateur, on l'envoie au serveur et on l'affiche dans le titre
24
-    $("#game").html("<input type=\"text\" id=\"pseudo\" /><input type=\"submit\" id=\"start\" value=\"Valider\" />");
27
+    $("#game").html("<input type=\"text\" id=\"pseudo\" placeholder=\"Nom ou pseudonyme\" /><input type=\"submit\" id=\"start\" value=\"Valider\" />");
25
     $("#start").on("click", function() {
28
     $("#start").on("click", function() {
26
       pseudo = $("#pseudo").val();
29
       pseudo = $("#pseudo").val();
27
       socket.emit('nouveau', pseudo);
30
       socket.emit('nouveau', pseudo);
28
       document.title = $("#pseudo").val() + ' - ' + document.title;
31
       document.title = $("#pseudo").val() + ' - ' + document.title;
29
-      $("#game").html("Recherche d'un adversare...");
32
+      $("#game").html("Recherche d'un adversaire...");
30
     });
33
     });
31
     $("#pseudo").on('keypress', function(event) {
34
     $("#pseudo").on('keypress', function(event) {
32
       if(event.which == 13) {
35
       if(event.which == 13) {
33
         pseudo = $("#pseudo").val();
36
         pseudo = $("#pseudo").val();
34
         socket.emit('nouveau', pseudo);
37
         socket.emit('nouveau', pseudo);
35
         document.title = $("#pseudo").val() + ' - ' + document.title;
38
         document.title = $("#pseudo").val() + ' - ' + document.title;
36
-        $("#game").html("Recherche d'un adversare...");
39
+        $("#game").html("Recherche d'un adversaire...");
37
       }
40
       }
38
     });
41
     });
39
 };
42
 };
48
   socket.on("questions", play);
51
   socket.on("questions", play);
49
   socket.on("lolheded", endGame);
52
   socket.on("lolheded", endGame);
50
   socket.on("end", onEnd);
53
   socket.on("end", onEnd);
54
+  socket.on("qpass", function() {
55
+    reponseUser = -1;
56
+    checkAnswer();
57
+  })
51
 };
58
 };
52
 
59
 
53
 function onEnd(score) {
60
 function onEnd(score) {
85
 var baseWidth;
92
 var baseWidth;
86
 
93
 
87
 var score = 0;
94
 var score = 0;
88
-var reponseUser = -1, bonneReponse;
89
 var canClick = true;
95
 var canClick = true;
90
 
96
 
91
 function apiReq() {
97
 function apiReq() {
126
     +theme.questions[id].intitule);
132
     +theme.questions[id].intitule);
127
   if(canClick) {
133
   if(canClick) {
128
     $("#rep1").off('click');
134
     $("#rep1").off('click');
129
-    $("#rep1").one("click", function() { reponseUser = 1; checkAnswer(); });
135
+    $("#rep1").one("click", function() { reponseUser = 1; checkAnswer(); socket.emit('nextQuestion'); });
130
     $("#rep2").off('click');
136
     $("#rep2").off('click');
131
-    $("#rep2").one("click", function() { reponseUser = 2; checkAnswer(); });
137
+    $("#rep2").one("click", function() { reponseUser = 2; checkAnswer(); socket.emit('nextQuestion'); });
132
     $("#both").off('click');
138
     $("#both").off('click');
133
-    $("#both").one("click", function() { reponseUser = 0; checkAnswer(); });
139
+    $("#both").one("click", function() { reponseUser = 0; checkAnswer(); socket.emit('nextQuestion'); });
134
   }
140
   }
135
 }
141
 }
136
 
142
 
191
 }
197
 }
192
 
198
 
193
 function play(questions) {
199
 function play(questions) {
194
-  console.log(questions);
195
   json = questions;
200
   json = questions;
196
   loadCat(id_cat);
201
   loadCat(id_cat);
197
 }
202
 }

web/multi/client/index.html → web/multi.php View File

12
           }
12
           }
13
         </style>
13
         </style>
14
     </head>
14
     </head>
15
-
16
     <body>
15
     <body>
17
-        <h1>Burger temps réel !</h1>
18
-
19
         <div id="game">
16
         <div id="game">
20
           Connexion au serveur...
17
           Connexion au serveur...
21
         </div>
18
         </div>
22
-
23
         <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
19
         <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
24
-        <script src="http://172.17.7.66:8000/socket.io/socket.io.js"></script>
25
-        <script src="js/game.js"></script>
26
-        <script>
27
-            init();
28
-
29
-        </script>
20
+        <?php
21
+          $params = file_get_contents("./params.cfg");
22
+    		  preg_match_all('/node_host\: (.+)/', $params, $matches);
23
+          echo '<script src="http://'.$matches[1][0].':8000/socket.io/socket.io.js"></script>';
24
+         ?>
25
+         <script src="js/multi.js"></script>
26
+         <script>
27
+          init();
28
+          </script>
30
     </body>
29
     </body>
31
 </html>
30
 </html>

+ 10
- 0
web/multi/server/server.js View File

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

+ 5
- 0
web/params.cfg View File

1
+node_host: localhost
2
+db_host: localhost
3
+db_dbname: burgerquizz
4
+db_user: alain
5
+db_pass: chabat