client

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Typoify added in v0.2.4

func Typoify(msg string, chanceFactor float64) []string

Typoify inserts typos into the given message and returns the message with typos. The odds of typos depends on the message but is multiplied by the typo chanceFactor. The resulting message may have been split up with "enter"

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/calamity-of-subterfuge/cos/pkg/client"
)

func main() {
	rand.Seed(74)
	og := "this is a test! look at me go: I'm doing great"
	fmt.Printf(" %q\n", og)
	fmt.Printf("%q", client.Typoify(og, 5))
}
Output:

 "this is a test! look at me go: I'm doing great"
["this is a t3st! look at me go: I\"mdoibggreat"]
Example (Diffseed)
package main

import (
	"fmt"
	"math/rand"

	"github.com/calamity-of-subterfuge/cos/pkg/client"
)

func main() {
	rand.Seed(5)
	og := "this is a test! look at me go: I'm doing great"
	fmt.Printf(" %q\n", og)
	fmt.Printf("%q", client.Typoify(og, 0.5))
}
Output:

 "this is a test! look at me go: I'm doing great"
["this is a tesf! look at me go: I'm doing great"]
Example (Diffseed2)
package main

import (
	"fmt"
	"math/rand"

	"github.com/calamity-of-subterfuge/cos/pkg/client"
)

func main() {
	rand.Seed(67)
	og := "this is a test! look at me go: I'm doing great"
	fmt.Printf(" %q\n", og)
	fmt.Printf("%q", client.Typoify(og, 0.5))
}
Output:

 "this is a test! look at me go: I'm doing great"
["this is at est! look at me go: I'm doing great"]
Example (Diffseed3)
package main

import (
	"fmt"
	"math/rand"

	"github.com/calamity-of-subterfuge/cos/pkg/client"
)

func main() {
	rand.Seed(15)
	og := "this is a test! look at me go: I'm doing great"
	fmt.Printf(" %q\n", og)
	fmt.Printf("%q", client.Typoify(og, 1))
}
Output:

 "this is a test! look at me go: I'm doing great"
["this is a test! look at me go? I'm doing great"]

Types

type BlankSmartObjectAdditional

type BlankSmartObjectAdditional struct{}

BlankSmartObjectAdditional is for objects with no additional information

func (*BlankSmartObjectAdditional) Update

Update is no-op

type Chat

type Chat struct {
	// LocalChatAuthorsByUID contains all of the chat authors that would hear
	// any messages we send locally right now.
	LocalChatAuthorsByUID map[string]*ChatAuthor

	// RecentChatAuthorsByUID contains all of the chat authors that are in the
	// message history
	RecentChatAuthorsByUID map[string]*ChatAuthor

	// RecentChatAuthorUIDsToCount maps from the keys in RecentChatAuthorsByUID
	// to the number of messages by that chat author in the message history. Each
	// value is positive - when it reaches 0 the key is deleted from the map
	// and the chat author is removed from RecentChatAuthorsByUID.
	RecentChatAuthorUIDsToCount map[string]int

	// MessageHistory is the ring of recent messages
	MessageHistory *rbuf.PointerRingBuf
	// contains filtered or unexported fields
}

Chat maintains the chat history and current set of chat authors

func NewChat

func NewChat(messageHistoryLength int) *Chat

NewChat initializes a blank chat event that will need a game sync event to fully initialize

func (*Chat) HandleMessage

func (c *Chat) HandleMessage(packet srvpkts.Packet)

HandleMessage should be called on any packet received from the server to update the state of the chat.

type ChatAuthor

type ChatAuthor struct {
	// UID is the identifier for the chat author
	UID string

	// Name is the display name of the chat author
	Name string

	// Color is the CSS class that represents the color that the chat author
	// should be rendered with. Any CSS class loaded on the play page of the
	// website is valid.
	Color string

	// BonusClasses contains all of the additional CSS classes for the text of
	// this author. Any CSS class loaded on the play page of the website is
	// valid.
	BonusClasses []string
}

ChatAuthor describes a chat author from the perspective of the client

func (*ChatAuthor) Update

func (a *ChatAuthor) Update(sync *srvpkts.ChatAuthorSync) *ChatAuthor

Update this chat author to match the given sync information, then return this chat author

type ChatMessage

type ChatMessage struct {
	// GameTime when this message was received by the server
	GameTime float64

	// Time is the real time when the message was received by the server
	Time time.Time

	// ChatAuthor is a pointer to the chat author for this chat message;
	// this pointer is only valid while the message is in the Chat buffer,
	// otherwise it may be stale.
	ChatAuthor *ChatAuthor

	// Text of the message
	Text string
}

ChatMessage describes a chat message

type GameObject

type GameObject struct {
	// UID of the object
	UID string

	// SheetURL describes where the PixiJS spritesheet file containing
	// the animations for this object can be found
	SheetURL string

	// SpriteScale describes the x/y scaling for this game object compared
	// to the underlying sprite.
	SpriteScale cp.Vector

	// SpriteRotation is the default rotation in radians for this game object
	// compared to how it is on the spritesheet, which can be used for object
	// packing
	SpriteRotation float64

	// RenderOffset is the visual offset of the sprite on the screen in pixels
	// at standard zoom.
	RenderOffset cp.Vector

	// Animation is the name of the animation within the sheet that
	// should currently be used to render this object.
	Animation string

	// AnimationSpeed is the speed of the animation, where 1 is 60
	// frames per second, 0.5 is 30 frames per second, and 2 is
	// 120 frames per second.
	AnimationSpeed float64

	// AnimationPlaying is true if the animation should actually play
	// and false if it should be frozen on an arbitrary frame.
	AnimationPlaying bool

	// AnimationLooping is true if the animation should loop when
	// finished and false if it should not.
	AnimationLooping bool

	// Body is the physics body for this game object, with the appropriate
	// shapes, position, velocity, angle, and angular velocity, but not
	// attached to any space and hence not simulated.
	Body *cp.Body
}

GameObject describes some object that's within the game. This is often wrapped.

func (*GameObject) Sync

func (o *GameObject) Sync(packet *srvpkts.GameObjectSync) *GameObject

Sync this game object using the given information. This overwrites everything

func (*GameObject) Update

Update this game object with the given information, which only affects the relatively frequently changing fields

type Player

type Player struct {
	// GameObject for this player
	GameObject *GameObject

	// Role this player is fulfilling
	Role utils.Role

	// Team this player is on
	Team int
}

Player describes a player in the world from the perspective of the client

func (*Player) Sync

func (p *Player) Sync(sync *srvpkts.PlayerSync) *Player

Sync this player to match the given sync information

func (*Player) Update

func (p *Player) Update(upd *srvpkts.GameObjectUpdatePacket) *Player

Update this player with the given information

type Resource

type Resource struct {
	// UID of this resource
	UID string

	// SheetURL where the PixiJS spritesheet containing this resource
	// can be found
	SheetURL string

	// Animation within the sheet for this resource
	Animation string

	// How much of this resource we have
	Amount int
}

Resource describes a resource from the perspective of the client.

func (*Resource) Sync

func (r *Resource) Sync(sync *srvpkts.ResourceSync) *Resource

Sync this resource with the given information

type SmartObject

type SmartObject struct {
	// GameObject for this smart object
	GameObject *GameObject

	// UnitType that this smart object is acting as
	UnitType string

	// CurrentHealth of this unit, may be a rounded representation of the
	// units true health
	CurrentHealth int

	// MaxHealth of this unit
	MaxHealth int

	// ControllingTeam is the team which controls this object.
	ControllingTeam int

	// ControllingRole is the role required to control this object. See utils.Role
	ControllingRole utils.Role

	// Additional contains the SyncInfo on the Unit controlling this smart object,
	// and depends on the type of unit.
	Additional SmartObjectAdditional
}

SmartObject describes a "fancy" non-player unit in the world. These are often controllable by someone and have special orders. The UnitType of the smart object acts as the enum for the special behavior of the object.

func (*SmartObject) Sync

Sync this smart object with the given sync information

func (*SmartObject) Update

Update this smart object with the given update packet

type SmartObjectAdditional

type SmartObjectAdditional interface {
	// Update the additional information using the given packet
	Update(update *srvpkts.SmartObjectUpdatePacket) error
}

SmartObjectAdditional describes additional information on a SmartObject

func ParseSmartObjectAdditional

func ParseSmartObjectAdditional(sync *srvpkts.SmartObjectSync) (SmartObjectAdditional, error)

ParseSmartObjectAdditional parses a SmartObjectAdditional from the given SmartObjectSync based on its UnitType

type SmartObjectAdditionalParser

type SmartObjectAdditionalParser func(*srvpkts.SmartObjectSync) (SmartObjectAdditional, error)

SmartObjectAdditionalParser parses a SmartObjectAdditional from a SmartObjectSync

type State

type State struct {
	// MyUID is the uid of the game object of the player for this client
	MyUID string

	// MyTeam is the team that the player for this client is on
	MyTeam int

	// MyRole is the role of the player for this client
	MyRole utils.Role

	// GameTime is the current game time
	GameTime float64

	// PlayersByUID contains all the currently visible players mapped from
	// their uid.
	PlayersByUID map[string]*Player

	// PlayerUIDsByTeamAndRole allows quickly looking up the set of players
	// which have a given team or a given team and role.
	PlayerUIDsByTeamAndRole TeamRoleUIDLookup

	// StaticObjects contains all of the static objects in the game. These
	// do not move or change and you can see them regardless of vision.
	StaticObjects []GameObject

	// SmartObjectsByUID contains all of the currently visible smart objects
	// mapped from their uid.
	SmartObjectsByUID map[string]*SmartObject

	// SmartObjectsByUnitType is an index on unit type for smart objects.
	SmartObjectsByUnitType UnitTypeLookup

	// GameObjectsByUID contains any generic game objects within vision.
	// Besides handling them for the purpose of collision these are largely
	// irrelevant to gameplay.
	GenericObjectsByUID map[string]*GameObject

	// ResourcesByUID contains all the resources on the clients team, mapped
	// from their uid.
	ResourcesByUID map[string]*Resource
	// contains filtered or unexported fields
}

State describes the general state of the world from the perspective of the client right now. This is generally used to take over the generic packet management for AIs so they can focus on unit / player controls rather than just synchronizing the client state.

func NewState

func NewState() *State

NewState initializes a blank state that will need the game sync packet in order to fill into normal representation. Typically the state can be considered invalid if the GameTime is 0.

func (*State) HandleMessage

func (s *State) HandleMessage(packet srvpkts.Packet)

HandleMessage should be called whenever a new server packet is received. If the packet is relevant to the client state, this updates the client state appropriately.

func (*State) OnControllableSmartObjectLoaded added in v0.1.0

func (s *State) OnControllableSmartObjectLoaded(listener func(*SmartObject))

OnControllableSmartObjectLoaded is called when a new smart object that the player can control is loaded from a packet, such as via a game sync or because it just came into vision or it was just created.

func (*State) OnControllableSmartObjectLost added in v0.1.0

func (s *State) OnControllableSmartObjectLost(listener func(*SmartObject))

OnControllableSmartObjectLost is called whenever a smart object which is controlled by the player is lost, such as at the beginning of a game sync, because it died, or because we lost vision of it.

func (*State) OnSelfLoaded added in v0.1.0

func (s *State) OnSelfLoaded(listener func(*Player))

OnSelfLoaded will register the given listener to be called whenever the Player with uid s.MyUID is loaded from a packet. Typically this is on game sync.

func (*State) OnSelfLost added in v0.1.0

func (s *State) OnSelfLost(listener func(*Player))

OnSelfLost will register the given listener to be called whenever the Player with uid s.MyUID is removed from the game. Typically is the first stage of a game sync packet.

type TeamRoleUIDLookup added in v0.2.2

type TeamRoleUIDLookup map[int]map[utils.Role]map[string]struct{}

TeamRoleUIDLookup provides a lookup of the set of UIDs for players which are within a particular team and role.

func (TeamRoleUIDLookup) Add added in v0.2.2

func (l TeamRoleUIDLookup) Add(team int, role utils.Role, uid string) bool

Add the given uid with the given team and role

func (TeamRoleUIDLookup) EachOnTeam added in v0.2.2

func (l TeamRoleUIDLookup) EachOnTeam(team int, fnc func(string))

EachOnTeam calls the given function on each player in the given team.

func (TeamRoleUIDLookup) EachOnTeamWithRole added in v0.2.20

func (l TeamRoleUIDLookup) EachOnTeamWithRole(team int, role utils.Role, fnc func(string))

EachOnTeamWithRole calls the given function with the uid of every player in the given team with the given role

func (TeamRoleUIDLookup) Remove added in v0.2.2

func (l TeamRoleUIDLookup) Remove(team int, role utils.Role, uid string) bool

Remove the given uid with the given team and role

type TentAdditional added in v0.2.17

type TentAdditional struct {
	// IncomingOffers are the offers from other teams which have been made
	// to the team owning this tent. The keys are uids of the offers.
	IncomingOffers map[string]TentOffer

	// OutgoingOffers are the offers from this team which have been made to
	// other teams. The keys are the uids of the offers.
	OutgoingOffers map[string]TentOffer
}

TentAdditional is the additional information for a tent

func (*TentAdditional) Update added in v0.2.17

type TentOffer added in v0.2.17

type TentOffer struct {
	// InitiatingTeam is the team initiating the offer.
	InitiatingTeam int

	// TargetTeam is the team recieving the offer
	TargetTeam int

	// Offer is the offer of resources being made, which will be taken from the
	// initiating team and given to the target team if the offer is accepted.
	Offer map[string]int

	// Request is the request of resources, which will be taken from the target
	// team and given to the initiating team if the offer is accepted.
	Request map[string]int
}

TentOffer describes a single offer in a tent. The offer is visible on both the initiating and target teams tents. You can only see offers on your own tent.

type UnitTypeLookup added in v0.2.6

type UnitTypeLookup map[string]map[string]struct{}

UnitTypeLookup allows looking up the UIDs of smart objects by their unit type

func (UnitTypeLookup) Add added in v0.2.6

func (l UnitTypeLookup) Add(obj *SmartObject)

Add the given smart object to this index

func (UnitTypeLookup) AddUID added in v0.2.11

func (l UnitTypeLookup) AddUID(unitType string, uid string)

AddUID adds the smart object with the given uid to this index

func (UnitTypeLookup) Each added in v0.2.6

func (l UnitTypeLookup) Each(unitType string, operator func(string))

Each calls the operator on every unit of the given type

func (UnitTypeLookup) Remove added in v0.2.6

func (l UnitTypeLookup) Remove(obj *SmartObject)

Remove the given smart object from this index

func (UnitTypeLookup) RemoveUID added in v0.2.11

func (l UnitTypeLookup) RemoveUID(unitType string, uid string)

RemoveUID removes the smart object with the given type and uid from this index

Jump to

Keyboard shortcuts

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