diff --git a/go.mod b/go.mod index 541e46e..67a3f9b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.2 require ( git.solsynth.dev/hypernet/nexus v0.0.0-20241123050605-25ab1371739b 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/eko/gocache/lib/v4 v4.1.6 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/google/uuid v1.6.0 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/rs/zerolog v1.33.0 github.com/samber/lo v1.47.0 @@ -59,13 +62,11 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // 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/nuid v1.0.1 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // 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_model v0.3.0 // indirect github.com/prometheus/common v0.37.0 // indirect diff --git a/go.sum b/go.sum index bc1c3c8..d5f2eb0 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/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= diff --git a/pkg/authkit/models/check_in.go b/pkg/authkit/models/check_in.go index 6484a7f..8e7e325 100644 --- a/pkg/authkit/models/check_in.go +++ b/pkg/authkit/models/check_in.go @@ -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. diff --git a/pkg/internal/services/check_in.go b/pkg/internal/services/check_in.go index e6f81af..8250019 100644 --- a/pkg/internal/services/check_in.go +++ b/pkg/internal/services/check_in.go @@ -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) }