1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | <?php /** * Any five card poker hand display interface * * @author Razvan Stanga <git@razvi.ro> */ class Display { private $smarty; private $dbConnection; function __construct () { $this->dbConnection = new dbConnection; require (appRoot."include/lib/Smarty.class.php"); $this->smarty = new Smarty; $this->smarty->compile_check = true; $this->smarty->debugging = false; $this->smarty->template_dir = appRoot."style/tpl"; $this->smarty->compile_dir = appRoot."style/tpl_c"; } /** * Collects all data needes to generate the page and shows the page * * @param int $page current page * @param int $hand current hand filter like for of a kind * @param string $card current card filter like 2 hearts * @param bool $ajax returnes ony the content with ajax * @return void */ public function page ($page, $hand="", $card="", $ajax=false) { $sqlFilter = "WHERE 1=1"; $params = ""; if ($hand != "") { $sqlFilter .= " AND `hand`='".$hand."'"; $params .= "&hand=".$hand; } if ($card != "") { $sqlFilter .= " AND FIND_IN_SET('".$card."', `combo`)"; $params .= "&card=".urlencode ($card); } $q = $this->dbConnection->dbQuery ("SELECT COUNT(*) as count FROM `cards` ".$sqlFilter); $total = $this->dbConnection->dbFetchArray ($q); $paginationData = $this->paginationData ($page, $total['count'], 100); $pagination = $this->pagination ($paginationData['total'], "index.php?", $paginationData['page'], $paginationData['offset'], 'page', $params); $this->smarty->assign ("pagination", $pagination); $databaseQuery = "SELECT * FROM `cards` ".$sqlFilter." LIMIT ".$paginationData['min'].", ".$paginationData['offset']; $startTime = microtime (true); $q = $this->dbConnection->dbQuery ($databaseQuery); $endTime = microtime (true); $this->smarty->assign ("queryTime", ($endTime-$startTime)); $rows = array (); while ( $row = $this->dbConnection->dbFetchArray ($q) ) { $rows[ $row['id'] ] = $row; } $this->smarty->assign ("rows", $rows); $this->smarty->assign ("hands", Anyfivecardpokerhand::$hands ); $this->smarty->assign ("cards", Anyfivecardpokerhand::$cards ); $this->smarty->assign ("types", Anyfivecardpokerhand::$types ); $this->smarty->assign ("databaseQuery", $databaseQuery); $this->smarty->assign ("currentCard", $card); $this->smarty->assign ("currentHand", $hand); if ( $ajax == true ) { $this->smarty->display ("table.tpl"); } else { $this->buildStats (); $this->smarty->display ("index.tpl"); } } /** * Stats builder. Fetches info from database like how many full hose combinations are there * * @return void */ private function buildStats () { $stats = array (); $q = $this->dbConnection->dbQuery ("SELECT COUNT(*) as count, hand FROM `cards` GROUP BY `hand`"); while ( $row = $this->dbConnection->dbFetchArray ($q) ) { $stats[ $row['hand'] ] = $row['count']; $stats['all'] += $row['count']; } $this->smarty->assign ("stats", $stats); } /** * Pagination. Returnes an array with the linkes for pagination to be used in template * * @param int $totalitems total items * @param string $link pagination link * @param int $page current page * @param int $offset how many items per page * @param string $pagename GET url param for page * @param string $params extra params to add to the pagination link * @return array */ private function pagination ($totalitems, $link, $page, $offset, $pagename='page', $params=null) { $pagination = array (); $pages = 6; $nrpages = ceil ($totalitems/$offset); $pagination['totalpages'] = $nrpages; $pagination['totalitems'] = $totalitems; $pagination['pages'] = array (); $pagination['page'] = $page; for ($i=1 ; $i<=$nrpages ; $i++) { if ( $i <= $page - $pages or $i >= $page + $pages ) { if ($i==1) { $pagination['firstpage'] = $link."&".$pagename."=".$i.$params; $pagination['firstpagenr'] = $i; } if ( $i == $nrpages ) { $pagination['lastpage'] = $link."&".$pagename."=".$i.$params; $pagination['lastpagenr'] = $i; } } if ($page > 1) { $prevpage = $page - 1; if ( $prevpage == 1 ) { $pagination['prevpage'] = $link.$params; } else { $pagination['prevpage'] = $link."&".$pagename."=".$prevpage.$params; } $pagination['prevpagenr'] = $prevpage; } if ($i <= $page + $pages && $i >= $page - $pages) { if ( $page == $i ) { $pagination['pages'][$i] = $link."&".$pagename."=".$i.$params; } else { if ( $i == 1 ) { $pagination['pages'][$i] = $link.$params; } else { $pagination['pages'][$i] = $link."&".$pagename."=".$i.$params; } } } if ($page < $nrpages) { $nextpage = $page + 1; $pagination['nextpage'] = $link."&".$pagename."=".$nextpage.$params; $pagination['nextpagenr'] = $nextpage; } } return $pagination; } /** * Pagination data calculator. Returnes data to be used in sql query and in pagination * * @param int $page current page * @param int $total total items * @param int $offset how many items per page * @return array */ private function paginationData ($page, $total, $offset=10) { $offset = $offset ? $offset : 1; $ret = array (); $ret['total'] = $total; $ret['offset'] = $offset ? $offset : 1; $ret['page'] = $page; $ret['page'] = (int) $ret['page'] ? $ret['page'] : 1; $ret['page'] = abs ( (int) $ret['page'] ); $ret['min'] = ($ret['page'] - 1) * $offset; $pages = ceil ($total / $offset); if ( $ret['page'] > $pages ) { $ret['page'] = 1; $ret['min'] = 0; } return $ret; } } ?> |