matcher

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2018 License: MIT Imports: 27 Imported by: 1

Documentation

Index

Constants

View Source
const (
	RANK_KEY_FMT    = "rank:%d"
	SESSION_KEY_FMT = "session:%s"
	ACCOUNT_KEY_FMT = "account:%s"
	SUBKEY_KEY_FMT  = "subkey:%s"
)

Variables

This section is empty.

Functions

func AddTokenContext

func AddTokenContext(next http.Handler) http.Handler

Unmarshal token from query parameter and put it in request context for later retrieval and authentication logic.

func Address

func Address(message string, signature string) (string, error)

Return the hex address (pub key) of a signed message given the message and signature

func Compact

func Compact(inputs ...interface{}) ([]byte, error)

func Hash

func Hash(message string) []byte

func IToB

func IToB(data interface{}) ([]byte, error)

Types

type Code

type Code int
const (
	TERMINATE Code = -2 // unrecoverable fatal error
	ERROR     Code = -1

	MSG              Code = 0 // code for messages passed between players during gameplay
	INIT             Code = 1 // match found
	SIGNED_TIMESTAMP Code = 2
	MATCH_VERIFIED   Code = 3 // all players in match session have passed all validation tests
)
const (
	UNSUBSCRIBE Code = -100 // a internal PubSubManager message type
)

type InitMessage

type InitMessage struct {
	Timestamp int64 `json:"timestamp"`
}

type MatchResponse

type MatchResponse struct {
	Account common.Address // Owner of seed deck; this value is derived
	Rank    uint32         // calculated rank of player based on seed "deck"
	*Token
}

type Message

type Message struct {
	*Meta   `json:"meta"`
	Payload string `json:"payload"`
}

Message send over the wire between players

func NewError

func NewError(message string) Message

func NewTerminateMessage

func NewTerminateMessage(message string) Message

type Meta

type Meta struct {
	Index  uint8           `json:"index"` // index of player in game, i.e., player ID
	Code   Code            `json:"code"`  // message type
	SubKey *common.Address `json:"subkey"`
}

type PlayerInfo

type PlayerInfo struct {
	Rank  uint32 `json:"rank"`  // Player rank as returned from Solidity pure function
	Index uint8  `json:"index"` // index in match session; arbitrarily set when match found and player joins a session
	*Token

	SeedHash     []byte            `json:"seedHash,string"` // Hash of seed as returned from Solidity pure function
	Account      *common.Address   `json:"account,string"`  // owner account of signed subkey (derived from Token); See Token
	TimestampSig *crypto.Signature `json:"timestampSig"`    // TimestampSig of session timestamp by this player
	Verified     bool              `json:"verified"`        // true if the player has proven their TimestampSig
}

type PubSubManager

type PubSubManager struct {
	Channels map[string]chan *Message // subkey -> listening channel
	*SessionManager
}

func NewPubSubManager

func NewPubSubManager(smgr *SessionManager) *PubSubManager

func (*PubSubManager) Publish

func (mgr *PubSubManager) Publish(channel string, msg Message) error

func (*PubSubManager) Subscribe

func (mgr *PubSubManager) Subscribe(key string, channel chan *Message)

func (*PubSubManager) Unsubscribe

func (mgr *PubSubManager) Unsubscribe(key string)

type Service

type Service struct {
	ArcClient *arcadeum.Client
	ENV       *config.ENVConfig
	Config    *config.MatcherConfig
	*SessionManager
	*PubSubManager
}

func NewService

func NewService(
	env *config.ENVConfig,
	cfg *config.MatcherConfig,
	ethcfg *config.ETHConfig,
	arcconfig *config.ArcadeumConfig,
	rediscfg *config.RedisConfig) *Service

func (*Service) AddToMatchPool

func (s *Service) AddToMatchPool(r *MatchResponse) error

func (*Service) Authenticate

func (s *Service) Authenticate(token *Token) (*MatchResponse, error)

func (*Service) BeginVerifiedMatch

func (srv *Service) BeginVerifiedMatch(sess *Session) error

Session has been verified so begin match and send message to each player

func (*Service) BuildMatchVerifiedMessageWithSignature

func (srv *Service) BuildMatchVerifiedMessageWithSignature(s *Session) (*arcadeum.MatchVerifiedMessage, error)

func (*Service) BuildPlayerInfo

func (s *Service) BuildPlayerInfo(p *MatchResponse) (*PlayerInfo, error)

func (*Service) Close

func (s *Service) Close(message string, p ...*MatchResponse)

func (*Service) CreateSession

func (s *Service) CreateSession(p *MatchResponse) (*Session, error)

func (*Service) FindMatch

func (s *Service) FindMatch(token *Token)

func (*Service) HandleMatchResponses

func (s *Service) HandleMatchResponses()

func (*Service) InitGame

func (s *Service) InitGame(uid UUID, r *MatchResponse) error

func (*Service) Match

func (s *Service) Match(rp *MatchResponse)

Invariant: rp has been authenticated

func (*Service) NewKeyedTransactor

func (s *Service) NewKeyedTransactor() *bind.TransactOpts

func (*Service) OnMessage

func (s *Service) OnMessage(msg *Message) error

func (*Service) OnWithdrawalStarted

func (s *Service) OnWithdrawalStarted(event *arcadeum.ArcadeumWithdrawalStarted)

Event handler when we have detected when a user has decided to withdraw money from their account

func (*Service) PrivKey

func (srv *Service) PrivKey() *ecdsa.PrivateKey

func (*Service) PublishToSubKey

func (s *Service) PublishToSubKey(subKey *common.Address, message Message) error

func (*Service) Reconnect

func (s *Service) Reconnect(token *Token) (bool, error)

func (*Service) RemoveFromWaitingPool

func (s *Service) RemoveFromWaitingPool(resps ...*MatchResponse)

func (*Service) RequestTimestampProof

func (s *Service) RequestTimestampProof(sess *Session) error

func (*Service) SendTimestampProof

func (s *Service) SendTimestampProof(p *PlayerInfo, timestamp int64) error

func (*Service) Sign

func (srv *Service) Sign(inputs ...interface{}) ([]byte, error)

func (*Service) SignElliptic

func (srv *Service) SignElliptic(inputs ...interface{}) (r, s *big.Int, err error)

func (*Service) SubscribeToSubKey

func (s *Service) SubscribeToSubKey(subkey *common.Address, messages chan *Message)

Subscribed to published messages for this player

func (*Service) VerifyTimestamp

func (s *Service) VerifyTimestamp(req *arcadeum.VerifyTimestampRequest, player *PlayerInfo) (bool, error)

type Session

type Session struct {
	ID        UUID              `json:"id,string"`
	GameID    uint32            `json:"gameID"`
	Player1   *PlayerInfo       `json:"player1"`
	Player2   *PlayerInfo       `json:"player2"`
	Timestamp int64             `json:"timestamp"` // Game start in Unix time
	Signature *crypto.Signature `json:"signature"` // Matcher's session signature
}

Represents a matched game session

func (*Session) FindOpponent

func (s *Session) FindOpponent(subKey *common.Address) *PlayerInfo

func (*Session) FindPlayerByAccount

func (s *Session) FindPlayerByAccount(account common.Address) (*PlayerInfo, error)

func (*Session) FindPlayerBySubKey

func (s *Session) FindPlayerBySubKey(subKey *common.Address) *PlayerInfo

func (*Session) IsEmpty

func (s *Session) IsEmpty() bool

func (*Session) IsVerified

func (s *Session) IsVerified() bool

func (*Session) IsWaiting

func (s *Session) IsWaiting() bool

func (*Session) Rank

func (s *Session) Rank() uint32

func (*Session) UUID

func (s *Session) UUID() string

type SessionManager

type SessionManager struct {
	SessionPool map[UUID]*Session // map session UUID -> in-game session
	RedisClient *redis.Client
}

func NewSessionManager

func NewSessionManager(rediscfg *config.RedisConfig) *SessionManager

func (*SessionManager) AddToMatchPool

func (mgr *SessionManager) AddToMatchPool(sess *Session) error

func (*SessionManager) GetSessionByAccount

func (mgr *SessionManager) GetSessionByAccount(key *common.Address) (*Session, error)

func (*SessionManager) GetSessionByID

func (mgr *SessionManager) GetSessionByID(uid UUID) (*Session, error)

func (*SessionManager) GetSessionBySubKey

func (mgr *SessionManager) GetSessionBySubKey(key *common.Address) (*Session, error)

func (*SessionManager) ReaddToMatchPool

func (mgr *SessionManager) ReaddToMatchPool(rank uint32, uid UUID) error

func (*SessionManager) TakeRandomSessionByRank

func (mgr *SessionManager) TakeRandomSessionByRank(rank uint32) (UUID, error)

Find a similarly ranked player to play, pseudo-randomly chosen. Returns the UUID of the game session. This method will remove the waiting session from the wait pool. If a session is not found and there are no errors, an empty UUID is returned.

func (*SessionManager) UpdateSession

func (mgr *SessionManager) UpdateSession(sess *Session) error

type Status

type Status int
const (
	Unknown      Status = 0
	Waiting      Status = 1
	Moving       Status = 2
	Won          Status = 3
	Lost         Status = 4
	Disqualified Status = 5
)

type Token

type Token struct {
	GameID          uint32            `json:"gameID"`        // globally unique game ID
	SubKey          *common.Address   `json:"subkey,string"` // public address of account owner of subkey; effectively the Match ID
	SubKeySignature *crypto.Signature `json:"signature"`     // Signed signature of SubKey to prove SubKey ownership
	Seed            []byte            `json:"seed,string"`   // game "deck"; unmarshalled base64; never share with opponents!
}

Request token. This token is returned as a base64 string by a client requesting to play a game.

func Context

func Context(r *http.Request) *Token

type UUID

type UUID string

func (UUID) IsEmpty

func (u UUID) IsEmpty() bool

Jump to

Keyboard shortcuts

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