Ejemplo n.º 1
0
<?php

define("TOURNAMENT_FILE", "./players.txt");
define("NUMBEROFACCEPTEDPLAYERS", 16);
date_default_timezone_set("UTC");
if (!file_exists(TOURNAMENT_FILE)) {
    save_tournament(array("players" => array(), "waiters" => array()));
}
function read_tournament()
{
    $filecontent = file_get_contents(TOURNAMENT_FILE);
    return json_decode($filecontent, true);
}
function save_tournament($tournament)
{
    file_put_contents(TOURNAMENT_FILE, json_encode($tournament, JSON_UNESCAPED_UNICODE));
}
Ejemplo n.º 2
0
    function saveRanking($game_id,$action,$playersColor,$flagFall=0){
        GLOBAL $CFG_MIN_ROUNDS,$CFG_LOG_PATH,$CFG_LOG_DEBUG,$CFG_ENABLE_TRIAL_RATING;

          if ($playersColor == "white")
            $opponentColor = "black";
        else
            $opponentColor = "white";

        $p = mysql_query("SELECT count(*) FROM history WHERE game_id='$game_id'");
        $row = mysql_fetch_array($p);
        $xRounds = ceil($row[0]/2);

        $p = mysql_query("SELECT white_player,black_player,dateCreated,ratingWhite,ratingBlack,ratingBlackM,ratingWhiteM,oficial,PVBlack,PVWhite,status,teamMatch FROM games WHERE game_id='$game_id'");
        $row = mysql_fetch_array($p);
        $xDateCreated = $row[2];
        $official = $row['oficial'];
        $Match = $row['teamMatch'];

        if ($row[status] != "")
            displayError("This game was already finished!","mainmenu.php");

        if ($playersColor == "white"){
            $xplayerID = $row[0];
            $xopponentID = $row[1];
            $white_player=$row[0];
            $black_player=$row[1];
            if ($row['PVWhite'] >=0 &&  $row['PVBlack'] >=0){
                $PVplayer = $row['PVWhite'];
                $PVopponent = $row['PVBlack'];
            }
            else{
                $PVplayer = getPV($row[0]);
                $PVopponent = getPV($row[1]);
            }
            if ($row['ratingWhite'] >0 &&  $row['ratingWhiteM'] >0 && $row['ratingBlack'] >0 && $row['ratingBlackM'] >0){
                $xplayerRating = $row['ratingWhite'];
                //$xplayerRating = round((($row['ratingWhite'] + getRating($xplayerID))/2),4);
                //$xplayerRatingM = $row['ratingBlackM'];
                $xopponentRating = $row['ratingBlack'];
                //$xopponentRating = round((($row['ratingBlack'] + getRating($xopponentID))/2),4);
                //$xopponentRatingM = $row['ratingWhiteM'];
           }else{
                $xplayerRating = getRating($xplayerID);
                //$xplayerRatingM = getRatingMonth($xplayerID);
                $xopponentRating = getRating($xopponentID);
                //$xopponentRatingM = getRatingMonth($xopponentID);
            }
        }else{
            $xopponentID = $row[0];
            $xplayerID = $row[1];
            $white_player=$row[1];
            $black_player=$row[0];
            if ($row['PVWhite'] >=0 &&  $row['PVBlack'] >=0){
                $PVplayer = $row['PVBlack'];
                $PVopponent = $row['PVWhite'];
            }
            else{
                $PVplayer = getPV($row[1]);
                $PVopponent = getPV($row[0]);
            }
            if ($row['ratingWhite'] >0 &&  $row['ratingWhiteM'] >0 && $row['ratingBlack'] >0 && $row['ratingBlackM'] >0){
                $xplayerRating = $row['ratingBlack'];
                //$xplayerRating = round((($row['ratingBlack'] + getRating($xplayerID))/2),4);
                //$xplayerRatingM = $row['ratingBlackM'];
                $xopponentRating = $row['ratingWhite'];
                //$xopponentRating = round((($row['ratingWhite'] + getRating($xopponentID))/2),4);
                //$xopponentRatingM = $row['ratingWhiteM'];
            }else{
                $xplayerRating = getRating($xplayerID);
                //$xplayerRatingM = getRatingMonth($xplayerID);
                $xopponentRating = getRating($xopponentID);
                //$xopponentRatingM = getRatingMonth($xopponentID);
           }
        }

        mysql_query("delete from {$db_prefix}messages where game_id=".$game_id);
        mysql_query("delete from chat where game_id=".$game_id);

        if ($action == "draw"){
            $pointsw = 1;
            $pointsl = 1;
            $sum = 15;
            $message = "draw";

            $winner = $xplayerID;
            $loser = $xopponentID;
            $winnerRating = $xplayerRating;
            $loserRating = $xopponentRating;
            $PVG = $PVplayer;
            $PVP = $PVopponent;

        }else if($action == "resign"){
            $pointsw = 2;
            $pointsl = 0;
            $sum = 30;
            $message = "playerResigned";
            $winner = $xopponentID;
            $loser = $xplayerID;
            $winnerRating = $xopponentRating;
            $loserRating = $xplayerRating;
            $PVG = $PVopponent;
            $PVP = $PVplayer;
        }else if($action == "checkmate"){
            $pointsw = 2;
            $pointsl = 0;
            $sum = 30;
            $message = "checkMate";
            $winner = $xplayerID;
            $loser = $xopponentID;
            $winnerRating = $xplayerRating;
            $loserRating = $xopponentRating;
            $PVG = $PVplayer;
            $PVP = $PVopponent;
        }

        if($official == "1" && ($xRounds>=$CFG_MIN_ROUNDS || $action == "checkmate")){

            $RatingG = getRating($winner);
            $RatingP = getRating($loser);
            ////////////////////////////////////////////////////////
            // Calculating the new Ratings
            // New Code by Helge Dietert ( winterfrost ) with help from Tilko Dietert ( tilko )
            // Implemented after ELO http://www.schachklub-hietzing.at/elosystem.html
            // Assume that every player has a rating, because every new player has a
            // initial rating of 1500. This behaviour does not match the official rules.
            // Perhaps set no rating as 0 and modify code.
            // Because this is an adaptive system the minimum condition must be reduced to 1.
            // New variables are prefixed with rh
            ////////////////////////////////////////////////////////

            // Number of games
        $rhStatsG = getStatsUser($winner,1);
            $rhGNumberGames = $rhStatsG[0]+$rhStatsG[1]+$rhStatsG[2];
        $rhStatsP = getStatsUser($loser,1);
            $rhPNumberGames = $rhStatsP[0]+$rhStatsP[1]+$rhStatsP[2];

            // Factors for the winner and loser
            $rhGFactor = ( (double) 3400 - $RatingG  ) * ( (double) 3400 - $RatingG  ) / 100000.0;
            $rhPFactor = ( (double) 3400 - $RatingP  ) * ( (double) 3400 - $RatingP  ) / 100000.0;
            // Reduce factor ?
            //if ($rhGNumberGames > 850.0 / $rhGFactor)
            //$rhGFactor = 850.0 / $rhGNumberGames;
            //if ($rhPNumberGames > 850.0 / $rhPFactor)
            //$rhPFactor = 850.0 / $rhPNumberGames;

            // Difference of Ratings
            $rhDiffGP = $RatingG-$RatingP;

            // Expectation of Result
            $rhExG  = 0.5
                + 0.001 * $rhDiffGP * ( 1.4217 - 0.001 * abs($rhDiffGP) * ( 0.24336
                    + 0.001 * abs($rhDiffGP) * ( 2.514 - abs($rhDiffGP) * 0.001991)));
            if ( $rhExG > 1 ) $rhExG = 1; // Maximal Result is 1
            if ( $rhExG < 0 ) $rhExG = 0; // Minimal Result is 0
            $rhExP = 1 - $rhExG;

            // real Result
            $rhReG = 1;
            $rhReP = 0;
            if ( $action == "draw" )
            {
              $rhReG = 0.5;
              $rhReP = 0.5;
            }

            // Set xpw ( number of won points ) ans xpl ( number of lost points )
            // xpl is the number of lost point, so if he lost points the value must be positiv
            $xpw = floor( ( $rhReG - $rhExG ) * $rhGFactor + 0.5 );
            $xpl = floor( ( $rhReP - $rhExP ) * $rhPFactor + 0.5 );

            // New Ratings
        $NewRatingG = $RatingG + $xpw;
            if ($action == "draw" )
            {
                 $NewRatingP = $RatingP + $xpl;
            }
            else
            {
                 $xpl = $xpl * (-1); // Because if he'll lose points this value is already negativ
                 $NewRatingP = $RatingP - $xpl;
            }

            // Minimum points at 900
            if ( $NewRatingP < 900 )
            {
                 $NewRatingP = 900;
                 $xpl = $RatingP - 900;
            }

            /////////////////////////////////////////////////////////////////
            // End of new rating-code
            /////////////////////////////////////////////////////////////////

            /* Old Version

            $RatingG = getRating($winner);
            $RatingP = getRating($loser);
            $dif1 = $loserRating-$winnerRating;
            $dif2 = $winnerRating-$loserRating;

            if ($dif1>300)$dif1=300;
            if ($dif1<-300)$dif1=-300;
            if ($dif2>300)$dif2=300;
            if ($dif2<-300)$dif2=-300;

            if ($action == "draw")
                $FOG = ($PVG+$dif2)/10;
            else
                $FOG = ($PVG+$dif1)/10;
            $FOP = ($PVP+$dif1)/10;

            $xpw = floor(($FOP+$sum)/2);
            $xpl = floor(($FOG+$sum)/2);

            if ($action == "draw"){
                if ($xpw<0)$xpw=0;
                if ($xpl<0)$xpl=0;
            }else{
                if ($xpw == 0)$xpw=1;
                if ($xpl == 0)$xpl=1;
            }

            $NewRatingG = $RatingG + $xpw;
            if ($action == "draw")
                $NewRatingP = $RatingP + $xpl;
            else
                $NewRatingP = $RatingP - $xpl;

            if ($NewRatingP < 900){
                $xpl = $RatingP - 900;
                $NewRatingP = 900;
            }
            if ($NewRatingG < 900){
                $xpw = $RatingG - 900;
                $NewRatingG = 900;
            }*/

            # Monthly
            $NewRatingGm = $NewRatingG;
            $NewRatingPm = $NewRatingP;

         /*
            $RatingGm = getRatingMonth($winner);
            $RatingPm = getRatingMonth($loser);
            $dif1m = $loserRatingM-$winnerRatingM;
            $dif2m = $winnerRatingM-$loserRatingM;
            if ($dif1m>300)$dif1m=300;
            if ($dif1m<-300)$dif1m=-300;
            if ($dif2m>300)$dif2m=300;
            if ($dif2m<-300)$dif2m=-300;
            if ($action == "draw")
                $FOGm = ($PVG+$dif2m)/10;
                    else
                $FOGm = ($PVG+$dif1m)/10;
                $FOPm = ($PVP+$dif1m)/10;
            $xpwm = floor(($FOPm+$sum)/2);
            $xplm = floor(($FOGm+$sum)/2);
            $NewRatingGm = $RatingGm + $xpwm;
        if ($action == "draw")
                $NewRatingPm = $RatingPm + $xplm;
            else
                $NewRatingPm = $RatingPm - $xplm;

            if ($NewRatingPm < 1300)
                $xplm = $RatingPm - 1300;
            if ($NewRatingGm < 1300)
                $xpwm = $RatingGm - 1300;

        */

            $maxG = getMaxRating($winner);
            $maxP = getMaxRating($loser);
            if ($NewRatingG > $maxG)
               $maxG = $NewRatingG;
            if ($NewRatingP > $maxP)
               $maxP = $NewRatingP;

            if ($NewRatingG < 900 || $NewRatingP < 900 || $xpw < -100 || $xpw > 100 || $xpl < -100 || $xpl > 100){
                $msg = "Fatal Error at chessdb!<BR><BR>";
                $msg .= "
                Game ID: $game_id<BR>
                Winner ID: $winner<BR>
                Loser ID: $loser<BR>
                Winner Rating: $RatingG<BR>
                Loser Rating: $RatingP<BR>
                X Winner Rating:: $winnerRating<BR>
                X Loser Rating: $loserRating<BR>
                Winner's % Victories: $PVG<BR>
                Loser's % Victories: $PVP<BR>
                DIF1: $dif1<BR>
                DIF2: $dif2<BR>

                FOG: $FOG<BR>
                FOP: $FOP<BR>
                Winner's new Rating: $NewRatingG<BR>
                Loser's new Rating: $NewRatingP<BR>
                Winner's earned: $xpw<BR>
                Loser's lost: $xpl<BR><BR>
                ";
                $log = "\"Faltal Error\" \"Game: $game_id\" \"Winner: $winner/$xpw/$NewRatingG\" \"Loser: $loser/$xpl/$NewRatingP\"";
                if ($CFG_LOG_PATH =! "" && $CFG_LOG_DEBUG)
                       grava_log("webchess.log",$log,$CFG_LOG_PATH);

                displayError($msg,"mainmenu.php");
            }

            mysql_query("UPDATE {$db_prefix}players SET pontos=pontos+$pointsw WHERE playerID='$winner'");
            mysql_query("UPDATE {$db_prefix}players SET pontos=pontos+$pointsl WHERE playerID='$loser'");

    //If the player has a team, save the points to the team:
   $tpt = mysql_query("SELECT * FROM games WHERE team > '0' AND ((white_player = '$winner' OR black_player = '$winner') AND (white_player = '$loser' OR black_player = '$loser')) AND status = ''");
   $t2 = mysql_query("SELECT * FROM games WHERE teamMatch > '0' AND ((white_player = '$winner' OR black_player = '$winner') AND (white_player = '$loser' OR black_player = '$loser')) AND status = ''");

    if (mysql_num_rows($t2) >0 && mysql_num_rows($tpt) ==0){
        $t3 = mysql_query("select * from team_members where fk_player='$winner' and level>'0'");
            if (mysql_num_rows($t3) >0){
               $rt = mysql_fetch_array($t);
           mysql_query("UPDATE team set points=points+$pointsw WHERE teamID='$rt[fk_team]'");
        }
        }

   if (mysql_num_rows($tpt) >= 1) {
   $gt = mysql_fetch_array($tpt);

            $t = mysql_query("select * from team_members where fk_player='$winner' and level>'0'");
            if (mysql_num_rows($t) >0){
               $rt = mysql_fetch_array($t);

               mysql_query("UPDATE team set points=points+$pointsw WHERE teamID='$rt[fk_team]'");

               mysql_query("UPDATE matches set match_points1=match_points1+$pointsw where team1='$rt[fk_team]' and match_id='$gt[team]' and status=''");
               //mysql_query("UPDATE matches set match_points1=match_points1+$pointsw where team1='$rt[fk_team]' and match_id='$gt[team]' and status=''");

               mysql_query("UPDATE matches set match_points2=match_points2+$pointsw where team2='$rt[fk_team]' and match_id='$gt[team]' and status=''");
               //mysql_query("UPDATE matches set match_points2=match_points2+$pointsw where team2='$rt[fk_team]' and match_id='$gt[team]' and status=''");

      }

   }

            mysql_query("UPDATE {$db_prefix}players SET rating_max='$maxG',rating_month=$NewRatingGm,rating='$NewRatingG' WHERE playerID='$winner'")
            or die("Update Error");
            mysql_query("UPDATE {$db_prefix}players SET rating_max='$maxP',rating_month=$NewRatingPm,rating='$NewRatingP' WHERE playerID='$loser'")
            or die("Update Error");
            mysql_query("UPDATE games SET flagFall='$flagFall', status = '$message', message_from = '".$playersColor."', xpl=$xpl, xpw=$xpw  WHERE game_id = ".$game_id)
            or die("Update Error");;

            $log = "\"Saving Ranking\" \"Game: $game_id\" \"Winner: $winner/$xpw/$NewRatingG\" \"Loser: $loser/$xpl/$NewRatingP\"";
            if ($CFG_LOG_PATH =! "" && $CFG_LOG_DEBUG)
               grava_log("webchess.log",$log,$CFG_LOG_PATH);

            }

        else{
            mysql_query("UPDATE games SET flagFall='$flagFall', status = '$message', message_from = '".$playersColor."' WHERE game_id = ".$game_id);


            if ($CFG_ENABLE_TRIAL_RATING){
                /*
                ## Rating Trial Validation System ##
                1. Check if the user has in rating trial:
                2. Count the number of games
                3. Know the number of victories
                4. Check if has at least 5 games
                5. Give the rating
                */

                if (getRating($xplayerID) == 0){
                    $player = $xplayerID;
                    $stats = getStatsUser($player,0);
                    $total = $stats[0]+$stats[1]+$stats[2];
                    if ($total >=5){
                        $initRating = 1300;
                        $newRating = $initRating + ($stats[0]*50);
                        mysql_query("UPDATE {$db_prefix}players SET rating_max='$newRating',rating_month=$newRating,rating='$newRating' WHERE playerID='$player'");
                    }
                }

                if (getRating($xopponentID) == 0){
                    $player = $xopponentID;
                    $stats = getStatsUser($player,0);
                    $total = $stats[0]+$stats[1]+$stats[2];
                    if ($total >=5){
                        $initRating = 1300;
                        $newRating = $initRating + ($stats[0]*50);
                        mysql_query("UPDATE {$db_prefix}players SET rating_max='$newRating',rating_month=$newRating,rating='$newRating' WHERE playerID='$player'");
                    }
                }
                //echo "$player: T:$total V:$stats[0] R:$newRating<BR>";
            }

        }

                 // NEW!!!

                 save_tournament($game_id, $winner);
                 save_match($game_id);
    }