<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class sendMessageInGame extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'message:send {message?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send Message in game like "! Hello players"';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$msg = $this->argument('message');
$host = 'xxx.xxx.xxx.xxx';
$port = join_server_port;
$this->send_msg($host, $port, $msg);
return 0;
}
private function send_msg($host, $port, $msg)
{
$header = "C144A10024000000"; // Header
$msglength = strlen($msg);
if ($msglength < 34 && $msglength != 0) {
$divisor = (34 - $msglength);
$start_space = $this->int_int_divide($divisor, 2);
for ($i = 0; $i <= $start_space; $i++) {
$header .= "20";
}
$header .= $this->ascii2hex($msg);
for ($j = 0; $j <= ($divisor - $start_space); $j++) {
$header .= "20";
}
} else {
$header .= $this->ascii2hex($msg);
}
$header .= "00BED3410000F8BBB90400000000FCBBB904A4FF1A06F8F04100FFFFFFFF"; // Header
$packet = $this->hex2ascii($header); // Converti l'intera stringa in ASCII
// Crea un socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$x = @socket_connect($socket, $host, $port)) {
echo "No access to the Joinserver or Server is down.";
} else {
socket_write($socket, $packet, strlen($packet));
socket_close($socket);
}
}
private function ascii2hex($ascii)
{
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii[$i])));
$byte = str_repeat('0', 2 - strlen($byte)) . $byte;
$hex .= $byte . " ";
}
$hex = str_replace(" ", "", $hex);
return $hex;
}
private function hex2ascii($hex)
{
$ascii = '';
$hex = str_replace(" ", "", $hex);
for ($i = 0; $i < strlen($hex); $i = $i + 2) {
$ascii .= chr(hexdec(substr($hex, $i, 2)));
}
return ($ascii);
}
private function int_int_divide($x, $y)
{
if ($x == 0) return 0;
if ($y == 0) return false;
return ($x % $y >= $y / 2) ? (($x - ($x % $y)) / $y) + 1 : ($x - ($x % $y)) / $y;
}
}