✨ Assocation with Wallet to give daily rewards
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user