Skip to content

Commit

Permalink
feat: ✨ add get diving fish player record, add init db optional param.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aimerny committed Jun 3, 2024
1 parent df5563c commit c5769a2
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
conf.json
session_id
dist/
main
main
.idea/
File renamed without changes.
46 changes: 46 additions & 0 deletions app/internal/client/onge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

import (
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"github/aimerny/elix/app/internal/dto"
"io"
"net/http"
)

const (
DivingFishPlayerScoreQuery = "https://www.diving-fish.com/api/maimaidxprober/player/records"
)

func Get(req *http.Request) ([]byte, error) {
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
log.WithError(err).Error("do get failed! err")
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.WithError(err).Error("action read body failed!")
return nil, err
}
return data, nil
}

func QueryRecord(importToken string) (*dto.DivingPlayerRecordsResp, error) {
req, err := http.NewRequest("GET", DivingFishPlayerScoreQuery, nil)
if err != nil {
log.Errorf("Http request construct failed! err: %e", err)
return nil, err
}
req.Header.Add("Import-Token", importToken)
body, err := Get(req)
resp := &dto.DivingPlayerRecordsResp{}
err = jsoniter.Unmarshal(body, resp)
if err != nil {
log.WithError(err).WithField("resp", string(body)).Error("unmarshal record failed")
return nil, err
}
return resp, nil
}
5 changes: 3 additions & 2 deletions app/internal/common/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package common
import "flag"

var (
ConfigPathParam *string
ConfigPathParam *string
UpgradeOngeDatabase *bool
)

func InitFlag() {
ConfigPathParam = flag.String("conf", "conf.json", "config file path of elix")

UpgradeOngeDatabase = flag.Bool("db-update", false, "update database of onge service")
flag.Parse()
}
25 changes: 25 additions & 0 deletions app/internal/dto/diving_fish_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,28 @@ type DivingMusicBasicInfo struct {
From string `json:"from"`
IsNew bool `json:"is_new"`
}

type DivingPlayerRecordsResp struct {
Username string `json:"username,omitempty"`
AdditionalRating int `json:"additional_rating,omitempty"`
Nickname string `json:"nickname,omitempty"`
Plate string `json:"plate,omitempty"`
Rating int `json:"rating"`
Records []DivingPlayerRecordInfo `json:"records"`
}

type DivingPlayerRecordInfo struct {
Achievements float64 `json:"achievements,omitempty"`
Ds float64 `json:"ds,omitempty"`
DxScore int `json:"dx_score,omitempty"`
Fc string `json:"fc"`
Fs string `json:"fs"`
Level string `json:"level"`
LevelIndex int `json:"level_index"`
LevelLabel string `json:"level_label"`
Ra int `json:"ra"`
Rate string `json:"rate"`
SongID int `json:"song_id"`
Title string `json:"title"`
Type string `json:"type"`
}
6 changes: 6 additions & 0 deletions app/internal/dto/onge.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ type ChuniChartInfo struct {
Charter string
Combo int
}

type OngeUserInfo struct {
gorm.Model
KookId string
DivingFishToken string
}
15 changes: 15 additions & 0 deletions app/internal/service/onge/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
log "github.com/sirupsen/logrus"
"github/aimerny/elix/app/internal/client"
"github/aimerny/elix/app/internal/common"
"github/aimerny/elix/app/internal/dto"
"gorm.io/gorm"
)

Expand All @@ -31,4 +32,18 @@ func InitOngeService(dbConf *common.DatasourceConf) {
OngeStatus = true
}
log.Infof("init onge service finished")
if *common.UpgradeOngeDatabase {
log.Infof("onge database upgrading ...")
err := OngeServiceDS.AutoMigrate(
&dto.MaiMusicInfo{},
&dto.MaiChartInfo{},
&dto.ChuniMusicInfo{},
&dto.ChuniChartInfo{},
&dto.OngeUserInfo{},
)
if err != nil {
log.WithError(err).Panicf("onge database upgrade failed")
}
log.Infof("onge database upgrade success")
}
}

0 comments on commit c5769a2

Please sign in to comment.