Jump to content

PHP how do I create this in PHP?

Joveice

So this site http://www.3dkingdoms.com/chess/elo.htm has this elo change calculator which is this

function CalculateRatingChange()

{

var Elo1 = document.rating.elo1.value * 1;

var Elo2 = document.rating.elo2.value * 1;

var K = document.rating.K.value * 1;

var EloDifference = Elo2 - Elo1;

var percentage = 1 / ( 1 + Math.pow( 10, EloDifference / 400 ) );

var win = Math.round( K * ( 1 - percentage ) );

var draw = Math.round( K * ( .5 - percentage ) );

if (win > 0 ) win = "+" + win;

if (draw > 0 ) draw = "+" + draw;

document.ratingchange.win.value = win;

document.ratingchange.draw.value = draw;

document.ratingchange.loss.value = Math.round( K * ( 0 - percentage ) );

document.ratingchange.percent.value =  Math.round( percentage * 100 ) + "%";

}

How can I create this in php? I don't need the form part of it, just the calulation part since it's gonna be used on the backend anyways, so just a simple php way to make this work where there is $elo1 $elo2 and $k

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Joveice said:

So this site http://www.3dkingdoms.com/chess/elo.htm has this elo change calculator which is this


function CalculateRatingChange()

{

var Elo1 = document.rating.elo1.value * 1;

var Elo2 = document.rating.elo2.value * 1;

var K = document.rating.K.value * 1;

var EloDifference = Elo2 - Elo1;

var percentage = 1 / ( 1 + Math.pow( 10, EloDifference / 400 ) );

var win = Math.round( K * ( 1 - percentage ) );

var draw = Math.round( K * ( .5 - percentage ) );

if (win > 0 ) win = "+" + win;

if (draw > 0 ) draw = "+" + draw;

document.ratingchange.win.value = win;

document.ratingchange.draw.value = draw;

document.ratingchange.loss.value = Math.round( K * ( 0 - percentage ) );

document.ratingchange.percent.value =  Math.round( percentage * 100 ) + "%";

}

How can I create this in php? I don't need the form part of it, just the calulation part since it's gonna be used on the backend anyways, so just a simple php way to make this work where there is $elo1 $elo2 and $k

1. Change anything with var to $:

var Elo1 becomes $Elo1

2. Figure out what the values represent

document.rating.elo1.value probably would become $elo1

3. Change function names

Math.pow() becomes pow(), Math.round() becomes round()

4. Whenever referencing variables, make sure to prepend $

if (win > 0) becomes if ($win > 0)

 

That should be all that's really necessary, then echo it onto the page or whatever.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, dannytech357 said:

1. Change anything with var to $:

var Elo1 becomes $Elo1

2. Figure out what the values represent

document.rating.elo1.value probably would become $elo1

3. Change function names

Math.pow() becomes pow(), Math.round() becomes round()

4. Whenever referencing variables, make sure to prepend $

if (win > 0) becomes if ($win > 0)

 

That should be all that's really necessary, then echo it onto the page or whatever.

Sounds straightforward, thanks.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

This is what I'v got so far 

<?php
$Elo1 = 2000;
$Elo2 = 2000;
$K = 32;
$EloDifference = $Elo2 - $Elo1;
$percentage = 1 / 1 + pow(10, $EloDifference / 400);
$win = round($K * 1 - $percentage);
$draw = round($K * .5 - $percentage);
if ($win > 0) {
$win = "+" + $win;
}if ($draw > 0) {
$draw = "+" + $draw;
}
echo $win;
echo '<br>';
echo $draw;
echo '<br>';
echo round($K * 0 - $percentage);
echo '<br>';
echo round($percentage * 100) + "%";



?>

Tho it returns the wrong values, when elo1 and elo2 is 2000 and K is 32 it should return win: +16 and draw = 0, and loss -16. this is not the case.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

php is case sensitive, make all variables lowercase so you won't get errors due to wrong variables.

use "."  to append two strings, not "+"  ...  not sure what you want with  $win = "+" + $win; and the other

add new lines after }  etc

 

$percentage = .. that line is wrong , you forgot the brackets .. ex 1 / (  1 +  ...  ) ;   as it is right now you have 1/1  +  ... which equals 1 + pow etc

brackets again for those round ( $K *  .5 +  )  .. round ($K * ( 0.5 +  ) )  ...   use 0.5 not .5

round ($k * 0 - $percentage )  ... will basically always do $k * 0 = 0  , so the result is round ( - $percentage)   ... the  ( ) are important, they establish order of computation just like with regular math.

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, mariushm said:

php is case sensitive, make all variables lowercase so you won't get errors due to wrong variables.

use "."  to append two strings, not "+"  ...  not sure what you want with  $win = "+" + $win; and the other

add new lines after }  etc

 

$percentage = .. that line is wrong , you forgot the brackets .. ex 1 / (  1 +  ...  ) ;   as it is right now you have 1/1  +  ... which equals 1 + pow etc

brackets again for those round ( $K *  .5 +  )  .. round ($K * ( 0.5 +  ) )  ...   use 0.5 not .5

round ($k * 0 - $percentage )  ... will basically always do $k * 0 = 0  , so the result is round ( - $percentage)   ... the  ( ) are important, they establish order of computation just like with regular math.

 

 

 

I just tryed to copy the thing so not sure what most of it does :P

<?php
$elo1 = 2000;
$elo2 = 2000;
$k = 32;
$elodifference = ($elo2 - $elo1);
$percentage = ((1 / 1) + pow(10, ($elodifference / 400)));
$win = round($k * (1 - $percentage));
$draw = round($k * (0.5 - $percentage));
if ($win > 0) {
$win = "+".$win;
}if ($draw > 0) {
$draw = "+".$draw;
}
echo $win;
echo '<br>';
echo $draw;
echo '<br>';
echo round($k * (0 - $percentage));
echo '<br>';
echo round($percentage * 100)."%";



?>

This returns 

-32
-48
-64
200%

Which should be, +16, 0, -16, 50%

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

why do server side? It isn't a function you need to keep private then you should move it client side to lower server load.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Jesus dude, I say one thing and you do another...

Does it make sense to you to have  (1/1)  ? If you open calculator and enter there 1/1 what's the result?  Do you think the original formula wanted you to do a pointless division by 1 ?

 

anyway, here's what the php page should probably be .. including a basic form to play around with

 

<html>
<form name="rating" method="post" >
elo1 <input name="elo1" type="text" value="0" /> <br/>
elo2 <input name="elo2" type="text" value="0" /> <br/>
k <input name="k" type="text" value="32" /> <br/>
<input name="submit" type="submit" value="Calculate" /> 
<input name="submitted" type="hidden" value="1" />
</form>

<?php

if ($_POST['submitted']=='1') { // user pressed calculate and data was posted to server
// make sure what user posts is only 0..9 
if (ctype_digit($_POST['elo1']) == FALSE) die('elo1 is not an integer number');
if (ctype_digit($_POST['elo2']) == FALSE) die('elo2 is not an integer number');
if (ctype_digit($_POST['k']) == FALSE) die('k is not an integer number');

$elo1 = intval($_POST['elo1']); // convert the texts from the form to actual integer variables
$elo2 = intval($_POST['elo2']); 
$k = intval($_POST['k']);

$elo_diff = $elo2-$elo1;

$percentage = 1 / ( 1 + pow(10,($elo_diff / 400)));
$win = round($k * (1-$percentage));
$draw = round ($k * ( 0.5 - $percentage));
$loss = round ($k * ( 0 - $percentage));

$pvalue = round ($percentage * 100);

echo 'Win : ' . $win . '<br/>';
echo 'Draw: ' . $draw . '<br/>';
echo 'Loss: '. $loss . '<br/>';
echo 'Percentage: '. $pvalue .'% <br/>';

}

?>
</html>

 

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, mariushm said:

Jesus dude, I say one thing and you do another...

Does it make sense to you to have  (1/1)  ? If you open calculator and enter there 1/1 what's the result?  Do you think the original formula wanted you to do a pointless division by 1 ?

 

anyway, here's what the php page should probably be .. including a basic form to play around with

 


<html>
<form name="rating" method="post" >
elo1 <input name="elo1" type="text" value="0" /> <br/>
elo2 <input name="elo2" type="text" value="0" /> <br/>
k <input name="k" type="text" value="32" /> <br/>
<input name="submit" type="submit" value="Calculate" /> 
<input name="submitted" type="hidden" value="1" />
</form>

<?php

if ($_POST['submitted']=='1') { // user pressed calculate and data was posted to server
// make sure what user posts is only 0..9 
if (ctype_digit($_POST['elo1']) == FALSE) die('elo1 is not an integer number');
if (ctype_digit($_POST['elo2']) == FALSE) die('elo2 is not an integer number');
if (ctype_digit($_POST['k']) == FALSE) die('k is not an integer number');

$elo1 = intval($_POST['elo1']); // convert the texts from the form to actual integer variables
$elo2 = intval($_POST['elo2']); 
$k = intval($_POST['k']);

$elo_diff = $elo2-$elo1;

$percentage = 1 / ( 1 + pow(10,($elo_diff / 400)));
$win = round($k * (1-$percentage));
$draw = round ($k * ( 0.5 - $percentage));
$loss = round ($k * ( 0 - $percentage));

$pvalue = round ($percentage * 100);

echo 'Win : ' . $win . '<br/>';
echo 'Draw: ' . $draw . '<br/>';
echo 'Loss: '. $loss . '<br/>';
echo 'Percentage: '. $pvalue .'% <br/>';

}

?>
</html>

 

You know you can just give the button a name and check if it posted, then you don't need the hidden input.

 

I still think a client side version is the way to go. You could even have sliders the change the output in real time.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×