PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jstetris/inc/Top10k.php

https://repo.or.cz/zzandy.git
PHP | 166 lines | 134 code | 32 blank | 0 comment | 41 complexity | 87b2ccdd14362bd6bf077d8eb15d402e MD5 | raw file
  1. <?php
  2. define('DATA_DIR', dirname(dirname(__FILE__)) . '/data/');
  3. class Top10k {
  4. const data_dir = DATA_DIR;
  5. const max_records = 1000;
  6. const show_records = 10;
  7. const goldsection = 1.6180339887498948482;
  8. private $table;
  9. private $stats;
  10. private $record;
  11. private $position;
  12. private $data;
  13. private $stats_file;
  14. private $table_file;
  15. function __construct($data) {
  16. $this->stats_file = self::data_dir . 'stats.dat';
  17. $this->table_file = self::data_dir . 'table.dat';
  18. $this->data = $data;
  19. if($this->data['classic'] == '1') {
  20. $this->table_file = self::data_dir . 'table-classic.dat';
  21. $this->stats_file = self::data_dir . 'stats-classic.dat';
  22. }
  23. $this->load_stats();
  24. $this->load_table();
  25. if($this->data_valid) {
  26. $this->record = new Record($this->data);
  27. $this->stats->add(new Stat($this->data));
  28. }
  29. }
  30. function __destruct() {
  31. $this->save_stats();
  32. $this->save_table();
  33. }
  34. private function save_stats() {
  35. file_put_contents($this->stats_file, serialize($this->stats));
  36. }
  37. private function save_table() {
  38. file_put_contents($this->table_file, serialize($this->table));
  39. }
  40. function __get($key) {
  41. if($key == 'data_valid')
  42. return $this->data_valid();
  43. }
  44. private function load_table() {
  45. if(file_exists($this->table_file))
  46. $this->table = unserialize(file_get_contents($this->table_file));
  47. if(!$this->table)
  48. $this->table = array();
  49. }
  50. private function load_stats() {
  51. if(file_exists($this->stats_file))
  52. $this->stats = unserialize(file_get_contents($this->stats_file));
  53. if(!$this->stats)
  54. $this->stats = new Stat();
  55. }
  56. private function data_valid() {
  57. return isset($this->data['nick']) && isset($this->data['score'])
  58. && isset($this->data['time']) && isset($this->data['lines'])
  59. && isset($this->data['figures']) && isset($this->data['drops'])
  60. && isset($this->data['dropLevels']) && isset($this->data['gameTime'])
  61. && isset($this->data['speed']) && is_numeric($this->data['speed'])
  62. && is_numeric($this->data['score']) && is_numeric($this->data['time'])
  63. && is_numeric($this->data['lines']) && is_numeric($this->data['figures'])
  64. && is_numeric($this->data['gameTime']) && is_numeric($this->data['drops'])
  65. && is_numeric($this->data['dropLevels']);
  66. }
  67. public function placeCurrent() {
  68. if(!$this->record) return $this->position = -1;
  69. $maxi = count($this->table) - 1;
  70. if($maxi<0) {
  71. $this->table = array($this->record);
  72. return $this->position = 0;
  73. }
  74. if(Record::compare($this->table[$maxi], $this->record) > 0) {
  75. if($maxi + 1 < self::max_records) {
  76. $this->table[] = $this->record;
  77. return $this->position = ++$maxi;
  78. }
  79. else
  80. return $this->position = -1;
  81. }
  82. $i = $maxi;
  83. while($i >= 0 && Record::Compare($this->table[$i], $this->record) <= 0) --$i;
  84. array_splice($this->table, $i+1, 0, array($this->record));
  85. if(count($this->table) > self::max_records)
  86. array_splice($this->table, self::max_records);
  87. return $this->position = ++$i;
  88. }
  89. public function toJson() {
  90. $maxi = count($this->table) - 1;
  91. $res = 'json={stats:' . $this->stats->toJson() . ',';
  92. $res .= 'total:' . count($this->table) . ',';
  93. $res .= 'pos:' . $this->position . ',';
  94. $extr = array();
  95. $i=0;
  96. $ints = $this->getIntervals($maxi);
  97. foreach($ints as $i => $int) {
  98. if($i) {
  99. if($int[0] - $ints[$i-1][1] == 2)
  100. $extr[] = $this->table[$int[0] - 1]->toJson($int[0] - 1);
  101. else
  102. $extr[] = 0;
  103. }
  104. for($j = $int[0]; $j<=$int[1]; ++$j)
  105. $extr[] = $this->table[$j]->toJson($j);
  106. }
  107. $res .= 'extract:[' . join(',', $extr) . ']';
  108. return $res . '}';
  109. }
  110. private function getIntervals($maxi) {
  111. if($maxi < self::show_records)return array(array(0, $maxi));
  112. $mid = floor((self::show_records-2) / 2); // part of records table shown in the middle
  113. $top = ceil($mid / self::goldsection); // first records shown
  114. $bot = self::show_records - 2 - $top - $mid;// records from the bottom
  115. if($bot==0) {
  116. ++$bot;
  117. --$mid;
  118. }
  119. $paddingTop = floor(($mid - 1) / 2);
  120. $paddingBot = $mid - 1 - $paddingTop;
  121. if($this->position == -1 || $this->position - $paddingTop - $top <= 0 || $this->position + $paddingBot + $bot >= $maxi) {
  122. $shift = $this->position + $paddingBot + $bot < $maxi;
  123. return array(
  124. array(0, floor(self::show_records * 2 / 3) -2 + ($shift)),
  125. array($maxi - ceil(self::show_records/3) + 1 + ($shift), $maxi)
  126. );
  127. }
  128. return array(
  129. array(0, $top - 1),
  130. array($this->position - $paddingTop, $this->position + $paddingBot),
  131. array($maxi - $bot + 1, $maxi)
  132. );
  133. }
  134. }