Passed
Push — master ( ab8373...e7b2e8 )
by Anthony
02:31
created

Nourriture::setUpdateLastCheckNourriture()   A

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_last_connexion")->where("ID_identite", "=", Bataille::getIdIdentite())->get();
44
			
45
			if ((is_array($query)) && (count($query) == 1)) {
46
				$today = Bataille::getToday();
47
				
48
				foreach ($query as $obj) {
49
					$last_check = $obj->last_check_nourriture;
50
				}
51
				
52
				$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...
53
				
54
				$last_co = new \DateTime($last_check);
55
				$last_co = $last_co->getTimestamp();
56
				
57
				return $today-$last_co;
58
			}
59
			
60
			return 0;
61
		}
62
		
63
		/**
64
		 * @return mixed
65
		 * renvoi la consommation de nourriture d'une unité par heure
66
		 */
67
		private function getConsommationNourritureUnite() {
68
			return  Bataille::getParam("unite_nourriture_heure");
69
		}
70
		
71
		/**
72
		 * @return number
73
		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base
74
		 */
75
		private function getNombreUniteMortHeure() {
76
			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine();
77
			
78
			return abs(round((0-($nb_unite*$this->getConsommationNourritureUnite()))/100));
79
		}
80
		
81
		/**
82
		 * @return number
83
		 * renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base
84
		 */
85
		private function getConsommationNourritureHeure() {
86
			$nb_unite = Bataille::getUnite()->getNombreUniteHumaine();
87
			
88
			return round(($nb_unite*$this->getConsommationNourritureUnite()));
89
		}
90
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
91
		
92
		
93
		
94
		//-------------------------- SETTER ----------------------------------------------------------------------------//
95
		/**
96
		 * remet la date de last_check_nourriture a Y-m-d H:i:s
97
		 */
98
		private function setUpdateLastCheckNourriture() {
99
			$dbc = App::getDb();
100
			
101
			$dbc->update("last_check_nourriture", date("Y-m-d H:i:s"))->from("_bataille_last_connexion")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
102
		}
103
		
104
		/**
105
		 * @param $nb_heure
106
		 * @param $nb_unite
107
		 * fonction qui calcule la nourriture consomee en 1h
108
		 * si elle est inférieur à 0 on tue un nombre d'unite defini en fonction de combien le nombre de
109
		 * nourriture est en dessous de 0 (valeur divisée par 100)
110
		 * puis on retire les ressources du grenier
111
		 */
112
		private function setNourritureConsomee($nb_heure, $nb_unite) {
113
			$nourriture_base = Bataille::getRessource()->getNourriture();
114
			$nourriture_consommee = ($nb_unite*$this->getConsommationNourritureUnite())*$nb_heure;
115
			
116
			$nourriture_retirer = $nourriture_base - $nourriture_consommee;
117
			
118
			if ($nourriture_retirer < 0) {
119
				$unite_tuer = abs(round($nourriture_retirer/100));
120
				
121
				Bataille::getUnite()->setTuerUnites($unite_tuer);
122
			}
123
			
124
			Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $nourriture_consommee, "-");
125
		}
126
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
127
	}