2024-09-01 16:38:09 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2025-01-29 02:37:57 +08:00
|
|
|
"context"
|
2024-09-01 16:38:09 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2025-01-29 02:37:57 +08:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2024-10-31 20:38:50 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2025-01-29 02:37:57 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/wallet/pkg/proto"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/samber/lo"
|
2024-09-01 16:38:09 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-11-27 21:57:10 +08:00
|
|
|
func CheckCanCheckIn(user models.Account) error {
|
2024-09-02 22:48:06 +08:00
|
|
|
probe := time.Now().Format("2006-01-02")
|
2024-09-01 16:51:13 +08:00
|
|
|
|
2024-11-27 21:57:10 +08:00
|
|
|
var record models.CheckInRecord
|
2024-10-31 00:17:53 +08:00
|
|
|
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
2024-09-01 16:38:09 +08:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-27 22:03:09 +08:00
|
|
|
return fmt.Errorf("unable get check in record: %v", err)
|
2024-09-01 16:38:09 +08:00
|
|
|
}
|
2024-11-27 22:03:09 +08:00
|
|
|
return fmt.Errorf("today's check in record exists")
|
2024-09-01 16:38:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-27 21:57:10 +08:00
|
|
|
func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
|
2024-09-02 22:48:06 +08:00
|
|
|
probe := time.Now().Format("2006-01-02")
|
2024-09-02 20:07:19 +08:00
|
|
|
|
2024-11-27 21:57:10 +08:00
|
|
|
var record models.CheckInRecord
|
2024-10-31 00:17:53 +08:00
|
|
|
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
|
2024-11-27 22:03:09 +08:00
|
|
|
return record, fmt.Errorf("unable get check in record: %v", err)
|
2024-09-02 20:07:19 +08:00
|
|
|
}
|
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
2024-11-27 22:03:09 +08:00
|
|
|
const CheckInResultModifiersLength = 4
|
|
|
|
|
2024-11-27 21:57:10 +08:00
|
|
|
func CheckIn(user models.Account) (models.CheckInRecord, error) {
|
2025-01-29 02:37:57 +08:00
|
|
|
var record models.CheckInRecord
|
|
|
|
if err := CheckCanCheckIn(user); err != nil {
|
|
|
|
return record, fmt.Errorf("today already signed")
|
|
|
|
}
|
|
|
|
|
2024-09-01 16:38:09 +08:00
|
|
|
tier := rand.Intn(5)
|
2025-01-29 02:37:57 +08:00
|
|
|
|
2024-11-30 01:01:54 +08:00
|
|
|
expMax := 100 * (tier + 1)
|
|
|
|
expMin := 100
|
2025-01-29 02:37:57 +08:00
|
|
|
exp := rand.Intn(expMax+1-expMin) + expMin
|
|
|
|
|
|
|
|
coinMax := 10.0 * float64(tier+1)
|
|
|
|
coinMin := 10.0
|
|
|
|
rawCoins := coinMax + rand.Float64()*(coinMax-coinMin)
|
|
|
|
|
|
|
|
record = models.CheckInRecord{
|
2024-09-01 16:38:09 +08:00
|
|
|
ResultTier: tier,
|
2025-01-29 02:37:57 +08:00
|
|
|
ResultExperience: exp,
|
|
|
|
ResultCoin: float64(int(rawCoins*100)) / 100,
|
2024-10-31 00:17:53 +08:00
|
|
|
AccountID: user.ID,
|
2024-09-01 16:38:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-27 22:03:09 +08:00
|
|
|
modifiers := make([]int, CheckInResultModifiersLength)
|
|
|
|
for i := 0; i < CheckInResultModifiersLength; i++ {
|
|
|
|
modifiers[i] = rand.Intn(1025) // from 0 to 1024 as the comment said
|
|
|
|
}
|
|
|
|
record.ResultModifiers = modifiers
|
|
|
|
|
|
|
|
tx := database.C.Begin()
|
|
|
|
|
2024-09-02 20:07:19 +08:00
|
|
|
var profile models.AccountProfile
|
2024-10-31 00:17:53 +08:00
|
|
|
if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
|
2024-09-02 20:07:19 +08:00
|
|
|
return record, fmt.Errorf("unable get account profile: %v", err)
|
|
|
|
} else {
|
|
|
|
profile.Experience += uint64(record.ResultExperience)
|
2024-11-27 22:03:09 +08:00
|
|
|
if err := tx.Save(&profile).Error; err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return record, fmt.Errorf("unable update account profile: %v", err)
|
|
|
|
}
|
2024-09-02 20:07:19 +08:00
|
|
|
}
|
|
|
|
|
2025-01-29 02:37:57 +08:00
|
|
|
conn, err := gap.Nx.GetClientGrpcConn("wa")
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Unable to connect with wallet to send daily rewards")
|
|
|
|
record.ResultCoin = 0
|
|
|
|
}
|
|
|
|
wc := proto.NewPaymentServiceClient(conn)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
|
|
defer cancel()
|
|
|
|
_, err = wc.MakeTransactionWithAccount(ctx, &proto.MakeTransactionWithAccountRequest{
|
|
|
|
PayeeAccountId: lo.ToPtr(uint64(user.ID)),
|
|
|
|
Amount: record.ResultCoin,
|
|
|
|
Remark: "Daily Check-In Rewards",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("Unable to make transaction with account to send daily rewards")
|
|
|
|
record.ResultCoin = 0
|
|
|
|
}
|
|
|
|
|
2024-11-27 22:03:09 +08:00
|
|
|
if err := tx.Save(&record).Error; err != nil {
|
|
|
|
return record, fmt.Errorf("unable do check in: %v", err)
|
2024-09-01 16:38:09 +08:00
|
|
|
}
|
|
|
|
|
2024-11-27 22:03:09 +08:00
|
|
|
tx.Commit()
|
|
|
|
|
2024-09-01 16:38:09 +08:00
|
|
|
return record, nil
|
|
|
|
}
|