<?php if (!defined('PmWiki')) exit(); /* Copyright 2007 Patrick R. Michaud (pmichaud@pobox.com) This file is cards.php; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This script adds some simple playing card markups to PmWiki. The simplest markups are " s ", " h ", " d ", and " c ", which display suit images for spades, hearts, diamonds, and clubs respectively. The spaces around each letter are significant. The images are held at the location given by $CardDirUrl (which defaults to $PubDirUrl/cards ). To use this recipe, simply place this script into the cookbook/ directory, and copy the suit images into pub/cards/ . Then add the following line as a local customization: include_once("cookbook/cards.php"); Updated for PHP 5.5-7.2 by Petko Yotov www.pmwiki.org/petko */ $RecipeInfo['CardsMarkup']['Version'] = '20191121'; ## $CardSuitHTML contains the HTML to use for displaying a suit. ## Note that one might want to change this to use HTML character ## entities instead. SDVA($CardSuitHTML, array( 's' => Keep("<img src='$PubDirUrl/cards/s.gif' alt='s' />"), 'h' => Keep("<img src='$PubDirUrl/cards/h.gif' alt='h' />"), 'd' => Keep("<img src='$PubDirUrl/cards/d.gif' alt='d' />"), 'c' => Keep("<img src='$PubDirUrl/cards/c.gif' alt='c' />"))); ## The default style for card tables. SDV($HTMLStylesFmt['cards'], ' table.cards td { text-align:left; width:120px; } '); SDVA($CardNames, array( 'a' => ' A', 'A' => ' A', 'k' => ' K', 'K' => ' K', 'q' => ' Q', 'Q' => ' Q', 'j' => ' J', 'J' => ' J', 't' => ' 10', 'T' => ' 10', '10' => ' 10', '9' => ' 9', '8' => ' 8', '7' => ' 7', '6' => ' 6', '5' => ' 5', '4' => ' 4', '3' => ' 3', '2' => ' 2', 'x' => ' x', 'X' => ' X')); ## handle inline display of suits Markup('cdhs', 'inline', '/(?<=\\s)([cdhs])(?=\\s)/', "mu_cdhs]"); function mu_cdhs($m) { return $GLOBALS['CardSuitHTML'][$m[1]]; } ## (:cards:) Markup('cards', 'block', '/\\(:cards\\s+(["\']?)(.+?)\\1\\s*:\\)/', "mu_cards"); function mu_cards($m) { return Keep(CardTable($m[2])); } ## PlayerHand($spec) takes a spec like "A8765.QT.K9.AT87" and ## displays it in the form ## s A 8 7 6 5 ## h Q 10 ## d K 9 ## c A 10 8 7 function PlayerHand($spec) { global $CardSuitHTML, $CardNames; $suit = array_keys($CardSuitHTML); $s = array_shift($suit); $out = $CardSuitHTML[$s]; $i = 0; while ($i < strlen($spec)) { $c2 = substr($spec, $i, 2); $c1 = $spec{$i}; if ($c2 == '.-') { $s = array_shift($suit); $i += 2; continue; } else if ($c1 == '.') { $s = array_shift($suit); $out .= ' <br />' . $CardSuitHTML[$s]; $i++; continue; } else if ($c1 == '-') { $out = ''; $i++; continue; } if (@$CardNames[$c2]) { $out .= $CardNames[$c2]; $i += 2; continue; } if (@$CardNames[$c1]) { $out .= $CardNames[$c1]; $i++; continue; } $i++; } while ($suit) { $s = array_shift($suit); $out .= '<br />' . $CardSuitHTML[$s]; } return $out; } ## Display a hand or set of hands (board) in a table. The board is ## represented as a space-separate list of hands -- each hand can ## be prefixed with one of N: S: E: W: to indicate the player. ## If the first hand doesn't have a prefix, N: is assumed, ## subsequent hands w/o prefix are one position clockwise from the ## previous hand. function CardTable($spec) { $hands = preg_split('/\\s+/', trim($spec)); $player = 'W'; while ($hands) { $phand = array_shift($hands); if (!preg_match('/^(([NESW]):)?(.*)$/i', $phand, $match)) continue; $player = (@$match[2]) ? strtoupper($match[2]) : substr('NESW', strpos('WNES', $player), 1); if ($match[3] != '-') $board[$player] = PlayerHand($match[3]); } $out = "<table class='cards' align='center'>"; if (@$board['N']) $out .= '<tr><td></td><td>' . $board['N'] . '</td><td></td></tr>'; if (@$board['W'] || @$board['E']) $out .= '<tr><td>' . @$board['W'] . '</td><td></td><td>' . @$board['E'] . '</td></tr>'; if (@$board['S']) $out .= '<tr><td></td><td>' . $board['S'] . '</td><td></td></tr>'; $out .= '</table>'; return $out; }