forked from jstar88/opbe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattle.php
142 lines (139 loc) · 4.33 KB
/
Battle.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
<?php
/**
* OPBE
* Copyright (C) 2013 Jstar
*
* This file is part of OPBE.
*
* OPBE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OPBE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OPBE. If not, see <http://www.gnu.org/licenses/>.
*
* @package OPBE
* @author Jstar <[email protected]>
* @copyright 2013 Jstar <[email protected]>
* @license http://www.gnu.org/licenses/ GNU AGPLv3 License
* @version 21-03-2015
* @link https://github.com/jstar88/opbe
*/
class Battle
{
private $attackers;
private $defenders;
private $report;
private $battleStarted;
/**
* Battle::__construct()
*
* @param PlayerGroup $attackers
* @param PlayerGroup $defenders
* @return Battle
*/
public function __construct(PlayerGroup $attackers, PlayerGroup $defenders)
{
$this->attackers = $attackers;
$this->defenders = $defenders;
$this->battleStarted = false;
$this->report = new BattleReport();
}
/**
* Battle::startBattle()
*
* @return null
*/
public function startBattle($debug = false)
{
if (!$debug)
ob_start();
$this->battleStarted = true;
//only for initial fleets presentation
log_var('attackers', $this->attackers);
log_var('defenders', $this->defenders);
$round = new Round($this->attackers, $this->defenders, 0);
$this->report->addRound($round);
for ($i = 1; $i <= ROUNDS; $i++)
{
$att_lose = $this->attackers->isEmpty();
$deff_lose = $this->defenders->isEmpty();
//if one of they are empty then battle is ended, so update the status
if ($att_lose || $deff_lose)
{
$this->checkWhoWon($att_lose, $deff_lose);
$this->report->setBattleResult($this->attackers->battleResult, $this->defenders->battleResult);
if (!$debug)
ob_get_clean();
return;
}
//initialize the round
$round = new Round($this->attackers, $this->defenders, $i);
$round->startRound();
//add the round to the combatReport
$this->report->addRound($round);
//if($i==2) die('Round: '.$this->report->getRound(0)->getNumber()); // ERRORE
//update the attackers and defenders after round
$this->attackers = $round->getAfterBattleAttackers();
$this->defenders = $round->getAfterBattleDefenders();
}
//check status after all rounds
$this->checkWhoWon($this->attackers->isEmpty(), $this->defenders->isEmpty());
if (!$debug)
ob_get_clean();
return true;
}
/**
* Battle::checkWhoWon()
* Assign to groups the status win,lose or draw
* @param boolean $att_lose
* @param boolean $deff_lose
* @return null
*/
private function checkWhoWon($att_lose, $deff_lose)
{
if ($att_lose && !$deff_lose)
{
$this->attackers->battleResult = BATTLE_LOSE;
$this->defenders->battleResult = BATTLE_WIN;
}
elseif (!$att_lose && $deff_lose)
{
$this->attackers->battleResult = BATTLE_WIN;
$this->defenders->battleResult = BATTLE_LOSE;
}
else
{
$this->attackers->battleResult = BATTLE_DRAW;
$this->defenders->battleResult = BATTLE_DRAW;
}
}
/**
* Battle::__toString()
*
* @return
*/
public function __toString()
{
return $this->report->__toString();
}
/**
* Battle::getReport()
* Start the battle if not and return the report.
* @return BattleReport
*/
public function getReport()
{
if (!$this->battleStarted)
{
$this->startBattle();
}
return $this->report;
}
}