models

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRoomNotFound when room does not exist
	ErrRoomNotFound = errors.New("room not found")

	// ErrRoomStopped when room is stopped
	ErrRoomStopped = errors.New("room stopped")
)
View Source
var (
	// Db saves database
	Db *gorm.DB
)

Functions

func Close

func Close()

Close Db

func Load

func Load()

Load init Db from config

func RestartAllRooms added in v0.1.1

func RestartAllRooms()

RestartAllRooms restart all not disabled rooms

func RoomUntilNextTick

func RoomUntilNextTick(id uint) (time.Duration, error)

RoomUntilNextTick return time until next tick if room open; if room is stopped, 0, ErrRoomStopped will be returned; if room not found, 0, ErrRoomNotFound will be returned

func UserNameValidate

func UserNameValidate(name string) bool

UserNameValidate validate user name

func UserPassValidate

func UserPassValidate(pass string) bool

UserPassValidate validate user password

func UserSubmitValidate

func UserSubmitValidate(submit float64) bool

UserSubmitValidate validate user submit

Types

type Room

type Room struct {
	ID        uint       `gorm:"primary_key"`
	CreatedAt time.Time  `json:"-"`
	UpdatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `sql:"index" json:"-"`

	Users        []User
	RoomHistorys []RoomHistory `json:",omitempty"`

	Interval   int
	RoundNow   int
	RoundTotal int

	Status int
}

Room hold user info

func (*Room) GetHistory

func (r *Room) GetHistory() (history []RoomHistory)

GetHistory return room's history

func (*Room) GetUsers

func (r *Room) GetUsers() (users []User)

GetUsers return room's users

func (*Room) GetUsersWithCache added in v0.1.2

func (r *Room) GetUsersWithCache() (users []User)

GetUsersWithCache return room's users from room cache

func (*Room) Runner

func (r *Room) Runner(worker *roomWorker)

Runner of room

func (*Room) Start

func (r *Room) Start() bool

Start the room

func (*Room) Stop

func (r *Room) Stop() bool

Stop the room

func (*Room) String

func (r *Room) String() string

String return formated room info

type RoomHistory

type RoomHistory struct {
	ID        uint       `gorm:"primary_key" json:"-"`
	CreatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `sql:"index" json:"-"`

	RoomID uint

	Round     int
	GoldenNum float64
}

RoomHistory holds room history

type User

type User struct {
	ID        uint       `gorm:"primary_key"`
	CreatedAt time.Time  `json:"-"`
	UpdatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `sql:"index" json:"-"`

	RoomID       uint
	UserHistorys []UserHistory `json:",omitempty"`

	Name  string
	Score int

	Hashed string `json:"-"`

	Submit1 float64 `gorm:"-" json:"-"`
	Submit2 float64 `gorm:"-" json:"-"`
}

User hold user info

func UserNew

func UserNew(roomid uint, name, pass string) (*User, error)

UserNew build a new user

func (*User) Auth

func (u *User) Auth(password string) error

Auth auth user

func (*User) GetHistory

func (u *User) GetHistory() (history []UserHistory)

GetHistory return user's history

func (*User) String

func (u *User) String() string

String return formatted user info

func (*User) Submit

func (u *User) Submit(s1, s2 float64) error

Submit user input

type UserHistory

type UserHistory struct {
	ID        uint       `gorm:"primary_key" json:"-"`
	CreatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `sql:"index" json:"-"`

	UserID uint

	Round    int
	Score    int
	ScoreGet int
	Submit1  float64
	Submit2  float64
}

UserHistory holds user history

Jump to

Keyboard shortcuts

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