Assocation with Wallet to give daily rewards

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

5
go.mod
View File

@ -5,6 +5,7 @@ go 1.23.2
require ( require (
git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b
git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e
git.solsynth.dev/hypernet/wallet v0.0.0-20250128182545-a311bddc2533
github.com/dgraph-io/ristretto v0.2.0 github.com/dgraph-io/ristretto v0.2.0
github.com/eko/gocache/lib/v4 v4.1.6 github.com/eko/gocache/lib/v4 v4.1.6
github.com/eko/gocache/store/ristretto/v4 v4.2.2 github.com/eko/gocache/store/ristretto/v4 v4.2.2
@ -15,6 +16,8 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1 github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/json-iterator/go v1.1.12 github.com/json-iterator/go v1.1.12
github.com/nats-io/nats.go v1.37.0
github.com/pquerna/otp v1.4.0
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.33.0 github.com/rs/zerolog v1.33.0
github.com/samber/lo v1.47.0 github.com/samber/lo v1.47.0
@ -59,13 +62,11 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nats-io/nats.go v1.37.0 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/pquerna/otp v1.4.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect github.com/prometheus/common v0.37.0 // indirect

2
go.sum
View File

@ -37,6 +37,8 @@ git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b h1:8yB9kMwEMY
git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b/go.mod h1:PhLCv2lsNoscPVJbkWnxwQnJ141lc4RIEkVffrHwl4s= git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b/go.mod h1:PhLCv2lsNoscPVJbkWnxwQnJ141lc4RIEkVffrHwl4s=
git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e h1:DtHhMjgxS/spUt/KEdbRFtaVnepI6Vx8pbHdJaNH1hs= git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e h1:DtHhMjgxS/spUt/KEdbRFtaVnepI6Vx8pbHdJaNH1hs=
git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e/go.mod h1:XHTqFU/vBe4JiuAjl87GUcL8+w/IizSNoqH6n3WkQFc= git.solsynth.dev/hypernet/pusher v0.0.0-20241026153052-cd2c326efa4e/go.mod h1:XHTqFU/vBe4JiuAjl87GUcL8+w/IizSNoqH6n3WkQFc=
git.solsynth.dev/hypernet/wallet v0.0.0-20250128182545-a311bddc2533 h1:KH8Hk6S2VZijmNiGLAKXrwmz1XBBLtD/e7nEa68AxBk=
git.solsynth.dev/hypernet/wallet v0.0.0-20250128182545-a311bddc2533/go.mod h1:WgS+wpp2TYU1ZFUhET9eEw9AMS6TRowQ41yto0FnZZY=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

View File

@ -5,8 +5,9 @@ import "gorm.io/datatypes"
type CheckInRecord struct { type CheckInRecord struct {
BaseModel BaseModel
ResultTier int `json:"result_tier"` ResultTier int `json:"result_tier"`
ResultExperience int `json:"result_experience"` ResultExperience int `json:"result_experience"`
ResultCoin float64 `json:"result_coin"`
// The result modifiers are some random tips that will show up in the client; // 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. // 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 package services
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"gorm.io/gorm"
"math/rand" "math/rand"
"time" "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 { func CheckCanCheckIn(user models.Account) error {
@ -36,12 +42,25 @@ func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
const CheckInResultModifiersLength = 4 const CheckInResultModifiersLength = 4
func CheckIn(user models.Account) (models.CheckInRecord, error) { 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) tier := rand.Intn(5)
expMax := 100 * (tier + 1) expMax := 100 * (tier + 1)
expMin := 100 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, ResultTier: tier,
ResultExperience: rand.Intn(expMax+1-expMin) + expMin, ResultExperience: exp,
ResultCoin: float64(int(rawCoins*100)) / 100,
AccountID: user.ID, AccountID: user.ID,
} }
@ -51,10 +70,6 @@ func CheckIn(user models.Account) (models.CheckInRecord, error) {
} }
record.ResultModifiers = modifiers record.ResultModifiers = modifiers
if err := CheckCanCheckIn(user); err != nil {
return record, fmt.Errorf("today already signed")
}
tx := database.C.Begin() tx := database.C.Begin()
var profile models.AccountProfile 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 { if err := tx.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do check in: %v", err) return record, fmt.Errorf("unable do check in: %v", err)
} }