Combu Server  3.1.1
PHP API Documentation
GameMatch.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class GameMatch extends DataClass {
11 
12  const TABLE_NAME = "Match";
13 
14  public $Id = 0;
15  public $IdTournament = 0;
16  public $Title = "";
17  public $DateCreation = NULL;
18  public $DateExpire = NULL;
19  public $Rounds = 0;
20  public $Finished = 0;
21 
25  public function __construct($src = null, $stripSlashes = false) {
26  global $Database;
27  if ($src == null)
28  return;
29  if (is_array($src)) {
30  // Load by array
31  $this->_loadByRow($src, $stripSlashes);
32  } else if (is_numeric($src) && intval($src) > 0) {
33  // Load by Id
34  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
35  }
36  }
37 
52  public static function Load($idTournament = 0, $idUser = 0, $activeOnly = FALSE, $title = "", $customData = array(), $order = NULL, $limit = NULL, $offset = NULL, $count = NULL, $returnArray = FALSE) {
53  global $Database;
54  $where = "";
55  if ($idTournament > 0) {
56  $where .= ($where == "" ? "" : " AND ") . sprintf("(IdTournament = %d)", $idTournament);
57  } else if ($idTournament == -1) {
58  $where .= ($where == "" ? "" : " AND ") . "(IdTournament = 0)";
59  }
60  if ($idUser > 0) {
61  if ($activeOnly) {
62  $where .= ($where == "" ? "" : " AND ") . "(DateExpire IS NULL OR DateExpire < CURRENT_DATE())" .
63  sprintf(" AND (Finished = 0 AND Id IN (SELECT IdMatch FROM %s WHERE IdAccount = %d))", self::GetTableName(GameMatch_Account::class), $idUser);
64  } else {
65  $where .= ($where == "" ? "" : " AND ") . sprintf("(Finished = 1 AND Id IN (SELECT IdMatch FROM %s WHERE IdAccount = %d))", self::GetTableName(GameMatch_Account::class), $idUser);
66  }
67  }
68  if ($title) {
69  $where .= sprintf("(Title REGEXP '%s')", $Database->Escape($title));
70  }
71  if ($customData && is_array($customData)) {
72  $whereCustom = "";
73  foreach ($customData as $search_row) {
74  if (!is_array($search_row)) {
75  continue;
76  }
77  $key = $search_row["key"];
78  $op = $search_row["op"];
79  $value = $search_row["value"];
80  $whereCustom .= ($whereCustom == "" ? "" : " AND ") . sprintf("(DataKey = '%s' AND DataValue %s '%s')", $Database->Escape($key), $op, $Database->Escape($value));
81  }
82  if ($whereCustom) {
83  $where .= ($where == "" ? "" : " AND ") . sprintf("(Id IN (SELECT IdMatch FROM %s WHERE %s))", self::GetTableName(GameMatch_CustomData::class), $whereCustom);
84  }
85  }
86  if (!$order) {
87  $order = "Id";
88  }
89  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, $order, $limit, $offset, $count);
90  }
91 
97  public function Save() {
98  global $Database;
99  if ($this->Id > 0) {
100  $query = sprintf("UPDATE %s SET Title = '%s', Rounds = %d, DateExpire = %s, Finished = %d WHERE Id = %d",
101  self::GetTableName(__CLASS__),
102  $Database->Escape($this->Title),
103  $this->Rounds,
104  $Database->EscapeDate($this->DateExpire),
105  $this->Finished,
106  $this->Id);
107  } else {
108  $this->DateCreation = date("Y-m-d H:i:s");
109  $query = sprintf("INSERT INTO %s (IdTournament, Title, Rounds, DateCreation, DateExpire, Finished) VALUES (%d, '%s', %d, %s, %s, 0)",
110  self::GetTableName(__CLASS__),
111  $this->IdTournament,
112  $Database->Escape($this->Title),
113  $this->Rounds,
114  $Database->EscapeDate($this->DateCreation),
115  $Database->EscapeDate($this->DateExpire));
116  }
117  if ($Database->Query($query)) {
118  if ($this->Id < 1) {
119  $this->Id = $Database->InsertedId();
120  }
121  return TRUE;
122  }
123  return FALSE;
124  }
125 
130  public function Delete() {
131  if ($this->Id > 0) {
132  if ($this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
133  $this->_Delete(self::GetTableName(GameMatch_CustomData::class), "IdMatch = " . $this->Id);
134  // Delete all match accounts and rounds
135  $accounts = GameMatch_Account::Load($this->Id);
136  foreach ($accounts as $account) {
137  $account->Delete();
138  }
139  return TRUE;
140  }
141  }
142  return FALSE;
143  }
144 
149  public static function Prune() {
150  self::TruncateClass(__CLASS__);
151  self::TruncateClass(GameMatch_Account::class);
152  self::TruncateClass(GameMatch_CustomData::class);
153  self::TruncateClass(GameMatch_Round::class);
154  }
155 
159  public function CheckFinished() {
160  $allScores = TRUE;
161  $accounts = GameMatch_Account::Load($this->Id);
162  foreach ($accounts as $a) {
163  $rounds = GameMatch_Round::Load($a->Id);
164  foreach ($rounds as $r) {
165  if (!$r->DateScore) {
166  $allScores = FALSE;
167  break;
168  }
169  }
170  }
171  if ($allScores) {
172  $this->Finished = 1;
173  if ($this->Save()) {
174  // Check if the tournament has finished
175  $tournament = new Tournament($this->IdTournament);
176  if ($tournament->Id > 0)
177  $tournament->CheckFinished();
178  }
179  }
180  }
181 
182  public function ToArray() {
183  $array = Utils::ObjectToArray($this);
184  // Add the Match Custom Data
185  $array["CustomData"] = array();
186  $customData = GameMatch_CustomData::Load($this->Id);
187  foreach ($customData as $data) {
188  $array["CustomData"][$data->DataKey] = $data->DataValue;
189  }
190  // Add the Accounts
191  $array["Users"] = array();
192  $accounts = GameMatch_Account::Load($this->Id);
193  foreach ($accounts as $i) {
194  $array["Users"][] = $i->ToArray();
195  }
196  // Return the full array
197  return $array;
198  }
199 
200  public function ToJson() {
201  $array = $this->ToArray();
202  return json_encode($array);
203  }
204 }
static Prune()
Definition: GameMatch.php:149
static Load($idTournament=0, $idUser=0, $activeOnly=FALSE, $title="", $customData=array(), $order=NULL, $limit=NULL, $offset=NULL, $count=NULL, $returnArray=FALSE)
Definition: GameMatch.php:52
__construct($src=null, $stripSlashes=false)
Definition: GameMatch.php:25
Definition: Account.php:3