Last active
August 29, 2015 13:56
-
-
Save paranoidsp/9182074 to your computer and use it in GitHub Desktop.
PHP code for the ELO rating system used in Saarang Select (saarang.iitm.ac.in/saarang_select/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| session_start(); | |
| ?> | |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
| <?php | |
| //DB connection | |
| $con = mysql_connect("localhost","saarango","password here"); | |
| if(!$con) | |
| { | |
| die('Could not connect:' . mysql_error()); | |
| } | |
| mysql_select_db("saarango_audiomash",$con); | |
| function getUserRank($song_id){ | |
| // query to get all result | |
| $sql = "SELECT * FROM songs ORDER BY rate DESC"; | |
| $result = mysql_query($sql); | |
| $rows = ''; | |
| $data = array(); | |
| $rows = mysql_num_rows($result); | |
| while ($rows = mysql_fetch_assoc($result)){ | |
| $data[] = $rows; | |
| } | |
| $rank = 1; | |
| foreach($data as $item) | |
| { | |
| if ($item['id'] == $song_id) | |
| { | |
| return $rank; | |
| } | |
| $rank=$rank+1; | |
| } | |
| return 1; | |
| } | |
| $_SESSION['viewsss']++; | |
| //echo $_SESSION['viewsss'] . "::"; | |
| if(isset($_SESSION['songa'])) | |
| { | |
| $lastSong[0] = (int)$_SESSION['songa']; | |
| $lastSong[1] = (int)$_SESSION['songb']; | |
| } | |
| //echo $lastSong[0] . ":" . $lastSong[1] . "::"; | |
| selectSong: | |
| $qsong = "SELECT id FROM songs ORDER BY RAND() LIMIT 1"; | |
| $songQ[0] = mysql_query($qsong); | |
| $songQ[1] = mysql_query($qsong); | |
| while($row = mysql_fetch_array($songQ[0])) | |
| { | |
| $song[0] =mysql_real_escape_string($row['id']); | |
| } | |
| while($row = mysql_fetch_array($songQ[1])) | |
| { | |
| $song[1] = mysql_real_escape_string($row['id']); | |
| } | |
| if($song[0] == $song[1]) | |
| goto selectSong; | |
| $result[0] = mysql_query("SELECT * FROM songs WHERE id='" . $song[0] . "'"); | |
| $result[1] = mysql_query("SELECT * FROM songs WHERE id='" . $song[1] . "'"); | |
| while($row = mysql_fetch_array($result[0])) | |
| { | |
| $id[0] = $row['id']; | |
| $name[0] = $row['name']; | |
| $url[0] = $row['url']; | |
| $Raten[0] = $row['rate']; | |
| $band[0] = $row['band']; | |
| $image[0]=$row['image']; | |
| } | |
| while($row = mysql_fetch_array($result[1])) | |
| { | |
| $id[1] = $row['id']; | |
| $name[1] = $row['name']; | |
| $url[1] = $row['url']; | |
| $Raten[1] = $row['rate']; | |
| $band[1] = $row['band']; | |
| $image[1]=$row['image']; | |
| } | |
| if($_POST['bucketlist'] != "") | |
| { | |
| //updating bucketlist if not null | |
| $listq = "INSERT INTO bucketlist (comments) VALUES ('" . $_POST['bucketlist'] . "') "; | |
| mysql_query($listq); | |
| } | |
| $use=2; | |
| if(isset($_POST['left'])) | |
| { | |
| $selected =0; | |
| $use=3; | |
| } | |
| else if(isset($_POST['right'])) | |
| { | |
| $selected=1; | |
| $use=3; | |
| } | |
| if($use==3) | |
| { | |
| $qRate[0] = "SELECT rate FROM songs WHERE id='" . $lastSong[0] . "'" ; | |
| $qRate[1] = "SELECT rate FROM songs WHERE id='" . $lastSong[1] . "'" ; | |
| $Rateq[0] = mysql_query($qRate[0]); | |
| $Rateq[1] = mysql_query($qRate[1]); | |
| while($row = mysql_fetch_array($Rateq[0])) | |
| { | |
| $Rate[0] = $row['rate']; | |
| } | |
| while($row = mysql_fetch_array($Rateq[1])) | |
| { | |
| $Rate[1] = $row['rate']; | |
| } | |
| $Expected[0] = 1/(1+pow(10,($Rate[1]-$Rate[0])/400)); | |
| $Expected[1] = 1-$Expected[0]; | |
| //echo "[" . $Expected[0] . "," . $Expected[1] . "]"; | |
| $k=30; | |
| //echo "[" . $Rate[(int)$selected] . "," . $Rate[(int)!$selected] . "]"; | |
| $Rate[(int)$selected] = $Rate[(int)$selected] + $k*(1-(float)$Expected[(int)$selected]); //changing rate of the winner | |
| $Rate[(int)!$selected] = $Rate[(int)!$selected] + $k*(0-(float)$Expected[(int)!$selected]); | |
| //echo "[" . $Rate[(int)$selected] . "," . $Rate[(int)!$selected] . "]"; | |
| //var_dump($Rate['$selected'],$selected); | |
| //var_dump($lastSong,$selected); | |
| //$RateNew = changeRate($lastSong,$selected); | |
| //var_dump($RateNew,$selected); | |
| //dbUpdate($RateNew,$selected); | |
| $qUpdate[(int)$selected] = "UPDATE songs SET rate='" . $Rate[(int)$selected] . "' WHERE id='" . $lastSong[(int)$selected] . "'"; | |
| $qUpdate[(int)!$selected] = "UPDATE songs SET rate='" . $Rate[(int)!$selected] . "' WHERE id='" . $lastSong[(int)!$selected] . "'"; | |
| for($i=0;$i<2;$i=$i+1) | |
| if(!mysql_query($qUpdate[$i])) | |
| die("Could not connect to database" . mysql_error()); | |
| } | |
| $_SESSION['songa'] = $song[0]; | |
| $_SESSION['songb'] = $song[1]; | |
| //var_dump($song,$_SESSION['songa'],$_SESSION['songb']); | |
| ?> | |
| <!-- HTML Code --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment