sampquery

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 10 Imported by: 6

README

go-samp-query

Implements the SA:MP query mechanism for getting data about a running server.

See GoDoc for full documentation

Examples

Most of the time, you'll only need one function:

server, err := GetServerInfo(ctx, "192.168.1.1", true)
if err != nil {
    // handle
}

The attemptDecode parameter determines whether or not the library should attempt to guess the encoding of the text fields such as hostname etc.

If you want to get specific data about a server, you can create a query and selectively query for data:

query, err := NewLegacyQuery(host)
if err != nil {
    // handle
}
defer query.Close()

rules, err := query.GetRules(ctx)
if err != nil {
    return
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Query

type Query struct {
	Data Server
	// contains filtered or unexported fields
}

Query stores state for masterlist queries

func NewQuery added in v1.1.0

func NewQuery(host string) (query *Query, err error)

NewQuery creates a new query handler for a server

func (*Query) Close

func (query *Query) Close() error

Close closes a query manager's connection

func (*Query) GetInfo

func (query *Query) GetInfo(ctx context.Context, attemptDecode bool) (server Server, err error)

GetInfo returns the core server info for displaying on the browser list.

func (*Query) GetOmpValidity added in v1.2.0

func (query *Query) GetOmpValidity(ctx context.Context) bool

GetOmpValidity sends and receives a packet to check if server is using open.mp or not

func (*Query) GetPing

func (query *Query) GetPing(ctx context.Context) (ping time.Duration, err error)

GetPing sends and receives a packet to measure ping

func (*Query) GetPlayers

func (query *Query) GetPlayers(ctx context.Context) (players []string, err error)

GetPlayers simply returns a slice of strings, score is rather arbitrary so it's omitted.

func (*Query) GetRules

func (query *Query) GetRules(ctx context.Context) (rules map[string]string, err error)

GetRules returns a map of rule properties from a server. The query uses established keys such as "Map" and "Version"

func (*Query) SendQuery

func (query *Query) SendQuery(ctx context.Context, opcode QueryType) (response []byte, err error)

SendQuery writes a SA:MP format query with the specified opcode, returns the raw response bytes

type QueryType

type QueryType uint8

QueryType represents a query method from the SA:MP set: i, r, c, d, x, p

const (
	// Info is the 'i' packet type
	Info QueryType = 'i'
	// Rules is the 'r' packet type
	Rules QueryType = 'r'
	// Players is the 'c' packet type
	Players QueryType = 'c'
	// Ping is the 'p' packet type
	Ping QueryType = 'p'
	// IsOmp is the 'o' packet type
	IsOmp QueryType = 'o'
)

type Server

type Server struct {
	Address    string            `json:"address"`
	Hostname   string            `json:"hostname"`
	Players    int               `json:"players"`
	MaxPlayers int               `json:"max_players"`
	Gamemode   string            `json:"gamemode"`
	Language   string            `json:"language"`
	Password   bool              `json:"password"`
	Rules      map[string]string `json:"rules"`
	Ping       int               `json:"ping"`
	IsOmp      bool              `json:"isOmp"`
}

Server contains all the information retreived from the server query API.

func GetServerInfo

func GetServerInfo(ctx context.Context, host string, attemptDecode bool) (server Server, err error)

GetServerInfo wraps a set of queries and returns a new Server object with the available fields populated. `attemptDecode` determines whether or not to attempt to decode ANSI into Unicode from servers that use different codepages such as Cyrillic. This function can panic if the socket it opens fails to close for whatever reason.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL