From 720c88875e149276544652c75590c813b1b0b7c6 Mon Sep 17 00:00:00 2001 From: Yoann Bonavero Date: Wed, 2 Feb 2011 11:11:15 +0100 Subject: [PATCH 1/4] =?UTF-8?q?Ajout=20du=20d=C3=A9but=20de=20la=20fonctio?= =?UTF-8?q?n=20de=20selection=20d'une=20partie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/serveur/php/function.php | 2 +- code/serveur/php/pticlic.php | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/code/serveur/php/function.php b/code/serveur/php/function.php index 1a35272..2d788f6 100755 --- a/code/serveur/php/function.php +++ b/code/serveur/php/function.php @@ -33,4 +33,4 @@ function writeRequest($request) { } -?> \ No newline at end of file +?> diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php index 237f3a5..e15d6d9 100644 --- a/code/serveur/php/pticlic.php +++ b/code/serveur/php/pticlic.php @@ -158,6 +158,13 @@ function create_game($cloudSize) { create_game(10); echo 'ok'; +function selectGame($gId) +{ + echo "id: ".$gid.','; + + $req = "select eid_central_word, (select name from node where eid=eid_central_word limit 1)"; +} + // // Sinon tout est bon on effectu l'opération correspondant à la commande passée. // if($action == 0) // "Get partie" // { @@ -210,4 +217,4 @@ echo 'ok'; // else // die("Commande inconnue"); -?> \ No newline at end of file +?> From 31e63f5fa3322634d08f3dbb951c4f806e3aeb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Thu, 3 Feb 2011 00:22:16 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Serveur=20:=20g=C3=A9n=C3=A9ration=20des=20?= =?UTF-8?q?parties=20100%=20ok,=20je=20passe=20=C3=A0=20la=20s=C3=A9lectio?= =?UTF-8?q?n=20d'une=20partie=20+=20r=C3=A9sultats.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/serveur/dump2sqlite.sh | 9 ++++-- code/serveur/php/pticlic.php | 57 ++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/code/serveur/dump2sqlite.sh b/code/serveur/dump2sqlite.sh index 928b11c..5b5e68a 100755 --- a/code/serveur/dump2sqlite.sh +++ b/code/serveur/dump2sqlite.sh @@ -7,6 +7,8 @@ echo " La progression est affichée avec pv. Si vous n'avez pas pv, supprimez l echo " Et c'est parti !" >&2 echo >&2 +# Played_game(type) : 0 => partie de référence, 1 => joueur + cat <exec("insert into user(login, mail, hash_passwd) values('foo', 'foo@isp.com', '".md5('bar')."');"); } -// initdb(); +if ($do_initdb) initdb(); if(!isset($_GET['action']) || !isset($_GET['user']) || !isset($_GET['passwd'])) mDie(2,"La requête est incomplète"); @@ -49,27 +50,28 @@ function cg_build_result_sets($cloudSize, $centerEid, $r1, $r2) { $typer1r2 = "type in ($r1, $r2)"; $sources = array( // Voisins 1 saut du bon type (= relations déjà existantes) - array('w'=>10, 'd'=>1, 's'=>"select end as eid, type = $r1 as r1, type = $r2 as r2 from relation where start = $centerEid and $typer1r2 order by random();"), + 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'=>10, 'd'=>2, 's'=>"select end as eid, 0 as r1, 0 as r2 from relation where start = $centerEid and type = 0 order by random();"), + 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'=>10, 'd'=>3, 's'=>"select end as eid, 0 as r1, 0 as r2 from relation where start = $centerEid and type not in (0, $r1, $r2) order by random();"), + // TODO ! certains de ces select pourraient renvoyer des mots de types systèmes (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001), il faut les éliminer. + array('w'=>20, 'd'=>3, '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) 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'=>10, 'd'=>4, 's'=>"select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3 as r1, ((A.type = $r2) + (B.type = $r2)) / 3 as r2 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();"), + array('w'=>30, 'd'=>3, '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'=>10, 'd'=>5, 's'=>"select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2 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();"), + 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'=>10, 'd'=>6, 's'=>"select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2 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();"), + 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) - array('w'=>10, 'd'=>7, 's'=>"select end as eid, 0.1 as r1, 0.1 as r2 from relation where start in (select end from relation where start = $centerEid) order by random();"), + array('w'=>10, 'd'=>7, 's'=>"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) 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and A.start = $centerEid and B.type not in (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) 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 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and A.start = 74860 and B.type not in (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and B.end = A.end limit $cloudSize) as X, (select 0 as dumb) as Y)) order by random();"), - 'rand' => array('w'=>10, 's'=>false) // random. Le r1 et r2 de random sont juste en-dessous + 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and A.start = 74860 and B.type not in (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and B.end = A.end limit $cloudSize) as X, (select 0 as dumb) as Y)) order by random();"), + 'rand' => array('w'=>5, 'd'=>10, 's'=>false) // random. Le r1 et r2 de random sont juste en-dessous ); $sumWeights = 0; @@ -88,7 +90,7 @@ function cg_build_result_sets($cloudSize, $centerEid, $r1, $r2) { } else { $sources[$k]['resultSet'] = array(); for ($i = 0; $i < 10; $i++) { - $sources[$k]['resultSet'][] = array('eid'=>random_node(), 'r1'=>-1, 'r2'=>-1); + $sources[$k]['resultSet'][] = array('eid'=>random_node(), 'r1'=>0, 'r2'=>0, 'r0'=>0, 'trash'=>1); $sources[$k]['rsSize']++; } } @@ -97,7 +99,7 @@ function cg_build_result_sets($cloudSize, $centerEid, $r1, $r2) { } function cg_choose_relations() { - $relations = array(5, 7, 9, 10); + $relations = array(5, 7, 9, 10, /* Pas d'icônes pour celles-ci. */ 13, 14, 22); $r1 = rand(0,count($relations)-1); $r2 = rand(0,count($relations)-2); if ($r2 >= $r1) $r2++; @@ -116,15 +118,21 @@ function cg_build_cloud($cloudSize, $sources, $sumWeights) { $rands = rand(1,$sumWeights); $sumw = 0; $src = $sources['rand']; - foreach ($sources as $x) { + $srck = null; + foreach ($sources as $k => $x) { $sumw += $x['w']; if ($rands < $sumw) { $src = $x; + $srck = $k; break; } } if ($src['rsPos'] >= $src['rsSize']) { $nbFailed++; + if ($srck !== null) { + $sumWeights -= $src['w']; + unset($sources[$srck]); + } continue; } $res = $src['resultSet'][$src['rsPos']++]; @@ -133,22 +141,28 @@ function cg_build_cloud($cloudSize, $sources, $sumWeights) { continue; } // position dans le nuage, difficulté, eid, probaR1, probaR2 - $cloud[$i] = array('pos'=>$i++, 'd'=>$src['d'], 'eid'=>$res['eid'], 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2']); + $cloud[$i] = array('pos'=>$i++, 'd'=>$src['d'], 'eid'=>$res['eid'], 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']); } + $res = $sources['rand']['resultSet'][0]; while ($i < $cloudSize) { - $cloud[$i] = array('pos'=>$i++, 'd'=>$sources['rand']['d'], 'eid'=>random_node(), 'probaR1'=>$sources['rand']['resultSet'][0]['r1'], 'probaR2'=>$sources['rand']['resultSet'][0]['r2']); + $cloud[$i] = array('pos'=>$i++, 'd'=>$sources['rand']['d'], 'eid'=>random_node(), 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']); } return $cloud; } function cg_insert($centerEid, $cloud, $r1, $r2) { + // Insère dans la base une partie avec le mot central $centerEid, le nuage $cloud et les relations $r1 et $r2 + global $db; $db->exec("begin transaction;"); $db->exec("insert into game(gid, eid_central_word, relation_1, relation_2) values (null, $centerEid, $r1, $r2);"); $gid = $db->lastInsertRowID(); + $db->exec("insert into played_game(pgid, gid, login) values (null, $gid, null);"); + $pgid = $db->lastInsertRowID(); foreach ($cloud as $c) { - $db->exec("insert into game_cloud(gid, num, difficulty, probaR1, probaR2, eid_word) values($gid, ".$c['pos'].", ".$c['d'].", ".$c['probaR1'].', '.$c['probaR2'].', '.$c['eid'].");"); + $db->exec("insert into game_cloud(gid, num, difficulty, eid_word, totalWeight, probaR1, probaR2, probaR0, probaTrash) values($gid, ".$c['pos'].", ".$c['d'].", ".$c['eid'].", 2, ".$c['probaR1'].", ".$c['probaR2'].", ".$c['probaR0'].", ".$c['probaTrash'].");"); + $db->exec("insert into played_game_cloud(pgid, gid, type, num, relation, weight, score) values($pgid, $gid, 0, ".$c['pos'].", $r1, ".$c['probaR1'].", 0);"); + $db->exec("insert into played_game_cloud(pgid, gid, type, num, relation, weight, score) values($pgid, $gid, 0, ".$c['pos'].", $r1, ".$c['probaR1'].", 0);"); } - // TODO : insert into game_played une partie de référence. $db->exec("commit;"); } @@ -159,13 +173,12 @@ function create_game($cloudSize) { $r1 = cg_choose_relations(); $r2 = $r1[1]; $r1 = $r1[0]; $sources = cg_build_result_sets($cloudSize, $centerEid, $r1, $r2); $sumWeights = $sources[1]; $sources = $sources[0]; $cloud = cg_build_cloud($cloudSize, $sources, $sumWeights); - cg_insert($centerEid, $cloud); - - var_dump($cloud); - exit;} + cg_insert($centerEid, $cloud, $r1, $r2); +} create_game(10); -echo 'ok'; +echo "ok\n"; +exit; // // Sinon tout est bon on effectu l'opération correspondant à la commande passée. // if($action == 0) // "Get partie" From ab3924cb1e0791489ffdae2f61cd4dd3be17ad51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Thu, 3 Feb 2011 01:03:36 +0100 Subject: [PATCH 3/4] =?UTF-8?q?Brouillon=20de=20la=20lecture=20/=20=C3=A9c?= =?UTF-8?q?riture=20des=20parties.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/serveur/dump2sqlite.sh | 2 +- code/serveur/php/pticlic.php | 124 +++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/code/serveur/dump2sqlite.sh b/code/serveur/dump2sqlite.sh index 5b5e68a..728da31 100755 --- a/code/serveur/dump2sqlite.sh +++ b/code/serveur/dump2sqlite.sh @@ -16,7 +16,7 @@ create table relation(rid integer primary key autoincrement, start, end, type, w create table type_node(name, num); create table type_relation(name, num, extended_name, info); create table user(login primary key, mail, hash_passwd); -create table game(gid integer primary key autoincrement, eid_central_word, relation_1, relation_2); +create table game(gid integer primary key autoincrement, eid_central_word, relation_1, relation_2, difficulty); create table game_cloud(gid, num, difficulty, eid_word, totalWeight, probaR1, probaR2, probaR0, probaTrash); create table played_game(pgid integer primary key autoincrement, gid, login); create table played_game_cloud(pgid, gid, type, num, relation, weight, score); diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php index f807354..e46be96 100644 --- a/code/serveur/php/pticlic.php +++ b/code/serveur/php/pticlic.php @@ -113,6 +113,7 @@ function cg_build_cloud($cloudSize, $sources, $sumWeights) { $cloud = array(); $nbFailed = 0; $i = 0; + $totalDifficulty = 0; while ($i < $cloudSize && $nbFailed < 5*$cloudSize) { // On choisit une source aléatoire en tennant compte des poids. $rands = rand(1,$sumWeights); @@ -141,20 +142,22 @@ function cg_build_cloud($cloudSize, $sources, $sumWeights) { continue; } // position dans le nuage, difficulté, eid, probaR1, probaR2 + $totalDifficulty += $src['d']; $cloud[$i] = array('pos'=>$i++, 'd'=>$src['d'], 'eid'=>$res['eid'], 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']); } $res = $sources['rand']['resultSet'][0]; while ($i < $cloudSize) { + $totalDifficulty += $sources['rand']['d']; $cloud[$i] = array('pos'=>$i++, 'd'=>$sources['rand']['d'], 'eid'=>random_node(), 'probaR1'=>$res['r1'], 'probaR2'=>$res['r2'], 'probaR0'=>$res['r0'], 'probaTrash'=>$res['trash']); } - return $cloud; + return array($cloud, $totalDifficulty); } -function cg_insert($centerEid, $cloud, $r1, $r2) { +function cg_insert($centerEid, $cloud, $r1, $r2, $totalDifficulty) { // Insère dans la base une partie avec le mot central $centerEid, le nuage $cloud et les relations $r1 et $r2 global $db; $db->exec("begin transaction;"); - $db->exec("insert into game(gid, eid_central_word, relation_1, relation_2) values (null, $centerEid, $r1, $r2);"); + $db->exec("insert into game(gid, eid_central_word, relation_1, relation_2, difficulty) values (null, $centerEid, $r1, $r2, $totalDifficulty);"); $gid = $db->lastInsertRowID(); $db->exec("insert into played_game(pgid, gid, login) values (null, $gid, null);"); $pgid = $db->lastInsertRowID(); @@ -172,64 +175,71 @@ function create_game($cloudSize) { $centerEid = random_node(); $r1 = cg_choose_relations(); $r2 = $r1[1]; $r1 = $r1[0]; $sources = cg_build_result_sets($cloudSize, $centerEid, $r1, $r2); $sumWeights = $sources[1]; $sources = $sources[0]; - $cloud = cg_build_cloud($cloudSize, $sources, $sumWeights); - cg_insert($centerEid, $cloud, $r1, $r2); + $cloud = cg_build_cloud($cloudSize, $sources, $sumWeights); $totalDifficulty = $cloud[1]; $cloud = $cloud[0]; + cg_insert($centerEid, $cloud, $r1, $r2, $totalDifficulty); } -create_game(10); -echo "ok\n"; -exit; +//create_game(10); -// // Sinon tout est bon on effectu l'opération correspondant à la commande passée. -// if($action == 0) // "Get partie" -// { -// // Requête sql de création de partie. -// $req = "..."; - -// $sql = sqlConnect(); -// $resp = mysql_query($req); - -// if(mysql_num_rows($resp) == 0) -// echo mysql_error(); -// else -// { -// $sequence = "..."; -// echo $sequence; -// } - -// mysql_close($sql); -// } -// else if($action == 1) // "Set partie" -// { -// // Requête sql d'ajout d'informations (et calcul de résultat). -// $req = "..."; - -// $sql = sqlConnect(); -// $resp = mysql_query($req); - -// if(mysql_num_rows($resp) == 0) -// echo mysql_error(); -// else -// { -// $sequence = "..."; -// echo $sequence; -// } - -// mysql_close($sql); -// } -// else if($action == 2) -// { +function random_game() { + global $db; + 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;"); +} -// } -// else if($action == 3) -// { +function game2json($game_id) { + global $db; + // 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 = ".$game_id.";"); + $game = $game->fetchArray(); + echo "{id:".$game['gid'].",cat1:".$game['relation_1'].",cat2:".$game['relation_2'].",cat3:0,cat4:-1,"; + echo "center:{id:".$game['eid_central_word'].",name:".json_encode("".$game['name_central_word'])."},"; + echo "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 = ".$game['gid'].";"); + while ($x = $res->fetchArray()) { + echo "{id:".$x['eid_word'].",name:".$x['name_word']."}\n"; + } + echo "]}"; +} -// } -// else if($action == 4) -// { - -// } -// else -// die("Commande inconnue"); +function main() { + // Sinon tout est bon on effectu l'opération correspondant à la commande passée. + if($action == 0) { // "Get partie" + if(!isset($_GET['nb']) || !isset($_GET['mode'])) + mDie(2,"La requête est incomplète"); + $nbGames = intval($_GET['nb']); + + echo "["; + for ($i=0; $i < $nbGames; $i) { + game2json(random_game()); + if ((++$i) < $nbGames) { + echo ","; + } + } + echo "]"; + } else if($action == 1) { // "Set partie" + // Requête sql d'ajout d'informations (et calcul de résultat). + // TODO : nettoyer, finir + $gid = $_GET['gid']; // TODO : vérifier qu'on a bien distribué cette partie à cet utilisateur, et qu'il n'y a pas déjà répondu (répercuter ça sur le random_game). + $userReputation = 5; // TODO : un nombre entre 0 et 5 environ. Par ex. log(pointsUtilisateur) est un bon choix. + $db->exec("begin transaction;"); + $db->exec("insert into played_game(pgid, gid, login) values (null, $gid, null);"); + $pgid = $db->lastInsertRowID(); + for ($i=0; $i < 10; $i++) { + $x = $_GET['$i']; + // TODO : calculer le score. Score = proba[réponse de l'utilisateur]*coeff - proba[autres reponses]*coeff + // TODO : adapter le score en fonction de la réputation de l'utilisateur (plus quand il est jeune, pour le motiver, par ex. avec un terme constant qu'on ajoute). + $score = 1; + $db->exec("insert into played_game_cloud(pgid, gid, type, num, relation, weight, score) values($pgid, $gid, 1, ".$c['pos'].", $r1, ".($x*$userReputation).", ".$score.");"); + // TODO : game_cloud(probaR_x_) += $réputationJoueur * $coeff + // TODO : game_cloud(totalWeight) += $réputationJoueur * $coeff (NOTE : même coeff que pour game_cloud(probaR_x_)) + } + $db->exec("commit;"); + // On renvoie une nouvelle partie pour garder le client toujours bien alimenté. + game2json(random_game()); + } else { + die("Commande inconnue"); + } +} ?> From d5b829e8cacc6233f79e497e2e7f98f7d72b6097 Mon Sep 17 00:00:00 2001 From: Yoann Bonavero Date: Thu, 3 Feb 2011 09:10:44 +0100 Subject: [PATCH 4/4] =?UTF-8?q?Oubli=20lors=20du=20pr=C3=A9c=C3=A9dent=20m?= =?UTF-8?q?erge.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/serveur/php/pticlic.php | 223 ----------------------------------- 1 file changed, 223 deletions(-) diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php index 75c0ab5..e46be96 100644 --- a/code/serveur/php/pticlic.php +++ b/code/serveur/php/pticlic.php @@ -1,225 +1,3 @@ -<<<<<<< HEAD -exec("insert into user(login, mail, hash_passwd) values('foo', 'foo@isp.com', '".md5('bar')."');"); -} - -// initdb(); - -if(!isset($_GET['action']) || !isset($_GET['user']) || !isset($_GET['passwd'])) - mDie(2,"La requête est incomplète"); - -// Login -$action = $_GET['action']; -$user = $_GET['user']; -$hash_passwd = md5($_GET['passwd']); -if ($hash_passwd !== $db->querySingle("SELECT hash_passwd FROM user WHERE login='".SQLite3::escapeString($user)."';")) - mDie(3,"Utilisateur non enregistré ou mauvais mot de passe"); - -function random_node() { - global $db; - return $db->querySingle("select eid from node where eid = (abs(random()) % (select max(eid) from node))+1 or eid = (select max(eid) from node where eid > 0) order by eid limit 1;"); -} - -// TODO : Yoann : peut-être faire une classe create_game avec les fonctions ci-dessous comme méthodes ? - -function cg_build_result_sets($cloudSize, $centerEid, $r1, $r2) { - global $db; - // 'w' => weight (poids), 'd' => difficulté, 's' => select - // 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). - // TODO : comment mettre un poids sur random, sachant qu'il ne peut / devrait pas être dans ces select, mais plutôt un appel à random_node() ? - $typer1r2 = "type in ($r1, $r2)"; - $sources = array( - // Voisins 1 saut du bon type (= relations déjà existantes) - array('w'=>10, 'd'=>1, 's'=>"select end as eid, type = $r1 as r1, type = $r2 as r2 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'=>10, 'd'=>2, 's'=>"select end as eid, 0 as r1, 0 as r2 from relation where start = $centerEid and type = 0 order by random();"), - // Voisins 1 saut via les autres relations - array('w'=>10, 'd'=>3, 's'=>"select end as eid, 0 as r1, 0 as r2 from relation where start = $centerEid and type not in (0, $r1, $r2) 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'=>10, 'd'=>4, 's'=>"select B.end as eid, ((A.type = $r1) + (B.type = $r1)) / 3 as r1, ((A.type = $r2) + (B.type = $r2)) / 3 as r2 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'=>10, 'd'=>5, 's'=>"select B.end as eid, (A.type = $r1) * 0.75 as r1, (A.type = $r2) * 0.75 as r2 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'=>10, 'd'=>6, 's'=>"select B.end as eid, (B.type = $r1) * 0.75 as r1, (B.type = $r2) * 0.75 as r2 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) - array('w'=>10, 'd'=>7, 's'=>"select end as eid, 0.1 as r1, 0.1 as r2 from relation where start in (select end from relation where start = $centerEid) 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and A.start = $centerEid and B.type not in (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) 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 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 (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and A.start = 74860 and B.type not in (4, 12, 36, 18, 29, 45, 46, 47, 48, 1000, 1001) and B.end = A.end limit $cloudSize) as X, (select 0 as dumb) as Y)) order by random();"), - array('w'=>10, 's'=>false) // random. Le r1 et r2 de random sont juste en-dessous - ); - - $sumWeights = 0; - foreach ($sources as $k => $x) { - $sumWeights += $x['w']; - if ($x['s'] !== false) { - $sources[$k]['resultSet'] = array(); - $res = $db->query($x['s']); - $i = 0; - while ($i < 10 && $sources[$k]['resultSet'][] = $res->fetchArray()) { - $i++; - } - } else { - $sources[$k]['resultSet'] = array(); - for ($i = 0; $i < 10; $i++) { - $sources[$k]['resultSet'][] = array('eid'=>random_node(), 'r1'=>-1, 'r2'=>-1); - } - } - } - return array($sources, $sumWeights); -} - -function cg_choose_relations() { - $relations = array(5, 7, 9, 10); - $r1 = rand(0,count($relations)-1); - $r2 = rand(0,count($relations)-2); - if ($r2 >= $r1) $r2++; - $r1 = $relations[$r1]; - $r2 = $relations[$r2]; - return array($r1, $r2); -} - -function cg_build_cloud($cloudSize, $sources, $sumWeights) { - // On boucle tant qu'il n'y a pas eu au moins 2 sources épuisées - $cloud = array(); - $nbFailed = 0; - $i = 0; - while ($i < $cloudSize && $nbFailed < 50) { - // On choisit une source aléatoire en tennant compte des poids. - $rands = rand(1,$sumWeights); - $sumw = 0; - $res = false; // TODO - foreach ($sources as $x) { - $sumw += $x['w']; - if ($rands < $sumw) { - $res = $x['resultSet']; - break; - } - } - if (/* dépassé la fin de ce set */) { - $nbFailed++; - continue; - } - $res = $res['eid']; - if (in_array($res, $cloud)) { - $nbFailed++; - continue; - } - $cloud[] = $res; - $i++; - } - return $cloud; -} - -function create_game($cloudSize) { - global $db; - // select random node - $centerEid = random_node(); - $r1 = cg_choose_relations(); $r2 = $r1[1]; $r1 = $r1[0]; - $sources = cg_build_result_sets($cloudSize, $centerEid, $r1, $r2); $sumWeights = $sources[1]; $sources = $sources[0]; - $cloud = cg_build_cloud($cloudSize, $sources, $sumWeights); - - var_dump($cloud); - exit; - - $db->exec("begin transaction;"); - $db->exec("insert into game(gid, eid_central_word, relation_1, relation_2, relation_3, relation_4, reference_played_game) values (null, ".$centerEid.", 1,2,3,4,null);"); - $gid = $db->lastInsertRowID(); - foreach ($cloud as $eid) { - $db->exec("insert into game_cloud(gid, num, difficulty, eid_word) values(".$gid.", ".$i.", ".$difficulty.", ".$eid.");"); - } - // TODO : insert into game_played une partie de référence. - $db->exec("commit;"); -} - -create_game(10); -echo 'ok'; - -function selectGame($gId) -{ - echo "id: ".$gid.','; - - $req = "select eid_central_word, (select name from node where eid=eid_central_word limit 1)"; -} - -// // Sinon tout est bon on effectu l'opération correspondant à la commande passée. -// if($action == 0) // "Get partie" -// { -// // Requête sql de création de partie. -// $req = "..."; - -// $sql = sqlConnect(); -// $resp = mysql_query($req); - -// if(mysql_num_rows($resp) == 0) -// echo mysql_error(); -// else -// { -// $sequence = "..."; -// echo $sequence; -// } - -// mysql_close($sql); -// } -// else if($action == 1) // "Set partie" -// { -// // Requête sql d'ajout d'informations (et calcul de résultat). -// $req = "..."; - -// $sql = sqlConnect(); -// $resp = mysql_query($req); - -// if(mysql_num_rows($resp) == 0) -// echo mysql_error(); -// else -// { -// $sequence = "..."; -// echo $sequence; -// } - -// mysql_close($sql); -// } -// else if($action == 2) -// { - -// } -// else if($action == 3) -// { - -// } -// else if($action == 4) -// { - -// } -// else -// die("Commande inconnue"); - -?> -======= ->>>>>>> c9d82a5818ec8da08c3d9ce6038a2ad59d9bf9af