database

package
v0.0.0-...-3dc37b2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB *gorm.DB

Functions

func Connect

func Connect()

func InitializeNewUser

func InitializeNewUser(userID string)

func PopulateDatabase

func PopulateDatabase()

Types

type Daily

type Daily struct {
	Model
	LastDailyAt        time.Time
	ConsecutiveStreaks uint16
	Streak             uint16
}

func (*Daily) AfterCreate

func (d *Daily) AfterCreate(tx *gorm.DB) error

func (*Daily) CanDoDaily

func (d *Daily) CanDoDaily() bool

CanDoDaily - Checks if the user can do their daily again Returns true if the user can do their daily and false if they cant

func (*Daily) CanDoDailyAt

func (d *Daily) CanDoDailyAt() string

Returns the time the user can do their daily next as a formatted discord string https://hammertime.cyou/

func (*Daily) DoDaily

func (d *Daily) DoDaily(user *User) (bool, string, string, string, string, string)

Does the daily Returns If the daily could be executed The money earned as a pretty string Streak reward value Percentage of streak completed Title for the message Footer for the message

func (*Daily) GetDailyInfo

func (d *Daily) GetDailyInfo(u *User)

Queries the database for the daily data with the given user object.

func (*Daily) Save

func (d *Daily) Save()

Saves the data to the database

func (Daily) TableName

func (Daily) TableName() string

type Debug

type Debug struct {
	Model
	DailyCount uint64
	WorkCount  uint64
}

func (Debug) TableName

func (Debug) TableName() string

type Farm

type Farm struct {
	Model
	Plots                   []*FarmPlot
	OwnedPlots              uint8
	LastWateredAt           time.Time // Last time the user watered the farm plots
	HighestPlantedCropIndex uint8

	PlotsChanged    bool `gorm:"-"` // Ignored by the database
	HarvestEarnings int  `gorm:"-"` // If 0 then no earnings
}

func (*Farm) BeforeCreate

func (f *Farm) BeforeCreate(tx *gorm.DB) error

Only triggers on DB.Create(...)

func (*Farm) CalcFarmPlotPrice

func (f *Farm) CalcFarmPlotPrice() int

Returns the cost of what buying a new plot would cost for the user

func (*Farm) CanHarvest

func (f *Farm) CanHarvest() bool

Returns true if the user can harvest any of their crops

func (*Farm) CanWater

func (f *Farm) CanWater() bool

Returns true if the user can water their crops

func (*Farm) CanWaterAt

func (f *Farm) CanWaterAt() string

Returns the time the user can water their crops as a formatted discord string https://hammertime.cyou/

func (*Farm) CreateEmbedDescription

func (f *Farm) CreateEmbedDescription() string

func (*Farm) CreateEmbedFields

func (f *Farm) CreateEmbedFields() []*discordgo.MessageEmbedField

func (*Farm) CreateFarmOverview

func (f *Farm) CreateFarmOverview(msg *discordgo.MessageSend, m *discordgo.MessageCreate, user *User)

CreateFarmOverview creates the message that will be sent to the user

func (*Farm) CropsPerishedCheck

func (f *Farm) CropsPerishedCheck() []string

Will return the amount of crops that have perished from plots Will also remove perished crop plots from the database Remember to have call GetFarmPlots() before calling this function

func (*Farm) DeletePlot

func (f *Farm) DeletePlot(plot *FarmPlot)

func (*Farm) GetUnusedPlots

func (f *Farm) GetUnusedPlots() int

Rember to run GetFarmPlots() before running this function

func (*Farm) HarvestPlots

func (f *Farm) HarvestPlots() []harvestResult

Returns an array containing the crop object that was harvested Money earned is saved in f.HarvestEarnings. Remember to add it to the user's balance Run QueryFarmPlots() before running this function

func (*Farm) HasFreePlot

func (f *Farm) HasFreePlot() bool

Returns true if the user has a free (unused) farm plot

func (*Farm) HasMaxAmountOfPlots

func (f *Farm) HasMaxAmountOfPlots() bool

func (*Farm) HasPlantedPlots

func (f *Farm) HasPlantedPlots() bool

Returns true if anything is planted on the farm Run QueryFarmPlots() first

func (*Farm) HasUserUnlocked

func (f *Farm) HasUserUnlocked(fc *FarmCrop) bool

func (*Farm) MissedWaterDeadline

func (f *Farm) MissedWaterDeadline() bool

func (*Farm) Peek

func (f *Farm) Peek() bool

Peek looks at the farm and updates it to reflect the current state Always run before doing anything with the farm Checks if crops have perished Returns true if any crop perished

func (*Farm) QueryFarmPlots

func (f *Farm) QueryFarmPlots()

Updates the object to contain all the farmplots

func (*Farm) QueryUserFarmData

func (f *Farm) QueryUserFarmData(u *User)

Queries the database for the farm data with the given user object. Needs to be called first

func (*Farm) ResetLastWatered

func (f *Farm) ResetLastWatered()

ResetLastWatered updates last watered to so that the user can water their plats again

func (*Farm) Save

func (f *Farm) Save()

Saves the data to the database

func (*Farm) SuccessfulHarvest

func (f *Farm) SuccessfulHarvest() bool

func (Farm) TableName

func (Farm) TableName() string

func (*Farm) UpdateInteractionOverview

func (f *Farm) UpdateInteractionOverview(discordUser *discordgo.User, me *discordgo.MessageEdit)

func (*Farm) WaterPlots

func (f *Farm) WaterPlots()

Functions waters every single plot Meaning it will update the plantedAt time Run QueryFarmPlots() before running this function

type FarmCrop

type FarmCrop struct {
	Model
	Name           string
	Emoji          string
	DurationToGrow time.Duration
	HarvestReward  int
}

func (*FarmCrop) GetAllCrops

func (fc *FarmCrop) GetAllCrops() []FarmCrop

func (*FarmCrop) GetCropByName

func (fc *FarmCrop) GetCropByName(name string) bool

func (*FarmCrop) GetDuration

func (fc *FarmCrop) GetDuration() string

Outputs the duration in a pretty format Example: 10 days; 1 day; 16 hours; 1 hour; 20 mins Does not handle days with hours, or hours with minutes Does not handle seconds

func (FarmCrop) TableName

func (FarmCrop) TableName() string

type FarmPlot

type FarmPlot struct {
	Model
	FarmID    uint `gorm:"index"`
	Farm      Farm `gorm:"references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` // The farm this plot belongs to
	CropID    int
	Crop      FarmCrop  `gorm:"references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` // The planted crop
	PlantedAt time.Time // When the user planted the crop
	Perished  bool      // Perished crops wont yeild any money
}

func (*FarmPlot) AfterCreate

func (fp *FarmPlot) AfterCreate(tx *gorm.DB) error

Only triggers on DB.Create(...)

func (*FarmPlot) BeforeCreate

func (fp *FarmPlot) BeforeCreate(tx *gorm.DB) error

Only triggers on DB.Create(...)

func (*FarmPlot) DeleteFromDB

func (fp *FarmPlot) DeleteFromDB()

Removes the entry from the database

func (*FarmPlot) HarvestableAt

func (fp *FarmPlot) HarvestableAt() string

Returns a discord formatted string showing when the crop will be harvestable

func (*FarmPlot) HasFullyGrown

func (fp *FarmPlot) HasFullyGrown() bool

Check if the crop has fully grown by comparing the duration to grow with the planted at time and the current time Call QueryCropInfo() first

func (*FarmPlot) HasPerished

func (fp *FarmPlot) HasPerished() bool

func (*FarmPlot) Perish

func (fp *FarmPlot) Perish()

Will mark the crop as perished and save it to the database

func (*FarmPlot) QueryCropInfo

func (fp *FarmPlot) QueryCropInfo()

func (*FarmPlot) Save

func (fp *FarmPlot) Save()

Saves the data to the database

func (FarmPlot) TableName

func (FarmPlot) TableName() string

func (*FarmPlot) Water

func (fp *FarmPlot) Water()

Wateres the plot by updating the PlantedAt time

type Model

type Model struct {
	ID        uint `gorm:"primarykey"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type User

type User struct {
	Model
	DiscordID        string `gorm:"uniqueIndex"`
	Money            uint64
	LifetimeEarnings uint64
	Work             Work  `gorm:"foreignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
	Daily            Daily `gorm:"foreignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
	Farm             Farm  `gorm:"foreignKey:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

func (*User) AddMoney

func (u *User) AddMoney(amount uint64)

func (*User) AfterCreate

func (u *User) AfterCreate(tx *gorm.DB) error

func (*User) CanAfford

func (u *User) CanAfford(number uint64) bool

func (*User) CreateProfileComponents

func (u *User) CreateProfileComponents(work *Work, daily *Daily) []discordgo.MessageComponent

func (*User) CreateProfileEmbeds

func (u *User) CreateProfileEmbeds(du *discordgo.User, work *Work, daily *Daily, embeds *[]*discordgo.MessageEmbed)

func (*User) DeductMoney

func (u *User) DeductMoney(amount uint64)

func (*User) DoesUserExist

func (u *User) DoesUserExist(discordID string) bool

Returns true if a user with that discord ID exists in the database

func (*User) PrettyPrintLifetimeEarnings

func (u *User) PrettyPrintLifetimeEarnings() string

func (*User) PrettyPrintMoney

func (u *User) PrettyPrintMoney() string

func (*User) QueryUserByDiscordID

func (u *User) QueryUserByDiscordID(discordID string)

Queries the database for the user with the given discord ID. The object which calls the method will be updated with the user's data

func (*User) Save

func (u *User) Save()

Saves the data to the database

func (User) TableName

func (User) TableName() string

type Work

type Work struct {
	Model
	LastWorkedAt       time.Time
	ConsecutiveStreaks uint16
	Streak             uint16
	Tools              uint8
}

func (*Work) AfterCreate

func (w *Work) AfterCreate(tx *gorm.DB) error

func (*Work) CalcBuyToolPrice

func (w *Work) CalcBuyToolPrice() (int, string)

Returns how much it would cost the user to buy a new tool Returns the amount as an int and formatted string

func (*Work) CanDoWork

func (w *Work) CanDoWork() bool

CanDoWork - Checks if the user can work again Returns true if the user can work and false if they cant

func (*Work) CanDoWorkAt

func (w *Work) CanDoWorkAt() string

Returns the time the user can work next as a formatted discord string https://hammertime.cyou/

func (*Work) CreateMessageComponents

func (w *Work) CreateMessageComponents() []discordgo.MessageComponent

func (*Work) GetWorkInfo

func (w *Work) GetWorkInfo(u *User)

Queries the database for the work data with the given user object.

func (*Work) HasHitMaxToolLimit

func (w *Work) HasHitMaxToolLimit() bool

func (*Work) Save

func (w *Work) Save()

Saves the data to the database

func (*Work) StreakPostMsgAction

func (w *Work) StreakPostMsgAction()

Wrap around the streak if the streak length in the config got updated/changed

func (*Work) StreakPreMsgAction

func (w *Work) StreakPreMsgAction()

StreakPreMsgAction - Checks the streak for the work object Resets it down to 0 if the user failed their streak. i.e. Waited too long since the last work If the user can work... Updates the streak for the user i.e. adding one to the counters and ensuring the streak is not over the max streak and updating the time of the last work

func (Work) TableName

func (Work) TableName() string

type YoutubeCache

type YoutubeCache struct {
	Model
	VideoID           string        `gorm:"uniqueIndex"`
	Title             string        `gorm:"not null"`
	Thumbnail         string        `gorm:"not null"`
	ChannelName       string        `gorm:"not null"`
	Duration          time.Duration `gorm:"not null"`
	URLCache          string
	URLCacheExpiresAt time.Time
}

func (*YoutubeCache) Cache

func (c *YoutubeCache) Cache(videoID, title, thumbnail, channelName string, duration time.Duration)

func (*YoutubeCache) Check

func (c *YoutubeCache) Check(videoID string, title, thumbnail, channelName, streamURL *string, duration *time.Duration) bool

Check checks if the videoID exists in the cache Populates the values if the video is found Returns true if it exists

func (*YoutubeCache) Save

func (c *YoutubeCache) Save()

Saves the data to the database

func (YoutubeCache) TableName

func (YoutubeCache) TableName() string

func (*YoutubeCache) UpdateStreamURL

func (c *YoutubeCache) UpdateStreamURL(videoID, streamURL string) error

Jump to

Keyboard shortcuts

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