first commit

This commit is contained in:
overflowerror 2014-03-07 19:45:30 +01:00
parent 57fa4b0d3b
commit db35c87596
8 changed files with 280 additions and 0 deletions

10
calc.php Executable file
View file

@ -0,0 +1,10 @@
<?php
function getRate($ra, $rb) {
return 1 / (1 + pow(10, ((abs($rb - $ra) > 400) ? ((($rb - $ra) > 0) ? 400 : -400) : ($rb - $ra)) / 400));
}
function getNew($win, $ra, $rate) {
$k = 10;
return $ra + $k * ($win - $rate);
}
?>

4
connect.php Executable file
View file

@ -0,0 +1,4 @@
<?php
$connection = mysql_connect('localhost', 'promash', 'prof');
mysql_select_db('promash');
?>

0
images/keep-dir Normal file
View file

87
index.php Executable file
View file

@ -0,0 +1,87 @@
<!DOCTYPE html>
<html>
<head>
<title>ProMash</title>
</head>
<body style="padding: 0; background-color: #ddd">
<h1 style="position: absolute; left: 0px; top:0px; width: 100%; text-align: center; height: 80px; margin: 0; padding-top: 50px; background-color: #8e0b11; color: #fff; ">ProMash</h1>
<?php
include("system.php");
if (!isset($_SESSION['active']))
resetParing();
if (!$_SESSION['active'])
$id = getRandom();
else {
if ($_SESSION['idLeft']) {
$id = $_SESSION['active'];
} else {
$id = $_SESSION['against'];
}
}
$i = 0;
while (true) {
echo "<!-- $i -->";
if ($i > (intval(getNumberOfPros()) / 2)) {
resetParing();
$i = 0;
$id = getRandom();
}
$pid = intval(getPartner($id));
echo "<!-- $id : $pid -->";
if ($id == $pid)
continue;
if ($pid === false)
$id = intval(getNextId($id));
else
break;
$i++;
}
setPartners($id, $pid);
if (!$_SESSION['idLeft']) {
$tmp = $id;
$id = $pid;
$pid = $tmp;
}
$_SESSION['active'] = $id;
$_SESSION['against'] = $pid;
$leftImg = getImage($id);
$rightImg = getImage($pid);
$leftRate = getRateByIds($id, $pid);
$rightRate = 1 - $leftRate;
?>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div style="text-align: center; font-size: 20px; font-weight: bold;">
Who's better? Click to choose.
</div>
<br /><br /><br /><br />
<div style="text-align: center">
<div style="margin: auto auto;">
<div style="display: inline; ">
<a href="result.php?left">
<img style="height: 150px;" src="images/<?php echo $leftImg; ?>" />
</a>
</div>
<div style="display: inline; font-size: 40px; position: relative; top: -55px;">
or
</div>
<div style="display: inline">
<a href="result.php?right">
<img style="height: 150px;" src="images/<?php echo $rightImg; ?>" />
</a>
</div>
</div>
</div>
<div style="display: none;">
Left: <?php echo $leftRate * 100; ?> %<br />
Right: <?php echo $rightRate * 100; ?> %<br />
</div>
<div style="margin: 0; position: absolute; left: 0px; bottom: 0px; width: 100%; height: 25px; text-align: center; background-color: #8e0b11; color: #fff;">
<a style="color: #fff" href="http://en.wikipedia.org/wiki/Elo_rating_system">Elo rating</a> - <a style="color: #fff" href="http://htlinn.ac.at">Image copyright</a> - You don't want to rate the current image? Just press F5.
</div>
</body>
</html>

39
result.php Executable file
View file

@ -0,0 +1,39 @@
<?php
include("system.php");
if (!isset($_SESSION['active']))
die("fische");
if (!($active = $_SESSION['active']))
die("fische");
if (!isset($_SESSION['against']))
die("fische");
if (!($against = $_SESSION['against']))
die("fische");
if (isset($_GET['left'])) {
win($active, $against);
$_SESSION['idLeft'] = true;
}
if (isset($_GET['right'])) {
win($against, $active);
$_SESSION['idLeft'] = false;
}
header("LOCATION: index.php");
function win($id, $pid) {
$ra = getPoints($id);
$rb = getPoints($pid);
$ea = getRate($ra, $rb);
$eb = getRate($rb, $ra);
$newRa = getNew(1, $ra, $ea);
$newRb = getNew(0, $rb, $eb);
$sql = "UPDATE pros SET value=" . intval($newRa) . " WHERE id=" . intval($id);
$result = mysql_query($sql);
$sql = "UPDATE pros SET value=" . intval($newRb) . " WHERE id=" . intval($pid);
$result = mysql_query($sql);
}
?>

20
setup.php Executable file
View file

@ -0,0 +1,20 @@
<?php
die("!!!");
include("connect.php");
$sql = "DROP TABLE pros";
$result = mysql_query($sql);
echo mysql_error();
$sql = "CREATE TABLE pros ( id INT AUTO_INCREMENT, image TEXT, value INT, PRIMARY KEY ( id ))";
$result = mysql_query($sql);
echo mysql_error();
$array = scandir("images");
foreach ($array as $i) {
if ($i == ".")
continue;
if ($i == "..")
continue;
echo $i . "<br />";
$sql = "INSERT INTO pros (image, value) VALUES ('" . $i . "', 1024)";
mysql_query($sql);
}
?>

110
system.php Executable file
View file

@ -0,0 +1,110 @@
<?php
session_start();
include("connect.php");
include("calc.php");
$staticNumberOfPros = 0;
function getRandom() {
return rand(1, getNumberOfPros());
}
function getNumberOfPros() {
if ($staticNumberOfPros > 0)
return $staticNumberOfPros;
$sql = "SELECT id FROM pros";
$resource = mysql_query($sql);
$staticNumberOfPros = mysql_num_rows($resource);
return $staticNumberOfPros;
}
function resetParing() {
$_SESSION['idLeft'] = true;
$_SESSION['active'] = false;
$_SESSION['against'] = false;
$_SESSION['pArray'] = array();
$_SESSION['debug'] = array();
}
function setPartners($id, $pid) {
$hash = getHash($id, $pid);
//$_SESSION['debug'][] = array("setPartners", $id, $pid, $hash);
$_SESSION['pArray'][] = $hash;
}
function getNextId($id) {
$number = getNumberOfPros();
$id++;
if ($id >= $number)
$id -= $number;
return $id;
}
function getPartner($id) {
$id = intval($id);
$sql = "SELECT id, value FROM pros ORDER BY value";
$resource = mysql_query($sql);
$all = array();
$i = 0;
$activ = -1;
while ($row = mysql_fetch_object($resource)) {
if (intval($row->id) == $id)
$activ = $i;
$all[] = $row;
$i++;
}
if ($activ < 0) {
echo "something happend:\n\n";
echo "$i, $activ, $id";
die();
}
$number = getNumberOfPros();
$up = true;
$offset = 1;
$i = 0;
do {
if ($i > ($number / 4 * 3))
return false;
$newIndex = $activ + (($up) ? ($offset) : (-$offset));
$newIndex %= $number;
$offset += ($up) ? (0) : (1);
$up = !$up;
$pid = $all[$newIndex]->id;
if ($id == $pid) {
echo "something happend:\n\n";
echo "$i, $number, $activ, " . intval($up) . ", $offset";
die();
}
$i++;
} while (allreadyPartner($id, $pid));
return $pid;
}
function allreadyPartner($id, $pid) {
foreach ($_SESSION['pArray'] as $hash)
if ($hash == getHash($id, $pid))
return true;
return false;
}
function getHash($id, $pid) {
return $id ^ $pid;
}
function getImage($id) {
$sql = "SELECT image FROM pros WHERE id = " . intval($id);
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
return $row->image;
}
function getPoints($id) {
$sql = "SELECT value FROM pros WHERE id = " . intval($id);
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
return $row->value;
}
function getRateByIds($id, $pid) {
$ra = getPoints($id);
$rb = getPoints($pid);
return getRate($ra, $rb);
}
?>

10
test.php Executable file
View file

@ -0,0 +1,10 @@
<?php
include("system.php");
echo 124 ^ 47;
echo "\n\n";
echo getNumberOfPros() . "<br />";
print_r($_SESSION);
?>