PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/models/game/move.php

https://github.com/MarxGames/E-Hobused
PHP | 328 lines | 292 code | 30 blank | 6 comment | 28 complexity | cde0b0ef0da097ce6fc5678bc64f29d3 MD5 | raw file
  1. <?php
  2. include_once 'models/game.php';
  3. class MoveFactory {
  4. static function fromMysqlRow($row) {
  5. $type = $row['type'];
  6. $move = NULL;
  7. if ($type == 'add_source') {
  8. $count = $row['param1'];
  9. $move = new AddFountainMove();
  10. } else if ($type == 'add_horse') {
  11. $player = $row['object'];
  12. $count = $row['param1'];
  13. $move = new AddHorseMove($player, $count);
  14. } else if ($type == 'remove_horse') {
  15. $player = $row['subject'];
  16. $fountain = $row['object'];
  17. $pos = $row['param1'];
  18. $length = $row['param2'];
  19. $status = $row['param3'];
  20. $move = new RemoveHorseMove($fountain, $player, $pos, $length, $status);
  21. } else if ($type == 'place_horse') {
  22. $player = $row['subject'];
  23. $fountain = $row['object'];
  24. $pos = $row['param1'];
  25. $length = $row['param2'];
  26. $status = $row['param3'];
  27. $move = new PlaceHorseMove($fountain, $player, $pos, $length, $status);
  28. } else if ($type == 'empty_move') {
  29. $fountain = $row['object'];
  30. $player = $row['subject'];
  31. $move = new EmptyMove($fountain, $player);
  32. } else if ($type == 'close_fountain'){
  33. $fountain = $row['object'];
  34. $player = $row['subject'];
  35. $status = $row['param3'];
  36. $move = new CloseFountainMove($fountain, $player, $status);
  37. } else if ($type == 'new_fountain'){
  38. $status = $row['param3'];
  39. $move = new AddFountainMove($status);
  40. }
  41. return $move;
  42. }
  43. }
  44. /*
  45. move : game_id , round , performed_at , type , object , subject , param1 , param2 , extra
  46. */
  47. abstract class Move {
  48. // Don forget call parent::save($game)!
  49. public function save(Game $game) {
  50. $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
  51. $game->values['checksum'] += 1;
  52. query($q);
  53. }
  54. abstract public function append_to_model(Game $game);
  55. }
  56. class AddHorseMove extends Move {
  57. public $color;
  58. public $count;
  59. function __construct($color, $count) {
  60. $this->color = $color;
  61. $this->count = $count;
  62. }
  63. function save(Game $game) {
  64. parent::save($game);
  65. $query_string = sprintf("INSERT INTO move(game_id, type, object, param1) ".
  66. "VALUES (%d, 'add_horse', %d, %d);",
  67. $game->values['id'],
  68. $this->color,
  69. $this->count);
  70. query($query_string);
  71. }
  72. function append_to_model(Game $game){
  73. $game->players[$this->color]->horses += $this->count;
  74. return true;
  75. }
  76. }
  77. class PlaceHorseMove extends Move{
  78. public $fountain;
  79. public $start;
  80. public $color;
  81. public $length;
  82. public $horse_nr;
  83. function __construct($fountain, $color, $start, $length, $horse_nr){
  84. $this->fountain = (int)$fountain;
  85. $this->color = (int)$color;
  86. $this->start = (int)$start;
  87. $this->length = (int)$length;
  88. $this->horse_nr = (int)$horse_nr;
  89. }
  90. function save(Game $game){
  91. parent::save($game);
  92. $query_string = sprintf("INSERT INTO move(game_id, type, subject, object, round, param1, param2, param3) ".
  93. "VALUES (%d, 'place_horse', %d, %d, %d, %d, %d, %d);",
  94. $game->values['id'],
  95. $this->color,
  96. $this->fountain,
  97. $game->fountains[$this->fountain]->move_count[$this->color],
  98. $this->start,
  99. $this->length,
  100. $this->horse_nr);
  101. query($query_string);
  102. }
  103. function append_to_model(Game $game){
  104. // TODO: better error handling
  105. if (!isset($game->players[$this->color])) {
  106. Log::e("Player with this color isn't logged in.");
  107. return false;
  108. }
  109. if ($game->players[$this->color]->horses < 1) {
  110. Log::e("Player with this color isn't logged in.");
  111. return false;
  112. }
  113. if (!$game->validPlaceHorse($this)) {
  114. Log::e("No space for new horse.");
  115. return false;
  116. }
  117. global $POINTS_FOR_HORSE;
  118. $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
  119. if($this->horse_nr != -1){
  120. $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
  121. $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count+1];
  122. $game->fountains[$this->fountain]->insertCells($this->color,$this->start,$this->length);
  123. } else {
  124. $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
  125. $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count+1];
  126. $game->players[$this->color]->horses--;
  127. $game->fountains[$this->fountain]->insertCells($this->color,$this->start,$this->length);
  128. }
  129. return true;
  130. }
  131. }
  132. class RemoveHorseMove extends Move{
  133. public $fountain;
  134. public $start;
  135. public $color;
  136. public $length;
  137. public $horse_nr;
  138. function __construct($fountain, $color, $start, $length, $horse_nr){
  139. $this->fountain = $fountain;
  140. $this->color = $color;
  141. $this->start = $start;
  142. $this->length = $length;
  143. $this->horse_nr = $horse_nr;
  144. }
  145. function save(Game $game){
  146. parent::save($game);
  147. $query_string = sprintf("INSERT INTO move(game_id, type, subject, object, round, param1, param2, param3) ".
  148. "VALUES (%d, 'remove_horse', %d, %d, %d, %d, %d, %d);",
  149. $game->values['id'],
  150. $this->color,
  151. $this->fountain,
  152. $game->fountains[$this->fountain]->move_count[$this->color],
  153. $this->start,
  154. $this->length,
  155. $this->horse_nr);
  156. query($query_string);
  157. $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
  158. $game->values['checksum'] += 1;
  159. query($q);
  160. }
  161. function append_to_model(Game $game){
  162. // player with this color couldn't participate in game
  163. if (!isset($game->players[$this->color])) {
  164. Log::e("Player with this color isn't logged in.");
  165. return false;
  166. }
  167. global $POINTS_FOR_HORSE;
  168. $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
  169. if($this->horse_nr != -1){
  170. // needed if we remove in previously placed horse with different number than the last
  171. $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
  172. $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
  173. $game->fountains[$this->fountain]->removeCellsWithCount($this->color,$this->start,$this->length,$this->horse_nr);
  174. } else {
  175. $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
  176. $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
  177. $game->players[$this->color]->horses+= 1;
  178. $game->fountains[$this->fountain]->removeCells($this->color,$this->start,$this->length);
  179. }
  180. return true;
  181. }
  182. }
  183. /*
  184. Empty move means that horse count is reduced, but no horses are not placed
  185. Needed in competition
  186. */
  187. class EmptyMove extends Move {
  188. public $fountain;
  189. public $color;
  190. function __construct($fountain,$color){
  191. $this->fountain = $fountain;
  192. $this->color = $color;
  193. }
  194. function save(Game $game){
  195. parent::save($game);
  196. $query_string = sprintf("INSERT INTO move(game_id, type, object, subject) ".
  197. "VALUES (%d, 'empty_move', %d, %d);",
  198. $game->values['id'],
  199. $this->fountain,
  200. $this->color);
  201. query($query_string);
  202. $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
  203. $game->values['checksum'] += 1;
  204. query($q);
  205. }
  206. function append_to_model(Game $game){
  207. if (!isset($game->players[$this->color])) {
  208. Log::e("Player with this color isn't logged in.");
  209. return false;
  210. }
  211. if ($game->players[$this->color]->horses < 1) {
  212. Log::e("Can't remove no more horses.");
  213. return false;
  214. }
  215. global $POINTS_FOR_HORSE;
  216. $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
  217. if($horse_count < 1){
  218. $game->players[$this->color]->horses--;
  219. } else {
  220. $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
  221. $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
  222. $game->players[$this->color]->horses--;
  223. }
  224. return true;
  225. }
  226. }
  227. class CloseFountainMove extends Move{
  228. public $fountain;
  229. public $color;
  230. public $status;
  231. function __construct($fountain,$color,$status){
  232. $this->fountain = (int)$fountain;
  233. $this->color = (int)$color;
  234. $this->status = (int)$status;
  235. }
  236. function save(Game $game){
  237. parent::save($game);
  238. $query_string = sprintf(
  239. "INSERT INTO move(game_id, type, subject, object, param3) ".
  240. "VALUES (%d, 'close_fountain', %d, %d, %d);",
  241. $game->values['id'],
  242. $this->color,
  243. $this->fountain,
  244. $this->status);// param3 marks if fountain is full or not
  245. query($query_string);
  246. }
  247. function append_to_model(Game $game){
  248. if($game->fountains[$this->fountain]->status != $this->status){
  249. $game->fountains[$this->fountain]->status = $this->status;
  250. return true;
  251. }
  252. return false;
  253. }
  254. }
  255. class AddFountainMove extends Move{
  256. private $status = 1;
  257. function __construct($status = 1){
  258. $this->status = (int)$status;
  259. }
  260. function save(Game $game){
  261. parent::save($game);
  262. $query_string = sprintf("INSERT INTO move(game_id, type, param3) ".
  263. "VALUES (%d, 'new_fountain', %d);",
  264. $game->values['id'],
  265. $this->status);
  266. query($query_string);
  267. }
  268. function append_to_model(Game $game){
  269. if($game->addFountain()){
  270. $game->fountains[] = new Fountain(1);
  271. return true;
  272. }
  273. return false;
  274. }
  275. }
  276. ?>