119 lines
3.0 KiB
119 lines
3.0 KiB
package models
|
|
|
|
import (
|
|
"github.com/gisle/stufflog/internal/generate"
|
|
"github.com/gisle/stufflog/slerrors"
|
|
)
|
|
|
|
// An Activity is a general hobby or group of related related sub-activities.
|
|
type Activity struct {
|
|
ID string `json:"id"`
|
|
UserID string `json:"userId"`
|
|
Name string `json:"name"`
|
|
Icon string `json:"icon"`
|
|
DailyBonus int `json:"dailyBonus"`
|
|
|
|
SubActivities []SubActivity `json:"subActivities"`
|
|
}
|
|
|
|
func (activity *Activity) GenerateIDs() {
|
|
activity.ID = generate.ID("A", 12)
|
|
for i := range activity.SubActivities {
|
|
activity.SubActivities[i].ID = generate.ID("AS", 16)
|
|
}
|
|
}
|
|
|
|
func (activity *Activity) SubActivity(id string) *SubActivity {
|
|
for i := range activity.SubActivities {
|
|
if activity.SubActivities[i].ID == id {
|
|
return &activity.SubActivities[i]
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (activity *Activity) ApplyUpdate(update ActivityUpdate) (changed bool, err error) {
|
|
if update.SetName != nil {
|
|
activity.Name = *update.SetName
|
|
changed = true
|
|
}
|
|
if update.SetIcon != nil {
|
|
activity.Icon = *update.SetIcon
|
|
changed = true
|
|
}
|
|
if update.SetDailyBonus != nil {
|
|
activity.DailyBonus = *update.SetDailyBonus
|
|
changed = true
|
|
}
|
|
|
|
if update.AddSub != nil {
|
|
sub := *update.AddSub
|
|
sub.ID = generate.ID("SA", 16)
|
|
|
|
activity.SubActivities = append(activity.SubActivities, sub)
|
|
changed = true
|
|
}
|
|
if update.EditSub != nil {
|
|
sub := activity.SubActivity(update.EditSub.ID)
|
|
if sub == nil {
|
|
err = slerrors.NotFound("Sub-activity you're trying to edit")
|
|
return
|
|
}
|
|
|
|
*sub = *update.EditSub
|
|
changed = true
|
|
}
|
|
if update.RemoveSub != nil {
|
|
found := false
|
|
for i, sub := range activity.SubActivities {
|
|
if sub.ID == *update.RemoveSub {
|
|
activity.SubActivities = append(activity.SubActivities[:i], activity.SubActivities[i+1:]...)
|
|
|
|
found = true
|
|
changed = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
err = slerrors.NotFound("Sub-activity you're trying to remove")
|
|
return
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// A SubActivity is a part of the activity, i.e. tutorial, writing, practice. This is what determines
|
|
// how many points should be awarded. The baseline multiplier should amount to 1000 points per hour,
|
|
// while activities that take longer (i.e. a published piece of writing vs writing for your eyes only).
|
|
type SubActivity struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
UnitName string `json:"unitName"`
|
|
Value float64 `json:"value"`
|
|
}
|
|
|
|
// ActivityUpdate describes a change to an activity
|
|
type ActivityUpdate struct {
|
|
AddSub *SubActivity `json:"addSub"`
|
|
EditSub *SubActivity `json:"editSub"`
|
|
RemoveSub *string `json:"removeSub"`
|
|
SetName *string `json:"setName"`
|
|
SetIcon *string `json:"setIcon"`
|
|
SetDailyBonus *int `json:"setDailyBonus"`
|
|
}
|
|
|
|
type ActivitiesByName []*Activity
|
|
|
|
func (activities ActivitiesByName) Len() int {
|
|
return len(activities)
|
|
}
|
|
|
|
func (activities ActivitiesByName) Less(i, j int) bool {
|
|
return activities[i].Name < activities[j].Name
|
|
}
|
|
|
|
func (activities ActivitiesByName) Swap(i, j int) {
|
|
activities[i], activities[j] = activities[j], activities[i]
|
|
}
|