summaryrefslogtreecommitdiff
path: root/internal/models/models.go
blob: bd38eaf924115dedb218231f3769a7519a758cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package models

import "time"

type ActionType string

const (
	ActionTypePlus  ActionType = "ActionTypePlus"
	ActionTypeMinus ActionType = "ActionTypeMinus"
)

type (
	UserScore struct {
		ID        uint32 `db:"id"`
		Username  string `db:"username"`
		Actions   []Action
		BurnTime  time.Time `db:"burn_time"`
		Score     int8      `db:"score"`
		CreatedAt time.Time `db:"created_at"`
	}
	Action struct {
		ID         uint32     `db:"id"`
		Name       string     `db:"name"`
		Magnitude  uint8      `db:"magnitude"`
		Repeatable bool       `db:"repeatable"`
		Type       ActionType `db:"type"`
		Done       bool       `db:"done"`
		Username   string     `db:"username"`
		CreatedAt  time.Time  `db:"created_at"`
	}
)

func (us *UserScore) UpdateScore(act *Action) {
	switch act.Type {
	case ActionTypePlus:
		us.Score += int8(act.Magnitude)
		if !act.Repeatable {
			act.Done = true
		}
	case ActionTypeMinus:
		us.Score -= int8(act.Magnitude)
	}
}