model

package
v0.0.0-...-911e024 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TyItemFlashCard      = "flash_card"
	TyItemMultipleChoice = "multiple_choice"
	TyItemCompletion     = "completion"
)

Variables

View Source
var CKBook = cachekey.MustNewSchema[utils.UInt64]("book:{book_id}", 10*time.Minute)
View Source
var CKDungeonMonsterCounts = cachekey.MustNewSchema[utils.UInt64](

	"dungeon:{dungeon_id}:monsters:count", time.Second*20) // 不主动淘汰,20s 左右更新
View Source
var CKDungeonNewStuffCountDaily = cachekey.MustNewSchema[CParamNewStuffCountDaily](

	"dungeon:{dungeon_id}:new_stuff_count_daily:{date}", time.Hour*24) // 按天淘汰

Functions

func AddEntityTags

func AddEntityTags(ctx context.Context, tx *gorm.DB, userID utils.UInt64, entityType EntityType, entityID utils.UInt64, tags ...string) error

AddEntityTags adds tags to an entity for a user.

func AddUserCash

func AddUserCash(db *gorm.DB, userID utils.UInt64, points int) error

AddUserCash 更新用户积分

func CreateItems

func CreateItems(ctx context.Context, tx *gorm.DB, userID utils.UInt64, items []*Item, itemTagRef map[*Item][]string) error

func GetItemIDsOfBook

func GetItemIDsOfBook(tx *gorm.DB, bookID utils.UInt64, offset, limit int) (itemIDs []utils.UInt64, err error)

GetItemIDsOfBook 获取某个 book 的 items

func GetItemIDsOfBooks

func GetItemIDsOfBooks(tx *gorm.DB, bookIDs []utils.UInt64) (itemBookMap map[utils.UInt64]utils.UInt64, err error)

GetItemIDsOfBooks 获取 book 关联的 items

func GetItemIDsOfTags

func GetItemIDsOfTags(ctx context.Context, userID utils.UInt64, tags []string) (map[utils.UInt64]string, error)

GetItemIDsOfTags 获取 tag 关联的 items

func MustInit

func MustInit(ctx context.Context, db *gorm.DB, queue *redismq.Queue)

func RemoveEntityTags

func RemoveEntityTags(ctx context.Context, tx *gorm.DB, userID utils.UInt64, entityID utils.UInt64, tagsToRemove ...string) error

RemoveEntityTags removes tags from an entity for a user.

func UpdateEntityTagsDiff

func UpdateEntityTagsDiff(ctx context.Context, tx *gorm.DB, userID utils.UInt64, entityID utils.UInt64, tags []string) error

UpdateEntityTagsDiff handles the difference between the tags of an entity and the new tags.

Types

type Book

type Book struct {
	ID          utils.UInt64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
	UserID      utils.UInt64 `gorm:"index:idx_user;not null" json:"user_id"`
	Title       string       `gorm:"not null"`
	Description string
	CreatedAt   time.Time
	UpdatedAt   time.Time

	DeletedAt gorm.DeletedAt `gorm:"index"`
}

func FindBook

func FindBook(ctx context.Context, tx *gorm.DB, id utils.UInt64) (*Book, error)

func FindBooksOfTag

func FindBooksOfTag(ctx context.Context, tx *gorm.DB, userID utils.UInt64, tag string, pager *utils.Pager) ([]Book, error)

FindBooksOfTag returns the items

func (*Book) AfterCreate

func (b *Book) AfterCreate(tx *gorm.DB) (err error)

func (*Book) AfterDelete

func (b *Book) AfterDelete(tx *gorm.DB) (err error)

func (*Book) AfterUpdate

func (b *Book) AfterUpdate(tx *gorm.DB) (err error)

func (*Book) BeforeCreate

func (b *Book) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 钩子

func (*Book) BeforeDelete

func (b *Book) BeforeDelete(tx *gorm.DB) (err error)

BeforeDelete is a GORM hook that is called before deleting a book.

func (*Book) GetTags

func (b *Book) GetTags(ctx context.Context) ([]string, error)

func (*Book) MPutItems

func (b *Book) MPutItems(ctx context.Context, tx *gorm.DB, itemIDs []utils.UInt64) (successItemIDs []utils.UInt64, err error)

func (*Book) TableName

func (b *Book) TableName() string

type BookItem

type BookItem struct {
	BookID utils.UInt64 `gorm:"primaryKey"`
	ItemID utils.UInt64 `gorm:"primaryKey"`
}

func (*BookItem) TableName

func (b *BookItem) TableName() string

type CParamNewStuffCountDaily

type CParamNewStuffCountDaily struct {
	ID   utils.UInt64 `cachekey:"dungeon_id"`
	Date string       `cachekey:"date"`
}

type Dungeon

type Dungeon struct {
	ID     utils.UInt64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
	UserID utils.UInt64 `gorm:"not null" json:"user_id,string"`

	Type        def.DungeonType `gorm:"not null" json:"type"`
	Title       string          `gorm:"not null" json:"title"`
	Description string          `json:"description"`

	MemorizationSetting

	// system
	CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
	DeletedAt gorm.DeletedAt
}

func CreateDungeon

func CreateDungeon(ctx context.Context, tx *gorm.DB, d *Dungeon) (*Dungeon, error)

func FindDungeon

func FindDungeon(ctx context.Context, tx *gorm.DB, dungeonID utils.UInt64) (*Dungeon, error)

func (*Dungeon) AddMonsterFromBook

func (d *Dungeon) AddMonsterFromBook(ctx context.Context, tx *gorm.DB, sourceEntityIDs []utils.UInt64) error

func (*Dungeon) AddMonsters

func (d *Dungeon) AddMonsters(ctx context.Context, tx *gorm.DB, items []utils.UInt64) error

func (*Dungeon) BeforeCreate

func (d *Dungeon) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 钩子

func (*Dungeon) BeforeDelete

func (d *Dungeon) BeforeDelete(tx *gorm.DB) (err error)

BeforeDelete hooks for cleaning up associations

func (*Dungeon) CountMonsters

func (d *Dungeon) CountMonsters(ctx context.Context, tx *gorm.DB) (int64, error)

func (*Dungeon) GetAssociations

func (d *Dungeon) GetAssociations(ctx context.Context, tx *gorm.DB) (books, items []utils.UInt64, tags []string, err error)

GetAssociations Helper function to get associated books, items, and tags for a dungeon

func (*Dungeon) GetBookIDs

func (d *Dungeon) GetBookIDs(ctx context.Context, tx *gorm.DB) ([]utils.UInt64, error)

func (*Dungeon) GetDirectMonsters

func (d *Dungeon) GetDirectMonsters(tx *gorm.DB, offset, limit int) ([]DungeonMonster, error)

GetDirectMonsters - 获取当前 Dungeon 的 DungeonMonster,不会尝试解析 books 和 tags 的关联

func (*Dungeon) GetItemIDs

func (d *Dungeon) GetItemIDs(ctx context.Context, tx *gorm.DB) ([]utils.UInt64, error)

func (*Dungeon) GetMonster

func (d *Dungeon) GetMonster(ctx context.Context, tx *gorm.DB, itemID utils.UInt64) (*DungeonMonster, error)

GetMonster retrieves monster in the dungeon by given itemID

func (*Dungeon) GetMonsters

func (d *Dungeon) GetMonsters(ctx context.Context, tx *gorm.DB, offset, limit int) ([]DungeonMonster, error)

GetMonsters retrieves the monsters for the dungeon with sorting and pagination

func (*Dungeon) GetMonstersForPractice

func (d *Dungeon) GetMonstersForPractice(ctx context.Context, tx *gorm.DB, count int) ([]DungeonMonster, error)

func (*Dungeon) GetMonstersWithExpandedAssociations

func (d *Dungeon) GetMonstersWithExpandedAssociations(ctx context.Context, tx *gorm.DB, offset, limit int) ([]DungeonMonster, error)

GetMonstersWithExpandedAssociations - 获取当前 Dungeon 的 DungeonMonster 及其关联的 Items, Books, Tags

func (*Dungeon) GetTags

func (d *Dungeon) GetTags(ctx context.Context) ([]string, error)

func (*Dungeon) SubtractBooks

func (d *Dungeon) SubtractBooks(ctx context.Context, tx *gorm.DB, books []utils.UInt64) (successIDs []utils.UInt64, err error)

type DungeonBook

type DungeonBook struct {
	DungeonID utils.UInt64 `gorm:"primaryKey"`
	BookID    utils.UInt64 `gorm:"primaryKey"`
}

type DungeonMonster

type DungeonMonster struct {
	DungeonID utils.UInt64
	ItemID    utils.UInt64

	SourceType MonsterSource
	SourceID   utils.UInt64

	// 用于 runtime
	PracticeAt     time.Time // 上次复习时间的记录
	NextPracticeAt time.Time // 下次复习时间
	PracticeCount  uint32    // 复习次数 (考虑到可能会有 merge 次数等逻辑,这里先用一个相对大的空间)

	// Gaming
	Visibility utils.Percentage `gorm:"default:0"` // Visibility 显影程度,根据复习次数变化
	Avatar     string           // 头像地址

	// 以下为宽表内容,为了加速查询
	Familiarity utils.Percentage `gorm:"default:0"` // UserMonster 向 DungeonMonster 单项同步

	Difficulty def.DifficultyLevel `gorm:"default:0x01"` // Item 向 DungeonMonster 单项同步
	Importance def.ImportanceLevel `gorm:"default:0x01"` // Item 向 DungeonMonster 单项同步

	CreatedAt time.Time

	// StoryTelling & Gaming
	Name        string
	Description string
}

type DungeonTag

type DungeonTag struct {
	DungeonID utils.UInt64 `gorm:"primaryKey"`
	TagID     utils.UInt64 `gorm:"primaryKey"`
}

type EntityType

type EntityType uint8
const (
	EntityTypeItem    EntityType = 1
	EntityTypeBook    EntityType = 2
	EntityTypeDungeon EntityType = 3
)

type GormScope

type GormScope = func(*gorm.DB) *gorm.DB

type Item

type Item struct {
	ID utils.UInt64 `gorm:"primaryKey;autoIncrement:true" json:"id"`

	CreatorID utils.UInt64 `gorm:"not null" json:"creator_id,string"`

	Type    string
	Content string

	// todo: 示意,后续应该放到 user、item 关联的表中去
	Difficulty def.DifficultyLevel `gorm:"default:0x01"` // 难度,默认值为 NoviceNormal (0x01)
	Importance def.ImportanceLevel `gorm:"default:0x01"` // 重要程度,默认值为 DomainGeneral (0x01)

	CreatedAt time.Time
	UpdatedAt time.Time

	DeletedAt gorm.DeletedAt
}

func FindItems

func FindItems(ctx context.Context, tx *gorm.DB, itemIDs []utils.UInt64) ([]Item, error)

func FindItemsOfTag

func FindItemsOfTag(ctx context.Context, tx *gorm.DB, userID utils.UInt64, tag string, pager *utils.Pager) ([]Item, error)

FindItemsOfTag returns the items

func GetItemsByID

func GetItemsByID(tx *gorm.DB, itemIDs []utils.UInt64) (items []*Item, err error)

func GetItemsOfBook

func GetItemsOfBook(tx *gorm.DB, bookID utils.UInt64, offset, limit int) (items []*Item, err error)

GetItemsOfBook 获取某个 book 的 items

func (*Item) BeforeCreate

func (i *Item) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 钩子

func (*Item) BeforeDelete

func (i *Item) BeforeDelete(tx *gorm.DB) (err error)

BeforeDelete is a GORM hook that is called before deleting an item.

func (*Item) TableName

func (i *Item) TableName() string

type MemorizationSetting

type MemorizationSetting struct {
	// 复习时间的配置,是一组时间,作为根据复习结算时的熟练度来选择下次复习时间的依据
	ReviewInterval def.RecallIntervalLevel `gorm:"type:string"`

	// 用户挑战偏好
	DifficultyPreference utils.Percentage `gorm:"type:tinyint unsigned"`

	// 倾向的战斗模式,决定了已经在时间内 monster 出场时,新增和复习的出现策略
	QuizMode def.QuizMode `gorm:"size:255"`

	// 倾向的战斗模式,决定了已经在时间内 monster 出场时,进行选择的优先级顺序
	PriorityMode def.PriorityMode `gorm:"size:255"`
}

ProfileMemorizationSetting 定义了用户记忆设置的模型

type MonsterSource

type MonsterSource uint8
const (
	MonsterSourceItem MonsterSource = 1
	MonsterSourceBook MonsterSource = 2
)

func (MonsterSource) String

func (ms MonsterSource) String() string

type Profile

type Profile struct {
	ID utils.UInt64 `gorm:"primaryKey;autoIncrement:false"`

	Nickname  string `gorm:"nickname,size:255"`
	Email     string `gorm:"email,size:255;not null;unique"`
	AvatarURL string `gorm:"avatar_url,size:255"`
	Bio       string `gorm:"bio,type:text"`

	CreatedAt time.Time
	UpdatedAt time.Time

	DeletedAt gorm.DeletedAt `gorm:"index"`
	// contains filtered or unexported fields
}

Profile 定义了用户个人信息的模型

func EnsureProfile

func EnsureProfile(ctx context.Context, db *gorm.DB, uid utils.UInt64) (*Profile, error)

EnsureProfile 从数据库中加载用户个人信息

func FindProfile

func FindProfile(ctx context.Context, db *gorm.DB, uid utils.UInt64) (*Profile, error)

FindProfile 读取个人信息

func (*Profile) BeforeCreate

func (p *Profile) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate 钩子

func (*Profile) BeforeUpdate

func (p *Profile) BeforeUpdate(tx *gorm.DB) (err error)

BeforeUpdate 钩子

func (*Profile) CreateProfile

func (p *Profile) CreateProfile(db *gorm.DB) error

CreateProfile 创建新的用户资料

func (*Profile) EnsureLoadProfilePoints

func (p *Profile) EnsureLoadProfilePoints(db *gorm.DB) (*ProfilePoints, error)

EnsureLoadProfilePoints 从数据库中"懒加载"用户高级设置

func (*Profile) EnsureLoadProfileSettingsAdvance

func (p *Profile) EnsureLoadProfileSettingsAdvance(db *gorm.DB) (*ProfileAdvanceSetting, error)

EnsureLoadProfileSettingsAdvance 从数据库中"懒加载"用户高级设置

func (*Profile) EnsureSettingsMemorization

func (p *Profile) EnsureSettingsMemorization(db *gorm.DB) (*ProfileMemorizationSetting, error)

EnsureSettingsMemorization 从数据库中"懒加载"用户记忆设置

func (*Profile) GetPoints

func (p *Profile) GetPoints() *ProfilePoints

GetPoints 检索用户点数

func (*Profile) GetSettingsAdvance

func (p *Profile) GetSettingsAdvance() *ProfileAdvanceSetting

GetSettingsAdvance 检索用户高级设置

func (*Profile) GetSettingsMemorization

func (p *Profile) GetSettingsMemorization() *ProfileMemorizationSetting

GetSettingsMemorization 检索用户记忆设置

func (*Profile) GetSettingsMemorizationOrDefault

func (p *Profile) GetSettingsMemorizationOrDefault(ctx context.Context, tx *gorm.DB) (*ProfileMemorizationSetting, error)

func (*Profile) UpdateProfile

func (p *Profile) UpdateProfile(db *gorm.DB) error

UpdateProfile 更新现有用户资料

func (*Profile) UpdateSettingsAdvance

func (p *Profile) UpdateSettingsAdvance(db *gorm.DB, updater *ProfileAdvanceSetting) error

UpdateSettingsAdvance 使用保存逻辑更新用户高级设置

func (*Profile) UpdateSettingsMemorization

func (p *Profile) UpdateSettingsMemorization(db *gorm.DB, updater *ProfileMemorizationSetting) error

UpdateSettingsMemorization 使用保存逻辑更新用户记忆设置

type ProfileAdvanceSetting

type ProfileAdvanceSetting struct {
	ID utils.UInt64 `gorm:"primaryKey;autoIncrement:false"`

	Theme              string `gorm:"theme,size:255;default:'light'"`
	Language           string `gorm:"language,size:255;default:'en'"`
	EmailNotifications bool   `gorm:"email_notifications,default:true"`
	PushNotifications  bool   `gorm:"push_notifications,default:true"`
}

ProfileAdvanceSetting 定义了用户高级设置的模型

type ProfileMemorizationSetting

type ProfileMemorizationSetting struct {
	ID utils.UInt64 `gorm:"primaryKey;autoIncrement:false"`
	MemorizationSetting
}

ProfileMemorizationSetting

在 User 的 ProfileMemorizationSetting 中,可以设置默认的 QuizMode 和 Priority Dungeon 创建时,会从 User 的 ProfileMemorizationSetting 中获取默认的 QuizMode 和 Priority 并独立于 User 的设置进行修改,后续 User 的设置不会覆盖 Dungeon 的设置 可以在 Dungeon 的 MemorizationSetting 中主动选择从 User 的 ProfileMemorizationSetting 中同步

type ProfilePoints

type ProfilePoints struct {
	ID        utils.UInt64   `gorm:"primaryKey;autoIncrement:false"` // 与用户ID一致
	Cash      utils.UInt64   `gorm:"default:0"`                      // 现金
	Gem       utils.UInt64   `gorm:"default:0"`                      // 宝石
	VipScore  utils.UInt64   `gorm:"default:0"`                      // VIP 积分
	CreatedAt time.Time      // 记录的创建时间
	UpdatedAt time.Time      // 记录的更新时间
	DeletedAt gorm.DeletedAt `gorm:"index"` // 记录的删除时间
}

ProfilePoints 定义了用户积分信息的模型

func EnsureLoadProfilePoints

func EnsureLoadProfilePoints(db *gorm.DB, userID utils.UInt64) (*ProfilePoints, error)

EnsureLoadProfilePoints 从数据库中加载用户积分信息

func (ProfilePoints) TableName

func (ProfilePoints) TableName() string

TableName 自定义表名

type TModel

type TModel struct {
	*tags.TagService[EntityType]
}

func TagModel

func TagModel() *TModel

type Tag

type Tag struct {
	UserID     utils.UInt64 `gorm:"primaryKey"`
	Tag        string       `gorm:"primaryKey"`
	EntityID   utils.UInt64 `gorm:"primaryKey"`
	EntityType EntityType   `gorm:"not null"`
	CreatedAt  time.Time
	UpdatedAt  time.Time
	DeletedAt  gorm.DeletedAt
}

func (Tag) TableName

func (Tag) TableName() string

type TagRepo

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

func (TagRepo) AddTags

func (t TagRepo) AddTags(ctx context.Context, userID utils.UInt64, entityID utils.UInt64, entityType EntityType, tags ...string) error

func (TagRepo) GetEntitiesByTag

func (t TagRepo) GetEntitiesByTag(ctx context.Context, userID utils.UInt64, tag string, entityType EntityType) ([]utils.UInt64, error)

func (TagRepo) GetTag

func (t TagRepo) GetTag(ctx context.Context, tag string) (*tags.Tag[EntityType], error)

func (TagRepo) GetTagsByEntity

func (t TagRepo) GetTagsByEntity(ctx context.Context, entityID utils.UInt64) ([]string, error)

func (TagRepo) GetTagsByUser

func (t TagRepo) GetTagsByUser(ctx context.Context, userID utils.UInt64) ([]string, error)

func (TagRepo) GetUsersByTag

func (t TagRepo) GetUsersByTag(ctx context.Context, tag string) ([]utils.UInt64, error)

func (TagRepo) RemoveTags

func (t TagRepo) RemoveTags(ctx context.Context, userID utils.UInt64, entityID utils.UInt64, tags ...string) error

func (TagRepo) RenameTag

func (t TagRepo) RenameTag(ctx context.Context, userID utils.UInt64, oldTag, newTag string) error

type UserMonster

type UserMonster struct {
	UserID utils.UInt64
	ItemID utils.UInt64

	Familiarity utils.Percentage `gorm:"default:0"` // 熟练度,范围为0-100,默认值为0
	// contains filtered or unexported fields
}

UserMonster - Item 对特定用户的属性

Jump to

Keyboard shortcuts

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