garrensilverwing
New Member
- Messages
- 148
- Reaction score
- 0
- Points
- 0
A friend of mine has been working on this code for a ladder/ranking system for this old school game we play. It works almost perfectly but there are some logical errors. I have been going through the code, tweaking what I can to try and get rid of the logical errors temporarily but it is not working. Some errors, for example, are people having a streak of 0, which should be impossible. Another problem is people will have a record of 4 wins and 4 losses but have only played 4 games. I was wondering if anyone wanted to help me find these logic errors and fix them. My head and eyes are starting to hurt looking at this code so help would be greatly appreciated!
PHP:
<?php
function Streak($Streak,$Result)
{
if($Streak > -1 && $Result == 1)$Streak = $Streak + 1;
if($Streak < 1 && $Result == 0)$Streak = $Streak - 1;
if($Streak < 1 && $Result == 1) $Streak = 1;
if($Streak > -1 && $Result == 0) $Streak = -1;
if($Streak == 0 && $Result == 1) $Streak = 1;
if($Streak == 0 && $Result == 0) $Streak = -1;
return $Streak;
}
function insertLadderGame($GameID, $map, $Winner, $Loser, $DateT)
{
$WResult = mysql_query("select * from standings where Player LIKE '$Winner'");
$LResult = mysql_query("select * from standings where Player LIKE '$Loser'");
if (mysql_num_rows($WResult) == 0 || mysql_num_rows($LResult) == 0) //If either player is new
{
if (mysql_num_rows($WResult) == 0) //If the winner is new
{
$New=1;
$result = mysql_query("SELECT Rank, Wins, Streak FROM standings order by Rank desc limit 1") or die(mysql_error());
$row = mysql_fetch_array($result);
$HighestRank = $row['Rank'];
$WOldRank = $WNewRank = $HighestRank + 1;
$Wins = 1;
$Losses = 0;
$Streak = 1;
$Series=0;
UpdatePlayer($New,$Winner,$WNewRank,$Wins,$Losses,$Streak,$WNewRank,$Series);
}
else //If the winner is NOT new
{
$New = 0;
$Result = 1;
$row = mysql_fetch_array($WResult);
$WOldRank = $WNewRank = $row['Rank'];
$Wins = $row['Wins'] + 1;
$Losses = $row['Losses'];
$WStreak = $row['Streak'];
$WStreak = Streak($WStreak,$Result);
$HighRank = $row['HighRank'];
$Series = $row['series'];
if($WOldRank < $HighRank) $HighRank = $WOldRank;
UpdatePlayer($New,$Winner,$WNewRank,$Wins,$Losses,$WStreak,$HighRank,$Series);
}
if (mysql_num_rows($LResult) == 0) //If the loser is new
{
$New = 1;
$result = mysql_query("SELECT Rank, Wins, Streak FROM standings order by Rank desc limit 1") or die(mysql_error());
$row = mysql_fetch_array($result);
$HighestRank = $row['Rank'];
$LOldRank = $LNewRank = $HighestRank + 1;
$Wins = 0;
$Losses = 1;
$Streak = -1;
$Series=0;
UpdatePlayer($New,$Loser,$LNewRank,$Wins,$Losses,$Streak,$LNewRank,$Series);
}
else //If the loser is NOT new
{
$New = 0;
$Result = 0;
$row = mysql_fetch_array($LResult);
$LOldRank = $LNewRank = $row['Rank'];
$Wins = $row['Wins'];
$Losses = $row['Losses'] + 1;
$LStreak = $row['Streak'];
$LStreak = Streak($LStreak,$Result);
$HighRank = $row['HighRank'];
$Series = $row['series'];
UpdatePlayer($New,$Winner,$WNewRank,$Wins,$Losses,$WStreak,$HighRank,$Series);
}
mysql_query("INSERT INTO checkedgames (GameID,Winner,Loser,WOldRank,WNewRank,LOldRank,LNewRank,Map,DateT)
VALUES('$GameID', '$Winner','$Loser','$WOldRank','$WNewRank','$LOldRank','$LNewRank','$map','$DateT' );") or die(mysql_error());
return 0;
}
else
{
// If neither Player is new
$New = 0;
//Winner
$row = mysql_fetch_array($WResult);
$Result = 1;
$WHighRank = $row['HighRank'];
$WOldRank = $row['Rank'];
$WWins = $row['Wins'] + 1;
$WLosses = $row['Losses'];
$WStreak = Streak($row['Streak'],$Result);
$WSeries = $row['series'];
//Loser
$row = mysql_fetch_array($LResult);
$Result = 0;
$LOldRank = $row['Rank'];
$LHighRank = $row['HighRank'];
$LWins = $row['Wins'];
$LLosses = $row['Losses'] + 1;
$LStreak = Streak($row['Streak'],$Result);
$LSeries = $row['series'];
if ($LOldRank + $WOldRank == 3) $WSeries = $WSeries + 1;
if ($WSeries > 0)
{
if ($WSeries == 3)
{
mysql_query("UPDATE standings SET Rank=Rank-1 WHERE Rank BETWEEN 3 AND 10");
$SeriesLoser = $Loser;
$WSeries = 0;
$LSeries = 0;
}
else
{
$WNewRank = $WOldRank;
$LNewRank = $LOldRank;
}
}
if ($WOldRank > $LOldRank) //If the winner has a lower standing than the loser
{
if($WSeries == 0)
{
$WNewRank = (int)(($WOldRank + $LOldRank)/2);
if ($WNewRank == 2)
{
$LSeries = 0;
mysql_query("UPDATE standings SET series='0' WHERE Rank=1") or die(mysql_error());
}
mysql_query("UPDATE standings SET Rank=Rank+1 WHERE Rank BETWEEN $WNewRank AND $WOldRank-1") or die(mysql_error());
}
}
else
{
$WNewRank = $WOldRank;
$LNewRank = $LOldRank;
}
if ($WNewRank < $HighRank) $HighRank = $WNewRank;
if ($WNewRank == $LOldRank) $LNewRank = $LOldRank + 1;
else $LNewRank = $LOldRank;
UpdatePlayer($New,$Winner,$WNewRank,$WWins,$WLosses,$WStreak,$WHighRank,$WSeries);
UpdatePlayer($New,$Loser,$LNewRank,$LWins,$LLosses,$LStreak,$LHighRank,$LSeries);
if($SeriesLoser)
{
$LNewRank = 10;
mysql_query("UPDATE standings SET Rank = $LNewRank WHERE Player LIKE '$SeriesLoser'") or die(mysql_error());
}
$result = mysql_query("SELECT Rank, HighRank, Player FROM standings") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$Rank = $row['Rank'];
$HighRank = $row['HighRank'];
$Player = $row['Player'];
if($Rank < $HighRank)
{
mysql_query("UPDATE standings SET HighRank=$Rank WHERE Player LIKE '$Player'") or die(mysql_error());
}
}
mysql_query("INSERT INTO checkedgames (GameID,Loser,Winner,WOldRank,WNewRank,LOldRank,LNewRank,map,DateT) VALUES('$GameID', '$Loser','$Winner','$WOldRank','$WNewRank','$LOldRank','$LNewRank','$map','$DateT');") or die(mysql_error());
return 0;
}
}
function UpdatePlayer($New,$Player,$Rank,$Wins,$Losses,$Streak,$HighRank,$Series)
{
if($New == 1)
{
mysql_query("INSERT INTO standings (Player,Rank,Wins,Losses,Streak,HighRank,series) VALUES('$Player','$Rank','$Wins','$Losses','$Streak','$HighRank','$Series');") or die(mysql_error());
}
else
{
mysql_query("UPDATE standings SET Rank='$Rank', Wins='$Wins', Losses='$Losses', Streak='$Streak', HighRank='$HighRank', series='$Series' where Player LIKE '$Player'") or die(mysql_error());
}
}