Result modifiers in check-in

This commit is contained in:
LittleSheep 2024-11-27 22:03:09 +08:00
parent c16019341f
commit 659548a544
3 changed files with 39 additions and 18 deletions

13
.idea/workspace.xml generated
View File

@ -4,13 +4,10 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":mute: Remove authenticate result logging">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":truck: Rename daily-sign to check-in">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/authkit/models/sign_record.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/authkit/models/check_in.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/database/migrator.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/database/migrator.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/sign_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/check_in_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/sign.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/check_in.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/authkit/models/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/authkit/models/check_in.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/check_in.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -163,7 +160,6 @@
</component>
<component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":rewind: Revert &quot;:recycle: Move models.Account to sec.UserInfo&quot; for a better solution&#10;&#10;This reverts commit 8fbb7960" />
<MESSAGE value=":truck: Update package name from Hypdrogen to Hypernet" />
<MESSAGE value=":sparkles: Support users related rpc calls" />
<MESSAGE value=":technologist: Improve DX by extending authkit" />
@ -188,7 +184,8 @@
<MESSAGE value=":loud_sound: Add verbose permission logging" />
<MESSAGE value=":bug: Fix group permission didn't extend properly" />
<MESSAGE value=":mute: Remove authenticate result logging" />
<option name="LAST_COMMIT_MESSAGE" value=":mute: Remove authenticate result logging" />
<MESSAGE value=":truck: Rename daily-sign to check-in" />
<option name="LAST_COMMIT_MESSAGE" value=":truck: Rename daily-sign to check-in" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component>
<component name="VgoProject">

View File

@ -1,10 +1,19 @@
package models
import "gorm.io/datatypes"
type CheckInRecord struct {
BaseModel
ResultTier int `json:"result_tier"`
ResultExperience int `json:"result_experience"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
ResultTier int `json:"result_tier"`
ResultExperience int `json:"result_experience"`
// 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.
// For now, this modifier slice will contain four random numbers from 0 to 1024.
// Client should mod this modifier by the length of total available tips.
ResultModifiers datatypes.JSONSlice[int] `json:"result_modifiers"`
Account Account `json:"account"`
AccountID uint `json:"account_id"`
}

View File

@ -19,9 +19,9 @@ func CheckCanCheckIn(user models.Account) error {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return fmt.Errorf("unable check daliy sign record: %v", err)
return fmt.Errorf("unable get check in record: %v", err)
}
return fmt.Errorf("daliy sign record exists")
return fmt.Errorf("today's check in record exists")
}
func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
@ -29,11 +29,13 @@ func GetTodayCheckIn(user models.Account) (models.CheckInRecord, error) {
var record models.CheckInRecord
if err := database.C.Where("account_id = ? AND created_at::date = ?", user.ID, probe).First(&record).Error; err != nil {
return record, fmt.Errorf("unable get daliy sign record: %v", err)
return record, fmt.Errorf("unable get check in record: %v", err)
}
return record, nil
}
const CheckInResultModifiersLength = 4
func CheckIn(user models.Account) (models.CheckInRecord, error) {
tier := rand.Intn(5)
record := models.CheckInRecord{
@ -42,21 +44,34 @@ func CheckIn(user models.Account) (models.CheckInRecord, error) {
AccountID: user.ID,
}
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
if err := CheckCanCheckIn(user); err != nil {
return record, fmt.Errorf("today already signed")
}
tx := database.C.Begin()
var profile models.AccountProfile
if err := database.C.Where("account_id = ?", user.ID).First(&profile).Error; err != nil {
return record, fmt.Errorf("unable get account profile: %v", err)
} else {
profile.Experience += uint64(record.ResultExperience)
database.C.Save(&profile)
if err := tx.Save(&profile).Error; err != nil {
tx.Rollback()
return record, fmt.Errorf("unable update account profile: %v", err)
}
}
if err := database.C.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do daliy sign: %v", err)
if err := tx.Save(&record).Error; err != nil {
return record, fmt.Errorf("unable do check in: %v", err)
}
tx.Commit()
return record, nil
}