/
PluginDmChessGame.class.php
162 lines (135 loc) · 3.32 KB
/
PluginDmChessGame.class.php
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
<?php
/**
* PluginDmChessGame
*
* This class has been auto-generated by the Doctrine ORM Framework
*
* @package ##PACKAGE##
* @subpackage ##SUBPACKAGE##
* @author ##NAME## <##EMAIL##>
* @version SVN: $Id: Builder.php 6820 2009-11-30 17:27:49Z jwage $
*/
abstract class PluginDmChessGame extends BaseDmChessGame
{
protected
$board;
public function getCopy()
{
if($this->hasCache('virtual_copy'))
{
return $this->getCache('virtual_copy');
}
$playerTable = dmDb::table('DmChessPlayer');
$pieceTable = dmDb::table('DmChessPiece');
$virtualCopy = $this->getTable()->create($this->getData());
foreach($this->get('Players') as $player)
{
$_player = $playerTable->create();
$_player->setData($player->getData());
$virtualCopy->get('Players')->add($_player);
foreach($player->get('Pieces') as $piece)
{
$_piece = $piece->getTable()->create();
$_piece->setData($piece->getData());
$_player->get('Pieces')->add($_piece);
}
}
return $this->setCache('virtual_copy', $virtualCopy);
}
public function movePieceByHumanPos($from, $to)
{
return $this->getBoard()->getPieceByKey($this->getBoard()->humanPosToKey($from))
->moveToSquareKey($this->getBoard()->humanPosToKey($to));
}
public function clearCache($cacheKey = null)
{
parent::clearCache($cacheKey);
if(null === $cacheKey)
{
if($virtualCopy = $this->getCache('virtual_copy'))
{
$virtualCopy->free(true);
}
$this->getBoard()->clearCache();
foreach($this->get('Players') as $player)
{
$player->clearCache();
}
}
return $this;
}
public function getBoard()
{
if(null === $this->board)
{
if($sc = $this->getTable()->getServiceContainer())
{
$this->board = $sc->setParameter('dm_chess.game', $this)->getService('dm_chess_board');
}
else
{
throw new dmException('Need a service container');
}
}
return $this->board;
}
public function getPlayerByColor($color)
{
foreach($this->get('Players') as $player)
{
if ($color === $player->get('color'))
{
return $player;
}
}
}
public function getPieces()
{
$pieces = array();
foreach($this->get('Players') as $player)
{
$pieces = array_merge($pieces, $player->get('Pieces')->getData());
}
return $pieces;
}
public function getCurrentPlayer()
{
foreach($this->get('Players') as $player)
{
if ($player->isMyTurn())
{
return $player;
}
}
}
public function getWinner()
{
foreach($this->get('Players') as $player)
{
if ($player->isWinner)
{
return $player;
}
}
}
public function addTurn()
{
$this->set('turns', $this->get('turns')+1);
return $this;
}
public function __toString()
{
return '#'.$this->get('id'). 'turn '.$this->get('turns');
}
public function preInsert($event)
{
parent::preInsert($event);
$this->code = dmString::random(8);
}
public function start()
{
$this->isStarted = true;
$this->getEventDispatcher()->notify(new dmChessEvent($this, 'dm.chess.game_start'));
return $this;
}
}