dummy

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package dummy implements a mocked Warcraft III game client that can be used to add dummy players to lobbies.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/nielsAD/gowarcraft3/network"
	"github.com/nielsAD/gowarcraft3/network/dummy"
	"github.com/nielsAD/gowarcraft3/network/lan"
	"github.com/nielsAD/gowarcraft3/protocol/w3gs"
)

func main() {
	var game = w3gs.GameVersion{
		Product: w3gs.ProductTFT,
		Version: w3gs.CurrentGameVersion,
	}

	// Find LAN game
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
	addr, id, secret, err := lan.FindGame(ctx, game)

	cancel()
	if err != nil {
		fmt.Println(err)
		return
	}

	// Join game
	player, err := dummy.Join(addr, "DummyPlayer", id, secret, -1, w3gs.Encoding{GameVersion: game.Version})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer player.Close()

	// Print incoming chat messages
	player.On(&dummy.Chat{}, func(ev *network.Event) {
		var msg = ev.Arg.(*dummy.Chat)
		fmt.Printf("[%s] %s\n", msg.Sender.PlayerName, msg.Content)
	})

	// Run() blocks until the connection is closed
	player.Run()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrJoinRejected       = errors.New("dummy: Join rejected")
	ErrGameFull           = errors.New("dummy: Join rejected (game full)")
	ErrGameStarted        = errors.New("dummy: Join rejected (game started)")
	ErrInvalidFirstPacket = errors.New("dummy: Invalid first packet")
)

Errors

Functions

func RejectReasonToError

func RejectReasonToError(r w3gs.RejectReason) error

RejectReasonToError converts w3gs.RejectReason to an appropriate error

Types

type Chat

type Chat struct {
	Sender  *w3gs.PlayerInfo
	Content string
}

Chat event

type Player

type Player struct {
	peer.Host
	network.W3GSConn

	// Set once before Join(), read-only after that
	HostAddr    string
	HostCounter uint32
	DialPeers   bool
}

Player represents a mocked player that can join a game lobby

func Join

func Join(addr string, name string, hostCounter uint32, entryKey uint32, listenPort int, encoding w3gs.Encoding) (*Player, error)

Join a game lobby as a mocked player

func (*Player) ChangeColor

func (p *Player) ChangeColor(c uint8) error

ChangeColor to c

func (*Player) ChangeHandicap

func (p *Player) ChangeHandicap(h uint8) error

ChangeHandicap to h

func (*Player) ChangeRace

func (p *Player) ChangeRace(r w3gs.RacePref) error

ChangeRace to r

func (*Player) ChangeTeam

func (p *Player) ChangeTeam(t uint8) error

ChangeTeam to t

func (*Player) Close

func (p *Player) Close() error

Close closes all connections to host and peers

func (*Player) InitDefaultHandlers

func (p *Player) InitDefaultHandlers()

InitDefaultHandlers adds the default callbacks for relevant packets

func (*Player) Join

func (p *Player) Join() error

Join opens a new connection to host Not safe for concurrent invocation

func (*Player) JoinWithConn added in v1.6.0

func (p *Player) JoinWithConn(conn net.Conn) error

JoinWithConn initializes a connection to host Not safe for concurrent invocation

func (*Player) Leave

func (p *Player) Leave(reason w3gs.LeaveReason) error

Leave game

func (*Player) Run

func (p *Player) Run() error

Run reads packets and emits an event for each received packet Not safe for concurrent invocation

func (*Player) Say

func (p *Player) Say(s string) error

Say sends a chat message

func (*Player) SendOrClose added in v1.5.0

func (p *Player) SendOrClose(pkt w3gs.Packet) (int, error)

SendOrClose sends pkt to player, closes connection on failure

type Say

type Say struct {
	Content string
}

Say event

Jump to

Keyboard shortcuts

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