model

package
v0.0.0-...-7518d33 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Rdb *redis.Client

Functions

func AutoMigrateDb

func AutoMigrateDb()

AutoMigrateDb migrates the database

func DeleteBaseContestByID

func DeleteBaseContestByID(id uint) error

func DeleteContestByID

func DeleteContestByID(id uint) error

func DeleteContestantByID

func DeleteContestantByID(id uint) error

func DeleteGameByID

func DeleteGameByID(id uint) error

func DeleteSdkByID

func DeleteSdkByID(id uint) error

func GetMatchNumByBaseContestID

func GetMatchNumByBaseContestID(baseContestID uint) (uint, error)

func GetVerificationCode

func GetVerificationCode(email string) (string, error)

func InitDb

func InitDb()

InitDb initializes the database connection

func InitRedis

func InitRedis()

InitRedis initializes the redis connection

func SaveVerificationCode

func SaveVerificationCode(code string, email string, expireInMinutes int) error

func UpdateAiByID

func UpdateAiByID(id uint, updates map[string]interface{}) error

func UpdateBaseContestByID

func UpdateBaseContestByID(id uint, updates map[string]interface{}) error

func UpdateContestByID

func UpdateContestByID(id uint, updates map[string]interface{}) error

func UpdateContestantByID

func UpdateContestantByID(id uint, updates map[string]interface{}) error

func UpdateGameByID

func UpdateGameByID(id uint, updates map[string]interface{}) error

func UpdateMatchByID

func UpdateMatchByID(id uint, updates map[string]interface{}) error

func UpdateSdkByID

func UpdateSdkByID(id uint, updates map[string]interface{}) error

func UpdateUserByEmail

func UpdateUserByEmail(email string, updates map[string]interface{}) error

func UpdateUserByID

func UpdateUserByID(id uint, updates map[string]interface{}) error

func UpdateUserByUsername

func UpdateUserByUsername(username string, updates map[string]interface{}) error

func UpsertUser

func UpsertUser(user User)

UpsertUser upserts a user. If the user exists(determined by username), update its password. If the user does not exist, create it.

Types

type Ai

type Ai struct {
	gorm.Model
	BaseContestID uint `gorm:"not null;index"`

	UserID uint `gorm:"not null;index"`
	User   User `gorm:"foreignKey:UserID"`
	SdkID  uint `gorm:"not null"`
	Sdk    Sdk  `gorm:"foreignKey:SdkID"`

	Note   string
	Status TaskStatus `gorm:"embedded;embeddedPrefix:task_"`

	// snapshot fields
	GameID uint
}

func GetAiByID

func GetAiByID(id uint, preload bool) (ai Ai, err error)

If preload is true, the following fields will be preloaded: Sdk, User

func GetAis

func GetAis(query QueryParams, preload bool) (ais []Ai, count int64, err error)

If preload is true, the following fields will be preloaded: Sdk, User

func (*Ai) BeforeCreate

func (a *Ai) BeforeCreate(tx *gorm.DB) (err error)

func (*Ai) Create

func (a *Ai) Create() error

Necessary fields: BaseContestID, UserID, SdkID Optional fields: Note

func (*Ai) Update

func (a *Ai) Update(updates map[string]interface{}) error

type BaseContest

type BaseContest struct {
	gorm.Model
	GameID uint          `gorm:"not null;index"`
	States ContestStates `gorm:"embedded"`
	Script string

	Admins []User `gorm:"many2many:base_contest_admins;"`
}

func GetBaseContestByID

func GetBaseContestByID(id uint) (bc BaseContest, err error)

func (*BaseContest) AddAdmin

func (bc *BaseContest) AddAdmin(userID uint) error

func (*BaseContest) Create

func (bc *BaseContest) Create(adminIDs []uint) error

Necessary fields: GameID Optional fields: Script, States

func (*BaseContest) Delete

func (bc *BaseContest) Delete() error

func (*BaseContest) DeleteContestantByUserID

func (bc *BaseContest) DeleteContestantByUserID(userID uint) error

func (*BaseContest) GetAdmins

func (bc *BaseContest) GetAdmins() ([]User, error)

func (*BaseContest) GetAis

func (bc *BaseContest) GetAis(query QueryParams, preload bool) ([]Ai, int64, error)

func (*BaseContest) GetContestantByUserID

func (bc *BaseContest) GetContestantByUserID(userID uint, preloads []PreloadQuery) (Contestant, error)

func (*BaseContest) GetContestants

func (bc *BaseContest) GetContestants(preloads []PreloadQuery) ([]Contestant, error)

Sorted by points in descending order. Currently supported preloads: "User", "AssignedAi"

func (*BaseContest) GetMatches

func (bc *BaseContest) GetMatches(query QueryParams, preload bool) ([]Match, int64, error)

func (*BaseContest) GetSdks

func (bc *BaseContest) GetSdks(fields ...string) (sdks []Sdk, err error)

func (*BaseContest) IsAdmin

func (bc *BaseContest) IsAdmin(userID uint) (bool, error)

func (*BaseContest) RemoveAdmin

func (bc *BaseContest) RemoveAdmin(userID uint) error

func (*BaseContest) Update

func (bc *BaseContest) Update(updates map[string]interface{}) error

func (*BaseContest) UpdateContestantByUserID

func (bc *BaseContest) UpdateContestantByUserID(userID uint, updates map[string]interface{}) error

type Contest

type Contest struct {
	gorm.Model
	BaseContest BaseContest `gorm:"foreignKey:ID"`

	Metadata Metadata `gorm:"embedded"`

	Registration    Registration `gorm:"embedded"`
	RegisteredUsers []User       `gorm:"many2many:contest_registrations;"`
}

func GetContestByID

func GetContestByID(id uint, fields ...string) (contest Contest, err error)

func GetContests

func GetContests(fields ...string) (contests []Contest, err error)

func (*Contest) Create

func (c *Contest) Create(gameID uint, adminIDs []uint) error

Optional fields: Metadata, Registration, BaseContest.Script, BaseContest.States

func (*Contest) Delete

func (c *Contest) Delete() error

func (*Contest) GetPrivilege

func (c *Contest) GetPrivilege(userID uint) (ContestPrivilege, error)

func (*Contest) Update

func (c *Contest) Update(updates map[string]interface{}) error

type ContestPrivilege

type ContestPrivilege string
const (
	ContestPrivilegeAdmin        ContestPrivilege = "admin"
	ContestPrivilegeRegistered   ContestPrivilege = "registered"
	ContestPrivilegeUnregistered ContestPrivilege = "unregistered"
)

type ContestStates

type ContestStates struct {
	AssignAiEnabled                 bool `gorm:"default: false"`
	CommitAiEnabled                 bool `gorm:"default: false"`
	ContestScriptEnvironmentEnabled bool `gorm:"default: false"`
	PrivateMatchEnabled             bool `gorm:"default: false"`
	PublicMatchEnabled              bool `gorm:"default: false"`
	TestMatchEnabled                bool `gorm:"default: false"`
}

type Contestant

type Contestant struct {
	gorm.Model
	BaseContestID uint `gorm:"not null;index"`
	UserID        uint `gorm:"not null;index"`
	User          User `gorm:"foreignKey:UserID"`

	Performance string                // editable by contest script
	Permissions ContestantPermissions `gorm:"embedded"`
	Points      int
	// AssignedAiID is 0 if the contestant doesn't assign an AI.
	AssignedAiID uint
	AssignedAi   Ai `gorm:"foreignKey:AssignedAiID"`
}

func GetContestant

func GetContestant(condition map[string]interface{}, preloads []PreloadQuery) (Contestant, error)

func GetContestantByID

func GetContestantByID(id uint, preloads []PreloadQuery) (Contestant, error)

func GetContestants

func GetContestants(filter map[string]interface{}, preloads []PreloadQuery) ([]Contestant, error)

Sorted by points in descending order. Currently supported preloads: "User", "AssignedAi"

func (*Contestant) Create

func (c *Contestant) Create() error

Necessary fields: BaseContestID, UserID Optional fields: Performance, Permissions, Points, AssignedAiID

func (*Contestant) Delete

func (c *Contestant) Delete() error

func (*Contestant) Update

func (c *Contestant) Update(updates map[string]interface{}) error

type ContestantPermissions

type ContestantPermissions struct {
	AssignAiEnabled    bool `gorm:"default:true"`
	PublicMatchEnabled bool `gorm:"default:true"`
}

type DockerTask

type DockerTask struct {
	Dockerfile string
	Status     TaskStatus `gorm:"embedded"`
}

type Game

type Game struct {
	gorm.Model
	BaseContest BaseContest `gorm:"foreignKey:ID"`

	Metadata Metadata `gorm:"embedded"`

	// game assets
	GameLogic   GameLogic   `gorm:"embedded;embeddedPrefix:game_logic_"`
	MatchDetail MatchDetail `gorm:"embedded;embeddedPrefix:match_detail_"`
}

func GetGameByID

func GetGameByID(id uint, fields ...string) (game Game, err error)

func GetGames

func GetGames(fields ...string) (games []Game, err error)

func (*Game) Create

func (g *Game) Create(adminIDs []uint) error

Optional fields: Metadata, GameLogic, MatchDetail, BaseContest.Script, BaseContest.States

func (*Game) Delete

func (g *Game) Delete() error

func (*Game) GetPrivilege

func (g *Game) GetPrivilege(userID uint) (GamePrivilege, error)

Note: Game doesn't need registration

func (*Game) Update

func (g *Game) Update(updates map[string]interface{}) error

type GameLogic

type GameLogic struct {
	Build  DockerTask `gorm:"embedded;embeddedPrefix:build_"`
	Run    DockerTask `gorm:"embedded;embeddedPrefix:run_"`
	Status TaskStatus `gorm:"embedded"`
}

type GamePrivilege

type GamePrivilege string
const (
	GamePrivilegeAdmin      GamePrivilege = "admin"
	GamePrivilegeRegistered GamePrivilege = "registered"
)

type GlobalPermissions

type GlobalPermissions struct {
	CanCreateGameOrContest bool `gorm:"default: false"`
}

type Match

type Match struct {
	gorm.Model
	BaseContestID uint `gorm:"not null;index"`

	Ais   []Ai `gorm:"many2many:match_ais;"`
	State TaskState
	Tag   string

	Scores []int `gorm:"serializer:json"`

	// Customized Info string passed to game logic. JSON format.
	ExtraInfo string
	MatchType MatchType `gorm:"default: public"`

	// snapshot fields
	GameID uint
}

func GetMatchByID

func GetMatchByID(id uint, preload bool) (match Match, err error)

If preload is true, the following fields will be preloaded: Ais, Ais.Usergi

func GetMatches

func GetMatches(query QueryParams, preload bool) (matches []Match, count int64, err error)

Sorted by id in descending order. If preload is true, the following fields will be preloaded: Ais, Ais.User

func (*Match) BeforeCreate

func (m *Match) BeforeCreate(tx *gorm.DB) (err error)

func (*Match) Create

func (m *Match) Create(playerIDs []uint) error

Necessary fields: BaseContestID Optional fields: Tag

func (*Match) Update

func (m *Match) Update(updates map[string]interface{}) error

type MatchDetail

type MatchDetail struct {
	Template string
}

type MatchType

type MatchType string
const (
	MatchTypePublic  MatchType = "public"
	MatchTypePrivate MatchType = "private"
	MatchTypeTest    MatchType = "test"
)

type Metadata

type Metadata struct {
	CoverUrl string
	Readme   string
	Title    string
}

type PreloadQuery

type PreloadQuery struct {
	Table   string
	Columns []string
}

type QueryParams

type QueryParams struct {
	Filter map[string]interface{}
	Offset int
	Limit  int
	Fields []string
}

type Registration

type Registration struct {
	RegistrationEnabled bool `gorm:"default: false"`
	Password            string
}

type Sdk

type Sdk struct {
	gorm.Model
	BaseContestID uint `gorm:"not null;index"`

	Name   string `gorm:"not null"`
	Readme string

	BuildAi DockerTask `gorm:"embedded;embeddedPrefix:build_ai_"`
	RunAi   DockerTask `gorm:"embedded;embeddedPrefix:run_ai_"`
}

func GetSdkByID

func GetSdkByID(id uint, fields ...string) (Sdk, error)

func (*Sdk) Create

func (s *Sdk) Create() error

Necessary fields: BaseContestID, Name Optional fields: Readme, BuildAi, RunAi

func (*Sdk) Delete

func (s *Sdk) Delete() error

func (*Sdk) Update

func (s *Sdk) Update(updates map[string]interface{}) error

type TaskState

type TaskState string
const (
	TaskStatePending     TaskState = "pending"
	TaskStateRunning     TaskState = "running"
	TaskStateFinished    TaskState = "finished"
	TaskStateInputError  TaskState = "input_error"
	TaskStateStopped     TaskState = "stopped"
	TaskStateSystemError TaskState = "system_error"
)

func (*TaskState) BeforeCreate

func (ts *TaskState) BeforeCreate(tx *gorm.DB) (err error)

type TaskStatus

type TaskStatus struct {
	State TaskState
	Msg   string
}

func (*TaskStatus) BeforeSave

func (ts *TaskStatus) BeforeSave(tx *gorm.DB) (err error)

BeforeSave truncates the message to 1000 characters.

type User

type User struct {
	gorm.Model
	AvatarURL   string
	Bio         string
	Department  string
	Email       string `gorm:"uniqueIndex,not null"`
	Name        string
	Nickname    string            `gorm:"index"`
	Password    []byte            `gorm:"not null"`
	Permissions GlobalPermissions `gorm:"embedded"`
	School      string
	Username    string `gorm:"uniqueIndex,not null"`

	ContestRegistered []Contest `gorm:"many2many:contest_registrations;"`
}

func GetUserByEmail

func GetUserByEmail(email string, fields ...string) (User, error)

func GetUserByID

func GetUserByID(id uint, fields ...string) (User, error)

func GetUserByUsername

func GetUserByUsername(username string, fields ...string) (User, error)

func SearchUsers

func SearchUsers(keyword string, searchFields []string, resultFields ...string) ([]User, error)

func (*User) Create

func (u *User) Create() error

Necessary fields: Username, Email, Password Optional fields: AvatarURL, Bio, Department, Name, Nickname, School, Permissions

func (*User) GetContestRegistered

func (u *User) GetContestRegistered(fields ...string) ([]Contest, error)

func (*User) Update

func (u *User) Update(updates map[string]interface{}) error

Jump to

Keyboard shortcuts

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