Skip to content

Commit

Permalink
ensure no duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
xathon committed Sep 22, 2020
1 parent b7d0ece commit 494bb4e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
6 changes: 6 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

//Check https://en.wikipedia.org/wiki/Pairing_function for more information
function cantor($zahl1,$zahl2) {
return 0.5 * ($zahl1 + $zahl2) * ($zahl1 + $zahl2 + 1) + $zahl2;
}
6 changes: 0 additions & 6 deletions upload.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php


// Legen Sie die Datei in xampp/htdocs/ ab und öffnen Sie mit dem Browser localhost:8080/Uploader.php
// Ändern Sie die Zeile 12 auf Ihre Zugangsdaten und Datenbank ab.

// wenn es Sie interessiert, wie das $_FILES Array aussieht, kommentieren Sie die folgende Zeile aus
// echo "<pre>".print_r($_FILES,1)."</pre>";

if (count($_FILES) > 0) {
$f = array_pop($_FILES);
$content = file_get_contents($f['tmp_name']); // bitte nicht ändern
Expand Down
60 changes: 48 additions & 12 deletions vote.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
session_start();
include_once "helpers.php";

require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::create(__DIR__, '.env');
Expand All @@ -16,6 +17,14 @@
}

if(isset($_POST['selected'])) {
//Store the Cantor number representing the IDs in the session.
//Since ⟨k1, k2⟩ != ⟨k2, k1⟩, we always use the smaller ID as k1. That way we avoid duplicates.
if($_POST['ID1'] < $_POST['ID2']) {
array_push($_SESSION['completed'],cantor($_POST['ID1'],$_POST['ID2']));
} else {
array_push($_SESSION['completed'],cantor($_POST['ID2'],$_POST['ID1']));
}

$query = "UPDATE logos SET matchups = matchups + 1, won_matchups = won_matchups + 1 WHERE id = ";
$query .= ($_POST['selected'] == $_POST['ID1'] ? $_POST['ID1'] : $_POST['ID2'])."; ";
$q = mysqli_query($remote,$query);
Expand All @@ -24,6 +33,44 @@
$q = mysqli_query($remote,$query);

}

if(!isset($_SESSION['count'])){
$query = "select count(id) from logos; ";
$rCount = mysqli_query($remote,$query);
$_SESSION['count'] = mysqli_fetch_assoc($rCount)['count(id)'];
//echo "queried!";
}

if(!isset($_SESSION['completed'])){
$_SESSION['completed'] = [];
}

// To ensure that no logo pair is shown twice, we use a Cantor Pairing Function to store the information about the already shown pairs.
$imageID1=0;
$imageID2=0;
$maxcount= ($_SESSION['count'] * ($_SESSION['count'] - 1) / 2); //maximum retries, redundant?

for($i = 0; $i < $maxcount; $i++) {
//get two different, random numbers
do {
$imageID1 = rand(1,$_SESSION['count']);
$imageID2 = rand(1,$_SESSION['count']);

} while ($imageID1 == $imageID2);

// Check if Cantor value is in session variable
if($imageID1 > $imageID2) {
$tmp1 = $imageID2;
$tmp2 = $imageID1;
} else {
$tmp1 = $imageID1;
$tmp2 = $imageID2;
}
if(!in_array(cantor($tmp1,$tmp2),$_SESSION['completed'])) break;
}


echo '
<html>
<head>
Expand All @@ -49,18 +96,7 @@
<div class="col-3" style="text-align: center">
';

if(!isset($_SESSION['count'])){
$query = "select count(id) from logos; ";
$rCount = mysqli_query($remote,$query);
$_SESSION['count'] = mysqli_fetch_assoc($rCount)['count(id)'];
echo "queried!";
}
$imageID1=0;
$imageID2=0;
while ($imageID1 == $imageID2) {
$imageID1 = rand(1,$_SESSION['count']);
$imageID2 = rand(1,$_SESSION['count']);
}


//image 1

Expand Down

0 comments on commit 494bb4e

Please sign in to comment.