PHP Scripts for the Tabletop

by Lev Lafayette

The following is a brief article on some example PHP scripts for GMs in their roleplaying games. The example is for Linux with an Apache webserver and assumes the user knows HTML. Obviously any other scripting language suitable could be used instead. The use of PHP is for the ease of browser display so one can add graphics, hyperlinks etc as desired. MySQL is added for further useful elaboration (e.g., a database of NPCs) which is not examined here.

1. Install Your Webserver

Not much detail is given here to describe how to install an webserver with php in Linux. The very short version, using packages, is as follows:

Debian distributions (Mint, Ubuntu, Debian)

sudo apt-get install tasksel
sudo tasksel install lamp-server

Red Hat distributions (Centos, Fedora, SuSE)

yum install httpd httpd-devel
yum install mysql mysql-server mysql-devel
yum install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml

Modify your document root according your preferences and restart apache.

Test with a test.php file in your document root with the phpinfo() function.

2. A Dice Roller

Although I am very fond of the visceral experience of rolling plastic (or metal, or gemstone) polyhedrons, the following can serve to replace that experience. Name it d6.php or similar. You could also create similar pages for other die types, or variants thereof (e.g., d37+13 to give that common range of 14 to 51). That sort of idea puts the designer of the d34 to shame (yes, it does exist).


<html>
<head><title>D6 Roller</title></head>
<body>
<h1>D6 Roller</h1>
<p>How many d6 to roll?</p>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name='number' />
<input type="submit" value="Roll them bones!" />
</form>
<?php
$dice = $_POST['number'];
for ($i = 1; $i <= $dice; $i++) {
$roll = mt_rand(1,6);
echo "$roll ";
$sum = $roll + $sum;
}
$sum_of_dice = 'The sum equals = ';
print ($sum_of_dice.$sum);
?>

With a little bit more work you could change the variable $roll to $rolld6 and so forth and thus generate a complete dice notation engine.

3. Encounter or Treasure Display

A whole collection of these can be created; for wilderness types, villages, towns, cities, dungeon levels, treasure types etc. If you want to get fancy you could include in the text file an image of the encounter or treasure in question and have the code snippet display that instead.

<?php
$file = "encounters.txt";
$fh = fopen($file, "r");
$string = fread($fh, filesize($file));
fclose($fh);
echo "<p>$string</p>";
?>

As a simple elaboration multiple files and multiple strings can be displayed simultaneously. For example a dungeon level text file that includes a list of encounters and the treasure that they have.

Randomness could be incorporated by specifying the lines in the file and turning those lines into a random array. e.g.,

$lines = file('encounters.txt');
echo $lines[array_rand($lines)]

4. Character Sheets

Among programmers PHP doesn't get much love, but at it does have a very low entry point from markup languages. The ability to easily combine PHP within HTML provides the opportunity to produce pleasingly formatted webbased character sheets which are easy to print.

Most roleplaying games include fairly simply functions that are easily replicated in HTML with PHP for example:

a. Random generation of numbers assigned to a variable
b. Selection of a single choice (“radio button”) assigned to a variable.
c. Selection of multiple choices (“check box”) assigned to a variable.
& etc.

5. Scenario Tangents in a Webpage

Why note make the effort to preprogram most of the scripted parts of a scenario, the plot trajectories and so forth? As an example one can look at the example given in RPG Review 4 in June 2009 (http://rpgreview.net/node/27) where James Hutchins gave an example of an online text roleplaying game, Age of Fable. James has kindly provided the entire source code for this game at the following URL: http://www.apolitical.info/webgame/sourcecode.php