-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.php
313 lines (280 loc) · 9.5 KB
/
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
class PokemonException extends Exception {}
class PokemonInvalidBattleException extends PokemonException {}
class PokemonBattleEndException extends PokemonException {}
class PokemonUnfinishedBattleException extends PokemonException {}
class Database {
private static $instance;
const PREFIX = 'rohajobu_';
private function __construct() {
}
static function getInstance() {
if(is_null(self::$instance)) {
self::$instance = new Predis\Client();
}
return self::$instance;
}
}
class Pokemon {
public $id;
public $nom;
public $famille;
public $type;
public $pv;
public $pvmax;
public $force;
public $defense;
public $initiative;
public $precision;
public $niveau = 1;
public $dernier_coup = null;
public $combattant = false;
protected $is_saved = true;
public $affinites = array('Electrik' => array('Electrik' => 1, 'Eau' => 2, 'Feu' => 1, 'Plante' => 1, 'Roche' => 0),
'Eau' => array('Electrik' => 0.5, 'Eau' => 0.5, 'Feu' => 2, 'Plante' => 0.5, 'Roche' => 2),
'Feu' => array('Electrik' => 1, 'Eau' => 0.5, 'Feu' => 0.5, 'Plante' => 2, 'Roche' => 0.5),
'Plante' => array('Electrik' => 1, 'Eau' => 2, 'Feu' => 0,5, 'Plante' => 0.5, 'Roche' => 2),
'Roche' => array('Electrik' => 2, 'Eau' => 0.5, 'Feu' => 1, 'Plante' => 2, 'Roche' => 1)
);
const COUP_TOUCHE = 0;
const COUP_RATE = 1;
const COUP_CRIT = 2;
const COUP_FORT = 3;
const COUP_FAIBLE = 4;
const COUP_CRIT_FORT = 5;
const COUP_CRIT_FAIBLE = 6;
public function __construct($nom) {
$this->id = self::getUniqueId();
$this->nom = $nom;
$this->pvmax = $this->pv;
$this->affinites = $this->affinites[$this->type];
$this->is_saved = false;
}
public function attaque($cible, $contre = false) {
$log = array();
// on vérifie si le coup touche
$log[] = $this->nom.' '.($contre ? 'contre-' : '').'attaque '.$cible->nom.'.';
if(mt_rand(1, 100) <= $this->precision) { // on touche !
$this->dernier_coup = self::COUP_TOUCHE;
// on calcule les dégâts
$degats = $this->force;
// gestion des affinités
$degats *= $this->affinites[$cible->type];
if($this->affinites[$cible->type] > 1) {
$this->dernier_coup = self::COUP_FORT;
$log[] = 'C’est super efficace !';
} elseif($this->affinites[$cible->type] < 1) {
$this->dernier_coup = self::COUP_FAIBLE;
$log[] = 'Ce n’est pas très efficace…';
}
$log = array_merge($log, $cible->defense($degats));
if($cible->pv <= 0) {
$log[] = $cible->nom.' est KO !';
}
} else { // on a raté
$this->dernier_coup = self::COUP_RATE;
$log[] = '… mais échoue.';
}
return $log;
}
public function defense($degats) {
$log = array();
// on applique la défense
$pv_perdus = round($degats * (1-($this->defense / 100)));
$log[] = $this->nom.' subit '.$pv_perdus.' points de dégats (pour '.$degats.' donnés).';
$this->pv -= $pv_perdus;
if($this->pv < 0) $this->pv = 0;
return $log;
}
static function getAll() {
$client = Database::getInstance();
$ids = $client->zrange(Database::PREFIX.'pokelist', 0, -1);
$pokemons = array();
foreach($ids as $id) {
$pokemons[$id] = self::get($id);
}
return $pokemons;
}
/**
* complexité : O(n)
* pour n valant le nombre total de pokémons
*/
static function getFree() {
$pokemons = self::getAll();
$free = array();
foreach($pokemons as $pkmn) {
if(!$pkmn->combattant) {
$free[$pkmn->id] = $pkmn;
}
}
return $free;
}
static function get($id) {
$client = Database::getInstance();
$data = $client->hget(Database::PREFIX.'pokemons', $id);
if($data) {
$obj = unserialize($data);
return $obj;
} else {
return false;
}
}
public function save() {
$data = serialize($this);
$client = Database::getInstance();
$client->hset(Database::PREFIX.'pokemons', $this->id, $data);
$client->zadd(Database::PREFIX.'pokelist', $this->id, $this->id);
if(!$this->is_saved) {
$client->set(Database::PREFIX.'pokeid', $this->id);
$this->is_saved = true;
}
}
public function delete() {
$client = Database::getInstance();
$client->hdel(Database::PREFIX.'pokemons', $this->id);
$client->zrem(Database::PREFIX.'pokelist', $this->id);
}
public function getUniqueId() {
$client = Database::getInstance();
$max_id = $client->zrevrange(Database::PREFIX.'pokelist', 0, 0);
if(!count($max_id)) {
return 0;
}
return $max_id[0]+1;
$max_id = $client->get(Database::PREFIX.'pokeid');
if($max_id === null) {
return 0;
} else {
return $max_id + 1;
}
}
}
class Pikachu extends Pokemon {
public $famille = 'Pikachu';
public $type = 'Electrik';
public $pv = 350;
public $force = 55;
public $defense = 40;
public $initiative = 90;
public $precision = 70;
}
class Carapuce extends Pokemon {
public $famille = 'Carapuce';
public $type = 'Eau';
public $pv = 440;
public $force = 48;
public $defense = 55;
public $initiative = 43;
public $precision = 80;
}
class Salameche extends Pokemon {
public $famille = 'Salamèche';
public $type = 'Feu';
public $pv = 390;
public $force = 52;
public $defense = 43;
public $initiative = 65;
public $precision = 50;
}
class Combat {
public $id;
const STATUT_DEBUT = 0;
const STATUT_ENCOURS = 1;
const STATUT_FIN = 2;
public $adversaires = array();
public $statut;
public $log = array();
public $precedents_logs = array();
protected $is_saved = true;
public function __construct(Pokemon $pkmn1, Pokemon $pkmn2) {
$this->id = $this->getUniqueId();
if($pkmn1->combattant || $pkmn2->combattant) {
throw new PokemonInvalidBattleException('Un pokémon en combat ne peut pas en combattre un autre.');
}
$this->statut = self::STATUT_DEBUT;
$this->adversaires[] = $pkmn1;
$this->adversaires[] = $pkmn2;
$pkmn1->combattant = true;
$pkmn2->combattant = true;
$pkmn1->save();
$pkmn2->save();
$this->is_saved = false;
$this->save();
}
public function round() {
if($this->statut == self::STATUT_FIN) {
throw new Exception('Le combat est terminé');
}
$this->statut = self::STATUT_ENCOURS;
$this->precedents_logs[] = $this->log;
$this->log = array();
$pkmn1 = $this->adversaires[0];
$pkmn2 = $this->adversaires[1];
if($pkmn1->initiative < $pkmn2->initiative) {
$this->log = array_merge($this->log, $pkmn2->attaque($pkmn1));
if($pkmn1->pv > 0) {
$this->log = array_merge($this->log, $pkmn1->attaque($pkmn2, true));
}
} else {
$this->log = array_merge($this->log, $pkmn1->attaque($pkmn2));
if($pkmn2->pv > 0) {
$this->log = array_merge($this->log, $pkmn2->attaque($pkmn1, true));
}
}
if($pkmn1->pv <= 0 || $pkmn2->pv <= 0) {
$this->statut = self::STATUT_FIN;
$pkmn1->combattant = false;
$pkmn2->combattant = false;
$this->save();
}
$pkmn1->save();
$pkmn2->save();
}
public function getUniqueId() {
$client = Database::getInstance();
$max_id = $client->get(Database::PREFIX.'combatid');
if($max_id === null) {
return 0;
} else {
return $max_id + 1;
}
}
static function getAll() {
$client = Database::getInstance();
$ids = $client->zrange(Database::PREFIX.'combatlist', 0, -1);
$combats = array();
foreach($ids as $id) {
$combats[$id] = self::get($id);
}
return $combats;
}
static function get($id) {
$client = Database::getInstance();
$data = $client->hget(Database::PREFIX.'combats', $id);
if($data) {
$obj = unserialize($data);
return $obj;
} else {
return false;
}
}
public function save() {
$data = serialize($this);
$client = Database::getInstance();
$client->hset(Database::PREFIX.'combats', $this->id, $data);
$client->zadd(Database::PREFIX.'combatlist', $this->id, $this->id);
if(!$this->is_saved) {
$client->set(Database::PREFIX.'combatid', $this->id);
$this->is_saved = true;
}
}
public function delete() {
if($this->statut == self::STATUT_FIN) {
$client = Database::getInstance();
$client->hdel(Database::PREFIX.'pokemons', $this->id);
$client->zrem(Database::PREFIX.'pokelist', $this->id);
} else {
throw new PokemonUnfinishedBattleException('Ce combat est en cours !');
}
}
}