commit 3a8080b8e66417b2b20430ee0876d32674a9e5dd
parent b63efaeb63138ec0376a65fbfa56224249a03c86
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Fri, 20 May 2011 10:08:44 +0200
Merge branch 'unstable' of github:jsmaniac/2011-m1s2-ter into unstable
Diffstat:
3 files changed, 120 insertions(+), 120 deletions(-)
diff --git a/code/serveur/php/ressources/backend.inc b/code/serveur/php/ressources/backend.inc
@@ -1,8 +1,9 @@
<?php
-
require_once("ressources/relations.inc");
require_once("ressources/db.inc");
+
+
/* Les prototypes des fonctions :
* ===============================>
* checkLogin($user, $passwd);
@@ -25,10 +26,19 @@ require_once("ressources/db.inc");
* get_game_relations();
* getGameRelationsJSON();
* setGameGetScore($user, $pgid, $gid, $answers);
-* insertNode($node);
* getNodeEid($node);
+* wordExist($node);
+* setUserInfo($user, $key, $value);
+* userPrefsDefaults();
+* userPrefs($user);
+* setUserPref($user, $key, $value);
*/
+
+
+/* Les définitions
+====================*/
+
function longStrVal($str) {
return preg_match('/^-?[0-9]+$/', ''.$str) ? ''.$str : '0';
}
@@ -285,10 +295,10 @@ function decodeAndInsertGame($user,$game) {
if(count($badWords) > 0) {
echo JSON_encode($badWords);
} else if (count($cloud) < 5) {
- echo "false";
+ echo JSON_encode(false);
} else {
insertCreatedGame($centerEid,$cloud,$r1,$r2,10,$user);
- echo "true";
+ echo JSON_encode(true);
}
}
@@ -535,6 +545,7 @@ function normalizeProbas($row) {
function setGame($user, $pgid, $gid, $answers)
{
$db = getDB();
+
if ('ok' !== $db->querySingle("SELECT 'ok' FROM played_game WHERE pgid = ".longStrVal($pgid)." and gid = ".intval($gid)." and login = '".SQLite3::escapeString($user)."' and timestamp = -1;")) {
return getGameScores($user, $pgid, $gid);
}
@@ -615,23 +626,9 @@ function getGameScores($user, $pgid, $gid) {
}
/** Fourni l'ensembles des relations pouvant apparaître dans le jeu.
-* @return array : un tableau de realtions.
+* @return JSON type et nom des relations.
*/
-function get_game_relations()
-{
- $reqlations = array();
-
- $relations[] = 5;
- $relations[] = 7;
- $relations[] = 9;
- $relations[] = 10;
- $relations[] = 13;
- $relations[] = 22;
-
- return $relations;
-}
-
-function getGameRelationsJSON() {
+function getGameRelations() {
$json = "{";
global $stringRelations;
@@ -655,20 +652,6 @@ function setGameGetScore($user, $pgid, $gid, $answers) {
echo json_encode($scores);
}
-/** Insère dans la base de données le noeud si il n'existe pas encore.
-* @param node : le noeud à ajouter.
-*/
-function insertNode($node) {
- $db = getDB();
-
- if($db->querySingle("SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."'") == null) {
- $db->exec("INSERT INTO node(name,type,weight) VALUES('".SQLite3::escapeString($node)."',1,50);");
- return true;
- }
-
- return false;
-}
-
/** retourne l'eid d'un mot.
* @param node : le mot dont on veut obtenir l'eid.
@@ -679,6 +662,10 @@ function getNodeEid($node) {
return $db->querySingle("SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."';");
}
+/* Permet de déterminer si le mot existe dans la base de données ou non.
+ * @param node : Le mot dont on veut connaître l'existance.
+ * @return char : "0" ou "1".
+*/
function wordExist($node) {
$db = getDB();
@@ -716,4 +703,4 @@ function setUserPref($user, $key, $value) {
}
}
-?>
-\ No newline at end of file
+?>
diff --git a/code/serveur/php/ressources/createGame.js b/code/serveur/php/ressources/createGame.js
@@ -109,7 +109,7 @@ $(function() {
url: "server.php?",
data: "action=4&word="+word, //+"&user="+user+"&passwd="+passwd,
success: function(msg){
- input.closest(".wordLine, #center").addClass(msg == "false" ? "invalid" : "valid");
+ input.closest(".wordLine, #center").addClass(msg == false ? "invalid" : "valid");
wordsOK[input.attr("id")] = !(msg == false);
}});
}
@@ -118,20 +118,20 @@ $(function() {
var formOK = function() {
displayError("");
- if ($("#relation1").val() == $("#relation2").val())
- displayError("Les deux relation doivent être différents");
- else if ($("#centralWord").val() == "")
- displayError("Le mot central doit être renseigné.");
+ if ($("#relation1").val() == $("#relation2").val())
+ displayError("Les deux relation doivent être différents");
+ else if ($("#centralWord").val() == "")
+ displayError("Le mot central doit être renseigné.");
else if (badWord())
displayError("Il existe des mots incorrects");
- else if (nbWordOK() < nbWordMin)
- displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
- else if (!relationsOK())
- displayError("Tout les mots ne sont pas liés à une relation");
- else
- sendGame();
+ else if (nbWordOK() < nbWordMin)
+ displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
+ else if (!relationsOK())
+ displayError("Tout les mots ne sont pas liés à une relation");
+ else
+ sendGame();
- return false;
+ return false;
};
var nbWordOK = function() {
@@ -150,64 +150,70 @@ $(function() {
return true;
return false;
- }
+ }
- var relationsOK = function() {
- for(i = 1; i < numWord; i++) {
- if(wordsOK["word-"+i]) {
- if(!$("#r1-"+i).is(":checked") && !$("#r2-"+i).is(":checked") && !$("#r3-"+i).is(":checked") && !$("#r4-"+i).is(":checked"))
- return false;
- }
- }
-
- return true;
- }
+ var relationsOK = function() {
+ for(i = 1; i < numWord; i++) {
+ if(wordsOK["word-"+i]) {
+ if(!$("#r1-"+i).is(":checked") && !$("#r2-"+i).is(":checked") && !$("#r3-"+i).is(":checked") && !$("#r4-"+i).is(":checked"))
+ return false;
+ }
+ }
+
+ return true;
+ }
- var sendGame = function() {
- var exit;
+ var sendGame = function() {
+ var exit;
var cloud = "";
-
- exit = {center:$("#centralWord").val(),
- relations:[$("#relation1").val(),$("#relation2").val(),0,-1],
- cloud:[]};
-
- for(i=1;i<numWord;i++) {
- exit.cloud.push({
- name:$("#word-"+i).val(),
- relations:[
- $("#r1-"+i).is(":checked") ? "1":"0",
- $("#r2-"+i).is(":checked") ? "1":"0",
- $("#r3-"+i).is(":checked") ? "1":"0",
- $("#r4-"+i).is(":checked") ? "1":"0"
- ]
- });
- }
-
- $.get("server.php",{action:"6",game:exit},function (data) {
- //$(".word").closest(".wordLine, #center").removeClass("valid invalid");
- if(data == "true") {
- displaySuccess("La partie à bien été enregistrée");
- $('#newCreationLink').show();
- $('#center').hide();
- $('#relations').hide();
- $('#wordLines').hide();
- $('#button').hide();
- } else if (data == "false") {
- displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
- } else if (data != "true") {
- $('input').removeAttr('disabled');
- var that = $(this);
- $.each(data,function(i,e) {
- $('.word')
- .filter(function() { return that.val() == e; })
- .closest(".wordLine, #center")
- .removeClass("valid invalid")
- .addClass("invalid");
+
+ exit = {center:$("#centralWord").val(),
+ relations:[$("#relation1").val(),$("#relation2").val(),0,-1],
+ cloud:[]};
+
+ for(i=1;i<numWord;i++) {
+ exit.cloud.push({
+ name:$("#word-"+i).val(),
+ relations:[
+ $("#r1-"+i).is(":checked") ? "1":"0",
+ $("#r2-"+i).is(":checked") ? "1":"0",
+ $("#r3-"+i).is(":checked") ? "1":"0",
+ $("#r4-"+i).is(":checked") ? "1":"0"
+ ]
});
- }
- });
- $('input').attr('disabled', 'disabled');
- }
+ }
+
+ $.get("server.php",{action:"6",game:exit},function (data) {
+ //$(".word").closest(".wordLine, #center").removeClass("valid invalid");
+ if(data == true) {
+ displaySuccess("La partie à bien été enregistrée");
+ $('#newCreationLink').show();
+ $('#center').hide();
+ $('#relations').hide();
+ $('#wordLines').hide();
+ $('#button').hide();
+ }
+ else if (data == false) {
+ $('input').removeAttr('disabled');
+ displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
+ }
+ else if (data != true) {
+ $('input').removeAttr('disabled');
+ var that = $(this);
+
+
+ $.each(data,function(i,e) {
+ $('.word')
+ .filter(function() { return that.val() == e; })
+ .closest(".wordLine, #center")
+ .removeClass("valid invalid")
+ .addClass("invalid");
+ });
+ }
+ });
+
+ $('input').attr('disabled', 'disabled');
+ }
var displayError = function(message) {
if (message != "")
diff --git a/code/serveur/php/server.php b/code/serveur/php/server.php
@@ -1,6 +1,5 @@
<?php
session_start();
-
require_once("ressources/backend.inc");
require_once("ressources/db.inc");
@@ -17,6 +16,7 @@ function logError($errNum, $msg, $other="")
$dumpParameters = str_replace("(\n","",print_r($_GET,true));
$dumpParameters = str_replace(")\n","",$dumpParameters);
+ // Ecriture dans le fichier.
fwrite($file,"\nErreur n° ".$errNum);
fwrite($file," : ".$msg);
if(!empty($other))
@@ -52,9 +52,9 @@ function main()
$action = $_GET['action'];
- if ($action != 3 && (!$loginIsOk)) {
+ if ($action != 3 && (!$loginIsOk))
throw new Exception("Vous n'êtes pas connecté.", 10);
- }
+
if ($action == 3) {
echo json_encode(
Array(
@@ -67,9 +67,9 @@ function main()
if ($action == 2) { // "Create partie"
// Requête POST : http://serveur/server.php?action=2&nb=2&mode=normal&user=foo&passwd=bar
- if (!isset($_GET['nb']) || !isset($_GET['mode'])) {
+ if (!isset($_GET['nb']) || !isset($_GET['mode']))
throw new Exception("La requête est incomplète", 2);
- }
+
createGame(intval($_GET['nb']), $_GET['mode']);
echo '{"success":1}';
}
@@ -77,37 +77,42 @@ function main()
// Requête POST : http://serveur/server.php?action=0&user=foo&passwd=bar
echo game2json($user, isset($_GET['pgid']) ? $_GET['pgid'] : null);
}
- else if($action == 1) { // "Set partie"
+ else if($action == 1) { // "Set partie"
// Requête POST : http://serveur/server.php?action=1&mode=normal&user=foo&passwd=bar&gid=1234&pgid=12357&0=0&1=-1&2=22&3=13&9=-1
- if (!isset($_GET['pgid']) || !isset($_GET['gid']) || !isset($_GET['answers'])) {
+ if (!isset($_GET['pgid']) || !isset($_GET['gid']) || !isset($_GET['answers']))
throw new Exception("La requête est incomplète", 2);
- }
+
setGameGetScore($user, $_GET['pgid'], $_GET['gid'], $_GET['answers']);
- } else if($action == 4) { // CheckWord
+ }
+ else if($action == 4) { // CheckWord
if (!isset($_GET['word']))
throw new Exception("La requête est incomplète", 2);
if(wordExist($_GET['word']))
- echo "true";
+ echo JSON_encode(true);
else
- echo "false";
+ echo JSON_encode(false);
}
- else if($action == 5) { // Get relations (JSON)
- echo getGameRelationsJSON();
+ else if($action == 5) { // Get relations (JSON)
+ echo getGameRelations();
}
else if($action == 6) {
if (!isset($_GET['game']))
throw new Exception("La requête est incomplète", 2);
decodeAndInsertGame($user,$_GET['game']);
- } elseif ($action == 7) { // Get user prefs
+ }
+ elseif ($action == 7) { // Get user prefs
userPrefs($user);
- } elseif ($action == 8) { // Set user pref
+ }
+ elseif ($action == 8) { // Set user pref
if (!isset($_GET['key']) || !isset($_GET['value']))
throw new Exception("La requête est incomplète", 2);
+
setUserPref($user, $_GET['key'], $_GET['value']);
userPrefs($user);
- } else {
+ }
+ else {
throw new Exception("Commande inconnue", 2);
}
}
@@ -119,7 +124,9 @@ function server() {
} else {
header("Content-Type: application/json; charset=utf-8");
}
+
ob_start();
+
try {
main();
ob_end_flush();
@@ -132,13 +139,15 @@ function server() {
"isError" => true
)
);
+
logError($e->getCode(), $e->getMessage(), date("c"));
closeDB();
}
+
if(isset($_GET['callback']))
echo ')';
}
server();
-?>
-\ No newline at end of file
+?>