Получение онлайна игроков SAMP сервера
Есть лаунчер написанный на C# WPF.
Нужно в label (который в лаунчере) вывести кол-во онлайн игроков на сервере SA:MP.
Библиотек, которые могли бы помочь с этим нет.
Как поступать в данный момент не знаю, буду рад любой подсказке, готов оплатить кофе с печеньками в качестве благодарности по итогу.
Просьба не ругать, буду рад каждой зацепке.
Нашел вот такой скрипт... Возможно с его помощью можно что-то реализовать.
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Windows;
namespace SAMP
{
public class SAMP : IDisposable
{
Socket qSocket;
IPAddress address;
int _port = 0;
string _password = null;
string[] results = new string[50];
int _count = 0;
public SAMP(string addr, int port, string password, bool dns)
{
qSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
qSocket.SendTimeout = 5000;
qSocket.ReceiveTimeout = 5000;
if (dns)
{
try
{
address = Dns.GetHostAddresses(addr)[0];
}
catch { }
}
else
{
try
{
address = IPAddress.Parse(addr);
}
catch { }
}
_port = port;
_password = password;
}
public bool Send(string command)
{
try
{
IPEndPoint endpoint = new IPEndPoint(address, _port);
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write("SAMP".ToCharArray());
string[] SplitIP = address.ToString().Split('.');
writer.Write(Convert.ToByte(SplitIP[0]));
writer.Write(Convert.ToByte(SplitIP[1]));
writer.Write(Convert.ToByte(SplitIP[2]));
writer.Write(Convert.ToByte(SplitIP[3]));
writer.Write((ushort)_port);
writer.Write('x');
writer.Write((ushort)_password.Length);
writer.Write(_password.ToCharArray());
writer.Write((ushort)command.Length);
writer.Write(command.ToCharArray());
}
if (qSocket.SendTo(stream.ToArray(), endpoint) > 0) return true;
}
}
catch
{
return false;
}
return false;
}
public int Receive()
{
try
{
for (int i = 0; i < results.GetLength(0); i++) results.SetValue(null, i);
_count = 0;
EndPoint endpoint = new IPEndPoint(address, _port);
byte[] rBuffer = new byte[500];
int count = qSocket.ReceiveFrom(rBuffer, ref endpoint);
using (MemoryStream stream = new MemoryStream(rBuffer))
{
using (BinaryReader reader = new BinaryReader(stream))
{
if (stream.Length <= 11) return _count;
reader.ReadBytes(11);
short len;
try
{
while ((len = reader.ReadInt16()) != 0) results[_count++] = new string(reader.ReadChars((int)len));
}
catch
{
return _count;
}
}
}
}
catch
{
return _count;
}
return _count;
}
public string[] Store(int count = -1)
{
string[] rString = new string[count != -1 ? count : _count];
for (int i = 0; (i < count || count == -1) && i < _count; i++) rString[i] = results[i];
_count = 0;
return rString;
}
public void Dispose()
{
try
{
qSocket.Dispose();
}
catch
{
}
}
}
public class Query : IDisposable
{
Socket qSocket;
IPAddress address;
int _port = 0;
string[] results;
int _count = 0;
DateTime[] timestamp = new DateTime[2];
public string passworded;
public string players;
public string max_players;
public string hostname;
public string gamemode;
public string mapname;
public enum PaketOpcode
{
Info = 'i',
Rules = 'r',
ClientList = 'c',
DetailedClientList = 'd',
Ping = 'p'
}
public Query(string addr, int port, bool dns)
{
qSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
qSocket.SendTimeout = 5000;
qSocket.ReceiveTimeout = 5000;
if (dns)
{
try
{
address = Dns.GetHostAddresses(addr)[0];
}
catch (Exception e)
{
MessageBox.Show("An error has occured in SAMP API!\n\n" + e.ToString());
Environment.Exit(2);
}
}
else
{
try
{
address = IPAddress.Parse(addr);
}
catch
{
}
}
_port = port;
}
public bool Send(char opcode, string sign = "1337")
{
try
{
EndPoint endpoint = new IPEndPoint(address, _port);
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write("SAMP".ToCharArray());
string[] SplitIP = address.ToString().Split('.');
writer.Write(Convert.ToByte(SplitIP[0]));
writer.Write(Convert.ToByte(SplitIP[1]));
writer.Write(Convert.ToByte(SplitIP[2]));
writer.Write(Convert.ToByte(SplitIP[3]));
writer.Write((ushort)_port);
writer.Write(opcode);
if (opcode == 'p') writer.Write(sign.ToCharArray());
timestamp[0] = DateTime.Now;
}
if (qSocket.SendTo(stream.ToArray(), endpoint) > 0) return true;
}
}
catch
{
return false;
}
return false;
}
public int Receive()
{
try
{
_count = 0;
EndPoint endpoint = new IPEndPoint(address, _port);
byte[] rBuffer = new byte[500];
qSocket.ReceiveFrom(rBuffer, ref endpoint);
timestamp[1] = DateTime.Now;
using (MemoryStream stream = new MemoryStream(rBuffer))
{
using (BinaryReader reader = new BinaryReader(stream))
{
if (stream.Length <= 10) return _count;
reader.ReadBytes(10);
switch (reader.ReadChar())
{
case 'i':
{
results = new string[6];
passworded = reader.ReadByte().ToString();
//results[_count++] = reader.ReadByte().ToString(); // either 0 or 1, depending whether if the password has been set.
players = reader.ReadInt16().ToString();
//results[_count++] = reader.ReadInt16().ToString(); // current amount of players online on the server
max_players = reader.ReadInt16().ToString();
//results[_count++] = reader.ReadInt16().ToString(); // maximum amount of players that can join the server
hostname = new string(reader.ReadChars(reader.ReadInt32()));
//results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // hostname
gamemode = new string(reader.ReadChars(reader.ReadInt32()));
//results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // gamemode
mapname = new string(reader.ReadChars(reader.ReadInt32()));
//results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // mapname
return 6;
}
case 'r':
{
int rulecount = reader.ReadInt16();
results = new string[rulecount * 2];
for (int i = 0; i < rulecount; i++)
{
results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule name (key)
results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule value (value)
}
return _count;
}
case 'c':
{
int playercount = reader.ReadInt16();
results = new string[playercount * 2];
for (int i = 0; i < playercount; i++)
{
results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // nickname
results[_count++] = reader.ReadInt32().ToString(); // score
}
return _count;
}
case 'd':
{
int playercount = reader.ReadInt16();
results = new string[playercount * 4];
for (int i = 0; i < playercount; i++)
{
results[_count++] = reader.ReadByte().ToString(); //playerid
results[_count++] = new string(reader.ReadChars(reader.ReadByte())); //nick
results[_count++] = reader.ReadInt32().ToString(); //score
results[_count++] = reader.ReadInt32().ToString(); //ping
}
return _count;
}
case 'p':
{
results = new string[1];
results[_count++] = timestamp[1].Subtract(timestamp[0]).Milliseconds.ToString(); // time difference
results[_count++] = new string(reader.ReadChars(4)); // paket signature
return _count;
}
default: return _count;
}
}
}
}
catch
{
return _count;
}
}
public string[] Store(int count)
{
string[] rString = new string[count];
for (int i = 0; i < count && i < _count; i++) rString[i] = results[i];
_count = 0;
return rString;
}
public void Dispose()
{
try
{
qSocket.Dispose();
}
catch
{
}
}
}
}
Еще реализация на PHP:
<?php
/**
* This file is part of GameQ.
*
* GameQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace GameQ\Protocols;
use GameQ\Exception\Protocol as Exception;
use GameQ\Result;
use GameQ\Server;
/**
* Grand Theft Auto Network Protocol Class
* https://stats.gtanet.work/
*
* Result from this call should be a header + JSON response
*
* References:
* - https://master.gtanet.work/apiservers
*
* @author Austin Bischoff <[email protected]>
*/
class Gtan extends Http
{
/**
* Packets to send
*
* @var array
*/
protected $packets = [
//self::PACKET_STATUS => "GET /apiservers HTTP/1.0\r\nHost: master.gtanet.work\r\nAccept: */*\r\n\r\n",
self::PACKET_STATUS => "GET /gtan/api.php?ip=%s&raw HTTP/1.0\r\nHost: multiplayerhosting.info\r\nAccept: */*\r\n\r\n",
];
/**
* Http protocol is SSL
*
* @var string
*/
protected $transport = self::TRANSPORT_SSL;
/**
* The protocol being used
*
* @var string
*/
protected $protocol = 'gtan';
/**
* String name of this protocol class
*
* @var string
*/
protected $name = 'gtan';
/**
* Longer string name of this protocol class
*
* @var string
*/
protected $name_long = "Grand Theft Auto Network";
/**
* Holds the real ip so we can overwrite it back
*
* @var string
*/
protected $realIp = null;
protected $realPortQuery = null;
/**
* Normalize some items
*
* @var array
*/
protected $normalize = [
// General
'general' => [
// target => source
'dedicated' => 'dedicated',
'hostname' => 'hostname',
'mapname' => 'map',
'mod' => 'mod',
'maxplayers' => 'maxplayers',
'numplayers' => 'numplayers',
'password' => 'password',
],
];
public function beforeSend(Server $server)
{
// Loop over the packets and update them
foreach ($this->packets as $packetType => $packet) {
// Fill out the packet with the server info
$this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query);
}
$this->realIp = $server->ip;
$this->realPortQuery = $server->port_query;
// Override the existing settings
//$server->ip = 'master.gtanet.work';
$server->ip = 'multiplayerhosting.info';
$server->port_query = 443;
}
/**
* Process the response
*
* @return array
* @throws Exception
*/
public function processResponse()
{
// No response, assume offline
if (empty($this->packets_response)) {
return [
'gq_address' => $this->realIp,
'gq_port_query' => $this->realPortQuery,
];
}
// Implode and rip out the JSON
preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
// Return should be JSON, let's validate
if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
throw new Exception("JSON response from Gtan protocol is invalid.");
}
$result = new Result();
// Server is always dedicated
$result->add('dedicated', 1);
$result->add('gq_address', $this->realIp);
$result->add('gq_port_query', $this->realPortQuery);
// Add server items
$result->add('hostname', $json->ServerName);
$result->add('serverversion', $json->ServerVersion);
$result->add('map', ((!empty($json->Map)) ? $json->Map : 'Los Santos/Blaine Country'));
$result->add('mod', $json->Gamemode);
$result->add('password', (int)$json->Passworded);
$result->add('numplayers', $json->CurrentPlayers);
$result->add('maxplayers', $json->MaxPlayers);
return $result->fetch();
}
}
Ответы (1 шт):
Автор решения: UniverstM
→ Ссылка
var sampQuery = new SampQuery("127.0.0.1", 7777);
SampServerInfoData data = sampQuery.GetServerInfo();
label.Text = $"Online {data.Players}/{data.MaxPlayers}";
Больше информации вот тут https://www.blast.hk/threads/70372/
Скачать скрипт: GitHub