Assocation with Wallet to give daily rewards

This commit is contained in:
2025-01-29 02:37:57 +08:00
parent 867a556204
commit 09010d5867
4 changed files with 50 additions and 13 deletions

View File

@ -5,8 +5,9 @@ import "gorm.io/datatypes"
type CheckInRecord struct {
BaseModel
ResultTier int `json:"result_tier"`
ResultExperience int `json:"result_experience"`
ResultTier int `json:"result_tier"`
ResultExperience int `json:"result_experience"`
ResultCoin float64 `json:"result_coin"`
// The result modifiers are some random tips that will show up in the client;
// This field is to use to make sure the tips will be the same when the client is reloaded.

View File

@ -1,13 +1,19 @@
package services
import (
"context"
"errors"
"fmt"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"gorm.io/gorm"
"math/rand"
"time"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
"git.solsynth.dev/hypernet/wallet/pkg/proto"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"gorm.io/gorm"
)
func CheckCanCheckIn(user models.Account) error {
@ -36,12 +42,25 @@ func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
const CheckInResultModifiersLength = 4
func CheckIn(user models.Account) (models.CheckInRecord, error) {
var record models.CheckInRecord
if err := CheckCanCheckIn(user); err != nil {
return record, fmt.Errorf("today already signed")
}
tier := rand.Intn(5)
expMax := 100 * (tier + 1)
expMin := 100
record := models.CheckInRecord{
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{
ResultTier: tier,
ResultExperience: rand.Intn(expMax+1-expMin) + expMin,
ResultExperience: exp,
ResultCoin: float64(int(rawCoins*100)) / 100,
AccountID: user.ID,
}
@ -51,10 +70,6 @@ func CheckIn(user models.Account) (models.CheckInRecord, error) {
}
record.ResultModifiers = modifiers
if err := CheckCanCheckIn(user); err != nil {
return record, fmt.Errorf("today already signed")
}
tx := database.C.Begin()
var profile models.AccountProfile
@ -68,6 +83,24 @@ func CheckIn(user models.Account) (models.CheckInRecord, error) {
}
}
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
}
if err := tx.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do check in: %v", err)
}