connection

package
v0.0.0-...-ed902dd Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

connection package handle connection to rest-api server Warships Online API

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoardRespons

type BoardRespons struct {
	Board []string
}

BoardRespons decode information get from GetBoard function about how the board of the game looks

type Client

type Client interface {
	SetStartingHeader(setHeader StartingHeader)
	StartGame(ctx context.Context) error
	GetBoard(ctx context.Context) (BoardRespons, error)
	GetDescription(ctx context.Context) (Description, error)
	Fire(ctx context.Context, coordinates string) (FireResponse, error)
	GetStatus(ctx context.Context) (GameStatus, error)
	GetPlayerList(ctx context.Context) (PlayerList, error)
	DeleteGame(ctx context.Context) error
	GetLeaderBoard(ctx context.Context) (StatsLeaderboard, error)
	GetPlayerScore(ctx context.Context, player string) (StatsPlayer, error)
	RefreshWaitingForGame(ctx context.Context) error
}

Client handle connection to Warships Online API

type ConnectionClient

type ConnectionClient struct {
	StartingHeader StartingHeader
	// contains filtered or unexported fields
}

ConnectionClient implementation of Client interface

func NewClient

func NewClient(host string) ConnectionClient

NewClient constructor of ConnectionClient struct

func (ConnectionClient) DeleteGame

func (c ConnectionClient) DeleteGame(ctx context.Context) error

DeleteGame - deleting the game from the server

func (ConnectionClient) Fire

func (c ConnectionClient) Fire(ctx context.Context, coordinates string) (FireResponse, error)

Fire is making a shot to a server with specified coordinates and return response about the outcome by decoding message to FireResponse struct and returning an error when function faild

func (ConnectionClient) GetBoard

func (c ConnectionClient) GetBoard(ctx context.Context) (BoardRespons, error)

GerBoard return game board from server using authentication token and decoding it to boardRespons struct and an error

func (ConnectionClient) GetDescription

func (c ConnectionClient) GetDescription(ctx context.Context) (Description, error)

GetDescription return your and enemy login and description using authentication token in Description struct and an error

func (ConnectionClient) GetLeaderBoard

func (c ConnectionClient) GetLeaderBoard(ctx context.Context) (StatsLeaderboard, error)

GetLeaderBoard - return StatsLeaderboard a statistic of top 10 players

func (ConnectionClient) GetPlayerList

func (c ConnectionClient) GetPlayerList(ctx context.Context) (PlayerList, error)

GetPlayerList retrieves the list of players waiting for game from the server using the authentication token. It returns the PlayerList struct containing the list of players and an error if the request fails.

func (ConnectionClient) GetPlayerScore

func (c ConnectionClient) GetPlayerScore(ctx context.Context, player string) (StatsPlayer, error)

GerPlayerScore - takes context which hold timeout of the api request and player nick and return statistic of given player

func (ConnectionClient) GetStatus

func (c ConnectionClient) GetStatus(ctx context.Context) (GameStatus, error)

GetStatus retrieves the current game status from the server using the authentication token. It returns the GameStatus struct containing information about the game status and an error if the request fails.

func (ConnectionClient) RefreshWaitingForGame

func (c ConnectionClient) RefreshWaitingForGame(ctx context.Context) error

RefreshWaitingForGame while waiting for game refresh to not get kicked out of the lobby

func (*ConnectionClient) SetStartingHeader

func (c *ConnectionClient) SetStartingHeader(setHeader StartingHeader)

SetStartingHeader set starting header

func (*ConnectionClient) StartGame

func (c *ConnectionClient) StartGame(ctx context.Context) error

StartGame starting game with given params in starting header which decide if you want to play against bot or player or wait for opponent, if you want send your nick and description if specify them if you want send your own board and at the end store authentication token in ConnectionClient struct

type Description

type Description struct {
	Desc     string `json:"desc"`
	Nick     string `json:"nick"`
	OppDesc  string `json:"opp_desc"`
	Opponent string `json:"opponent"`
}

Description store information from GetDescription function about your and enemy login and description

type ErrorMessage

type ErrorMessage struct {
	Message string `json:"message"`
}

ErrorMessage Store server message when response status code is 403 usualy "session not found"

type FireCoord

type FireCoord struct {
	Coord string `json:"coord"`
}

FireCoord header for fire function which store infrormation about coordination of the shot

type FireResponse

type FireResponse struct {
	Result string `json:"result"`
}

FireResponse store result of the fire function obout if you hit or missed the shot

type GameStatus

type GameStatus struct {
	GameStatus     string   `json:"game_status"`
	ShouldFire     bool     `json:"should_fire"`
	OppShots       []string `json:"opp_shots"`
	LastGameStatus string   `json:"last_game_status"`
	Timer          int      `json:"timer"`
}

GameStatus store information get from GetStatus function about GameStatus - status of the game ShoulFire - boolean if this is your turn to fire OppShots - oponnent shots coordinates during their turn LastGameStatus - information about outcome of last game Timer - information about your how long turn will last

type PlayerList

type PlayerList []struct {
	GameStatus string `json:"game_status"`
	Nick       string `json:"nick"`
}

PlayerList decode information got from GetPlayerList GameStatus - tell the status of the game of the player Nick - tell a nick of the waiting player

type RequestError

type RequestError struct {
	StatusCode int
	Err        string
}

RequestError error for status code not ok

func (*RequestError) Error

func (e *RequestError) Error() string

type StartingHeader

type StartingHeader struct {
	Desc       string   `json:"desc"`
	Nick       string   `json:"nick"`
	TargetNick string   `json:"target_nick"`
	Wpbot      bool     `json:"wpBot"`
	Coords     []string `json:"coords"`
}

StartingHeader header for StartGame function which store information about game you want to begin Desc - if specified send your description to a server if not server will assign it automatically for you Nick - if specified send your Nick to a server if not server will assign it automatically for you TargetNick - if specified attack chosen player Wpbot - if true you chose to fighr WPbot Coords - contain positions of the ships position by the player

type Stats

type Stats struct {
	Games  int    `json:"games"`
	Nick   string `json:"nick"`
	Points int    `json:"points"`
	Rank   int    `json:"rank"`
	Wins   int    `json:"wins"`
}

Stats contain statistick of players Games - how many games played Nick - nick of player which are those statistics Points - points of the player Rank - rank of the player Wins - how many games player won

type StatsLeaderboard

type StatsLeaderboard struct {
	GotStats []Stats `json:"stats"`
}

StatsLeaderboard caontain Stats of 10 best players

type StatsPlayer

type StatsPlayer struct {
	GotStat Stats `json:"stats"`
}

StatsPlayer contain Stats of the player

type TokenError

type TokenError struct {
	Token string
}

ToekenError error for wrong token

func (*TokenError) Error

func (e *TokenError) Error() string

Jump to

Keyboard shortcuts

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