Browse Source

Tirage au sort

isundil 10 years ago
parent
commit
d3605d6feb

+ 2 - 2
src/classes/Secret.Entity.php

@@ -20,7 +20,7 @@ class SecretEntity extends \Entity
 		$this->description = $data["description"];
 		$this->admin = $data["admin"];
 		$this->id = $data["id"];
-		$this->result = $data["result"];
+		$this->result = json_decode($data["result"]);
 	}
 
 	public function getName() { return $this->name; }
@@ -58,7 +58,7 @@ class SecretEntity extends \Entity
 	{
 		$this->result = $result;
 		$req = self::getPdo()->prepare("UPDATE `campain` SET `result`=? WHERE `id`={$this->id}");
-		var_dump($req->execute(array($result)));
+		$req->execute(array(json_encode($result)));
 	}
 
 	public static function fromId($id)

+ 39 - 2
src/classes/SecretRandomizer.class.php

@@ -11,6 +11,7 @@ class SecretRandomizer
 
 	public function __construct($campain, $users, $constraints)
 	{
+		mt_srand();
 		$this->campain = $campain;
 		$this->users = $users;
 		$this->constraints = $constraints;
@@ -24,9 +25,45 @@ class SecretRandomizer
 		return ($this->result = $this->_getResult());
 	}
 
+	private static function count_candidates($candidates, $used)
+	{
+		$result = 0;
+		foreach ($candidates as $i)
+			if (!in_array($i, $used))
+				$result++;
+		return $result;
+	}
+
+	private function getNextPresent($candidates, $used = array())
+	{
+		if (count($candidates) == count($used))
+		{
+			$lastUser = $used[count($used) -1];
+			return in_array($used[0], $candidates[$lastUser]) ? $used : null;
+		}
+		$start = $i = mt_rand(0, count($candidates) - count($used) -1);
+		do
+		{
+			if (!in_array(array_keys($candidates)[$i], $used) &&
+				(!count($used) || in_array($used[count($used)-1], $candidates[array_keys($candidates)[$i]])))
+				if (self::count_candidates($candidates[array_keys($candidates)[$i]], $used) > 0 ||
+					count($candidates) == count($used) +1)
+				{
+					$used[] = array_keys($candidates)[$i];
+					$r = $this->getNextPresent($candidates, $used);
+					if ($r != null)
+						return $r;
+					array_pop($used);
+				}
+			if (--$i < 0)
+				$i = count($candidates) -1;
+		}
+		while ($i != $start);
+		return null;
+	}
+
 	private function _getResult()
 	{
-		$r = new SecretResult();
 		$candidates = array();
 		$allUsers = array();
 		foreach ($this->users as $i)
@@ -41,7 +78,7 @@ class SecretRandomizer
 			unset($candidates[$i->getUserA()][array_search($i->getUserB(), $candidates[$i->getUserA()])]);
 			unset($candidates[$i->getUserB()][array_search($i->getUserA(), $candidates[$i->getUserB()])]);
 		}
-		return $r;
+		return new SecretResult($this->getNextPresent($candidates));
 	}
 }
 

+ 7 - 2
src/classes/SecretResult.class.php

@@ -2,13 +2,18 @@
 
 class SecretResult
 {
-	public function toString()
+	private $result;
+
+	public function __construct($result)
 	{
-		return "result";
+		$this->result = $result;
 	}
 
 	public function sendMails()
 	{
 	}
+
+	public function getResult()
+	{ return $this->result; }
 }
 

+ 2 - 4
src/hash.php

@@ -21,7 +21,7 @@ class CreateController extends Controller
 		}
 		$this->users = $this->campain->getUsers();
 		$this->constraints = $this->campain->getConstraints();
-		if (!empty($_POST))
+		if (!empty($_POST) && !$this->campain->isFinished())
 			$this->managePost();
 		$this->render('hash');
 	}
@@ -44,10 +44,8 @@ class CreateController extends Controller
 		{
 			$s = new SecretRandomizer($this->campain, $this->users, $this->constraints);
 			$result = $s->getResult();
-			var_dump($result);
-			die;
 			$result->sendMails();
-			$this->campain->setResult($result->toString());
+			$this->campain->setResult($result->getResult());
 			return;
 		}
 		$this->campain->removeConstraints();