...

Source file src/code.rocketnine.space/tslocum/zenith/tracker.go

Documentation: code.rocketnine.space/tslocum/zenith

     1  package zenith
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  )
     7  
     8  // Tracker records match results and provides player standings.
     9  type Tracker struct {
    10  	db *Database
    11  	sync.RWMutex
    12  }
    13  
    14  // TrackerOptions specify options when creating a new Tracker.
    15  type TrackerOptions struct {
    16  	Database *Database
    17  	Web      string // Web interface address
    18  }
    19  
    20  // NewTracker returns a new score tracker. A game ID greater than zero must be specified.
    21  func NewTracker(options *TrackerOptions) (*Tracker, error) {
    22  	if options == nil || options.Database == nil {
    23  		return nil, errors.New("failed to initialize tracker: database is a required option")
    24  	}
    25  
    26  	t := &Tracker{
    27  		db: options.Database,
    28  	}
    29  
    30  	if options.Web != "" {
    31  		go hostWebInterface(t, options.Web)
    32  	}
    33  
    34  	return t, nil
    35  }
    36  
    37  // Track tracks one or more match results.
    38  func (t *Tracker) Track(results ...*MatchResult) error {
    39  	t.Lock()
    40  	defer t.Unlock()
    41  
    42  	err := t.db.Track(results...)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  // PlayersByStanding returns the Players within a specified range of standings.
    51  func (t *Tracker) PlayersByStanding(start int, end int) []*Player {
    52  	return nil
    53  }
    54  
    55  // Player returns the Player with the specified ID.
    56  func (t *Tracker) Player(game, player int) (*Player, error) {
    57  	return t.db.Player(game, player)
    58  }
    59  

View as plain text