Skip to content

Instantly share code, notes, and snippets.

@hackimov
Last active May 5, 2020 21:21
Show Gist options
  • Select an option

  • Save hackimov/057dc97083215c7c97c96d7e0d42c7f5 to your computer and use it in GitHub Desktop.

Select an option

Save hackimov/057dc97083215c7c97c96d7e0d42c7f5 to your computer and use it in GitHub Desktop.
timezero game authorization procedure and online stand bot
<?php
// автор [Топор Правосудия]
class TzBot
{
protected static $login = 'Логин';
protected static $password = 'Пароль';
protected static $local_ip = 'Локальный IP';
protected static $last_time;
protected static $session_id;
protected static $client;
protected static $chat;
protected static $key;
/** функция соединяет бота с игровым сервером timezero */
public static function ConnectClient()
{
// city1 terra prima - city2 rapa nui
self::$client = fsockopen('city1.timezero.ru', 5190);
if (self::$client !== false) {
socket_set_blocking(self::$client, false);
$key_data = '';
while(!preg_match('/.*KEY s=\"(.*)\".*/m', $key_data, $key_match)){
$key_data .= fgets(self::$client);
}
self::$key = $key_match[1];
fputs(self::$client, "\r\n\r\n");
fputs(self::$client, "<LOGIN lang=\"ru\" v3=\"" . self::$local_ip . "\" v2=\"7.0.1 (7.1.2.6)\" v=\"108\" open_pssw=\"1\" p=\"" . self::$password . "\" l=\"" . self::$login . "\" />\r\n\r\n");
$pocket_data = '';
while(!preg_match('/.*ses="(.*)".*/U', $pocket_data, $session_preg_data)){
$pocket_data .= fgets(self::$client);
}
self::$session_id = $session_preg_data[1];
} else {
die('неправильный сервак сука!');
}
}
/** функция соединяет бота с сервером чата timezero */
public static function ConnectChat()
{
fputs(self::$client, "<CHAT />\r\n\r\n");
self::$chat = fsockopen("chat.timezero.ru", 5190);
fputs(self::$chat, "<CHAT ses=\"" . self::$session_id . "\" l=\"" . self::$login . "\" />\r\n\r\n");
socket_set_blocking(self::$client, false);
socket_set_blocking(self::$chat, false);
}
/** функция каждые N секунд говорит серверу тз о том, что мы на связи */
public static function ImOnline($seconds = 15)
{
if(self::$last_time === null){
self::$last_time = time();
}
$timer = time() - self::$last_time;
if ($timer >= $seconds) {
fputs(self::$client, "<N />\r\n\r\n");
fputs(self::$chat, "<N />\r\n\r\n");
self::$last_time = time();
}
}
/** завершение PHP процесса в случае потери соединения */
public static function DieIfDisconnected($data)
{
if (!is_resource($data) || feof($data)) {
die ();
}
}
/** функция Encryptor служит для шифрования паролей от банковских ячеек (ранее шифровала пароли персов) */
public static function Encryptor($pass, $key){
$pass = substr ($pass,0,1) . substr($key,0,10) . substr ($pass,1) . substr($key,10);
$str = strtoupper(sha1($pass));
$out = '';
$mix = [35, 6, 4, 25, 7, 8, 36, 16, 20, 37, 12, 31, 39, 38, 21, 5, 33, 15, 9, 13, 29, 23, 32, 22, 2, 27, 1, 10, 30, 24, 0, 19, 26, 14, 18, 34, 17, 28, 11, 3];
for($i=0; $i<40; $i++) {$out[$mix[$i]] = substr($str, $i, 1);}
return ($out);
}
/** функция Str2Hex является системной, и была переписана мной на PHP из декомпилированного AS2 */
public static function Str2Hex($s){
return '%' . implode('%', str_split(bin2hex($s), 2));
}
/** функция декодирования ответов timezero содержащих Unicode символы */
public static function TzResponseDecoder($data)
{
$replace = [
"\x01S" => ".1\" name=\"b1-g2\" txt=\"Boulder\" massa=\"5\" st=\"G,H\" made=\"AR$\" section=\"0\" damage=\"S2-5\" shot=\"7-1\" nskill=\"4\" OD=\"1\" type=\"9.1\"/>",
"\x01i" => " psy=\"0\" man=\"3\" maxPsy=\"0\" ODratio=\"1\" img=\"rat\" group=\"2\" battleid=\"",
"\x01z" => ".1\" name=\"b2-s6\" txt=\"Radioactive materials\" massa=\"800\" ",
"\x01x" => ".1\" name=\"b2-s2\" txt=\"Precious metals\" massa=\"500\" ",
"\x01Z" => "\" ne=\",,,,,\" ne2=\",,,,,\" nark=\"0\" gluk=\"0\" ",
"\x01|" => ".1\" name=\"b2-s3\" txt=\"Polymers\" massa=\"30\" ",
"\x01k" => ".1\" name=\"b2-s5\" txt=\"Silicon\" massa=\"50\" ",
"\x01w" => ".1\" name=\"b2-s4\" txt=\"Organic\" massa=\"30\" ",
"\x01}" => "<BATTLE t=\"45\" t2=\"45\" turn=\"1\" cl=\"0\" ",
"\x01Q" => ".1\" name=\"b2-s8\" txt=\"Venom\" massa=\"70\" ",
"\x01y" => ".1\" name=\"b2-s7\" txt=\"Gems\" massa=\"80\" ",
"\x01^" => "t=\"2\"/></USER><USER login=\"",
"\x01T" => " psy=\"0\" man=\"1\" maxHP=\"",
"\x01K" => "\" txt=\"Coins\" massa=\"1\" ",
"\x01J" => " txt=\"BankCell Key (copy) #",
"\x01V" => " ODratio=\"1\" loc_time=\"",
"\x01B" => "><a sf=\"6\" t=\"2\"/><a ",
"\x01v" => ".1\" slot=\"GH\" name=\"b",
"\x01b" => ".1\" slot=\"A\" name=\"b",
"\x01d" => ".1\" slot=\"B\" name=\"b",
"\x01h" => ".1\" slot=\"C\" name=\"b",
"\x01e" => ".1\" slot=\"D\" name=\"b",
"\x01g" => ".1\" slot=\"E\" name=\"b",
"\x01{" => ".1\" slot=\"F\" name=\"b",
"\x01Y" => "\" virus=\"0\" login=\"",
"\x01~" => "section=\"0\" damage=\"",
"\x01W" => "></USER><USER login=\"",
"\x01R" => "\" p=\"\"/></L><L X=\"",
"\x01r" => "<TURN><USER login=\"",
"\x01M" => "\" t=\"2\"/><a sf=\"",
"\x01N" => "\" t=\"1\" direct=\"",
"\x01D" => "\"/>\n</O>\n<O id=\"",
"\x01U" => " freeexchange=\"1\" ",
"\x01o" => "/><a sf=\"6\" t=\"",
"\x01`" => "><a sf=\"0\" t=\"",
"\x01P" => "\" ODratio=\"1\" ",
"\x01c" => "\" t=\"5\" xy=\"",
"\x01n" => "\" maxquality=\"",
"\x01q" => "\" made=\"AR$\" ",
"\x01t" => "\" st=\"G,H\" ",
"\x01A" => "=\"1\" type=\"",
"\x01E" => "\"/>\n<O id=\"",
"\x01\\" => "\" calibre=\"",
"\x01O" => "\" build_in=\"",
"\x01p" => "\" damage=\"S",
"\x01f" => "\"/><MAP v=\"",
"\x01l" => "\" quality=\"",
"\x01u" => "\" type=\"1\"",
"\x01" => "\" section=\"",
"\x01j" => ".1\" slot=\"",
"\x01m" => "\" massa=\"1",
"\x01s" => "\" nskill=\"",
"\x01I" => " txt=\"ammo ",
"\x01L" => " cost=\"0\" ",
"\x01C" => "protect=\"S",
"\x01]" => "\" count=\"",
"\x02" => "\"/><a sf=\"",
"\x01a" => "\" range=\"",
"\x01_" => "\" shot=\"",
"\x01X" => "\" slot=\"",
"\x03" => ">\n<O id=\"",
"\x04" => "><O id=\"",
"\x01H" => " min=\"",
"\x01F" => "level=",
"\x01G" => "zzzzzz",
"\b" => " name=\"b",
"\x07" => " txt=\"",
"\x05" => " ",
"\x06" => "00\" "
];
foreach ($replace as $rep_key => $rep_val) {
$data = str_replace($rep_key, $rep_val, $data);
}
return $data;
}
/** функция тела бота в котором мы делаем действия */
public static function BotBody()
{
while (true) {
// завершение PHP процесса в случае потери соединения
self::DieIfDisconnected(self::$client);
// сообщение серверу ТЗ о том, что мы на связи
self::ImOnline(15);
// принимаем ответы от сервера, если в запросе присутствует говно то декодируем
$client_response = self::TzResponseDecoder(fgets(self::$client));
// принимаем ответы чата от сервера
$chat_response = fgets(self::$chat);
// тут можно оперировать с API timezero и писать свои условия используя пакеты приходящие от сервера.
// данные приходящие от сервера лежат в переменных chat_response и client_response
}
}
}
// запуск
TzBot::ConnectClient();
TzBot::ConnectChat();
TzBot::BotBody();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment