Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Zapisywanie liczby graczy do bazy danych
Forum PHP.pl > Forum > PHP
teez
Witam,

Mam skrypt oparty o class.hlsocket.php.

  1. <?php
  2. /**
  3.  * File is released under GPL as can be found on
  4.  * <a href="http://www.gnu.org/licenses/gpl.html" target="_blank">http://www.gnu.org/licenses/gpl.html</a>
  5.  */
  6.  
  7.  
  8. /* Info string */
  9. define('A2S_INFO', "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00");
  10.  
  11. /* Replies for HL Version 1 and Version 2 (aka Source) */
  12. define('REPLY_INFO_HL1', 'm');
  13. define('REPLY_INFO_HL2', 'I');
  14.  
  15. /* Definitions of the bytes */
  16. define('BYTE', 1);
  17. define('BYTE_NUM', BYTE + 1);
  18. define('SHORT', BYTE_NUM + 1);
  19. define('LONG', SHORT + 1);
  20. define('FLOAT', LONG + 1);
  21. define('STRING', FLOAT + 1);
  22.  
  23. /**
  24.  * The socket class
  25.  * @author Herwin Weststrate aka Hdez
  26.  * @contact hdez@counter-strike.nl
  27.  * @version 2005.10.21
  28.  */
  29. class HLSocket
  30. {
  31. /* The socket file descriptor */
  32. var $_socket;
  33.  
  34. /* The way to split the incoming data */
  35. var $_split_info_hl2 = array('type' => BYTE, 'id' => BYTE, 'bersion' => BYTE_NUM, 'hostname' => STRING, 'map' => STRING, 'gamedir' => STRING, 'gamedesc' => STRING, 'appid' => SHORT, 'unknown' => BYTE_NUM, 'players' => BYTE_NUM, 'max' => BYTE_NUM, 'bots' => BYTE_NUM, 'dedicated' => BYTE, 'os' => BYTE, 'passworded' => BYTE_NUM, 'secure' => BYTE_NUM, 'gameversion' => STRING );
  36. var $_split_info_hl1 = array('type' => BYTE, 'id' => BYTE, 'ip' => STRING, 'hostname' => STRING, 'map' => STRING, 'gamedir' => STRING, 'gamedesc' => STRING, 'players' => BYTE_NUM, 'max' => BYTE_NUM, 'version' => BYTE_NUM, 'dedicated' => BYTE, 'os' => BYTE, 'passworded' => BYTE_NUM, 'secure' => BYTE_NUM, 'gameversion' => STRING);
  37.  
  38.  
  39. /**
  40.   * Create a new socket
  41.   * @param $host The ip or hostname
  42.   * @param $port The port
  43.   */
  44. function HLSocket($host, $port)
  45. {
  46. $this->connect($host, $port);
  47. $this->port = $port;
  48. $this->host = $host;
  49. }
  50.  
  51. /**
  52.   * Actually make the connection to the host
  53.   * @param $host The ip or hostname
  54.   * @param $port The port
  55.   */
  56. function connect($host, $port)
  57. {
  58. $this->_socket = @fsockopen('udp://'.$host, $port);
  59. if (!$this->_socket)
  60. echo 'Error met connecten';
  61. stream_set_timeout($this->_socket, 1); // Set timeout to 1 sec
  62. }
  63.  
  64. /**
  65.   * Close the connection (and the socket fd)
  66.   */
  67. function close()
  68. {
  69. fclose($this->_socket);
  70. }
  71.  
  72. /**
  73.   * Query the server for the details
  74.   * @return associative array with the game info
  75.   */
  76. function details()
  77. {
  78. $this->write(A2S_INFO);
  79. $data = $this->read();
  80. $res = array();
  81. switch(substr($data, 0, 1))
  82. {
  83. case REPLY_INFO_HL1:
  84. $res = $this->split($this->_split_info_hl1, $data);
  85. break;
  86. case REPLY_INFO_HL2:
  87. $res = $this->split($this->_split_info_hl2, $data);
  88. break;
  89. }
  90. return $res;
  91. }
  92.  
  93. /**
  94.   * Write the given message over the socket
  95.   * @param $msg The message to be written
  96.   * @deprecated This should be issued as a private function
  97.   */
  98. function write($msg)
  99. {
  100. fwrite($this->_socket, $msg);
  101. }
  102.  
  103. /**
  104.   * Read from the socket
  105.   * @return The data from the socket (excluding the first four [useless] bytes)
  106.   * @deprecated This should be issued as a private function
  107.   */
  108. function read()
  109. {
  110. $data = fread($this->_socket, 1);
  111. $status = socket_get_status($this->_socket);
  112. if (isset($status['unread_bytes']) && $status['unread_bytes'] > 0)
  113. $data .= fread($this->_socket, $status['unread_bytes']);
  114. return substr($data, 4);
  115. }
  116.  
  117. /**
  118.   * Split the given datatype from $data String and return the value
  119.   * @param $type The data type [BYTE .. STRING]
  120.   * @param $data The current data String
  121.   * @return The value of the given data type from $data
  122.   * @deprecated This should be issued as a private function
  123.   */
  124. function splititem($type, &$data) {
  125. $add = '';
  126. switch ($type)
  127. {
  128. case BYTE:
  129. $add = substr($data, 0, 1);
  130. $data = substr($data, 1);
  131. break;
  132. case BYTE_NUM:
  133. $add = ord(substr($data, 0, 1));
  134. $data = substr($data, 1);
  135. break;
  136. case SHORT:
  137. $add = ord(substr($data, 0, 1));
  138. $data = substr($data, 1);
  139. break;
  140. case LONG:
  141. $add = ord(substr($data, 0, 1));
  142. $data = substr($data, 1);
  143. break;
  144. case STRING:
  145. do
  146. {
  147. $char = substr($data, 0, 1);
  148. if ($char != "\x00")
  149. $add .= $char;
  150. $data = substr($data, 1);
  151. }
  152. while ($char != "\x00");
  153. break;
  154. }
  155. return $add;
  156. }
  157.  
  158. /**
  159.   * Split the given datatypes from $data String and return the value
  160.   * @param $array The data type [BYTE .. STRING] as values of an
  161.   * associative array. The keys are also the keys of
  162.   * the return array
  163.   * @param $data The current data String
  164.   * @return Associative array with keys of $array and values read from $data
  165.   * @deprecated This should be issued as a private function
  166.   */
  167. function split($array, $data)
  168. {
  169. $res = array();
  170. foreach ($array as $k=>$v)
  171. $res[$k] = $this->splititem($v, $data);
  172. return $res;
  173. }
  174. }
  175. ?>





Chciałbym aby zapisywał liczbę graczy co np. 10 minut do bazy danych oraz odświeżał połączenie z serwerem co 5-6 minut, bo przy 1 000+ serwerów będę musiał lecieć na nie wiadomo jakim dedyku.
Liczę na jakąkolwiek podowiedź z waszej strony, nie gotowy kod. smile.gif
Pozdrawiam.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.