commit 5110c0d379ac27096c2cc393b699ab70fda528f9
parent c12242f53d396a38f91ff1f99185d2f24e1e5775
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Sun, 10 Apr 2011 14:13:45 +0200
Merge branch 'unstable' of github:jsmaniac/2011-m1s2-ter into unstable
Diffstat:
4 files changed, 140 insertions(+), 67 deletions(-)
diff --git a/code/serveur/php/createGamejs.php b/code/serveur/php/createGamejs.php
@@ -167,8 +167,19 @@ else
<script type="text/javascript" src="ressources/createGame.js" /></script>
<style type="text/css">
#wordLines input{
- border : 2px solid grey;
- }
+ border : 2px solid grey;
+ }
+ .wordLine .status {
+ visibility: hidden;
+ }
+ .wordLine.valid .status, #center.valid .status {
+ color: green;
+ visibility: visible;
+ }
+ .wordLine.invalid .status, #center.invalid .status, #center .status {
+ color: red;
+ visibility: visible;
+ }
</style>
</head>
<body>
@@ -176,10 +187,34 @@ else
<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="center">Mot central</div>
- <div id="wordLines"></div>
+ <div id="errorDiv" class="message warning" style="display:none;"></div>
+
+ <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 style="display:none">
+ <div class="wordLine" id="templateWordLine">
+ <label for="word"></label>
+ <input id="word"/>
+ <span class="status">●</span>
+ </div>
+ </div>
+ </div>
<div id="button"></div>
</div>
+ <div id="templates" style="display:none">
+ </div>
<?php include("ressources/footer.inc"); ?>
</body>
</html>
diff --git a/code/serveur/php/pticlic.php b/code/serveur/php/pticlic.php
@@ -1,5 +1,6 @@
<?php
+require_once("relations.php");
require_once("db.php");
/* Les prototypes des fonctions :
@@ -630,6 +631,7 @@ function get_game_relations()
function getGameRelationsJSON() {
$json = "{";
+ global $stringRelations;
foreach($stringRelations as $id=>$description)
if($id == -1)
diff --git a/code/serveur/php/ressources/createGame.js b/code/serveur/php/ressources/createGame.js
@@ -1,68 +1,102 @@
-$(function () {
- var numWord = 1;
- var user = "foo";
- var passwd = "bar";
-
- var displayNWordLines = function (nb) {
-
- for(var i=numWord; i<numWord+nb; i++){
- $("#wordLines").append('<div><label for="word'+i+'">'+i+' </label><input id="word'+i+'" /></div>');
+$(function() {
+ $.getJSON("server.php", {action:"5", user:"foo", passwd:"bar"}, function (data) {
+ var numWord = 1;
+ var user = "foo";
+ var passwd = "bar";
+ var relations = data;
+ var nbWordMin = 10;
+ var wordsOK = new Array();
+ var centerOK = false;
+
+
+ var displayNWordLines = function (nb) {
- function f(id) {
- $("#word"+id).focusout(function () {
- var input = "word"+id;
+ for(var i=numWord; i<numWord+nb; i++){
+ $("#templateWordLine")
+ .clone()
+ .find("label").attr("for", "word"+i).text(i).end()
+ .find("input").attr("id", "word"+i).end()
+ .appendTo("#wordLines");
- checkWord(input)});
- };
-
- f(i);
- }
+ (function (i) {
+ $("#word"+i).focusout(checkWord);
+ wordsOK[i] = false;
+ })(i);
+ }
- numWord += nb;
- }
-
- var displayCentralWord = function () {
- $("#center").html('<label for="centralWord"> Le mot central : </label><input type="text" id="centralWord" name="centralWord" />');
- $("#centralWord").focusout(function () {
- var input = "centralWord";
- checkWord(input)
- }
- );
- }
-
- var displayButtons = function () {
- $("#button").html('<input type="button" id="addLine" name="addLine" value="Ajouter" />');
- $("#addLine").click(function(){displayNWordLines(1)});
+ numWord += nb;
+ };
+
+ var displayCentralWordAndRelations = function() {
+ $("#centralWord").focusout(checkWord);
+
+ $.each(relations, function(i, value) {
+ $('<option/>').val(i).text(value).appendTo("#relations select");
+ });
+ $("#relation1, #relation2").change(function() {
+ if ($("#relation1").val() == $("#relation2").val())
+ displayError("Les relations doivent être différentes");
+ else
+ displayError("");
+ });
+ };
- $("#button").append('<input type="button" id="validate" name="validate" value="Valider" />');
- $("#validate").click(function(){});
- }
-
- var checkWord = function (inputId) {
- var input = "#"+inputId;
- var word = $(input).val();
+ var displayButtons = 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(); });
+ };
+
+ var checkWord = function () {
+ var input = $(this);
+ var word = input.val();
+
+ input.parent(".wordLine, #center").removeClass("valid invalid");
- if(word == "")
- $(input).css("background-color", "white");
- else {
- $.ajax({type: "GET", url: "server.php?",
+ if (word != "") {
+ $.ajax({
+ type: "GET",
+ url: "server.php?",
data: "action=4&word="+word+"&user="+user+"&passwd="+passwd,
success: function(msg){
- if(msg == "false")
- $(input).css("background-color", "orange");
- else
- $(input).css("background-color", "#55FF55");
- }});
- }
- }
+ input.parent(".wordLine, #center").addClass((msg == false) ? "invalid" : "valid");
+ wordsOK[input.val()] = !(msg == false);
+ }});
+ }
+ };
+
+ 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é.");
+ else if (nbWordOK() < nbWordMin)
+ displayError("Le nuage doit contenir au moins "+nbWordMin+" mots valides.");
+ };
+
+ var nbWordOK = function() {
+ var count = 0;
+
+ for (word in wordsOK)
+ if (word == true)
+ count++;
+
+ return count;
+ };
- var getRelationsList = function () {
- $.getJSON("server.php?action=5&user=foo&passwd=ba",function (data) {
- $.debug(data);
- });
- }
- getRelationsList();
- displayCentralWord();
- displayNWordLines(10);
- displayButtons();
+ var displayError = function(message) {
+ if (message != "")
+ $("#errorDiv").text(message).show();
+ else
+ $("#errorDiv").hide();
+ };
+
+ displayCentralWordAndRelations();
+ displayNWordLines(10);
+ displayButtons();
+ });
});
diff --git a/code/serveur/php/server.php b/code/serveur/php/server.php
@@ -79,21 +79,23 @@ function main()
throw new Exception("La requête est incomplète", 2);
if(wordExist($_GET['word']))
- echo 'true';
+ echo true;
else
- echo 'false';
+ echo false;
}
else if($action == 5) { // Get relations (JSON)
- echo "mqslkjfmlqskjfqmskf";//echo getGameRaltionsJSON();
+ echo getGameRelationsJSON();
}
else {
throw new Exception("Commande inconnue", 2);
}
+
+
}
function server() {
ob_start();
-
+
try {
main();
ob_end_flush();