models

package
v0.0.0-...-de1c0ac Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusWaiting means the status is "waiting"
	StatusWaiting = 0
	// StatusStarting means the status is "starting"
	StatusStarting = 1
	// StatusOngoing means the status is "on-going"
	StatusOngoing = 2
	// StatusEnded means the status is "ended"
	StatusEnded = 3
)

Variables

View Source
var (

	// ErrUserNotFound common error on login form when the user is not found
	ErrUserNotFound = errors.New("user not found")

	// ErrInvalidLogin common error on login form when the user does not match its login credentials
	ErrInvalidLogin = errors.New("invalid login")

	// ErrUsernameTaken common error on registration form when the username already existed
	ErrUsernameTaken = errors.New("username taken")

	// ErrQuestionDuplicate specific error on whether the question has already been asked
	ErrQuestionDuplicate = errors.New("question already existed")

	// ErrEmptyUserQuestion specific error for capturing uninitialized array error
	ErrEmptyUserQuestion = errors.New("user has no question")

	// ErrTypeMismatch specific error for capturing type mismatch
	ErrTypeMismatch = errors.New("the type didn't match")

	// ErrNilClient gives error message when redis client variable is nil
	ErrNilClient = errors.New("client is nil")

	// ErrUserInLobby gives error message when user attempts to join a lobby when is already in lobby
	ErrUserInLobby = errors.New("user is currently joined in to a lobby")

	// ErrUserNotInLobby gives error message when user attempts to get its current lobby when is not in lobby
	ErrUserNotInLobby = errors.New("user is currently not joined in to a lobby")

	// ErrLobbyEmptyMembers gives error message when the lobby has no members
	ErrLobbyEmptyMembers = errors.New("lobby is currently empty")

	// ErrGameEnded gives error message when the game has already ended
	ErrGameEnded = errors.New("game has already end")
)

Functions

func GetRank

func GetRank(userID int64) int64

GetRank returns the current rank of the user

func GetUserIDByUser

func GetUserIDByUser(user *User) int64

GetUserIDByUser gets the user id using the user

func GetUserQuestionsLen

func GetUserQuestionsLen() (int64, error)

GetUserQuestionsLen Questions len getter

func IsUser

func IsUser(userID int64) (bool, error)

IsUser checks if username has registered in this site

func JoinLobby

func JoinLobby(code string, userID int64) error

JoinLobby joins the user to the lobby from the given code

func JoinOrCreateLobby

func JoinOrCreateLobby(choice, code string, userID int64) error

JoinOrCreateLobby process whether the lobby is joined or created by the current user

func MigrateQuestions

func MigrateQuestions(client *redis.Client, path string) error

MigrateQuestions sends the questions.json to the redis server

func NewQuestion

func NewQuestion(statement, answer string, choices []string) error

NewQuestion creates a new question, saves it to the database, and returns the newly created question

func NewRedisDB

func NewRedisDB() error

NewRedisDB instantiates a package-level redis client access

func PostUpdate

func PostUpdate(userID int64, body string) error

PostUpdate adds a new update

func RegisterUser

func RegisterUser(username, password string) error

RegisterUser register a valid user

func UpdateRank

func UpdateRank(userID, points int64) error

UpdateRank replaces the user's points with a new value

func UserConfirmAnswer

func UserConfirmAnswer(userID int64, choice string) (bool, error)

UserConfirmAnswer returns true of false if the answer is correct and it gives the user's points wtih new question as a result of choosing the right answer

Types

type Lobby

type Lobby struct {
	// contains filtered or unexported fields
}

Lobby is a manager for accessing users in the database

func GetLobbyByCode

func GetLobbyByCode(code string) (*Lobby, error)

GetLobbyByCode get the lobby based on the given code

func GetLobbyByUserID

func GetLobbyByUserID(userID int64) (*Lobby, error)

func NewLobby

func NewLobby(hostID int64, length int) (*Lobby, error)

NewLobby creates new lobby, saves it to the database, and returns the newly created lobby

func (*Lobby) AddMember

func (l *Lobby) AddMember(userID int64) error

AddMember adds a member to the lobby

func (*Lobby) CloseLobby

func (l *Lobby) CloseLobby() error

CloseLobby sets the lobby status to ended then permits entering of member

func (*Lobby) GetCode

func (l *Lobby) GetCode() (string, error)

GetCode Lobby Code getter

func (*Lobby) GetHostID

func (l *Lobby) GetHostID() (int64, error)

GetHostID Lobby HostID getter

func (*Lobby) GetLobbyID

func (l *Lobby) GetLobbyID() (int64, error)

GetLobbyID LobbyID getter

func (*Lobby) GetMembers

func (l *Lobby) GetMembers() ([]*User, error)

GetMembers gets all the members

func (*Lobby) GetPlayers

func (l *Lobby) GetPlayers() ([]string, error)

GetPlayers get all the players' usernames

func (*Lobby) GetStatus

func (l *Lobby) GetStatus() (int64, error)

func (*Lobby) GetTopMember

func (l *Lobby) GetTopMember() (*User, error)

GetTopMember gets the member supposedly inherits the host role

func (*Lobby) IsMember

func (l *Lobby) IsMember(userID int64) (bool, error)

IsMember checks if the user is a member

func (*Lobby) LeaveLobby

func (l *Lobby) LeaveLobby(userID int64) error

LeaveLobby makes the user leave from the lobby

func (*Lobby) SetHostID

func (l *Lobby) SetHostID(hostID int64) error

SetHostID Lobby HostID setter

func (*Lobby) SetStatus

func (l *Lobby) SetStatus(status int64) error

type Question

type Question struct {
	// contains filtered or unexported fields
}

Question is a manager for accessing questions in the database

func GetAllQuestions

func GetAllQuestions() ([]*Question, error)

GetAllQuestions All Updates getter

func GetQuestions

func GetQuestions(userID int64) ([]*Question, error)

GetQuestions gets all updates related to the user

func (*Question) GetAnswer

func (q *Question) GetAnswer() (string, error)

GetAnswer Answer getter

func (*Question) GetChoices

func (q *Question) GetChoices() ([]string, error)

GetChoices Choices getter

func (*Question) GetQuestionID

func (q *Question) GetQuestionID() int64

GetQuestionID QuestionID getter

func (*Question) GetStatement

func (q *Question) GetStatement() (string, error)

GetStatement Statement getter

type QuestionT

type QuestionT struct {
	Statement string   `csv:"statement" json:"statement"`
	Answer    string   `csv:"answer" json:"answer"`
	Choices   []string `csv:"choices" json:"choices"`
}

QuestionT is a unit for the exam

func GetQuestion

func GetQuestion(q *Question) (*QuestionT, error)

GetQuestion retrieves a whole struct of question from the database

type RankT

type RankT struct {
	Rank  int
	Name  string
	Score int64
}

RankT is a simple data struct for a sorted leaderboard

func GetCurrentStandings

func GetCurrentStandings(userID int64) ([]RankT, error)

GetCurrentStandings list the raks of the 12 users above and below your current standing

func TopRanks

func TopRanks() ([]RankT, error)

TopRanks lists the overall top 25

type Update

type Update struct {
	// contains filtered or unexported fields
}

Update is a manager for accessing updates in the database

func GetAllUpdates

func GetAllUpdates() ([]*Update, error)

GetAllUpdates All Updates getter

func GetUpdates

func GetUpdates(userID int64) ([]*Update, error)

GetUpdates gets all updates related to the user

func NewUpdate

func NewUpdate(userID int64, body string) (*Update, error)

NewUpdate creates a new update, saves it to the database, and returns the newly created question

func (*Update) GetBody

func (u *Update) GetBody() (string, error)

GetBody Body getter

func (*Update) GetUser

func (u *Update) GetUser() (*User, error)

GetUser User getter

type User

type User struct {
	// contains filtered or unexported fields
}

User is a manager for accessing users in the database

func AuthenticateUser

func AuthenticateUser(username, password string) (*User, error)

AuthenticateUser authenticates the user by its username and password

func GetUserByUserID

func GetUserByUserID(userID int64) (*User, error)

GetUserByUserID gets user using a user id

func GetUserByUsername

func GetUserByUsername(username string) (*User, error)

GetUserByUsername gets the user using the username

func NewUser

func NewUser(username string, hash []byte) (*User, error)

NewUser create a new user, saves it to the database, and returns the newly created user

func (*User) Authenticate

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

Authenticate will validates the login attempt

func (*User) GetHash

func (u *User) GetHash() ([]byte, error)

GetHash Hash getter

func (*User) GetLobby

func (u *User) GetLobby() (*Lobby, error)

GetLobby Lobby getter

func (*User) GetUserID

func (u *User) GetUserID() int64

GetUserID UserID getter

func (*User) GetUsername

func (u *User) GetUsername() (string, error)

GetUsername Username getter

type UserQuestion

type UserQuestion struct {
	// contains filtered or unexported fields
}

UserQuestion is a manager for accessing users' current question status in the database

func GetUserQuestion

func GetUserQuestion(userID int64) (*UserQuestion, error)

GetUserQuestion gets UserQuestion using user id

func (*UserQuestion) AddPoints

func (uq *UserQuestion) AddPoints(amount int64) error

AddPoints Points setter

func (*UserQuestion) GetPoints

func (uq *UserQuestion) GetPoints() (int64, error)

GetPoints Points getter

func (*UserQuestion) GetQuestion

func (uq *UserQuestion) GetQuestion() (*Question, error)

GetQuestion Question getter

func (*UserQuestion) GetUnansweredQuestions

func (uq *UserQuestion) GetUnansweredQuestions() ([]*Question, error)

GetUnansweredQuestions gets all questions user haven't answered

func (*UserQuestion) GetUser

func (uq *UserQuestion) GetUser() (*User, error)

GetUser User getter

func (*UserQuestion) GetUserQuestions

func (uq *UserQuestion) GetUserQuestions() ([]*Question, error)

GetUserQuestions Questions by the user getter

func (*UserQuestion) RegisterQuestion

func (uq *UserQuestion) RegisterQuestion(questionID int64) error

RegisterQuestion Question setter

Jump to

Keyboard shortcuts

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