Nourriture::getConsommationNourritureHeure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	
7
	class Nourriture {
8
		private $date_last_check;
9
		
10
		
11
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
12
		public function __construct() {
13
			$last_check = $this->getLastCheckNourriture();
14
			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine();
15
			
16
			if ($last_check >= 3600) {
17
				$this->setNourritureConsomee(round($last_check/3600), $nb_unite);
18
				$this->setUpdateLastCheckNourriture();
19
			}
20
			else if (($last_check == 0) && ($nb_unite > 0)) {
21
				$this->setUpdateLastCheckNourriture();
22
			}
23
			
24
			if (Bataille::getRessource()->getNourriture() == 0) {
25
				Bataille::setValues(["nourriture_mort_heure" => $this->getNombreUniteMortHeure()]);
26
			}
27
			else {
28
				Bataille::setValues(["nourriture_consom_heure" => $this->getConsommationNourritureHeure()]);
29
			}
30
		}
31
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
32
		
33
		
34
		
35
		//-------------------------- GETTER ----------------------------------------------------------------------------//
36
		/**
37
		 * @return int
38
		 * fonction qui renvoi la durée écoulée depuis le dernier check de la nourriture
39
		 */
40
		private function getLastCheckNourriture() {
41
			$dbc= App::getDb();
42
			
43
			$query = $dbc->select("last_check_nourriture")->from("_bataille_base")
44
				->where("ID_identite", "=", Bataille::getIdIdentite(), "AND")
45
				->where("ID_base", "=", Bataille::getIdBase())
46
				->get();
47
			
48
			if ((is_array($query)) && (count($query) == 1)) {
49
				$today = Bataille::getToday();
50
				
51
				foreach ($query as $obj) {
52
					$last_check = $obj->last_check_nourriture;
53
				}
54
				
55
				$this->date_last_check = $last_check;
0 ignored issues
show
Bug introduced by
The variable $last_check does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
				
57
				$last_co = new \DateTime($last_check);
58
				$last_co = $last_co->getTimestamp();
59
				
60
				return $today-$last_co;
61
			}
62
			
63
			return 0;
64
		}
65
		
66
		/**
67
		 * @return mixed
68
		 * renvoi la consommation de nourriture d'une unité par heure
69
		 */
70
		private function getConsommationNourritureUnite() {
71
			return  Bataille::getParam("unite_nourriture_heure");
72
		}
73
		
74
		/**
75
		 * @return number
76
		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base
77
		 */
78
		private function getNombreUniteMortHeure() {
79
			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine();
80
			
81
			if ($nb_unite == 0) {
82
				return 0;
83
			}
84
			
85
			return abs(round((0-($nb_unite*$this->getConsommationNourritureUnite()))/100))+1;
86
		}
87
		
88
		/**
89
		 * @return number
90
		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base
91
		 */
92
		private function getConsommationNourritureHeure() {
93
			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine();
94
			
95
			return round(($nb_unite*$this->getConsommationNourritureUnite()));
96
		}
97
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
98
		
99
		
100
		
101
		//-------------------------- SETTER ----------------------------------------------------------------------------//
102
		/**
103
		 * remet la date de last_check_nourriture a Y-m-d H:i:s
104
		 */
105 View Code Duplication
		private function setUpdateLastCheckNourriture() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
			$dbc = App::getDb();
107
			
108
			$dbc->update("last_check_nourriture", date("Y-m-d H:i:s"))->from("_bataille_base")
109
				->where("ID_identite", "=", Bataille::getIdIdentite(), "AND")
110
				->where("ID_base", "=", Bataille::getIdBase())
111
				->set();
112
		}
113
		
114
		/**
115
		 * @param $nb_heure
116
		 * @param $nb_unite
117
		 * fonction qui calcule la nourriture consomee en 1h
118
		 * si elle est inférieur à 0 on tue un nombre d'unite defini en fonction de combien le nombre de
119
		 * nourriture est en dessous de 0 (valeur divisée par 100)
120
		 * puis on retire les ressources du grenier
121
		 */
122
		private function setNourritureConsomee($nb_heure, $nb_unite) {
123
			$nourriture_base = Bataille::getRessource()->getNourriture();
124
			$nourriture_consommee = ($nb_unite*$this->getConsommationNourritureUnite())*$nb_heure;
125
			
126
			$nourriture_retirer = $nourriture_base - $nourriture_consommee;
127
			
128
			if ($nourriture_retirer < 0) {
129
				$unite_tuer = (abs(round($nourriture_retirer/100)))+1;
130
				
131
				Bataille::getUnite()->setTuerUnites($unite_tuer);
132
			}
133
			
134
			Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $nourriture_consommee, "-");
135
		}
136
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
137
	}