From 5f4594124832c6eee2f295dee4710ff2b05e6357 Mon Sep 17 00:00:00 2001 From: emkael Date: Sun, 15 Sep 2013 14:10:59 +0200 Subject: * nowa wersja interfejsu webowego, kompatybilna wstecznie --- web/index.html | 30 +++++- web/osika.js | 99 ++++++++++---------- web/osika.php | 291 +++++---------------------------------------------------- 3 files changed, 96 insertions(+), 324 deletions(-) diff --git a/web/index.html b/web/index.html index c8a547c..5763dea 100644 --- a/web/index.html +++ b/web/index.html @@ -3,7 +3,11 @@ OSiKa - + + + + + @@ -12,13 +16,13 @@

OSiKa

Podaj rękę do oceny:
- + - + - + -
+
@@ -35,5 +39,21 @@
  • blotki nie są weryfikowane (kolor może posiadać zduplikowane blotki, może też posiadać niemożliwą liczbę blotek, np. 10)
  • + + + + + + + + + + + + + + + + diff --git a/web/osika.js b/web/osika.js index 82fab72..5491938 100644 --- a/web/osika.js +++ b/web/osika.js @@ -1,52 +1,53 @@ -function getHand(hand) { - jQuery('#evaluate').val('Czekaj...'); - jQuery('#evaluate').attr('disabled', 'disabled'); - jQuery.getJSON( - 'osika.php', - { - 'h': hand, - 'f': 'json' - }, - function (data) { - jQuery('#evaluate').val('Oceń'); - jQuery('#evaluate').removeAttr('disabled'); - if (data.error) { - jQuery('#result').html('
    '+data.error+'
    '); - } - else { - var html = ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += ''; - html += '
    Σ
    Honory (lewy)'+data.lh[0]+''+data.lh[1]+''+data.lh[2]+''+data.lh[3]+''+data.lh.total+'
    Poprawka za zgrupowania honorów'+data.lh_plus[0]+''+data.lh_plus[1]+''+data.lh_plus[2]+''+data.lh_plus[3]+''+data.lh_plus.total+'
    Poprawka za podwiązania honorów'+data.lh_10[0]+''+data.lh_10[1]+''+data.lh_10[2]+''+data.lh_10[3]+''+data.lh_10.total+'
    Poprawka za krótkie honory'+data.lh_short[0]+''+data.lh_short[1]+''+data.lh_short[2]+''+data.lh_short[3]+''+data.lh_short.total+'
    Lewy honorowe'+(data.subtotal[0]-data.lu[0])+''+(data.subtotal[1]-data.lu[1])+''+(data.subtotal[2]-data.lu[2])+''+(data.subtotal[3]-data.lu[3])+''+(data.subtotal.total-data.lu.total)+'
    Lewy układowe'+data.lu[0]+''+data.lu[1]+''+data.lu[2]+''+data.lu[3]+''+data.lu.total+'
    Razem'+data.subtotal[0]+''+data.subtotal[1]+''+data.subtotal[2]+''+data.subtotal[3]+''+data.subtotal.total+'
    Poprawka za lewy szybkie'+data.lsz.total+'
    Poprawka za wysokie blotki'+data.lu_plus.total+'
    Poprawka za kolory krótkie'+data.short_suit.total+'
    Poprawka za kolory starsze'+data.major_suit.total+'
    Poprawka za lokalizację'+data.l10n.total+'
    Łącznie'+data.total.total+'
    '; - jQuery('#result').html(html); - location.hash = hand; +var Osika = function() { + var that = this; + + that.resultTemplate = jQuery('#resultTemplate').remove().html(); + + that.handleData = function(data) { + jQuery('#evaluate').val('Oceń'); + jQuery('#evaluate').removeAttr('disabled'); + if (data.error) { + jQuery('#result').html('
    '+data.error+'
    '); + } + else { + var html = Mustache.to_html(that.resultTemplate, data); + jQuery('#result').html(html); + } + }; + + that.getHand = function(hand) { + jQuery('#evaluate').val('Czekaj...'); + jQuery('#evaluate').attr('disabled', 'disabled'); + jQuery.getJSON( + 'osika.php', + { + 'h': hand, + 'f': 'json' + }, + that.handleData + ); + location.hash = hand; + }; + + that.init = function() { + jQuery('#evaluate').unbind('click').click(function () { + var hand = jQuery('#suit0').val()+'|'+jQuery('#suit1').val()+'|'+jQuery('#suit2').val()+'|'+jQuery('#suit3').val(); + that.getHand(hand); + return false; + }); + if (location.hash) { + var hand = location.hash.substring(1).split('|'); + for (h = 0; h < hand.length; h++) { + jQuery('#suit'+h).val(hand[h]); } - } - ); -} + that.getHand(location.hash.substring(1)); + } + jQuery(window).hashchange(that.init); + }; + that.init(); +}; jQuery(document).ready(function () { - jQuery('#evaluate').click(function () { - var hand = jQuery('#suit0').val()+'|'+jQuery('#suit1').val()+'|'+jQuery('#suit2').val()+'|'+jQuery('#suit3').val(); - getHand(hand); - return false; - }); - if (location.hash) { - var hand = location.hash.substring(1).split('|'); - for (h = 0; h < hand.length; h++) { - jQuery('#suit'+h).val(hand[h]); - } - getHand(location.hash.substring(1)); - } -}); \ No newline at end of file + var o = new Osika(); +}); + diff --git a/web/osika.php b/web/osika.php index 092cc9c..a5abb4f 100644 --- a/web/osika.php +++ b/web/osika.php @@ -1,282 +1,33 @@ 2) { - return 0; - } - $count = preg_match_all('/a|k|q|j/', $suit, $dummy); - if ($len == 1) { - return ($count)*(-0.125); - } - return $count*(-0.0625); -} - -function blot($length) { - if ($length < 4) { - return 0; - } - switch ($length) { - case 4: - return 0.4375; - case 5: - return 1.5; - case 6: - return 2.75; - case 7: - return 3.9375; - } - if ($length >= 8) { - return $length - 3; - } -} - -function blotPlus($suit) { - $dummy = array(); - return 0.125*((strlen($suit) > 3) && substr_count($suit, 't') && preg_match_all('/a|k|q/', $suit, $dummy))+(substr_count($suit, 't9'))*0.0625; -} - -function quickTricks($hand) { - $hand = '|'.$hand; - $high = substr_count($hand, 'a')+substr_count($hand, 'k'); - $low = substr_count($hand, 'q')+substr_count($hand, 'j')-substr_count($hand, '|j'); - $diff = $high - $low; - if ($diff > 2) { - return 0.125; - } - if ($diff > 1) { - return 0.0625; - } - if ($diff < -2) { - return -0.125; - } - if ($diff < -1) { - return -0.0625; - } - return 0; -} - -function luBlots($hand) { - $nonshort = ''; - foreach ($hand as $suit) { - if (strlen($suit) >= 3) { - $nonshort .= $suit; - } - } - $dummy = array(); - $count = preg_match_all('/t|9/', $nonshort, $dummy)-1; - if ($count > 0) { - return 0.0625; - } - if ($count < 0) { - return -0.0625; - } - return 0; -} - -function shortSuit($distribution) { - $suits = array(); - foreach ($distribution as $suit) { - if ($suit <= 3) { - $suits[] = $suit; - } - } - if (count($suits) < 2) { - return 0; - } - sort($suits); - $diff = $suits[1] - $suits[0]; - return ($diff > 1) ? 0.0625 : 0; -} - -function majorSuit($distribution) { - if ((($distribution[0] + $distribution[1]) >= 8) && ($distribution[0] > 2) && ($distribution[1] > 2)) { - return 0.0625; - } - return 0; -} +// path to Evaluator goes here. +require_once('../bin/lib/OsikaEvaluator.php'); -function localization($distribution, $result) { - $strength = array(); - $length = array(); - foreach ($result['lh'] as $index => $value) { - if (is_numeric($index)) { - if ($distribution[$index] >= 3) { - if (isset($strength[$distribution[$index]])) { - $strength[$distribution[$index]] += ($result['lh'][$index]+$result['lh_plus'][$index]+$result['lh_10'][$index]+$result['lh_short'][$index]); - } - else { - $strength[$distribution[$index]] = ($result['lh'][$index]+$result['lh_plus'][$index]+$result['lh_10'][$index]+$result['lh_short'][$index]); - } - if (isset($length[$distribution[$index]])) { - $length[$distribution[$index]] += $distribution[$index]; - } - else { - $length[$distribution[$index]] = $distribution[$index]; - } - } - } - } - ksort($strength); - ksort($length); - $sumLength = array_sum($length); - $sumStrength = array_sum($strength); - $longestDiff = end($strength)-$sumStrength*end($length)/$sumLength; - $shortestDiff = reset($strength)-$sumStrength*reset($length)/$sumLength; - if (abs($longestDiff) > 0.5) { - if (abs($longestDiff) > 1) { - return 0.25*(abs($longestDiff)/$longestDiff); - } - else { - return 0.125*(abs($longestDiff)/$longestDiff); - } - } - if (abs($shortestDiff) > 0.5) { - if (abs($shortestDiff) > 1) { - return -0.125*(abs($shortestDiff)/$shortestDiff); - } - else { - return -0.0625*(abs($shortestDiff)/$shortestDiff); - } - } - return 0; +$hand = $_REQUEST['h']; +try { + $eval = new OsikaEvaluator($hand); + $res = $eval->evaluate(); } - - -function osika($hand) { - if (!$hand) { - return array('error' => 'Brak podanej ręki!'); - } - $hand = strtolower($hand); - $hand = str_replace(array('10', 'w', 'd'), array('t', 'j', 'q'), $hand); - $hand = preg_replace('/\s/', '', $hand); - $suits = explode('|', $hand); - if (count($suits) != 4) { - return array('error' => 'Ręka nie zawiera 4 kolorów!'); - } - $distribution = array(); - $result = array('lu' => array('total' => 0), - 'lu_plus' => array('total' => 0), - 'lh' => array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0), - 'lh_plus' => array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0), - 'lh_10' => array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0), - 'lh_short' => array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0), - 'lsz' => array('total' => 0), - 'l10n' => array('total' => 0), - 'short_suit' => array('total' => 0), - 'major_suit' => array('total' => 0)); - foreach ($suits as $index => $suit) { - $distribution[] = strlen($suit); - if (preg_match('/[^akqjtx2-9]/', $suit)) { - return array('error' => 'Kolor '.$suit.' zawiera nieprawidłowe znaki!'); - } - foreach (array('a', 'k', 'q', 'j', 't', '9') as $honor) { - if (substr_count($suit, $honor) > 1) { - return array('error' => 'Kolor '.$suit.' zawiera zduplikowany honor!'); - } - } - sortHand($suit); - $result['lh_short'][$index] = honorShort($suit); - $result['lh_short']['total'] += $result['lh_short'][$index]; - $result['lh_10'][$index] = blotPlus($suit); - $result['lh_10']['total'] += $result['lh_10'][$index]; - $suit = substr($suit, 0, 3); - $result['lh'][$index] = honor($suit); - $result['lh']['total'] += $result['lh'][$index]; - $result['lh_plus'][$index] = honorPlus($suit); - $result['lh_plus']['total'] += $result['lh_plus'][$index]; - } - if (array_sum($distribution) != 13) { - return array('error' => 'Ręka nie zawiera 13 kart!'); - } - foreach ($distribution as $index => $suit) { - $result['lu'][$index] = blot($suit); - $result['lu']['total'] += $result['lu'][$index]; - } - $result['lsz']['total'] = quickTricks(implode('|',$suits)); - $result['lu_plus']['total'] = luBlots($suits); - $result['short_suit']['total'] = shortSuit($distribution); - $result['major_suit']['total'] = majorSuit($distribution); - $result['l10n']['total'] = localization($distribution, $result); - $subtotal = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0); - $total = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 'total' => 0); - foreach ($result as $i => $factor) { - if (count($factor) == 5) { - foreach ($factor as $index => $subt) { - $subtotal[$index] += $subt; - } - } - $total['total'] += $factor['total']; - } - $result['subtotal'] = $subtotal; - $result['total'] = $total; - return $result; +catch (OsikaParserException $e) { + $res = array('error' => $e->getMessage()); } -$hand = $_REQUEST['h']; -$res = osika($hand); if (!isset($_REQUEST['f'])) { - $_REQUEST['f'] = ''; + $_REQUEST['f'] = ''; } + switch ($_REQUEST['f']) { - case 'json': - echo json_encode($res); - exit; - default: - if (isset($res['error'])) { - echo 'Błąd: '.$res['error']."\n"; - exit; - } - else { - print_r($res); - } +case 'json': + echo json_encode($res); + exit; +default: + if (isset($res['error'])) { + echo 'Błąd: '.$res['error']."\n"; + exit; + } + else { + print_r($res); + } } - - ?> \ No newline at end of file -- cgit v1.2.3