PHP - Escribe en consola solo al terminar de ejecutar todo el código

 
Vista:
sin imagen de perfil

Escribe en consola solo al terminar de ejecutar todo el código

Publicado por fanaes (1 intervención) el 05/10/2017 19:17:44
Hola intente de buscar en Google pero no encuentro la forma de buscarlo u.u
lo que hago es ejecutar estos 4 métodos, en la cual cada uno va escribiendo en pantalla (print). pero el problema es que no me va escribiendo a medida que se ejecuta el código sino cuando finaliza por completo. No se si se configura desde php o que. Supongo que puede ser debido a que tarda mucho en ejecutar algunas lineas ya que obtiene datos externos (aprox 1 - 2 segundos cada vez que pide datos). estoy casi seguro de que cuando el código no era tan grande no pasaba esto. desde ya muchas gracias. =D
Datos:
PHP 7.0.21
Ejecutado en windows 7 con el cmd y con cygwin64.
El código lo dejo ahí abajo y esto es lo que me devuelve en consola.

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
Sesion iniciada correctamente!
Obteniendo ciudades :
    polisVino
    polisMarmol
    Polis
Optimizar el uso de vino:
    polisVino
        Vino: 18493
        Poblacion: 542 / 542
        Felicidad neta: 52
        Felicidad por el vino: 240
        Lv vino optimo: 4
        Ya se encontraba optimizado.
    polisMarmol
        Vino: 2778
        Poblacion: 295 / 580
        Felicidad neta: 188
        Felicidad por el vino: 240
        Lv vino optimo: 6
        Se deberia ampliar la taberna 4 / 6
        vino otimizado: 18/Hs
    Polis
        Vino: 0
        Poblacion: 130 / 142
        Felicidad neta: 0
        Felicidad por el vino: 0
        Lv vino optimo: 1
        Se deberia ampliar la taberna 0 / 1
        No se tiene vino suficiente para esta operacion.
Vino suficienteterminamos con la revision
terminamos con la revision


(El codigo esta incompleto falta la clase regular, y los métodos login y save pero son irrelevantes y se extendería mucho mas el codigo)
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
<?php
	#Build 20171005.1
	class Cuenta
	{
		#Atributos
		public $dom;
		public $headers= array();
		public $http;
		public $usuario;
		private $pwd;
		public $servidor;
		public $ciudades=array();
		public $token='DefaultToken';
		#Metodos
		function __construct($usuario, $pwd,$servidor)
		{
			$this->usuario = $usuario;
			$this->pwd = $pwd;
			$this->servidor=$servidor;
			$this->http= 'http://'.$servidor.'.ikariam.gameforge.com/index.php?';
			array_push($this->headers, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Content-Type: application/x-www-form-urlencoded","Host: ".$servidor.".ikariam.gameforge.com");
		}
		public function token(){
			#Revisar! no me gusta como se repite el codigo ese.
			$token = $this->dom;
			$token = strstr($token, 'actionRequest: "');
    		$token = substr($token, 16);
    		$token = strstr($token, '"',true);
    		if($this->token ==$token){
				$token = self::save('view=city');
				$token = strstr($token, 'actionRequest: "');
	    		$token = substr($token, 16);
	    		$token = strstr($token, '"',true);
    		}
    		return $token;
		}
		public function verificarEstado(){
			$this->dom=self::save('view=city');
			if(preg_match(regular::Usuario(), $this->dom)){
				print "Sesion iniciada correctamente!\n";
			}else{
				print "Problema al iniciar Sesion \n";
				self::ReintentarLogeo();
			}
		}
		public function RecolectarDatos(){
			#self::ObtenerCiudades();
			#self::OptimizarVino();
			#self::RevisarVino();
		}
		public function ObtenerCiudades(){
			print "Obteniendo ciudades :\n";
			$rta= preg_match_all(regular::Ciudad(), $this->dom, $M);
			if($rta > 0){
				for($i=0; $i < $rta;$i++){
					self::AgregarCiudad($M[1][$i], $M[2][$i], $M[3][$i], $M[4][$i], $M[5][$i]);
				}
			}
		}
		public function AgregarCiudad($id,$nombre ,$x, $y, $trade){
			print "    " . $nombre . "\n";
			array_push($this->ciudades, new Ciudad($id,$nombre ,$x, $y, $trade));
		}
		public function OptimizarVino(){
			print "Optimizar el uso de vino: \n";
			foreach ($this->ciudades as $ciudad) {
				print "    " . $ciudad->nombre . "\n";
				self::CambiarCiudad($ciudad->id);
				$this->dom=self::save('view=city');
				$rta=preg_match_all(regular::Recursos(), $this->dom, $M);
				$recursos=array();
				for($i=0;$i<$rta;$i++){
					array_push($recursos, $M[2][$i]);
				}
				$conv = array("," => "");
				$ciudad->madera 	= strtr($recursos[0], $conv);
				$ciudad->vino 		= strtr($recursos[1], $conv);
				$ciudad->marmol 	= strtr($recursos[2], $conv);
				$ciudad->cristal 	= strtr($recursos[3], $conv);
				$ciudad->azufre 	= strtr($recursos[4], $conv);
				print "        Vino: " . $ciudad->vino . "\n";
				preg_match(regular::ConsumoVino(), $this->dom, $M);
				$token=self::token();
				$this->dom=self::save('view=townHall&cityId='.$ciudad->id.'&position=0&backgroundView=city&currentCityId='.$ciudad->id.'&actionRequest='.$token.'&ajax=1');
				$rta=preg_match(regular::Felicidad(), $this->dom, $M);
				print "        Poblacion: " . $M[3] . " / " . $M[1] . "\n";
				print "        Felicidad neta: " . $M[4] . "\n";
				print "        Felicidad por el vino: " . $M[2] . "\n";
				$ceil = ( $M[1] - $M[3] - $M[4] + $M[2] ) / 60;
				$lv = ceil($ceil);
				print "        Lv vino optimo: " . $lv . "\n";
				if($lv*60 == $M[2]){
					print "        Ya se encontraba optimizado.\n";
				}else{
					$lvTaberna = self::ObtenerLv('Tavern');
					if($lv > $lvTaberna){
						print "        Se deberia ampliar la taberna " . $lvTaberna . " / " . $lv ."\n";
						$lv = $lvTaberna;
					}
					if(Taberna::VinoPorHora($lv) <= $ciudad->vino && $lv > 0){
						print "        vino otimizado: " . Taberna::VinoPorHora($lv) . "/Hs\n";
						$this->DarVino($lv, $ciudad);
					}else{
						print "        No se tiene vino suficiente para esta operacion.\n";
					}
					$ciudad->consumoVino = Taberna::VinoPorHora($lv); #Relativo ya que no se aplico el descuento.
				}
			}
		}
		public function ObtenerLv($Edificio){
			$this->dom = self::save('view=city');
			$rta= preg_match_all(regular::Edificio(), $this->dom, $M);
			for ($i=0; $i < $rta; $i++) {
				if($M[1][$i] == $Edificio){
					return $M[3][$i];
				}
			}
			return 0;
		}
		public function ObtenerPosicion($Edificio){
			$this->dom = self::save('view=city');
			$rta= preg_match_all(regular::Edificio(), $this->dom, $M);
			for ($i=0; $i < $rta; $i++) {
				if($M[1][$i] == $Edificio){
					return $M[2][$i];
				}
			}
			return 0;
		}
		public function DarVino($lv, $ciudad){
			$token = self::token();
			self::save('action=CityScreen&function=assignWinePerTick&cityId='.$ciudad->id.'&position='.self::ObtenerPosicion('Tavern').'&amount='.$lv.'&backgroundView=city&currentCityId='.$ciudad->id.'&templateView=tavern&actionRequest='.$token.'&ajax=1');
		}
		public function CambiarCiudad($id){
			$token=self::token();
			self::save('action=header&function=changeCurrentCity&actionRequest='.$token.'&oldView=city&cityId='.$id.'&backgroundView=city&currentCityId='.$id.'&ajax=1');
		}
		public function cantidadCiudades(){
			return count($this->ciudades);
		}
		public function RevisarVino(){
			print "";
			foreach ($this->ciudades as $ciudad) {
				$this->dom = self::save('view=city');
				$rta = preg_match(regular::Deposito(), $this->dom,$M);
				if($ciudad->vino >  ($ciudad->consumoVino * 24 * 7)){
					print "Vino suficiente";
				}else{
					print 'terminamos con la revision' . "\n";
				}
			}
		}
	}
 
 
 
	class Ciudad
	{
		public $deposito;
		public $id;
		public $nombre;
		public $x;
		public $y;
		public $bienLujo;
		public $madera;
		public $vino;
		public $marmol;
		public $cristal;
		public $azufre;
		public $consumoVino;
		function __construct($id,$nombre ,$x, $y, $trade)
		{
			$this->id = $id;
			$this->nombre = $nombre;
			$this->x = $x;
			$this->y = $y;
			$this->bienLujo = $trade;
		}
	}
	class Transporte
	{
		public $id;
		public $materiales = array();
		public $origen;
		public $destino;
		public $llegada;
		function __construct($id, $materiales, $origen, $destino, $llegada)
		{
 
		}
	}
	class Taberna
	{
		public static function VinoPorHora($lv){
			$vinoPorHora = array(0, 4, 8, 13, 18, 24, 30, 37, 44, 51, 60, 68, 78, 88, 99, 110, 122, 136, 150, 165, 180, 197, 216, 235, 255, 277, 300, 325, 351, 378, 408, 439, 472, 507, 544, 584, 626, 670, 717, 766, 818, 874, 933, 995, 1060, 1129, 1202, 1280, 1362);
			return $vinoPorHora[$lv];
		}
	}
$cuenta = new Cuenta('usuario','pass','test');
$cuenta->verificarEstado();
$cuenta->ObtenerCiudades();
$cuenta->OptimizarVino();
$cuenta->RevisarVino();
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder