www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 5b16d89784f2be912545e66d46d4058698bb76e4
parent c2443c4623b8110874c1c38dd3f5960e38321583
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date:   Thu, 26 May 2011 00:36:02 +0200

Merge branch 'unstable' of github:jsmaniac/2011-m1s2-ter into unstable

Diffstat:
Mcode/serveur/dump2sqlite.sh | 7++++---
Mcode/serveur/php/createGame.php | 289+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mcode/serveur/php/ressources/backend.inc | 53+++++++++++++++++++++++++++++++++++++++++++++++++----
Mcode/serveur/php/ressources/createGame.js | 55+++++++++++++++++++++++++++----------------------------
Mcode/serveur/php/server.php | 16+++++++++++++---
Mcode/serveur/php/signup.php | 4++--
6 files changed, 244 insertions(+), 180 deletions(-)

diff --git a/code/serveur/dump2sqlite.sh b/code/serveur/dump2sqlite.sh @@ -26,7 +26,7 @@ create table node(eid integer primary key autoincrement, name, type, weight); create table relation(rid integer primary key autoincrement, start, end, type, weight); create table type_node(name, num); create table type_relation(name, num, extended_name, info); -create table user(login primary key, mail, hash_passwd, score, ugroup); +create table user(login primary key, mail, hash_passwd, score, ugroup, cgCount); create table game(gid integer primary key autoincrement, eid_central_word, relation_1, relation_2, difficulty, author, nb_like, nb_dislike); create table game_cloud(gid, num, difficulty, eid_word, totalWeight, probaR1, probaR2, probaR0, probaTrash); create table played_game(pgid, gid, login, timestamp, like, primary key(login, pgid)); @@ -37,8 +37,9 @@ create table random_cloud_node(eid,nbneighbors); create table random_center_node(eid); create table user_info(user, key, value, primary key (user, key)); -insert into user(login, mail, hash_passwd, score, ugroup) values('$(echo "$user" | sed -e "s/'/''/g")', 'foo@isp.com', '$(echo "$passwd" | dd bs=1 count="${#passwd}" | (if which md5sum >/dev/null 2>&1; then md5sum; else md5; fi) | cut -d ' ' -f 1)', 0, 1); -insert into user(login, mail, hash_passwd, score, ugroup) values('bot', 'no mail', 'no password', 0, 1); +insert into user(login, mail, hash_passwd, score, ugroup, cgCount) values('$(echo "$user" | sed -e "s/'/''/g")', 'foo@isp.com', '$(echo "$passwd" | +dd bs=1 count="${#passwd}" | (if which md5sum >/dev/null 2>&1; then md5sum; else md5; fi) | cut -d ' ' -f 1)', 0, 1, 0); +insert into user(login, mail, hash_passwd, score, ugroup, cgCount) values('bot', 'no mail', 'no password', 0, 1, 0); EOF # tr : pour virer le CRLF qui traîne diff --git a/code/serveur/php/createGame.php b/code/serveur/php/createGame.php @@ -6,7 +6,8 @@ session_start(); if(!isset($_SESSION['userId'])) header("location:login.php?return=createGame&showmsg=oth_login_createGame_nauth"); - + +$cgCount = getNbGameCreationRemained($_SESSION['userId']); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> @@ -15,153 +16,161 @@ if(!isset($_SESSION['userId'])) <title>PtiClic - Création de partie</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="ressources/simple.css" /> - <script type="text/javascript" src="ressources/jquery-1.5.1.min.js" /></script> - <script type="text/javascript" src="ressources/createGame.js" /></script> -<style type="text/css"> - #wordLines input{ - border : 2px solid grey; - } - .status { - visibility: hidden; - } - .valid .status, #center.valid .status { - color: #20FF20; - visibility: visible; - } - .invalid .status, #center.invalid .status, #center .status { - color: #FF2020; - visibility: visible; - } - - .wordLinesTable { - min-height : 20px; - min-width : 20px; - border-collapse : collapse; - border-spacing : 0px; - margin-left : 30px; - } - - .wordLinesTable td { - padding : 6px; - padding-left : 10px; - } - - .wordLinesTable .lightLine { - background-color : #F0F0D0; - } - - .wordLinesTable td:first-child { - text-align : right; - } + + <?php if($cgCount > 0) {?> + <script type="text/javascript" src="ressources/jquery-1.5.1.min.js" /></script> + <script type="text/javascript" src="ressources/createGame.js" /></script> + <?php } ?> + <style type="text/css"> + #wordLines input{ + border : 2px solid grey; + } + .status { + visibility: hidden; + } + .valid .status, #center.valid .status { + color: #20FF20; + visibility: visible; + } + .invalid .status, #center.invalid .status, #center .status { + color: #FF2020; + visibility: visible; + } + + .wordLinesTable { + min-height : 20px; + min-width : 20px; + border-collapse : collapse; + border-spacing : 0px; + margin-left : 30px; + } + + .wordLinesTable td { + padding : 6px; + padding-left : 10px; + } + + .wordLinesTable .lightLine { + background-color : #F0F0D0; + } + + .wordLinesTable td:first-child { + text-align : right; + } - #center { - margin-left : 100px; - margin-top : 20px; - margin-bottom : 30px; - } - - #center label { - border-bottom : 1px solid grey; - border-left : 1px solid grey; - -moz-border-radius : 100%; - padding-left : 10px; - } - - #relations { - margin-bottom : 20px; - margin-top : 10px; - } - - #relations label { - margin-left : 40px; - border-bottom : 1px solid grey; - border-left : 1px solid grey; - -moz-border-radius : 100%; - padding-left : 10px; - padding-right : 10px; - } - - #button { - margin-top : 30px; - margin-left : 50px; - margin-bottom : 40px; - } - - #button input { - margin-left : 10px; - margin-right : 40px; - padding : 4px; - padding-left : 8px; - padding-right : 8px; - font-weight : bold; - } - -</style> + #center { + margin-left : 100px; + margin-top : 20px; + margin-bottom : 30px; + } + + #center label { + border-bottom : 1px solid grey; + border-left : 1px solid grey; + -moz-border-radius : 100%; + padding-left : 10px; + } + + + #relations { + margin-bottom : 20px; + margin-top : 10px; + } + + #relations label { + margin-left : 40px; + border-bottom : 1px solid grey; + border-left : 1px solid grey; + -moz-border-radius : 100%; + padding-left : 10px; + padding-right : 10px; + } + + #button { + margin-top : 30px; + margin-left : 50px; + margin-bottom : 40px; + } + + #button input { + margin-left : 10px; + margin-right : 40px; + padding : 4px; + padding-left : 8px; + padding-right : 8px; + font-weight : bold; + } + + </style> </head> <body> <?php include("ressources/menu.inc"); ?> - <div class="content creategame"> - <h2>Création de parties</h2> - <p>Cette page vous permet de créer des parties personalisées en indiquant les mots qui seront affiché pour un mot central.<br /><br /> - <div id="errorDiv" class="message warning" style="display:none;"></div> - <div id="successDiv" class="message success" style="display:none;"></div> - <a id="newCreationLink" style="display:none;" href="createGame.php">Créer une autre partie</a> - - <div id="center"> - <label for="centralWord"> Le mot central : </label> - <input type="text" id="centralWord" name="centralWord" /> - <span class="status">●</span> - </div> - <div id="relations"> - <label for="relation1">Relation 1</label> - <select name="relation1" id="relation1"> - </select> - <label for="relation2">Relation 2</label> - <select name="relation2" id="relation2"> - </select> - </div> - <div id="wordLines"> - <div id="templates" style="display:none"> - <table> - <thead> </thead> - <tbody> - <tr class="wordLine"> - <td> - <label for="word-"></label> - </td> - <td> - <input value="" class="word" type="text" id="word-"/> - </td> - <td> - <span class="status">●</span> - </td> - <td> - <input type="checkbox" id="r1-"/><label class="r1 relationLabel" for="r1-">Blabla</label> - </td> - <td> - <input type="checkbox" id="r2-"/><label class="r2 relationLabel" for="r2-">Blabla</label> - </td> - <td> - <input type="checkbox" id="r3-"/><label class="r3 relationLabel" for="r3-">Blabla</label> - </td> - <td> - <input type="checkbox" id="r4-"/><label class="r4 relationLabel" for="r4-">Blabla</label> - </td> - </tr> - </tbody> + <h2>Création de parties</h2> + <?php if($cgCount > 0) { ?> + <h3>Vous pouvez créer encore <?php echo $cgCount;?> partie(s).</h3> + <form class="content creategame"> + <p>Cette page vous permet de créer des parties personalisées en indiquant les mots qui seront affiché pour un mot central.<br /><br /> + <div id="errorDiv" class="message warning" style="display:none;"></div> + <div id="successDiv" class="message success" style="display:none;"></div> + <a id="newCreationLink" style="display:none;" href="createGame.php">Créer une autre partie</a> + + <div id="center"> + <label for="centralWord"> Le mot central : </label> + <input type="text" id="centralWord" name="centralWord" /> + <span class="status">●</span> + </div> + <div id="relations"> + <label for="relation1">Relation 1</label> + <select name="relation1" id="relation1"> + </select> + <label for="relation2">Relation 2</label> + <select name="relation2" id="relation2"> + </select> + </div> + <div id="wordLines"> + <div id="templates" style="display:none"> + <table> + <thead> </thead> + <tbody> + <tr class="wordLine"> + <td> + <label for="word-"></label> + </td> + <td> + <input value="" class="word" type="text" id="word-"/> + </td> + <td> + <span class="status">●</span> + </td> + <td> + <input type="checkbox" id="r1-"/><label class="r1 relationLabel" for="r1-">Blabla</label> + </td> + <td> + <input type="checkbox" id="r2-"/><label class="r2 relationLabel" for="r2-">Blabla</label> + </td> + <td> + <input type="checkbox" id="r3-"/><label class="r3 relationLabel" for="r3-">Blabla</label> + </td> + <td> + <input type="checkbox" id="r4-"/><label class="r4 relationLabel" for="r4-">Blabla</label> + </td> + </tr> + </tbody> + </table> + </div> + + <table class="wordLinesTable"> + <tr><td></td></tr> </table> </div> - - <table class="wordLinesTable"> - <tr><td></td></tr> - </table> - </div> - <div id="button"></div> - </div> - <div id="templates" style="display:none"> - </div> + <div id="button"></div> + </form> + <?php } + else { + echo '<span class="message warning">Vous ne disposez pas assez de points pour pouvoir créer des partes.</span>'; + }?> <?php include("ressources/footer.inc"); ?> </body> </html> diff --git a/code/serveur/php/ressources/backend.inc b/code/serveur/php/ressources/backend.inc @@ -23,8 +23,7 @@ require_once("ressources/db.inc"); * computeUserReputation($score); * normalizeProbas($row); * setGame($user, $pgid, $gid, $answers); -* get_game_relations(); -* getGameRelationsJSON(); +* getGameRelations(); * setGameGetScore($user, $pgid, $gid, $answers); * getNodeEid($node); * wordExist($node); @@ -34,10 +33,12 @@ require_once("ressources/db.inc"); * setUserPref($user, $key, $value); * getJAimePgid($user, $pgid); * setJAimePgid($user, $pgid, $value); +* addGameCreationBonus($user, $nb); +* function deleteOneGameCreation($user); +* getNbGameCreationRemained($user); */ - /* Les définitions ====================*/ @@ -300,6 +301,7 @@ function decodeAndInsertGame($user,$game) { echo JSON_encode(false); } else { insertCreatedGame($centerEid,$cloud,$r1,$r2,10,$user); + deleteOneGameCreation($user); echo JSON_encode(true); } } @@ -356,7 +358,32 @@ function insertCreatedGame($centerEid, $cloud, $r1, $r2, $totalDifficulty, $user */ 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;"); + + $game = $db->query('SELECT gid,(nb_like - nb_dislike + 5 * (author != "bot")) AS coef FROM game'); + $game = $game->fetchArray(); + + foreach($game as $g) { + if($g['coef'] < -5) + $g['coef'] = 0; + else + $g['coef'] += 5; + + $sum += $g['coef']; + } + + $game = rand(0,$sum); + + foreach($game as $g) { + $count += $g['coef']; + + if($count >= $randomValue) + return $g['gid']; + } + + return 0; + + // Ancienne requêtes de sélection. + //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;"); } /** Sélection aléatoire d'une partie de la base de données parmis les parties à jouer. @@ -725,4 +752,22 @@ function setJAimePgid($user, $pgid, $value) { getDB()->querySingle("UPDATE game SET nb_dislike = nb_dislike".($original == -1 ? " - 1" : "").($value == -1 ? " + 1" : "")." WHERE login = '".SQLite3::escapeString($user)."' and pgid = ".longStrVal($pgid).";"); } +function addGameCreationBonus($user, $nb) { + if($nb > 0) { + getDB()->exec('UPDATE user SET cgCount = cgCount + '.$ng.' WHERE login=\''.$user.'\''); + return true; + } + + return false; +} + +function deleteOneGameCreation($user) { + if(getNbGameCreationRemained($user) > 0) + getDB()->exec('UPDATE user SET cgCount=cgCount-1 WHERE login=\''.$user.'\''); +} + +function getNbGameCreationRemained($user) { + return getDB()->querySingle('SELECT cgCount FROM user WHERE login=\''.$user.'\''); +} + ?> diff --git a/code/serveur/php/ressources/createGame.js b/code/serveur/php/ressources/createGame.js @@ -42,14 +42,14 @@ $(function() { var updateRelationLabels = function() { $('#relations option').each(function(i,e) { - $(e).text(applyFormat($(e).data("format"), $('#centralWord').val() || 'mot central', '…')); + $(e).text(applyFormat($(e).data("format"), $('#centralWord').val().trim() || 'mot central', '…')); }); $('.relationLabel').each(function(i,e) { $(e).text(applyFormat( $(e).data("format"), - $('#centralWord').val() || 'mot central', - $(e).closest('.wordLine').find('.word').val() || '…')); + $('#centralWord').val().trim() || 'mot central', + $(e).closest('.wordLine').find('.word').val().trim() || '…')); }); } @@ -92,26 +92,22 @@ $(function() { $("#button").html('<input type="button" id="addLine" name="addLine" value="Ajouter" />'); $("#addLine").click(function(){ displayNWordLines(1); }); - $("#button").append('<input type="button" id="validate" name="validate" value="Valider" />'); - $("#validate").click(function(){ formOK(); }); + $("#button").append('<input type="submit" id="validate" name="validate" value="Valider" />'); + $("form").submit(formOK); }; var checkWord = function () { updateRelationLabels(); var input = $(this); - var word = input.val(); + var word = input.val().trim(); input.closest(".wordLine, #center").removeClass("valid invalid"); if (word != "") { - $.ajax({ - type: "GET", - url: "server.php?", - data: "action=4&word="+word, //+"&user="+user+"&passwd="+passwd, - success: function(msg){ - input.closest(".wordLine, #center").addClass(msg == false ? "invalid" : "valid"); - wordsOK[input.attr("id")] = !(msg == false); - }}); + $.getJSON("server.php",{action:4,word:word}, function(msg){ + input.closest(".wordLine, #center").addClass(msg ? "valid" : "invalid"); + wordsOK[input.attr("id")] = !(msg == false); + }); } }; @@ -120,7 +116,7 @@ $(function() { if ($("#relation1").val() == $("#relation2").val()) displayError("Les deux relation doivent être différents"); - else if ($("#centralWord").val() == "") + else if ($("#centralWord").val().trim() == "") displayError("Le mot central doit être renseigné."); else if (badWord()) displayError("Il existe des mots incorrects"); @@ -146,7 +142,7 @@ $(function() { var badWord = function() { for (word in wordsOK) - if ($("#"+word).val() != "" && wordsOK[word] == false) + if ($("#"+word).val().trim() != "" && wordsOK[word] == false) return true; return false; @@ -167,13 +163,13 @@ $(function() { var exit; var cloud = ""; - exit = {center:$("#centralWord").val(), + exit = {center:$("#centralWord").val().trim(), relations:[$("#relation1").val(),$("#relation2").val(),0,-1], cloud:[]}; for(i=1;i<numWord;i++) { exit.cloud.push({ - name:$("#word-"+i).val(), + name:$("#word-"+i).val().trim(), relations:[ $("#r1-"+i).is(":checked") ? "1":"0", $("#r2-"+i).is(":checked") ? "1":"0", @@ -183,9 +179,9 @@ $(function() { }); } - $.get("server.php",{action:"6",game:exit},function (data) { + $.getJSON("server.php",{action:6,game:exit},function (data) { //$(".word").closest(".wordLine, #center").removeClass("valid invalid"); - if(data == true) { + if(data === true) { displaySuccess("La partie à bien été enregistrée"); $('#newCreationLink').show(); $('#center').hide(); @@ -193,22 +189,25 @@ $(function() { $('#wordLines').hide(); $('#button').hide(); } - else if (data == false) { + else if (data === false) { $('input').removeAttr('disabled'); displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides."); } - else if (data != true) { + else if (data.length) { $('input').removeAttr('disabled'); - var that = $(this); - - - $.each(data,function(i,e) { + + $.each(data,function(i,bad) { $('.word') - .filter(function() { return that.val() == e; }) + .removeClass("invalid") + .addClass("valid") + .filter(function(i,w) { return $(w).val().trim() == bad; }) .closest(".wordLine, #center") - .removeClass("valid invalid") + .removeClass("valid") .addClass("invalid"); }); + } else { + $('input').removeAttr('disabled'); + displayError("Une erreur inconnue est survenue. 42."); } }); diff --git a/code/serveur/php/server.php b/code/serveur/php/server.php @@ -111,15 +111,25 @@ function main() setUserPref($user, $_GET['key'], $_GET['value']); userPrefs($user); - } elseif ($action == 9) { + } + elseif ($action == 9) { session_destroy(); echo '{"disconnected":true}'; - } elseif ($action == 10) { // Set J'aime / J'aime pas + } + elseif ($action == 10) { // Set J'aime / J'aime pas if (!isset($_GET['value']) || !isset($_GET['pgid'])) throw new Exception("La requête est incomplète", 2); + setJAimePgid($user, $_GET['pgid'], intval($_GET['value'])); echo '{"JAimePas":true}'; - } else { + } + elseif ($action == 11) { // bonus création de parties + if (!isset($_GET['value'])) + throw new Exception("La requête est incomplète", 2); + + addGameCreationBonus($user, $_GET['value']); + } + else { throw new Exception("Commande inconnue", 2); } } diff --git a/code/serveur/php/signup.php b/code/serveur/php/signup.php @@ -79,10 +79,10 @@ if(isset($_POST['signuppswd2'])){ if(count($msg) == 0 && $newpage == false) { - $ok = ($db->query("INSERT INTO user(mail, login, hash_passwd, score, ugroup) VALUES ('" . SQLite3::escapeString($signupemail) + $ok = ($db->query("INSERT INTO user(mail, login, hash_passwd, score, ugroup, cgCount) VALUES ('" . SQLite3::escapeString($signupemail) . "', '" . SQLite3::escapeString($signupid) . "', '" . SQLite3::escapeString(md5($signuppswd1)) - . "', 0, 1);")); + . "', 0, 1, 0);")); if($ok == true) { $_SESSION['userId'] = $signupid;