Skip to content

Commit 622a2e2

Browse files
my atempt to add disegn patern factory to the code
1 parent d68049a commit 622a2e2

File tree

12 files changed

+232
-334
lines changed

12 files changed

+232
-334
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
C:30:"PHPUnit\Runner\TestResultCache":679:{a:2:{s:7:"defects";a:9:{s:7:"Warning";i:6;s:17:"HeroTest::testOne";i:3;s:18:"HeroTest::testHero";i:4;s:31:"GameTest::testFirstTurnFunction";i:4;s:28:"GameTest::testSkillsFunction";i:4;s:26:"GameTest::testLogsMessages";i:4;s:24:"HeroTest::testHeroUpdate";i:4;s:21:"MonsterTest::testHero";i:4;s:27:"MonsterTest::testHeroUpdate";i:4;}s:5:"times";a:9:{s:7:"Warning";d:0.008;s:17:"HeroTest::testOne";d:0.008;s:18:"HeroTest::testHero";d:0.001;s:24:"HeroTest::testHeroUpdate";d:0;s:31:"GameTest::testFirstTurnFunction";d:0.035;s:28:"GameTest::testSkillsFunction";d:0.009;s:26:"GameTest::testLogsMessages";d:0.03;s:21:"MonsterTest::testHero";d:0;s:27:"MonsterTest::testHeroUpdate";d:0;}}}
1+
C:30:"PHPUnit\Runner\TestResultCache":802:{a:2:{s:7:"defects";a:9:{s:7:"Warning";i:6;s:17:"HeroTest::testOne";i:3;s:18:"HeroTest::testHero";i:4;s:31:"GameTest::testFirstTurnFunction";i:4;s:28:"GameTest::testSkillsFunction";i:4;s:26:"GameTest::testLogsMessages";i:4;s:24:"HeroTest::testHeroUpdate";i:4;s:21:"MonsterTest::testHero";i:4;s:27:"MonsterTest::testHeroUpdate";i:4;}s:5:"times";a:12:{s:7:"Warning";d:0.008;s:17:"HeroTest::testOne";d:0.008;s:18:"HeroTest::testHero";d:0.001;s:24:"HeroTest::testHeroUpdate";d:0;s:31:"GameTest::testFirstTurnFunction";d:0.018;s:28:"GameTest::testSkillsFunction";d:0.027;s:26:"GameTest::testLogsMessages";d:0.04;s:21:"MonsterTest::testHero";d:0;s:27:"MonsterTest::testHeroUpdate";d:0;s:24:"MonsterTest::testMonster";d:0;s:30:"MonsterTest::testMonsterUpdate";d:0;s:28:"HeroTest::testSkillsFunction";d:0.004;}}}

class/gameEngineClass.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ function __construct(){
1111
}
1212

1313
function hero(){
14-
$buildHero = new buildHeroClass;
15-
return $buildHero->buildHero();
14+
$buildHero = new PlayerFactory;
15+
return $buildHero->createPlayer('hero');
1616
}
1717

1818
function monster(){
1919
// $monsterEncounter = (new DataBase)->runQuery('SELECT `id` FROM `monster` ORDER BY RAND() LIMIT 0,1;');
2020
$monsterEncounter = (new DataBase)->runQuery('SELECT `id` FROM `monster` where id = 1');
21-
$buildMonster = new buildMonsterClass;
22-
return $buildMonster->buildMonster($monsterEncounter[0]['id']);
21+
$buildMonster = new PlayerFactory;
22+
return $buildMonster->createPlayer('monster', $monsterEncounter[0]['id']);
2323
}
2424

2525
function figth($hero, $monster, $turn, $turnCounter = 1, $log_pass = ''){
@@ -51,9 +51,8 @@ function figth($hero, $monster, $turn, $turnCounter = 1, $log_pass = ''){
5151
// set damage
5252
$damage = $attacker->getStrength() - $defender->getDefence(); // Damage = Attacker strength – Defender defence
5353

54-
5554
// skils encounter
56-
$skill = $this->skills();
55+
$skill = $hero->skills();
5756
if($skill !== null && $skill['skill_type'] === 'attack' && $attacker->getName() === "Orderus"){
5857
$oldDamage = $damage;
5958
$damage = $skill['number_strikes'] * $damage; // 2 strikes results double the damage
@@ -94,6 +93,7 @@ function figth($hero, $monster, $turn, $turnCounter = 1, $log_pass = ''){
9493
}
9594

9695
function determinFirstTurnToAttack($hero, $monster, $turn){ // The first attack
96+
9797
if ($monster->getSpeed() > $hero->getSpeed()) {
9898
$turn = ['turn' => 'monster', 'id' => $monster->getId()];
9999
} elseif ($monster->getSpeed() < $hero->getSpeed()) {
@@ -107,23 +107,8 @@ function determinFirstTurnToAttack($hero, $monster, $turn){ // The first attack
107107
$turn = ['turn' => 'hero', 'id' => $hero->getId()];
108108
}
109109
}
110-
return $turn;
111-
}
112110

113-
function skills(){
114-
$skills = (new DataBase)->runQuery(
115-
'SELECT s.* FROM `hero` h
116-
INNER JOIN heros_skills hs on hs.id_hero = h.id
117-
inner join skill s on s.id = hs.id_skill
118-
WHERE h.id = 1'
119-
);
120-
121-
foreach($skills as $skill){
122-
if(rand(0, 100) < $skills[0]['skill_chance']) { // check the chance of the skill to occur
123-
return $skill;
124-
}
125-
}
126-
return null;
111+
return $turn;
127112
}
128113

129114

class/hero.php

Lines changed: 15 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,23 @@
11
<?php
2-
class Hero
3-
{
4-
private $id;
5-
private $level;
6-
private $experience;
7-
private $name;
8-
private $health;
9-
private $strength;
10-
private $defence;
11-
private $speed;
12-
private $luck;
13-
private $stats;
14-
15-
// function __construct($id, $level, $experience, $name, $health, $strength, $defence, $speed, $luck) {
16-
// $this->id = $id;
17-
// $this->level = $level;
18-
// $this->experience = $experience;
19-
// $this->name = $name;
20-
// $this->health = $health;
21-
// $this->strength = $strength;
22-
// $this->defence = $defence;
23-
// $this->speed = $speed;
24-
// $this->luck = $luck;
25-
// }
26-
27-
function getId(){
28-
return $this->id;
29-
}
30-
31-
function getLevel(){
32-
return $this->level;
33-
}
34-
35-
36-
function getExperience(){
37-
return $this->experience;
38-
}
39-
40-
41-
function getName(){
42-
return $this->name;
43-
}
44-
45-
46-
function getHealth(){
47-
return $this->health;
48-
}
49-
50-
function getStrength(){
51-
return $this->strength;
52-
}
53-
54-
function getDefence(){
55-
return $this->defence;
56-
}
57-
58-
function getSpeed(){
59-
return $this->speed;
60-
}
612

62-
public function getLuck(){
63-
return $this->luck;
64-
}
65-
66-
public function getStats(){
67-
return $this->stats;
68-
}
69-
70-
function setId($id){
71-
$this->id = $id;
72-
}
73-
74-
function setLevel($level){
75-
$this->level = $level;
76-
}
77-
78-
function setExperience($experience){
79-
$this->experience = $experience;
80-
}
81-
82-
function setName($name){
83-
$this->name = $name;
84-
}
85-
86-
function setHealth($health){
87-
$this->health = $health;
88-
}
89-
90-
function setStrength($strength){
91-
$this->strength = $strength;
92-
}
3+
class Hero extends Player
4+
{
5+
function skills(){
936

94-
function setDefence($defence){
95-
$this->defence = $defence;
96-
}
7+
$skills = (new DataBase)->runQuery(
8+
'SELECT s.* FROM `hero` h
9+
INNER JOIN heros_skills hs on hs.id_hero = h.id
10+
inner join skill s on s.id = hs.id_skill
11+
WHERE h.id = 1'
12+
);
9713

98-
function setSpeed($speed){
99-
$this->speed = $speed;
100-
}
14+
foreach($skills as $skill){
15+
if(rand(0, 100) < $skills[0]['skill_chance']) { // check the chance of the skill to occur
16+
return $skill;
17+
}
18+
}
10119

102-
public function setLuck($luck){
103-
$this->luck = $luck;
20+
return null;
10421
}
105-
106-
public function setStats($stats){
107-
$this->stats = $stats;
108-
}
109-
11022
}
11123
?>

class/heroBuild.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

class/monster.php

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,6 @@
11
<?php
2-
class Monster
2+
class Monster extends Player
33
{
4-
private $id;
5-
private $level;
6-
private $experience;
7-
private $name;
8-
private $health;
9-
private $strength;
10-
private $defence;
11-
private $speed;
12-
private $luck;
13-
private $stats;
14-
15-
function __construct($id, $level, $experience, $name, $health, $strength, $defence, $speed, $luck) {
16-
$this->id = $id;
17-
$this->level = $level;
18-
$this->experience = $experience;
19-
$this->name = $name;
20-
$this->health = $health;
21-
$this->strength = $strength;
22-
$this->defence = $defence;
23-
$this->speed = $speed;
24-
$this->luck = $luck;
25-
}
26-
27-
function setId($id){
28-
$this->id = $id;
29-
}
30-
31-
function setLevel($level){
32-
$this->level = $level;
33-
}
34-
35-
function setExperience($experience){
36-
$this->experience = $experience;
37-
}
38-
39-
function setName($name){
40-
$this->name = $name;
41-
}
42-
43-
function setHealth($health){
44-
$this->health = $health;
45-
}
46-
47-
function setStrength($strength){
48-
$this->strength = $strength;
49-
}
50-
51-
function setDefence($defence){
52-
$this->defence = $defence;
53-
}
54-
55-
function setSpeed($speed){
56-
$this->speed = $speed;
57-
}
58-
59-
function setLuck($luck){
60-
$this->luck = $luck;
61-
}
62-
63-
function getId(){
64-
return $this->id;
65-
}
66-
67-
function getLevel(){
68-
return $this->level;
69-
}
70-
71-
function getExperience(){
72-
return $this->experience;
73-
}
74-
75-
function getName(){
76-
return $this->name;
77-
}
78-
79-
function getHealth(){
80-
return $this->health;
81-
}
82-
83-
function getStrength(){
84-
return $this->strength;
85-
}
86-
87-
function getDefence(){
88-
return $this->defence;
89-
}
90-
91-
function getSpeed(){
92-
return $this->speed;
93-
}
94-
95-
function getLuck(){
96-
return $this->luck;
97-
}
98-
99-
function getStats(){
100-
return $this->stats;
101-
}
102-
103-
function setStats($stats){
104-
$this->stats = $stats;
105-
}
1064

1075
}
1086
?>

class/monsterBuild.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)