PHP Chess Rank Algorithm from Facebook Movie
Last Updated on Wednesday, 28 December 2011 08:37pm Written by spunky Wednesday, 28 December 2011 08:29pm
Following on from the previous post, here is the algorithim used for ranking chess players as seen on The Social Network when Mark Zuckerberg is developing Facemash.
The algorithm shown in the movie looked a bit like this:
- Ea = 1 / (1 + 10 ^ ((Rb-Ra)/400))
- Eb = 1 / (1 + 10 ^ ((Ra-Rb)/400))
Here is something a bit more practical in the form of a PHP script:
/**
* Calculates odds of winning or new score.
* New players start on 2000 points.
*
* @param int $player1 Player 1 Score
* @param int $player2 Player 2 Score
* @param int $result Determine the result
* 0 = Calculate Odds (%)
* 1 = Player 1 Won
* 2 = Player 2 Won
* 3 = It's a Draw
*/
function calculateRank($player1, $player2, $result=0)
{
$odds_p1 = 1/(1+pow(10,(($player2-$player1)/400)));
$odds_p2 = 1/(1+pow(10,(($player1-$player2)/400)));
if ($result==0)
return array('player1' => $odds_p1*100, 'player2' => $odds_p2*100);
$result_p1 = ($result==1) ? 1 : (($result==2) ? 0 : 0.5);
$result_p2 = ($result==1) ? 0 : (($result==2) ? 1 : 0.5);
$score_p1 = $player1 + (32*($result_p1-$odds_p1));
$score_p2 = $player2 + (32*($result_p2-$odds_p2));
return array('player1' => $score_p1, 'player2' => $score_p2);
}
Enjoy!
Read more ...PHP Experience Points (XP) Level System
Last Updated on Friday, 8 July 2011 11:07pm Written by spunky Friday, 8 July 2011 11:05pm
Here’s a fun little snippet that lets you create a XP leveling system similar to World of Warcraft.
*ding!*
function calculateXP($myPoints) {
$calcStart = 0; // Level 1 Start XP
$calcEnd = 10; // Level 1 End XP
$calcInc = 0; // Increase by extra how many per level?
$calcLevel = 6; // Multiply by how many per level? (1- easy / 20- hard)
/* Calculate Level */
$myLevel = 0;
$calcCount = 0;
do {
$calcCount = $calcCount+1;
if ($calcCount % 2 == 0 ) { $calcInc = $calcInc + $calcLevel; }
if (($myPoints < $calcEnd) && ($myPoints >= $calcStart)) { $myLevel = $calcCount; $myStart = $calcStart; $myEnd = $calcEnd; }
$calcStart = $calcEnd;
$calcEnd = $calcEnd + $calcInc;
} while ($myLevel == 0);
$myLevel--;
/* Calculate Percentage to Next Level */
$myPercent = (($myPoints - $myStart) / ($myEnd - $myStart)) * 100;
$myPercent = round($myPercent);
if ($myPercent == 0) { $myPercent = 1; }
return array('percent'=>$myPercent,'level'=>$myLevel);
}
Please excuse the primitive logic and bad variable naming, this was written many years ago.
Read more ...Half measures for 1140px Grid by Andy Taylor
Last Updated on Thursday, 23 June 2011 11:17am Written by spunky Wednesday, 15 June 2011 06:13pm
Who uses 1024×768 resolution anymore besides your grandma, give me a break. Andy Taylor’s 1140px CSS Grid could be considered the new 960 grid. Although super handy 12 columns doesn’t quite cut it for tight spaces. So using the amazing trickery of maths, this additional CSS beefs it up to potentially 24 by adding “half” columns.
Appendage:
.halfcol { width: 2.425%; float: left; margin-right: 3.8%; }
.onecol.half { width: 9.175%; }
.twocol.half { width: 17.825%; }
.threecol.half { width: 26.475%; }
.fourcol.half { width: 35.125%; }
.fivecol.half { width: 43.775%; }
.sixcol.half { width: 52.425%; }
.sevencol.half { width: 61.075%; }
.eightcol.half { width: 69.725%; }
.ninecol.half { width: 78.375%; }
.tencol.half { width: 87.025%; }
.elevencol.half { width: 93.775%; }
Usage:
<div class="halfcol">Half a column wide</div> <div class="onecol half">One and a half columns wide</div> <div class="twocol half">Two and a half columns wide</div> <div class="threecol half">Three and a half columns wide</div> <div class="fourcol half">Four and a half columns wide</div> <div class="fivecol half">Five and a half columns wide</div> <div class="sixcol half">Six and a half columns wide</div> <div class="sevencol half">Seven and a half columns wide</div> <div class="eightcol half">Eight and a half columns wide</div> <div class="ninecol half">Nine and a half columns wide</div> <div class="tencol half">Ten and a half columns wide</div> <div class="elevencol half">Eleven and a half columns wide</div>
Aware this doesn’t cover the IE component, this is intentional because anything < IE9 sux.
Enjoy!
Read more ...Dreamweaver color scheme for Netbeans editor
Last Updated on Saturday, 2 April 2011 05:26pm Written by spunky Saturday, 2 April 2011 01:55pm
If you’ve stepped up your game and ditched Dreamweaver in favour of using a real PHP IDE like Netbeans then this Dreamweaver-style theme will help make the transition that much easier.
Being unable to find a Dreamweaver color scheme for Netbeans code editor anywhere, this one was built from scratch today using exact color codes from Dreamweaver.
The Netbeans theme also takes a slight influence from Visual Studio’s default colors.

Mmmmm now that’s easy on the eyes.
- Download Netbeans/Dreamweaver color scheme
Making a Side Page Menu in WordPress
Last Updated on Monday, 7 March 2011 08:05am Written by spunky Monday, 7 March 2011 07:26am
For WordPress sites with nested pages a side menu is a valuable element to any design. First we determine if the current page is in fact the parent or a child and collect it’s ID. Then by calling the wp_list_pages command we can list the children and determine if a side menu is even required.
<?php
$parent_id = ($post->post_parent)?$post->post_parent:$post->ID;
$children = wp_list_pages("title_li=&child_of=".$parent_id."&echo=0");
if ($children) { ?>
<h3><?php echo get_the_title($parent_id)?></h3>
<ul>
<li class="<?=($parent_id==$post->ID)?'current_page_item':''?>"><a href="<?=get_permalink($parent_id)?>">Overview</a></li>
<?php echo $children; ?>
</ul>
<?php } ?>
Beautifully simple.
Read more ...How to Recreate/Reset Table ID Fields (1,2,3,4…) using mySQL
Last Updated on Wednesday, 23 February 2011 10:09pm Written by spunky Wednesday, 23 February 2011 07:54am
If you are anal retentive or obsessive–compulsive about having clean ID numbers in your mySQL tables, this code should prove itself useful as it will automatically:
- clean up, convert, recount, reset, recompile, recreate, repopulate the ID column for every entry
- modify the auto increment counter for the table
It is important to note this script should not be used on tables with cross referenced identifiers meaning if a different table relies on the IDs of the modified table it will potentially break the references as a result of assigning the new IDs.
The code might also be useful for other purposes, such as cleaning up CMS page orders or providing a package with clean sample data.
Read more ...Really Simple AJAX Contact Form using jQuery and PHP
Last Updated on Saturday, 5 March 2011 04:54pm Written by spunky Monday, 21 February 2011 09:58pm
Making a contact forms from scratch every time can be a mind numbing task to code. The following is a handy reference for a start to finish contact form. Using PHP and jQuery, it’s features include AJAX form submission, Javascript field validation, a Captcha security image verification and room to breathe for further customisation.
Dependencies
This code snippet requires the following:
- PHPMailer – PHP email class
- jQuery Framework
- jQuery Validate plugin
- any TTF font file
Anahita Project: An Open source Social Network Platform/Framework/Something
Last Updated on Wednesday, 23 February 2011 01:58pm Written by spunky Friday, 18 February 2011 09:48pm
The impact of social networking or online socialisng is fascinating. When asked, “what does a social network mean to you?” a weighted answer is often enhancing exisiting relationships, in contrast to what could be the original intention, creating new ones. A more purist response might be simply a platform for self expression.
Whatever the meaning, another new and fascinating space captured some attention today; an open source project named Anahita claiming itself to be a Social Framework, Engine, Platform and Remarkable amongst other things. At first glance the project site does not appear to have a solid focus on explaining exactly what it does.
Perhaps open to some interpretation it should be noted that certain users have requested a refund after beleiving it is a component for Joomla, a popular Content Management System.
Read more ...