models

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2016 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//NoReset means the value does not reset
	NoReset ResetType = iota
	//DailyReset means the value resets daily
	DailyReset = iota
	//WeeklyReset means the value resets weekly
	WeeklyReset = iota
	//MonthlyReset means the value resets monthly
	MonthlyReset = iota
)

Variables

View Source
var NotFoundString = "not found"

NotFoundString is the string returned when an element is not found

Functions

func EnsurePlayerExists

func EnsurePlayerExists(gameID, id string, db *mgo.Database, logger zap.Logger) error

EnsurePlayerExists upserts the player

func GetDonationRequestsCollection

func GetDonationRequestsCollection(db *mgo.Database) *mgo.Collection

GetDonationRequestsCollection to update or query games

func GetDonationWeightForClan

func GetDonationWeightForClan(gameID, clanID string, dt time.Time, resetType ResetType, r redis.Conn, logger zap.Logger) (int, error)

GetDonationWeightForClan returns the donation weight for a clan in a given interval

func GetDonationWeightForPlayer

func GetDonationWeightForPlayer(playerID string, from, to int64, db *mgo.Database, logger zap.Logger) (int, error)

GetDonationWeightForPlayer returns the donation weight for a given player in a given interval

func GetDonationWeightKey

func GetDonationWeightKey(prefix, gameID, clanID string, date time.Time, resetType ResetType) string

GetDonationWeightKey returns the key to be used in redis for the scores

func GetDonationsCollection

func GetDonationsCollection(db *mgo.Database) *mgo.Collection

GetDonationsCollection to update or query games

func GetExpirationDate

func GetExpirationDate(date time.Time, resetType ResetType, clock Clock) int64

GetExpirationDate returns the date to set the expiration in seconds to for the given reset type

func GetGamesCollection

func GetGamesCollection(db *mgo.Database) *mgo.Collection

GetGamesCollection to update or query games

func GetPlayersCollection

func GetPlayersCollection(db *mgo.Database) *mgo.Collection

GetPlayersCollection to update or query games

func IncrementDonationWeightForClan

func IncrementDonationWeightForClan(redis redis.Conn, gameID, clanID string, weight int, clock Clock) error

IncrementDonationWeightForClan should increment the donation weight for a clan for all time periods

func IncrementDonationWeightForPlayer

func IncrementDonationWeightForPlayer(redis redis.Conn, gameID, playerID string, weight int, clock Clock) error

IncrementDonationWeightForPlayer should increment the donation weight for a player for all time periods

func UpdateDonationWindowStart

func UpdateDonationWindowStart(gameID, id string, timestamp int64, db *mgo.Database, logger zap.Logger) error

UpdateDonationWindowStart updates the donation window start for the player with specified id

Types

type Clock

type Clock interface {
	GetUTCTime() time.Time
}

Clock identifies a clock to be used by the model

type Donation

type Donation struct {
	ID                string `json:"id" bson:"_id,omitempty"`
	GameID            string `json:"gameID" bson:"gameID"`
	Clan              string `json:"clan" bson:"clan"`
	Player            string `json:"player" bson:"player"`
	DonationRequestID string `json:"donationRequestID" bson:"donationRequestID"`
	Amount            int    `json:"amount" bson:"amount"`
	Weight            int    `json:"weight" bson:"weight"`
	CreatedAt         int64  `json:"createdAt" bson:"createdAt"`
}

Donation represents a specific donation a player made to a request

func GetDonationFromJSON

func GetDonationFromJSON(data []byte) (*Donation, error)

GetDonationFromJSON unmarshals the donation from the specified JSON

func (Donation) MarshalEasyJSON

func (v Donation) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Donation) UnmarshalEasyJSON

func (v *Donation) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type DonationRequest

type DonationRequest struct {
	ID        string     `json:"id" bson:"_id,omitempty"`
	Item      string     `json:"item" bson:"item,omitempty"`
	Player    string     `json:"player" bson:"player"`
	Clan      string     `json:"clan" bson:"clan"`
	GameID    string     `json:"gameID" bson:"gameID"`
	Donations []Donation `json:"donations" bson:""`

	CreatedAt  int64 `json:"createdAt" bson:"createdAt"`
	UpdatedAt  int64 `json:"updatedAt" bson:"updatedAt,omitempty"`
	FinishedAt int64 `json:"finishedAt" bson:"finishedAt,omitempty"`

	Clock Clock `json:"-" bson:"-"`
}

DonationRequest represents a request for an item donation a player made in a game

func GetDonationRequestByID

func GetDonationRequestByID(id string, db *mgo.Database, logger zap.Logger) (*DonationRequest, error)

GetDonationRequestByID rtrieves the game by its id

func GetDonationRequestFromJSON

func GetDonationRequestFromJSON(data []byte) (*DonationRequest, error)

GetDonationRequestFromJSON unmarshals the donation request from the specified JSON

func NewDonationRequest

func NewDonationRequest(gameID string, item, player, clan string, clock ...Clock) *DonationRequest

NewDonationRequest returns a new instance of DonationRequest

func (*DonationRequest) Create

func (d *DonationRequest) Create(db *mgo.Database, logger zap.Logger) error

Create new donation request

func (*DonationRequest) Donate

func (d *DonationRequest) Donate(playerID string, amount, maxWeightPerPlayer int, r redis.Conn, db *mgo.Database, logger zap.Logger) error

Donate an item in a given Donation Request

func (*DonationRequest) GetDonationCount

func (d *DonationRequest) GetDonationCount() int

GetDonationCount returns the total amount of donations

func (*DonationRequest) GetDonationCountForPlayer

func (d *DonationRequest) GetDonationCountForPlayer(playerID string) int

GetDonationCountForPlayer returns the total amount of donations for a given player ID

func (*DonationRequest) GetGame

func (d *DonationRequest) GetGame(db *mgo.Database, logger zap.Logger) (*Game, error)

GetGame associated to this donation request

func (DonationRequest) MarshalEasyJSON

func (v DonationRequest) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*DonationRequest) ToJSON

func (d *DonationRequest) ToJSON() ([]byte, error)

ToJSON returns the game as JSON

func (*DonationRequest) UnmarshalEasyJSON

func (v *DonationRequest) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*DonationRequest) ValidateDonationRequestLimit

func (d *DonationRequest) ValidateDonationRequestLimit(game *Game, amount int, logger zap.Logger) error

ValidateDonationRequestLimit ensures that no more than the allowed number of donations has been donated

func (*DonationRequest) ValidateDonationRequestLimitPerPlayer

func (d *DonationRequest) ValidateDonationRequestLimitPerPlayer(game *Game, player string, amount int, logger zap.Logger) error

ValidateDonationRequestLimitPerPlayer ensures that no more than the allowed number of donations has been donated per player

type Game

type Game struct {
	ID   string `json:"id" bson:"_id,omitempty"`
	Name string `json:"name" bson:"name,omitempty"`

	// List of items in this game
	Items map[string]Item `json:"items" bson:""`

	// Number of hours since player's first donation before the limit of donations reset.
	// If player donates item A at timestamp X, then their donations limit resets at x + DonationCooldownHours.
	DonationCooldownHours int `json:"donationCooldownHours" bson:"donationCooldownHours"`

	// Cooldown a player must wait before doing his next donation request. Defaults to 8hs
	DonationRequestCooldownHours int `json:"donationRequestCooldownHours" bson:"donationRequestCooldownHours"`

	UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
}

Game represents each game in UR

func GetGameByID

func GetGameByID(id string, db *mgo.Database, logger zap.Logger) (*Game, error)

GetGameByID rtrieves the game by its id

func GetGameFromJSON

func GetGameFromJSON(data []byte) (*Game, error)

GetGameFromJSON unmarshals the game from the specified JSON

func NewGame

func NewGame(
	name, id string,
	donationCooldownHours,
	donationRequestCooldownHours int,
) *Game

NewGame returns a new instance of Game

func (*Game) AddItem

func (g *Game) AddItem(
	key string, metadata map[string]interface{},
	limitOfItemsInEachDonationRequest,
	limitOfItemsPerPlayerDonation,
	weightPerDonation int,
	db *mgo.Database, logger zap.Logger,
) (*Item, error)

AddItem to this game

func (Game) MarshalEasyJSON

func (v Game) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Game) Save

func (g *Game) Save(db *mgo.Database, logger zap.Logger) error

Save new game if no ID and updates it otherwise

func (*Game) ToJSON

func (g *Game) ToJSON() ([]byte, error)

ToJSON marshals game to json

func (*Game) UnmarshalEasyJSON

func (v *Game) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type Item

type Item struct {
	Key      string                 `json:"item" bson:"key,omitempty"`
	Metadata map[string]interface{} `json:"metadata" bson:"metadata"`

	LimitOfItemsInEachDonationRequest int `json:"limitOfItemsInEachDonationRequest" bson:"limitOfItemsInEachDonationRequest"`
	LimitOfItemsPerPlayerDonation     int `json:"limitOfItemsPerPlayerDonation" bson:"limitOfItemsPerPlayerDonation"`

	//This weight counts for the donation cooldown limits of each player
	WeightPerDonation int `json:"weightPerDonation" bson:"weightPerDonation"`

	UpdatedAt int64 `json:"updatedAt" bson:"updatedAt"`
}

Item represents one donatable item in a given game

func GetItemFromJSON

func GetItemFromJSON(data []byte) (*Item, error)

GetItemFromJSON unmarshals the item from the specified JSON

func NewItem

func NewItem(
	key string, metadata map[string]interface{},
	weightPerDonation,
	limitOfItemsPerPlayerDonation,
	limitOfItemsInEachDonationRequest int,
) *Item

NewItem returns a configured new item

func (Item) MarshalEasyJSON

func (v Item) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Item) ToJSON

func (i *Item) ToJSON() ([]byte, error)

ToJSON of this struct

func (*Item) UnmarshalEasyJSON

func (v *Item) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type M

type M bson.M

M is an alias for bson.M

type Player

type Player struct {
	GameID              string `json:"gameID" bson:"gameID"`
	ID                  string `json:"id" bson:"_id,omitempty"`
	DonationWindowStart int64  `json:"donationWindowStart" bson:"donationWindowStart"`
}

Player represents one player in a given game

func GetPlayerByID

func GetPlayerByID(id string, db *mgo.Database, logger zap.Logger) (*Player, error)

GetPlayerByID rtrieves the game by its id

func GetPlayerFromJSON

func GetPlayerFromJSON(data []byte) (*Player, error)

GetPlayerFromJSON unmarshals the player from the specified JSON

func NewPlayer

func NewPlayer(gameID, playerID string, donationWindowStart int64) *Player

NewPlayer returns a new player

func (Player) MarshalEasyJSON

func (v Player) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Player) ToJSON

func (p *Player) ToJSON() ([]byte, error)

ToJSON of this struct

func (*Player) UnmarshalEasyJSON

func (v *Player) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type RealClock

type RealClock struct{}

RealClock returns the current time as UTC Date

func (*RealClock) GetUTCTime

func (r *RealClock) GetUTCTime() time.Time

GetUTCTime returns the current time as UTC Date

type ResetType

type ResetType int

ResetType represents the type of time that resets scores

Jump to

Keyboard shortcuts

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