/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
- <?php
- include_once 'models/game.php';
- class MoveFactory {
- static function fromMysqlRow($row) {
- $type = $row['type'];
- $move = NULL;
-
- if ($type == 'add_source') {
- $count = $row['param1'];
- $move = new AddFountainMove();
-
- } else if ($type == 'add_horse') {
- $player = $row['object'];
- $count = $row['param1'];
- $move = new AddHorseMove($player, $count);
-
- } else if ($type == 'remove_horse') {
- $player = $row['subject'];
- $fountain = $row['object'];
- $pos = $row['param1'];
- $length = $row['param2'];
- $status = $row['param3'];
- $move = new RemoveHorseMove($fountain, $player, $pos, $length, $status);
-
- } else if ($type == 'place_horse') {
- $player = $row['subject'];
- $fountain = $row['object'];
- $pos = $row['param1'];
- $length = $row['param2'];
- $status = $row['param3'];
- $move = new PlaceHorseMove($fountain, $player, $pos, $length, $status);
-
- } else if ($type == 'empty_move') {
- $fountain = $row['object'];
- $player = $row['subject'];
-
- $move = new EmptyMove($fountain, $player);
- } else if ($type == 'close_fountain'){
- $fountain = $row['object'];
- $player = $row['subject'];
- $status = $row['param3'];
- $move = new CloseFountainMove($fountain, $player, $status);
- } else if ($type == 'new_fountain'){
- $status = $row['param3'];
- $move = new AddFountainMove($status);
- }
- return $move;
- }
- }
- /*
- move : game_id , round , performed_at , type , object , subject , param1 , param2 , extra
- */
- abstract class Move {
- // Don forget call parent::save($game)!
- public function save(Game $game) {
- $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
- $game->values['checksum'] += 1;
- query($q);
- }
-
- abstract public function append_to_model(Game $game);
- }
- class AddHorseMove extends Move {
- public $color;
- public $count;
-
- function __construct($color, $count) {
- $this->color = $color;
- $this->count = $count;
- }
-
- function save(Game $game) {
- parent::save($game);
-
- $query_string = sprintf("INSERT INTO move(game_id, type, object, param1) ".
- "VALUES (%d, 'add_horse', %d, %d);",
- $game->values['id'],
- $this->color,
- $this->count);
- query($query_string);
- }
-
- function append_to_model(Game $game){
- $game->players[$this->color]->horses += $this->count;
- return true;
- }
- }
- class PlaceHorseMove extends Move{
- public $fountain;
- public $start;
- public $color;
- public $length;
- public $horse_nr;
-
- function __construct($fountain, $color, $start, $length, $horse_nr){
- $this->fountain = (int)$fountain;
- $this->color = (int)$color;
- $this->start = (int)$start;
- $this->length = (int)$length;
- $this->horse_nr = (int)$horse_nr;
- }
-
- function save(Game $game){
- parent::save($game);
-
- $query_string = sprintf("INSERT INTO move(game_id, type, subject, object, round, param1, param2, param3) ".
- "VALUES (%d, 'place_horse', %d, %d, %d, %d, %d, %d);",
- $game->values['id'],
- $this->color,
- $this->fountain,
- $game->fountains[$this->fountain]->move_count[$this->color],
- $this->start,
- $this->length,
- $this->horse_nr);
- query($query_string);
- }
-
- function append_to_model(Game $game){
- // TODO: better error handling
- if (!isset($game->players[$this->color])) {
- Log::e("Player with this color isn't logged in.");
- return false;
- }
-
- if ($game->players[$this->color]->horses < 1) {
- Log::e("Player with this color isn't logged in.");
- return false;
- }
-
- if (!$game->validPlaceHorse($this)) {
- Log::e("No space for new horse.");
- return false;
- }
-
- global $POINTS_FOR_HORSE;
-
- $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
- if($this->horse_nr != -1){
- $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
- $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count+1];
- $game->fountains[$this->fountain]->insertCells($this->color,$this->start,$this->length);
- } else {
- $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
- $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count+1];
- $game->players[$this->color]->horses--;
- $game->fountains[$this->fountain]->insertCells($this->color,$this->start,$this->length);
- }
-
- return true;
- }
- }
- class RemoveHorseMove extends Move{
- public $fountain;
- public $start;
- public $color;
- public $length;
- public $horse_nr;
- function __construct($fountain, $color, $start, $length, $horse_nr){
- $this->fountain = $fountain;
- $this->color = $color;
- $this->start = $start;
- $this->length = $length;
- $this->horse_nr = $horse_nr;
- }
-
- function save(Game $game){
- parent::save($game);
- $query_string = sprintf("INSERT INTO move(game_id, type, subject, object, round, param1, param2, param3) ".
- "VALUES (%d, 'remove_horse', %d, %d, %d, %d, %d, %d);",
- $game->values['id'],
- $this->color,
- $this->fountain,
- $game->fountains[$this->fountain]->move_count[$this->color],
- $this->start,
- $this->length,
- $this->horse_nr);
- query($query_string);
-
- $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
- $game->values['checksum'] += 1;
- query($q);
- }
-
- function append_to_model(Game $game){
- // player with this color couldn't participate in game
- if (!isset($game->players[$this->color])) {
- Log::e("Player with this color isn't logged in.");
- return false;
- }
- global $POINTS_FOR_HORSE;
- $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
- if($this->horse_nr != -1){
- // needed if we remove in previously placed horse with different number than the last
- $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
- $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
- $game->fountains[$this->fountain]->removeCellsWithCount($this->color,$this->start,$this->length,$this->horse_nr);
- } else {
- $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
- $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
- $game->players[$this->color]->horses+= 1;
- $game->fountains[$this->fountain]->removeCells($this->color,$this->start,$this->length);
- }
- return true;
- }
- }
- /*
- Empty move means that horse count is reduced, but no horses are not placed
- Needed in competition
- */
- class EmptyMove extends Move {
- public $fountain;
- public $color;
- function __construct($fountain,$color){
- $this->fountain = $fountain;
- $this->color = $color;
- }
-
- function save(Game $game){
- parent::save($game);
- $query_string = sprintf("INSERT INTO move(game_id, type, object, subject) ".
- "VALUES (%d, 'empty_move', %d, %d);",
- $game->values['id'],
- $this->fountain,
- $this->color);
- query($query_string);
-
- $q = sprintf("UPDATE game SET checksum = checksum + 1 WHERE id = %d;", $game->values['id']);
- $game->values['checksum'] += 1;
- query($q);
- }
-
- function append_to_model(Game $game){
- if (!isset($game->players[$this->color])) {
- Log::e("Player with this color isn't logged in.");
- return false;
- }
-
- if ($game->players[$this->color]->horses < 1) {
- Log::e("Can't remove no more horses.");
- return false;
- }
-
- global $POINTS_FOR_HORSE;
- $horse_count = $game->fountains[$this->fountain]->move_count[$this->color];
- if($horse_count < 1){
- $game->players[$this->color]->horses--;
- } else {
- $game->players[$this->color]->score -= $POINTS_FOR_HORSE[$horse_count];
- $game->players[$this->color]->score += $POINTS_FOR_HORSE[$horse_count-1];
- $game->players[$this->color]->horses--;
- }
-
-
- return true;
- }
- }
- class CloseFountainMove extends Move{
- public $fountain;
- public $color;
- public $status;
-
- function __construct($fountain,$color,$status){
- $this->fountain = (int)$fountain;
- $this->color = (int)$color;
- $this->status = (int)$status;
- }
-
- function save(Game $game){
- parent::save($game);
- $query_string = sprintf(
- "INSERT INTO move(game_id, type, subject, object, param3) ".
- "VALUES (%d, 'close_fountain', %d, %d, %d);",
- $game->values['id'],
- $this->color,
- $this->fountain,
- $this->status);// param3 marks if fountain is full or not
- query($query_string);
- }
- function append_to_model(Game $game){
- if($game->fountains[$this->fountain]->status != $this->status){
- $game->fountains[$this->fountain]->status = $this->status;
- return true;
- }
- return false;
-
- }
- }
- class AddFountainMove extends Move{
- private $status = 1;
-
- function __construct($status = 1){
- $this->status = (int)$status;
- }
-
- function save(Game $game){
- parent::save($game);
-
- $query_string = sprintf("INSERT INTO move(game_id, type, param3) ".
- "VALUES (%d, 'new_fountain', %d);",
- $game->values['id'],
- $this->status);
- query($query_string);
- }
-
- function append_to_model(Game $game){
- if($game->addFountain()){
- $game->fountains[] = new Fountain(1);
- return true;
- }
- return false;
- }
- }
- ?>