extinfo

package module
v0.0.0-...-0387c69 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2019 License: BSD-2-Clause Imports: 6 Imported by: 1

README

extinfo

A Go package to query information ('extinfo') from a Sauerbraten game server.

Usage

Get the package:

$ go get github.com/sauerbraten/extinfo

Import the package:

import (
	"github.com/sauerbraten/extinfo"
)

Documentation

Detailed Documentation here.

Example

Here is code to get you the state of the PSL1 server:

package main

import (
	"fmt"
	"time"

	"github.com/sauerbraten/extinfo"
)

func main() {
	psl1, err := extinfo.NewServer("sauerleague.org", 10000, 3*time.Second)
	if err != nil {
		fmt.Println(err)
		return
	}

	basicInfo, err := psl1.GetBasicInfo()
	if err != nil {
		fmt.Print("Error getting basic information: ", err)
		return
	}

	fmt.Print("Basic Server Information:\n")
	fmt.Printf("Description:                %v\n", basicInfo.Description)
	fmt.Printf("Master Mode:                %v\n", basicInfo.MasterMode)
	fmt.Printf("Game Mode:                  %v\n", basicInfo.GameMode)
	fmt.Printf("Map:                        %v\n", basicInfo.Map)
	fmt.Printf("Clients:                    %v\n", basicInfo.NumberOfClients)
	fmt.Printf("Maximum Number of Clients:  %v\n", basicInfo.MaxNumberOfClients)
	fmt.Printf("Time Left (seconds):        %v\n", basicInfo.SecsLeft)
	fmt.Printf("Protocol Version:           %v\n", basicInfo.ProtocolVersion)
}

The output should be something like this:

Basic Server Information:
Description:                PSL.sauerleague.org
Master Mode:                auth
Game Mode:                  insta ctf
Map:                        garden
clients:                    14
Maximum Number of Clients:  23
Time Left (seconds):        262
Protocol Version:           259

GetClientInfo() and GetClientInfoRaw() work pretty much the same; here is an example to get the client information of the client with the cn 4 on PSL1:

...

func main() {
	psl1, err := extinfo.NewServer("sauerleague.org", 10000, 3*time.Second)
	if err != nil {
		fmt.Println(err)
		return
	}

	clientInfo, err := psl1.GetClientInfo(14)
	if err != nil {
		fmt.Print("Error getting client information: ", err)
		return
	}

	fmt.Print("Client Information:\n")
	fmt.Printf("Name:                       %v\n", clientInfo.Name)
	fmt.Printf("Client Number:              %v\n", clientInfo.ClientNum)
	fmt.Printf("Ping:                       %v\n", clientInfo.Ping)
	fmt.Printf("Team:                       %v\n", clientInfo.Team)
	fmt.Printf("Frags:                      %v\n", clientInfo.Frags)
	// here you could get more things like deaths, health, armour, the client state (dead/alive/spectator/...), and so on
	fmt.Printf("Privilege:                  %v\n", clientInfo.Privilege)
	fmt.Printf("IP:                         %v\n", clientInfo.IP)
}

Output would look like this:

Client Information:
Name:                       [tBMC]Rsn
Client Number:              14
Ping:                       45
Team:                       good
Frags:                      8
Privilege:                  none
IP:                         85.8.108.0

There is also GetTeamsScores() which returns all teams' scores (a TeamsScores containing a TeamScore (team score) for every team in the current game). Example code:

...

func main() {
	psl1, err := extinfo.NewServer("sauerleague.org", 10000, 3*time.Second)
	if err != nil {
		fmt.Println(err)
		return
	}

	scores, err := psl1.GetTeamScores()
	if err != nil {
		fmt.Print("Error getting teams' scores: ", err)
		return
	}

	fmt.Print("Team Scores:\n")

	fmt.Printf("Game Mode:                  %v\n", scores.GameMode)
	fmt.Printf("Time Left (seconds):        %v\n", scores.SecsLeft)
	fmt.Print("Scores:\n")

	for _, score := range scores.Scores {
		fmt.Printf("   Team:                    %v\n", score.Name)
		fmt.Printf("   Score:                   %v\n", score.Score)

		if scores.GameMode == "capture" || scores.GameMode == "regen capture" {
			fmt.Printf("   Bases:                   %v\n", score.Bases)
		}
	}
}

Output:

Team Scores:
Game Mode:                  insta ctf
Time Left (seconds):        114
Scores:
   Team:                    good
   Score:                   6
   Team:                    evil
   Score:                   4

More methods:

  • GetUptime(): returns the amount of seconds the sauerbraten server is running
  • GetAllClientInfo(): returns a ClientInfo for every client connected to the server
  • GetTeamScoresRaw(): returns a TeamScoresRaw containing a TeamScore for every team in the current game

Documentation

Overview

Package extinfo provides easy access to the state information of a Sauerbraten game server (called 'extinfo' in the Sauerbraten source code).

Index

Constants

View Source
const (
	// Constants describing the type of information to query for
	InfoTypeExtended byte = 0x00
	InfoTypeBasic    byte = 0x01

	// Constants used in responses to extended info queries
	ExtInfoACK     byte = 0xFF // -1
	ExtInfoVersion byte = 105
	ExtInfoError   byte = 0x01

	// Constants describing the type of extended information to query for
	ExtInfoTypeUptime     byte = 0x00
	ExtInfoTypeClientInfo byte = 0x01
	ExtInfoTypeTeamScores byte = 0x02

	// Constants used in responses to client info queries
	ClientInfoResponseTypeCNs  byte = 0xF6 // -10
	ClientInfoResponseTypeInfo byte = 0xF5 // -11
)

Protocol constants

View Source
const (
	MaxPlayerCN     = 127 // Highest CN an actual player can have; bots' CNs start at 128
	MaxPacketLength = 512 // better to be safe
)

Constants generally useful in this package

Variables

This section is empty.

Functions

func IsTeamMode

func IsTeamMode(mode string) bool

IsTeamMode returns true when mode is a team mode, false otherwise.

Types

type BasicInfo

type BasicInfo struct {
	BasicInfoRaw
	GameMode   string `json:"gameMode"`   // current game mode
	MasterMode string `json:"masterMode"` // the current master mode of the server
}

BasicInfo contains the parsed information sent back from the server, i.e. game mode and master mode are translated into human readable strings.

type BasicInfoRaw

type BasicInfoRaw struct {
	NumberOfClients    int    `json:"numberOfClients"`    // the number of clients currently connected to the server (players and spectators)
	ProtocolVersion    int    `json:"protocolVersion"`    // version number of the protocol in use by the server
	GameMode           int    `json:"gameMode"`           // current game mode
	SecsLeft           int    `json:"secsLeft"`           // the time left until intermission in seconds
	MaxNumberOfClients int    `json:"maxNumberOfClients"` // the maximum number of clients the server allows
	MasterMode         int    `json:"masterMode"`         // the current master mode of the server
	Paused             bool   `json:"paused"`             // wether the game is paused or not
	GameSpeed          int    `json:"gameSpeed"`          // the gamespeed
	Map                string `json:"map"`                // current map
	Description        string `json:"description"`        // server description
}

BasicInfoRaw contains the information sent back from the server in their raw form, i.e. no translation from ints to strings, even if possible.

type ClientInfo

type ClientInfo struct {
	ClientInfoRaw
	Weapon    string `json:"weapon"`    // weapon the client currently has selected
	Privilege string `json:"privilege"` // "none", "master" or "admin"
	State     string `json:"state"`     // client state, e.g. "dead" or "spectator"
}

ClientInfo contains the parsed information sent back from the server, i.e. weapon, state and privilege are translated into human readable strings.

type ClientInfoRaw

type ClientInfoRaw struct {
	ClientNum int    `json:"clientNum"` // client number or cn
	Ping      int    `json:"ping"`      // client's ping to server
	Name      string `json:"name"`      //
	Team      string `json:"team"`      // name of the team the client is on, e.g. "good"
	Frags     int    `json:"frags"`     // kills
	Flags     int    `json:"flags"`     // number of flags the player scored
	Deaths    int    `json:"deaths"`    //
	Teamkills int    `json:"teamkills"` //
	Accuracy  int    `json:"accuracy"`  // damage the client could have dealt * 100 / damage actually dealt by the client
	Health    int    `json:"health"`    // remaining HP (health points)
	Armour    int    `json:"armour"`    // remaining armour
	Weapon    int    `json:"weapon"`    // weapon the client currently has selected
	Privilege int    `json:"privilege"` // 0 ("none"), 1 ("master"), 2 ("auth") or 3 ("admin")
	State     int    `json:"state"`     // client state, e.g. 1 ("alive") or 5 ("spectator"), see names.go for int -> string mapping
	IP        net.IP `json:"ip"`        // client IP (only the first 3 bytes)
}

ClientInfoRaw contains the raw information sent back from the server, i.e. state and privilege are ints.

type Server

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

Server represents a Sauerbraten game server.

func NewServer

func NewServer(addr net.UDPAddr, timeOut time.Duration) (*Server, error)

NewServer returns a Server to query information from.

func (*Server) GetAllClientInfo

func (s *Server) GetAllClientInfo() (allClientInfo map[int]ClientInfo, err error)

GetAllClientInfo returns the ClientInfo of all Players (including spectators) as a []ClientInfo

func (*Server) GetBasicInfo

func (s *Server) GetBasicInfo() (BasicInfo, error)

GetBasicInfo queries a Sauerbraten server at addr on port and returns the parsed response or an error in case something went wrong. Parsed response means that the int values sent as game mode and master mode are translated into the human readable name, e.g. '12' -> "insta ctf".

func (*Server) GetBasicInfoRaw

func (s *Server) GetBasicInfoRaw() (basicInfoRaw BasicInfoRaw, err error)

GetBasicInfoRaw queries a Sauerbraten server at addr on port and returns the raw response or an error in case something went wrong. Raw response means that the int values sent as game mode and master mode are NOT translated into the human readable name.

func (*Server) GetClientInfo

func (s *Server) GetClientInfo(clientNum int) (clientInfo ClientInfo, err error)

GetClientInfo returns the parsed information about the client with the given clientNum.

func (*Server) GetClientInfoRaw

func (s *Server) GetClientInfoRaw(clientNum int) (clientInfoRaw ClientInfoRaw, err error)

GetClientInfoRaw returns the raw information about the client with the given clientNum.

func (*Server) GetServerMod

func (s *Server) GetServerMod() (serverMod string, err error)

GetServerMod returns the name of the mod in use at this server.

func (*Server) GetTeamScores

func (s *Server) GetTeamScores() (TeamScores, error)

GetTeamScores queries a Sauerbraten server at addr on port for the teams' names and scores and returns the parsed response and/or an error in case something went wrong or the server is not running a team mode. Parsed response means that the int value sent as game mode is translated into the human readable name, e.g. '12' -> "insta ctf".

func (*Server) GetTeamScoresRaw

func (s *Server) GetTeamScoresRaw() (teamScoresRaw TeamScoresRaw, err error)

GetTeamScoresRaw queries a Sauerbraten server at addr on port for the teams' names and scores and returns the raw response and/or an error in case something went wrong or the server is not running a team mode.

func (*Server) GetUptime

func (s *Server) GetUptime() (uptime int, err error)

GetUptime returns the uptime of the server in seconds.

type TeamScore

type TeamScore struct {
	Name  string `json:"name"`  // name of the team, e.g. "good"
	Score int    `json:"score"` // flags in ctf modes, frags in deathmatch modes, points in capture, skulls in collect
	Bases []int  `json:"bases"` // the numbers/IDs of the bases the team possesses (only used in capture modes)
}

TeamScore contains the name of the team and the score, i.e. flags scored in flag modes / points gained for holding bases in capture modes / frags achieved in DM modes / skulls collected

type TeamScores

type TeamScores struct {
	TeamScoresRaw
	GameMode string `json:"gameMode"` // current game mode
}

TeamScores contains the game mode as human readable string, the seconds left in the game, and a slice of TeamScores

type TeamScoresRaw

type TeamScoresRaw struct {
	GameMode int                  `json:"gameMode"` // current game mode
	SecsLeft int                  `json:"secsLeft"` // the time left until intermission in seconds
	Scores   map[string]TeamScore `json:"scores"`   // a team score for each team, mapped to the team's name
}

TeamScoresRaw contains the game mode as raw int, the seconds left in the game, and a slice of TeamScores

Jump to

Keyboard shortcuts

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