diff options
author | emkael <emkael@tlen.pl> | 2015-02-05 16:05:00 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2015-02-05 16:05:00 +0100 |
commit | a7685f1bc2be4993ac568188ff6e6eee03c43281 (patch) | |
tree | fbdef7a5034b97b94be4ed0fbeafb6f927fef651 | |
parent | 0ee6b2788ce92cf9cfeeebffc40162decaa9e0d6 (diff) |
Fixes #1
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | bin/lib/OsikaEvaluator.php | 70 | ||||
-rw-r--r-- | bin/lib/OsikaParser.php | 20 | ||||
-rwxr-xr-x | bin/osika | 66 |
4 files changed, 80 insertions, 84 deletions
@@ -8,9 +8,7 @@ Wymagania systemowe ### Wersja konsolowa: - * interpreter PHP 5.3+[1] z modułem JSON - -[1]: z wyjątkiem testów jednostkowych z katalogu [/tests/](tests) - kompletnie niepotrzebnych użytkownikowi końcowemu - te wymagają PHP 5.4+ + * interpreter PHP 5.4+ z modułem JSON ### Wersja interaktywna (WWW), dodatkowo: @@ -19,7 +17,7 @@ Wymagania systemowe + [jQuery](http://jquery.com) + [jQuery-hashchange](http://benalman.com/projects/jquery-hashchange-plugin/) + [Mustache](https://github.com/janl/mustache.js/) - + Instalacja ---------- @@ -34,7 +32,7 @@ Po ściągnięciu z repozytorium katalogów [/web/](web/) oraz [/bin/](bin/), na * w podkatalogu /web/lib/ umieścić wymagane biblioteki JavaScript (niedostarczane z aplikacją) * jeśli potrzeba, w pliku /web/index.html edytować ścieżki do bibliotek JavaScript * w pliku [/web/osika.php](web/osika.php) edytować linię rozpoczynającą się od `require_once` tak, aby wskazywała na odpowiedni plik katalogu [/bin/lib/](bin/lib/) - + Użycie ------ diff --git a/bin/lib/OsikaEvaluator.php b/bin/lib/OsikaEvaluator.php index 474cffe..6b14c7a 100644 --- a/bin/lib/OsikaEvaluator.php +++ b/bin/lib/OsikaEvaluator.php @@ -17,24 +17,23 @@ class OsikaEvaluator { $this->_hand = $parser->parse(); } - private static $_honorCounts = array(); + private static $_honorCounts = []; private function _countHonors($suit, $major = FALSE) { if (!isset(self::$_honorCounts[$suit])) { - self::$_honorCounts[$suit] = array(NULL, NULL); + self::$_honorCounts[$suit] = [NULL, NULL]; } if (self::$_honorCounts[$suit][$major] === NULL) { - $dummy = array(); - self::$_honorCounts[$suit][$major] = preg_match_all($major ? '/a|k|q/' : '/a|k|q|j/', $suit, $dummy); + self::$_honorCounts[$suit][$major] = preg_match_all($major ? '/a|k|q/' : '/a|k|q|j/', $suit); } return self::$_honorCounts[$suit][$major]; } - private static $_cardHonorTricks = array( - 'a' => 1.125, - 'k' => 0.8125, - 'q' => 0.4375, - 'j' => 0.125 - ); + private static $_cardHonorTricks = [ + 'a' => 1.125, + 'k' => 0.8125, + 'q' => 0.4375, + 'j' => 0.125 + ]; private function _honorTricks($suit) { // only 3 highest cards in the suit holding count towards honor tricks $suit = substr($suit, 0, 3); @@ -87,12 +86,12 @@ class OsikaEvaluator { return $count * (($length === 1) ? -0.125 : -0.0625); } - private static $_lengthDistributionTricks = array( - 4 => 0.4375, - 5 => 1.5, - 6 => 2.75, - 7 => 3.9375 - ); + private static $_lengthDistributionTricks = [ + 4 => 0.4375, + 5 => 1.5, + 6 => 2.75, + 7 => 3.9375 + ]; private function _distributionTricks($suit) { $length = strlen($suit); if ($length < 4) { @@ -145,15 +144,14 @@ class OsikaEvaluator { // statistically, we should have 2 of those // but we're not counting short suits and some long suit 10 configurations // so the par for the course is 1 middle card - $dummy = array(); // this is humiliating - $count = preg_match_all('/t|9/', $nonshort, $dummy) - 1; + $count = preg_match_all('/t|9/', $nonshort) - 1; // if we're better than that single middle card -> 1/16 of a trick // if we're worse -> -1/16 return max(-0.0625, min(0.0625, $count * 0.0625)); } private function _shortSuitCorrections($distribution) { - $shortSuits = array(); + $shortSuits = []; foreach ($distribution as $suit) { // short suit is a 3- card suit here if ($suit <= 3) { @@ -184,8 +182,8 @@ class OsikaEvaluator { // I honestly have no idea what the hell's going on below. private function _localizationCorrections($result, $distribution) { - $strength = array(); - $length = array(); + $strength = []; + $length = []; foreach ($result['lh'] as $index => $value) { if (strlen($index) === 1) { if ($distribution[$index] >= 3) { @@ -229,14 +227,14 @@ class OsikaEvaluator { return 0; } - private static $_suits = array('s','h','d','c'); + private static $_suits = ['s','h','d','c']; public function evaluate() { - $result = array(); - $result['lh'] = array(); - $result['lh_plus'] = array(); - $result['lh_pod'] = array(); - $result['lh_short'] = array(); - $result['lu'] = array(); + $result = []; + $result['lh'] = []; + $result['lh_plus'] = []; + $result['lh_pod'] = []; + $result['lh_short'] = []; + $result['lu'] = []; foreach ($this->_hand as $ind => $suit) { $suitChar = self::$_suits[$ind]; $result['lh'][$suitChar] = $this->_honorTricks($suit); @@ -251,17 +249,17 @@ class OsikaEvaluator { $result['lh_short']['total'] = array_sum($result['lh_short']); $result['lu']['total'] = array_sum($result['lu']); - $result['lsz'] = array('total' => $this->_quickTricks($this->_hand)); - $result['lu_plus'] = array('total' => $this->_middleCardCorrections($this->_hand)); + $result['lsz'] = ['total' => $this->_quickTricks($this->_hand)]; + $result['lu_plus'] = ['total' => $this->_middleCardCorrections($this->_hand)]; $distribution = array_combine(self::$_suits, array_map('strlen', $this->_hand)); - $result['short_suit'] = array('total' => $this->_shortSuitCorrections($distribution)); - $result['major_suit'] = array('total' => $this->_majorSuitCorrections($distribution)); + $result['short_suit'] = ['total' => $this->_shortSuitCorrections($distribution)]; + $result['major_suit'] = ['total' => $this->_majorSuitCorrections($distribution)]; - $result['l10n'] = array('total' => $this->_localizationCorrections($result, $distribution)); + $result['l10n'] = ['total' => $this->_localizationCorrections($result, $distribution)]; - $subtotal = array(); - $lhSubtotal = array(); + $subtotal = []; + $lhSubtotal = []; $total = 0; foreach ($result as $category => $factor) { if (count($factor) === 5) { @@ -280,7 +278,7 @@ class OsikaEvaluator { } $result['lh_subtotal'] = $lhSubtotal; $result['subtotal'] = $subtotal; - $result['total'] = array('total' => $total); + $result['total'] = ['total' => $total]; return $result; } diff --git a/bin/lib/OsikaParser.php b/bin/lib/OsikaParser.php index d2046f4..b134bb9 100644 --- a/bin/lib/OsikaParser.php +++ b/bin/lib/OsikaParser.php @@ -39,19 +39,19 @@ class OsikaParser { } // ...queens... if ($cardA == 'q') { - return (in_array($cardB, array('a', 'k'))) ? 1 : -1; + return (in_array($cardB, ['a', 'k'])) ? 1 : -1; } // ...jacks... if ($cardA == 'j') { - return (in_array($cardB, array('a', 'k', 'q'))) ? 1 : -1; + return (in_array($cardB, ['a', 'k', 'q'])) ? 1 : -1; } // ...tens... if ($cardA == 't') { - return (in_array($cardB, array('a', 'k', 'q', 'j'))) ? 1 : -1; + return (in_array($cardB, ['a', 'k', 'q', 'j'])) ? 1 : -1; } // ... and nines if ($cardA == '9') { - return (in_array($cardB, array('a', 'k', 'q', 'j', 't'))) ? 1 : -1; + return (in_array($cardB, ['a', 'k', 'q', 'j', 't'])) ? 1 : -1; } // anything else goes last, as it was return 1; @@ -62,7 +62,7 @@ class OsikaParser { **/ private function _sortSuit(&$suit) { $temp = str_split($suit); - usort($temp, array($this, '_sort')); + usort($temp, [$this, '_sort']); $suit = implode('', $temp); } @@ -77,10 +77,10 @@ class OsikaParser { $this->_hand = strtolower($this->_hand); // allow (and interpret) Polish figures abbrevs. and "10" as Ten $this->_hand = strtr($this->_hand, - array( - '10' => 't', - 'w' => 'j', - 'd' => 'q')); + [ + '10' => 't', + 'w' => 'j', + 'd' => 'q']); // strip whitespace $this->_hand = preg_replace('/\s/', '', $this->_hand); $suits = explode('|', $this->_hand); @@ -95,7 +95,7 @@ class OsikaParser { throw new OsikaParserException('Kolor '.$suit.' zawiera nieprawidłowe znaki', OsikaParserException::INVALID_CHARS); } // check for duplicate cards - foreach (array('a', 'k', 'q', 'j', 't', '9') as $honor) { + foreach (['a', 'k', 'q', 'j', 't', '9'] as $honor) { if (substr_count($suit, $honor) > 1) { throw new OsikaParserException('Kolor '.$suit.' zawiera zduplikowany honor (lub 9)', OsikaParserException::DUPLICATE_CHARS); } @@ -3,28 +3,28 @@ class Osika { - private static $suitHeaders = array( - 'c' => '♣', - 'd' => '♦', - 'h' => '♥', - 's' => '♠', - 'total' => 'Σ' - ); - private static $catHeaders = array( - 'lh' => "Honory\t", - 'lh_plus' => 'Zgrupowania', - 'lh_10' => 'Podwiązania', - 'lh_short' => 'Krótkie honory', - 'lh_subtotal' => 'Lewy honorowe', - 'lu' => 'Lewy układowe', - 'subtotal' => "Razem\t", - 'lsz' => 'Lewy szybkie', - 'lu_plus' => 'Wysokie blotki', - 'short_suit' => 'Kolory krótkie', - 'major_suit' => 'Kolory starsze', - 'l10n' => 'Lokalizacja', - 'total' => "Łącznie\t" - ); + private static $suitHeaders = [ + 'c' => '♣', + 'd' => '♦', + 'h' => '♥', + 's' => '♠', + 'total' => 'Σ' + ]; + private static $catHeaders = [ + 'lh' => "Honory\t", + 'lh_plus' => 'Zgrupowania', + 'lh_10' => 'Podwiązania', + 'lh_short' => 'Krótkie honory', + 'lh_subtotal' => 'Lewy honorowe', + 'lu' => 'Lewy układowe', + 'subtotal' => "Razem\t", + 'lsz' => 'Lewy szybkie', + 'lu_plus' => 'Wysokie blotki', + 'short_suit' => 'Kolory krótkie', + 'major_suit' => 'Kolory starsze', + 'l10n' => 'Lokalizacja', + 'total' => "Łącznie\t" + ]; public static function printHelp() { @@ -99,7 +99,7 @@ require_once('lib/OsikaEvaluator.php'); $hand = str_replace(',', '|', array_pop($argv)); -$options = getopt('hf:c:s:', array('help', 'format:', 'categories:', 'suits:')); +$options = getopt('hf:c:s:', ['help', 'format:', 'categories:', 'suits:']); if (isset($options['h']) || isset($options['help']) || $argc < 2) { Osika::printHelp(); exit; @@ -110,49 +110,49 @@ if (is_array($format)) { $format = array_pop($format); } -$catOptions = array(); +$catOptions = []; if (isset($options['c'])) { $catOptions = array_merge($catOptions, (array)$options['c']); } if (isset($options['categories'])) { $catOptions = array_merge($catOptions, (array)$options['categories']); } -$categories = array(); +$categories = []; foreach ($catOptions as $cat) { $categories = array_merge($categories, explode(',', $cat)); } if (empty($categories)) { - $categories = array('all'); + $categories = ['all']; } if (in_array('all', $categories)) { - $categories = array('lh','lh_plus','lh_10','lh_short','lh_subtotal','lu','subtotal','lsz','lu_plus','short_suit','major_suit','l10n','total'); + $categories = ['lh','lh_plus','lh_10','lh_short','lh_subtotal','lu','subtotal','lsz','lu_plus','short_suit','major_suit','l10n','total']; } -$suitOptions = array(); +$suitOptions = []; if (isset($options['s'])) { $suitOptions = array_merge($suitOptions, (array)$options['s']); } if (isset($options['suits'])) { $suitOptions = array_merge($suitOptions, (array)$options['suits']); } -$suits = array(); +$suits = []; foreach ($suitOptions as $suit) { $suits = array_merge($suits, explode(',', $suit)); } if (empty($suits)) { - $suits = array('all'); + $suits = ['all']; } if (in_array('all', $suits)) { - $suits = array('s','h','d','c','total'); + $suits = ['s','h','d','c','total']; } try { $eval = new OsikaEvaluator($hand); $result = $eval->evaluate(); - $output = array(); + $output = []; foreach ($result as $categoryName => $category) { if (in_array($categoryName, $categories)) { - $outCat = array(); + $outCat = []; foreach ($category as $suitName => $suit) { if (in_array($suitName, $suits)) { $outCat[$suitName] = $suit; |