From 1327ccfcb6868b1dd7da50668dacb01919e66c04 Mon Sep 17 00:00:00 2001 From: Bertrand BRUN Date: Wed, 6 Apr 2011 11:55:33 +0200 Subject: [PATCH] Refactoring du SQL et des Exception --- code/serveur/php/pticlic.php | 82 +++++++---------- code/serveur/php/ressources/errors.inc | 39 ++++++++ code/serveur/php/ressources/sql.inc | 119 +++++++++++++++++++++++++ code/serveur/php/server.php | 12 +-- 4 files changed, 197 insertions(+), 55 deletions(-) create mode 100644 code/serveur/php/ressources/errors.inc create mode 100644 code/serveur/php/ressources/sql.inc diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php index 5567ec0..5fcafea 100644 --- a/code/serveur/php/pticlic.php +++ b/code/serveur/php/pticlic.php @@ -1,6 +1,7 @@ @@ -38,7 +39,7 @@ require_once("db.php"); function checkLogin($user, $passwd) { $db = getDB(); $hashPasswd = md5($passwd); - $loginIsOk = ($hashPasswd == $db->querySingle("SELECT hash_passwd FROM user WHERE login='".$user."';")); + $loginIsOk = ($hashPasswd == $db->querySingle(sqlGetPassword($user))); return $loginIsOk; } @@ -48,7 +49,7 @@ function checkLogin($user, $passwd) { function randomCenterNode() { $db = getDB(); - return $db->querySingle("select eid from random_center_node where rowid = (abs(random()) % (select max(rowid) from random_center_node))+1;"); + return $db->querySingle(sqlGetEIDCenterNode()); } /** Selectionne aléatoirement un noeud d'un nuage. @@ -57,7 +58,7 @@ function randomCenterNode() function randomCloudNode() { $db = getDB(); - return $db->querySingle("select eid from random_cloud_node where rowid = (abs(random()) % (select max(rowid) from random_cloud_node))+1;"); + return $db->querySingle(sqlGetEIRCloudNode()); } /** @@ -73,32 +74,16 @@ function cgBuildResultSets($cloudSize, $centerEid, $r1, $r2) // Le select doit ranvoyer trois colonnes : // eid => l'eid du mot à mettre dans le nuage, // r1 => la probabilité pour que le mot soit dans r1, entre -1 et 1 (négatif = ne devrait pas y être, positif = devrait y être à coup sûr, 0 = on sait pas). - $typer1r2 = "type in ($r1, $r2)"; - $banned_types = "4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001"; $sources = array( - // Voisins 1 saut du bon type (= relations déjà existantes) - array('w'=>40, 'd'=>1, 's'=>"select end as eid, type = $r1 as r1, type = $r2 as r2, 0 as r0, 0 as trash from relation where start = $centerEid and $typer1r2 order by random();"), - // Voisins 1 saut via r_associated (0), donc qu'on voudrait spécifier si possible. - array('w'=>40, 'd'=>2, 's'=>"select end as eid, 0.25 as r1, 0.25 as r2, 0.5 as r0, 0 as trash from relation where start = $centerEid and type = 0 order by random();"), - // Voisins 1 saut via les autres relations - array('w'=>20, 'd'=>3.1, 's'=>"select end as eid, 0.1 as r1, 0.1 as r2, 0.8 as r0, 0 as trash from relation where start = $centerEid and type not in (0, $r1, $r2, $banned_types) order by random();"), - // Voisins 2 sauts, avec un mix de R1 et R2 pour les liens. Par ex [ A -R1-> B -R2-> C ] ou bien [ A -R2-> B -R2-> C ] - // Version optimisée de : "select end as eid from relation where $typer1r2 and start in oneHopWithType order by random();" - array('w'=>30, 'd'=>3.2, 's'=>"select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3. as r1, ((A.type = $r2) + (B.type = $r2)) / 3. as r2, 1/6. as r0, 1/6. as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.$typer1r2 order by random();"), - // Voisins 1 saut r1/r2 + 1 saut synonyme - // Version optimisée de : "select end as eid from relation where start in oneHopWithType and type = 5 order by random()"; - array('w'=>20, 'd'=>5, 's'=>"select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.type = 5 order by random();"), - // Version optimisée de : "select end as eid from relation where start in (select end from relation where start = $centerEid and type = 5) and $typer1r2 order by random();" - array('w'=>20, 'd'=>6, 's'=>"select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.type = 5 and B.start = A.end and B.$typer1r2 order by random();"), - // Voisins 2 sauts (tous) - // Version optimisée de : "select end as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from relation where start in (select end from relation where start = $centerEid and type not in ($banned_types)) and type not in ($banned_types) order by random();" - array('w'=>10, 'd'=>8, 's'=>"select x as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from (select x from (select X.eid + Y.dumb as x from (select B.end as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.start = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"), - // Centre pointe vers X, M pointe vers X aussi, on prend M. - // Version optimisée de : "select start as eid from relation where end in (select end from relation where start = $centerEid) and type not in ($banned_types) order by random();" - // Ce n'est toujours pas ça… : "select eid from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit 1) order by random();" - // Tordu, mais ça marche \o/ . En fait il faut empêcher l'optimiseur de ramener le random avant le limit (et l'optimiseur est malin… :) - array('w'=>10, 'd'=>8, 's'=>"select x as eid, 0.1 as r1, 0.1 as r2, 0.2 as r0, 0.6 as trash from (select x from (select X.eid + Y.dumb as x from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"), + array('w'=>40, 'd'=>1, 's'=>sql1JumpGoodType($r1, $r2, $centerEid)), + array('w'=>40, 'd'=>2, 's'=>sql1JumpViaRAssociated0($centerEid)), + array('w'=>20, 'd'=>3.1, 's'=>sql1JumpViaOtherRelation($centerEid, $r1, $r2, $banned_types)), + array('w'=>30, 'd'=>3.2, 's'=>sql2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid)), + array('w'=>20, 'd'=>5, 's'=>sql1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid)), + array('w'=>20, 'd'=>6, 's'=>sql1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid)), + array('w'=>10, 'd'=>8, 's'=>sql2JumpAll($centerEid, $cloudSize)), + array('w'=>10, 'd'=>8, 's'=>sqlXPointsToMMPointsToXTakeM($cloudSize)), 'rand' => array('w'=>5, 'd'=>10, 's'=>false) // random. Le r1 et r2 de random sont juste en-dessous ); @@ -214,7 +199,7 @@ function cgBuildCloud($centerEid, $cloudSize, $sources, $sumWeights) $rejected = false; // Ne pas mettre le mot central dans le nuage. if ($res['eid'] == $centerEid) { continue; } - $nodeName = $db->querySingle("select name from node where eid=".$res['eid'].";"); + $nodeName = $db->querySingle(sqlGetNameFromNode($res)); if (substr($nodeName, 0, 2) == "::") { continue; } foreach ($cloud as $c) { if ($c['eid'] == $res['eid']) { @@ -300,7 +285,7 @@ function cgInsert($centerEid, $cloud, $r1, $r2, $totalDifficulty) */ function randomGameCore() { $db = getDB(); - return $db->querySingle("select gid from game where gid = (abs(random()) % (select max(gid) from game))+1 or gid = (select max(gid) from game where gid > 0) order by gid limit 1;"); + return $db->querySingle(sqlGetGidFromGame()); } /** Sélection aléatoire d'une partie de la base de données parmis les parties à jouer. @@ -318,7 +303,7 @@ function randomGame() $gid = randomGameCore(); if ($gid === null) - throw new Exception("Erreur lors de la récupération de la partie. Vérifiez qu'il y a au moins une partie.", 6); + errGetGame(); } return $gid; } @@ -335,11 +320,11 @@ function formatWord($word) { while (($pos = strpos($word, ">")) !== false) { $res .= substr($word,0,$pos) . " ("; $eid = intval(substr($word,$pos+1)); - if ($eid == 0) { throw new Exception("Erreur lors du suivi des pointeurs de spécialisation du mot $word.", 7); } - if (in_array($eid, $stack)) { throw new Exception("Boucle rencontrée lors du suivi des pointeurs de spécialisation du mot $word.", 8); } - if (count($stack) > 10) { throw new Exception("Trop de niveaux de récursions lors du suivi des pointeurs de spécialisation du mot $word.", 9); } + if ($eid == 0) { errFollowingPointer($word); } + if (in_array($eid, $stack)) { errLoopDetected($word); } + if (count($stack) > 10) { errTooMuchRecursion($word); } $stack[] = $eid; - $word = $db->querySingle("select name from node where eid = $eid"); + $word = $db->querySingle(sqlGetNameFromNodeWithEid($eid)); } $res .= $word; @@ -362,8 +347,7 @@ function game2json($user, $gameId) $db->exec("INSERT INTO played_game(pgid, gid, login, timestamp) VALUES (null, ".$gameId.", '$user', -1);"); $pgid = $db->lastInsertRowID(); - // TODO Yoann : faire des tests d'erreur pour ces select ? - $game = $db->query("select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";"); + $game = $db->query(sqlGetGamesForId($gameId)); $game = $game->fetchArray(); $retstr = ""; @@ -371,7 +355,7 @@ function game2json($user, $gameId) $retstr .= '"center":{"id":'.$game['eid_central_word'].',"name":'.json_encode(''.formatWord($game['name_central_word'])).'},'; $retstr .= '"cloudsize":10,"cloud":['; // TODO ! compter dynamiquement. - $res = $db->query("select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = ".$gameId.";"); + $res = $db->query(sqlGetWordEidAndName($gameId)); $notfirst = false; while ($x = $res->fetchArray()) @@ -401,7 +385,7 @@ function game2array($user, $gameId) $pgid = $db->lastInsertRowID(); // TODO Yoann : faire des tests d'erreur pour ces select ? - $game = $db->query("select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";"); + $game = $db->query(sqlGetGamesForId($gameId)); $game = $game->fetchArray(); $ret = array(); @@ -414,7 +398,7 @@ function game2array($user, $gameId) $ret['center'] = array('id' => $game['eid_central_word'], 'name' => formatWord($game['name_central_word'])); $ret['cloud'] = array(); // TODO ! compter dynamiquement. - $res = $db->query("select eid_word,(select name from node where eid=eid_word) as name_word, num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash from game_cloud where gid = ".$gameId.";"); + $res = $db->query(sqlGetInformationAboutGame($gameId)); while ($x = $res->fetchArray()) { @@ -531,11 +515,11 @@ function normalizeProbas($row) { function setGame($user, $pgid, $gid, $answers) { $db = getDB(); - if ('ok' !== $db->querySingle("SELECT 'ok' FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user' and timestamp = -1;")) { + if ('ok' !== $db->querySingle(sqlGameIsOK($pgid, $gid, $user))) { return getGameScores($user, $pgid, $gid); } - $userReputation = computeUserReputation($db->querySingle("SELECT score FROM user WHERE login='".$user."';")); + $userReputation = computeUserReputation($db->querySingle(sqlGetScoreForUser($user))); $db->exec("begin transaction;"); $db->exec("update played_game set timestamp = ".time()." where pgid = $pgid;"); @@ -555,7 +539,7 @@ function setGame($user, $pgid, $gid, $answers) $num = intval($row['num']); $nbScores++; if (!isset($answers[$num])) { - throw new Exception("Cette requête \"Set partie\" ne donne pas de réponse (une relation) pour le mot numéro $num de la partie.", 5); + errSetPartiNoRelation($num); } $relanswer = intval($answers[$num]); @@ -565,7 +549,7 @@ function setGame($user, $pgid, $gid, $answers) case $r2: $answer = 1; $probaRx = "probaR2"; break; case $r0: $answer = 2; $probaRx = "probaR0"; break; case $trash: $answer = 3; $probaRx = "probaTrash"; break; - default: throw new Exception("Réponse ($relanswer) invalide pour le mot $num. Les réponses possibles sont : $r1, $r2, $r0, $trash", 5); + default: errAnswerInvalidForWord($r1, $r2, $r0, $trash); } $wordScore = computeScore(normalizeProbas($row), $row['difficulty'], $answer, $userReputation); @@ -587,17 +571,17 @@ function setGame($user, $pgid, $gid, $answers) function getGameScores($user, $pgid, $gid) { $db = getDB(); - $timestamp = $db->querySingle("SELECT timestamp FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user';"); + $timestamp = $db->querySingle(sqlGetPlayedGameTime($pgid, $gid, $user)); if ($timestamp == -1) { - throw new Exception("Cette partie n'a jamais été jouée.", 4); // TODO : code d'erreur en doublon avec celui ci-dessous. + errGameNeverPlayed(); } else if ($timestamp == null) { - throw new Exception("Cette partie n'est associée à votre nom d'utilisateur.", 4); + errGameNotAssociatedWithUser(); } $gameScore = 0; $scores = array(); $nbScores = 0; - $res = $db->query("SELECT num,score from played_game_cloud where pgid = $pgid and gid = $gid;"); + $res = $db->query(sqlGetNumAndScoreFromGame($pgid, $gid)); while ($row = $res->fetchArray()) { $nbScores++; @@ -648,7 +632,7 @@ function setGameGetScore($user, $pgid, $gid, $answers) { function insertNode($node) { $db = getDB(); - if($db->querySingle("SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."'") == null) { + if($db->querySingle(sqlGetEidFromNode($node)) == null) { $db->exec("INSERT INTO node(name,type,weight) VALUES('".SQLite3::escapeString($node)."',1,50);"); return true; } @@ -664,6 +648,6 @@ function insertNode($node) { function getNodeEid($node) { $db = getDB(); - return $db->querySingle("SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."';"); + return $db->querySingle(sqlGetEidFromNode($node)); } ?> diff --git a/code/serveur/php/ressources/errors.inc b/code/serveur/php/ressources/errors.inc new file mode 100644 index 0000000..90715b6 --- /dev/null +++ b/code/serveur/php/ressources/errors.inc @@ -0,0 +1,39 @@ +function errRequestIncomplete() { + throw new Exception("La requête est incomplète", 1); +} + +function errUserUnknownOrBadPassword() { + throw new Exception("Utilisateur non enregistré ou mauvais mot de passe", 2); +} + +function errGameNeverPlayed() { + throw new Exception("Cette partie n'a jamais été jouée.", 3); // TODO : code d'erreur en doublon avec celui ci-dessous. +} + +function errGameNotAssociatedWithUser() { + throw new Exception("Cette partie n'est pas associée à votre nom d'utilisateur.", 4); +} + +function errAnswerInvalidForWord($r1, $r2, $r0, $trash) { + throw new Exception("Réponse ($relanswer) invalide pour le mot $num. Les réponses possibles sont : $r1, $r2, $r0, $trash", 5); +} + +function errSetPartiNoRelation($num) { + throw new Exception("Cette requête \"Set partie\" ne donne pas de réponse (une relation) pour le mot numéro $num de la partie.", 6); +} + +function errGetGame() { + throw new Exception("Erreur lors de la récupération de la partie. Vérifiez qu'il y a au moins une partie.", 7); +} + +function errFollowingPointer($word) { + throw new Exception("Erreur lors du suivi des pointeurs de spécialisation du mot $word.", 8); } +} + +function errLoopDetected($word) { + throw new Exception("Boucle rencontrée lors du suivi des pointeurs de spécialisation du mot $word.", 9); } +} + +function errTooMuchRecursion($word) { + throw new Exception("Trop de niveaux de récursions lors du suivi des pointeurs de spécialisation du mot $word.", 10); } +} diff --git a/code/serveur/php/ressources/sql.inc b/code/serveur/php/ressources/sql.inc new file mode 100644 index 0000000..f93a00c --- /dev/null +++ b/code/serveur/php/ressources/sql.inc @@ -0,0 +1,119 @@ +$typer1r2 = "type in ($r1, $r2)"; +$banned_types = "4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001"; + +function sqlGetPassword($user) { + return "SELECT hash_passwd FROM user WHERE login='".$user."';"; +} + +function sqlGetEIDCenterNode() { + return "select eid from random_center_node where rowid = (abs(random()) % (select max(rowid) from random_center_node))+1;"; +} + +function sqlGetEIRCloudNode() { + return "select eid from random_cloud_node where rowid = (abs(random()) % (select max(rowid) from random_cloud_node))+1;"; +} + +// Voisins 1 saut du bon type (= relations déjà existantes) +function sql1JumpGoodType($r1, $r2, $centerEid) { + global $typer1r2; + + return "select end as eid, type = $r1 as r1, type = $r2 as r2, 0 as r0, 0 as trash from relation where start = $centerEid and $typer1r2 order by random();"; +} + +// Voisins 1 saut via r_associated (0), donc qu'on voudrait spécifier si possible. +function sql1JumpViaRAssociated0($centerEid) { + return "select end as eid, 0.25 as r1, 0.25 as r2, 0.5 as r0, 0 as trash from relation where start = $centerEid and type = 0 order by random();"; +} + +// Voisins 1 saut via les autres relations +function sql1JumpViaOtherRelation($centerEid, $r1, $r2) { + global $banned_types; + + return "select end as eid, 0.1 as r1, 0.1 as r2, 0.8 as r0, 0 as trash from relation where start = $centerEid and type not in (0, $r1, $r2, $banned_types) order by random();"; +} + +// Voisins 2 sauts, avec un mix de R1 et R2 pour les liens. Par ex [ A -R1-> B -R2-> C ] ou bien [ A -R2-> B -R2-> C ] +// Version optimisée de : "select end as eid from relation where $typer1r2 and start in oneHopWithType order by random();" +function sql2JumpWithMixR1R2ForLinks($r1, $r2, $centerEid) { + global $typer1r2; + + return "select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3. as r1, ((A.type = $r2) + (B.type = $r2)) / 3. as r2, 1/6. as r0, 1/6. as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.$typer1r2 order by random();"; +} + +// Voisins 1 saut r1/r2 + 1 saut synonyme +// Version optimisée de : "select end as eid from relation where start in oneHopWithType and type = 5 order by random()"; +function sql1JumpR1DivR2Plus1JumpSynonymOneHopWithType($r1, $r2, $centerEid) { + global $typer1r2; + + return "select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.$typer1r2 and B.start = A.end and B.type = 5 order by random();"; +} + +// Version optimisée de : "select end as eid from relation where start in (select end from relation where start = $centerEid and type = 5) and $typer1r2 order by random();" +function sql1JumpR1DivR2Plus1JumpSynonym($r1, $r2, $centerEid) { + global $typer1r2; + + return "select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2, 0.25 as r0, 0 as trash from relation as A, relation as B where A.start = $centerEid and A.type = 5 and B.start = A.end and B.$typer1r2 order by random();"; +} + +// Voisins 2 sauts (tous) +// Version optimisée de : "select end as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from relation where start in (select end from relation where start = $centerEid and type not in ($banned_types)) and type not in ($banned_types) order by random();" +function sql2JumpAll($centerEid, $cloudSize) { + global $banned_types; + + return "select x as eid, 0.1 as r1, 0.1 as r2, 0.3 as r0, 0.5 as trash from (select x from (select X.eid + Y.dumb as x from (select B.end as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.start = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"; +} + +// Centre pointe vers X, M pointe vers X aussi, on prend M. +// Version optimisée de : "select start as eid from relation where end in (select end from relation where start = $centerEid) and type not in ($banned_types) order by random();" +// Ce n'est toujours pas ça… : "select eid from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit 1) order by random();" +// Tordu, mais ça marche \o/ . En fait il faut empêcher l'optimiseur de ramener le random avant le limit (et l'optimiseur est malin… :) +function sqlXPointsToMMPointsToXTakeM($cloudSize) { + global $banned_types; + + return "select x as eid, 0.1 as r1, 0.1 as r2, 0.2 as r0, 0.6 as trash from (select x from (select X.eid + Y.dumb as x from (select B.start as eid from relation as A, relation as B where A.type not in ($banned_types) and A.start = $centerEid and B.type not in ($banned_types) and B.end = A.end limit ".($cloudSize*4).") as X, (select 0 as dumb) as Y)) order by random();"; +} + +function sqlGetNameFromNode($res) { + return "select name from node where eid=".$res['eid'].";"; +} + +function sqlGetGidFromGame() { + return "select gid from game where gid = (abs(random()) % (select max(gid) from game))+1 or gid = (select max(gid) from game where gid > 0) order by gid limit 1;"; +} + +function sqlGetNameFromNodeWithEid($eid) { + return "select name from node where eid = $eid"; +} + +// TODO Yoann : faire des tests d'erreur pour ces select ? +function sqlGetGamesForId($gameId) { + return "select gid, (select name from node where eid = eid_central_word) as name_central_word, eid_central_word, relation_1, relation_2 from game where gid = ".$gameId.";"; +} + +function sqlGetWordEidAndName($gameId) { + return "select eid_word,(select name from node where eid=eid_word) as name_word from game_cloud where gid = ".$gameId.";"; +} + +function sqlGetInformationAboutGame($gameId) { + return "select eid_word,(select name from node where eid=eid_word) as name_word, num, difficulty, totalWeight, probaR1, probaR2, probaR0, probaTrash from game_cloud where gid = ".$gameId.";"; +} + +function sqlGameIsOK($pgid, $gid, $user) { + return "SELECT 'ok' FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user' and timestamp = -1;"; +} + +function sqlGetScoreForUser($user) { + return "SELECT score FROM user WHERE login='".$user."';"; +} + +function sqlGetPlayedGameTime($pgid, $gid, $user) { + return "SELECT timestamp FROM played_game WHERE pgid = $pgid and $gid = $gid and login = '$user';"; +} + +function sqlGetNumAndScoreFromGame($pgid, $gid) { + return "SELECT num,score from played_game_cloud where pgid = $pgid and gid = $gid;"; +} + +function sqlGetEidFromNode($node) { + return "SELECT eid FROM node WHERE name='".SQLite3::escapeString($node)."'"; +} \ No newline at end of file diff --git a/code/serveur/php/server.php b/code/serveur/php/server.php index b248b78..3dcdc83 100644 --- a/code/serveur/php/server.php +++ b/code/serveur/php/server.php @@ -31,7 +31,7 @@ function logError($errNum, $msg, $other="") function main() { if(!isset($_GET['action']) || !isset($_GET['user']) || !isset($_GET['passwd'])) { - throw new Exception("La requête est incomplète", 2); + errRequestIncomplete(); } // Login @@ -39,7 +39,7 @@ function main() $user = SQLite3::escapeString($_GET['user']); $loginIsOk = checkLogin($user, $_GET['passwd']); if ($action != 3 && (!$loginIsOk)) { - throw new Exception("Utilisateur non enregistré ou mauvais mot de passe", 3); + errUserUnknownOrBadPassword(); } if ($action == 3) { echo '{"login_ok":' . ($loginIsOk ? 'true' : 'false') . '}'; @@ -53,26 +53,26 @@ 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'])) { - throw new Exception("La requête est incomplète", 2); + errRequestIncomplete(); } createGame(intval($_GET['nb']), $_GET['mode']); echo '{"success":1}'; } else if($action == 0) { // "Get partie" // Requête POST : http://serveur/server.php?action=0&nb=2&mode=normal&user=foo&passwd=bar if(!isset($_GET['nb']) || !isset($_GET['mode'])) { - throw new Exception("La requête est incomplète", 2); + errRequestIncomplete(); } getGame($user, intval($_GET['nb']), $_GET['mode']); } 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'])) { - throw new Exception("La requête est incomplète", 2); + errRequestIncomplete(); } // TODO : il faudrait filtrer les paramètres qui correspondent à une réponse // au lieu d'envoyer $_GET en entier, mais on ne connaît pas leur nom à l'avance. setGameGetScore($user, $_GET['pgid'], $_GET['gid'], $_GET); } else { - throw new Exception("Commande inconnue", 2); + errRequestIncomplete(); } }