diff --git a/.gitignore b/.gitignore index 1cfdd69..4dd2419 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,8 @@ honoka-chan honoka-chan.exe data +temp assets/data.db -assets/unit.sd .vscode diff --git a/handler/webui.go b/handler/webui.go index 255d78d..2813aa2 100644 --- a/handler/webui.go +++ b/handler/webui.go @@ -1,14 +1,24 @@ package handler import ( + "encoding/json" "honoka-chan/model" + "honoka-chan/tools" + "honoka-chan/utils" "net/http" + "path" + "time" "github.com/forgoer/openssl" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" ) +type ErrMsg struct { + Error int `json:"error"` + Msg string `json:"msg"` +} + func WebLogin(ctx *gin.Context) { area := ctx.PostForm("area") user := ctx.PostForm("user") @@ -23,7 +33,8 @@ func WebLogin(ctx *gin.Context) { } userName := " " + area + "-" + user - exists, err := UserEng.Table("users").Where("phone = ? AND password = ?", userName, openssl.Md5ToString(pass)).Exist() + var userId int + exists, err := UserEng.Table("users").Where("phone = ? AND password = ?", userName, openssl.Md5ToString(pass)).Cols("userid").Get(&userId) CheckErr(err) if !exists { ctx.JSON(http.StatusOK, model.Msg{ @@ -38,7 +49,7 @@ func WebLogin(ctx *gin.Context) { session.Options(sessions.Options{ MaxAge: 3600 * 24, }) - session.Set("username", userName) + session.Set("userid", userId) session.Save() ctx.JSON(http.StatusOK, model.Msg{ @@ -59,3 +70,104 @@ func WebLogout(ctx *gin.Context) { ctx.Redirect(http.StatusFound, "/admin/login") } + +func Upload(ctx *gin.Context) { + file, err := ctx.FormFile("file") + CheckErr(err) + + tmpPath := path.Join("./temp", file.Filename) + err = ctx.SaveUploadedFile(file, tmpPath) + CheckErr(err) + + data := model.Data{} + err = json.Unmarshal([]byte(utils.ReadAllText(tmpPath)), &data) + CheckErr(err) + + session := UserEng.NewSession() + defer session.Close() + if err = session.Begin(); err != nil { + session.Rollback() + panic(err) + } + + for _, team := range data.Team { + if team.Cardid == 0 { + continue + } + var unitId, unitExp, unitRarity, unitHp, unitSigned int + exists, err := MainEng.Table("common_unit_m").Join("LEFT", "unit_m", "common_unit_m.unit_id = unit_m.unit_id"). + Where("unit_m.unit_number = ?", team.Cardid). + Cols("common_unit_m.unit_id,common_unit_m.exp,unit_m.rarity,common_unit_m.max_hp,common_unit_m.is_signed"). + Get(&unitId, &unitExp, &unitRarity, &unitHp, &unitSigned) + CheckErr(err) + + if !exists { + session.Rollback() + ctx.JSON(http.StatusOK, ErrMsg{Error: 1, Msg: "卡片不存在!"}) + return + } + + if unitRarity != 4 { + session.Rollback() + ctx.JSON(http.StatusOK, ErrMsg{Error: 1, Msg: "仅支持导入UR卡片!"}) + return + } + + var diffExp, diffSmile, diffPure, diffCool int + _, err = MainEng.Table("unit_level_limit_pattern_m").Where("unit_level_limit_id = 1 AND unit_level = 350"). + Cols("next_exp,smile_diff,pure_diff,cool_diff").Get(&diffExp, &diffSmile, &diffPure, &diffCool) + CheckErr(err) + + isSigned := false + if unitSigned == 1 { + isSigned = true + } + + var skillExp int + if team.Skilllevel != 8 { + skillExp = 0 + } else { + skillExp = 29900 + } + + unitData := tools.UnitData{ + UserID: ctx.GetInt("userid"), + UnitID: unitId, + Exp: unitExp + diffExp, + NextExp: 0, + Level: 350, + MaxLevel: 350, + LevelLimitID: 1, + Rank: 2, + MaxRank: 2, + Love: 1000, + MaxLove: 1000, + UnitSkillExp: skillExp, + UnitSkillLevel: team.Skilllevel, + MaxHp: unitHp, + UnitRemovableSkillCapacity: 8, + FavoriteFlag: false, + DisplayRank: 2, + IsRankMax: true, + IsLoveMax: true, + IsLevelMax: true, + IsSigned: isSigned, + IsSkillLevelMax: true, + IsRemovableSkillCapacityMax: true, + InsertDate: time.Now().Format("2006-01-02 03:04:05"), + } + + _, err = session.Table("user_unit_m").Insert(&unitData) + if err != nil { + session.Rollback() + panic(err) + } + + if err = session.Commit(); err != nil { + session.Rollback() + panic(err) + } + } + + ctx.JSON(http.StatusOK, ErrMsg{Error: 0, Msg: "上传成功!"}) +} diff --git a/llhelper/helper.go b/llhelper/helper.go deleted file mode 100644 index e87af2d..0000000 --- a/llhelper/helper.go +++ /dev/null @@ -1,119 +0,0 @@ -package llhelper - -import ( - "encoding/json" - "fmt" - "honoka-chan/config" - "honoka-chan/tools" - "honoka-chan/utils" - "os" -) - -var ( - unitFile = "assets/unit.sd" -) - -func CheckErr(err error) { - if err != nil { - panic(err) - } -} - -func init() { - LoadData() -} - -func LoadData() { - _, err := os.Stat(unitFile) - if err != nil { - fmt.Println(err) - return - } - data := Data{} - err = json.Unmarshal([]byte(utils.ReadAllText(unitFile)), &data) - CheckErr(err) - - session := config.UserEng.NewSession() - defer session.Close() - if err = session.Begin(); err != nil { - session.Rollback() - panic(err) - } - - for _, team := range data.Team { - fmt.Println(team.Cardid) - if team.Cardid == 0 { - continue - } - var unitId, unitExp, unitRarity, unitHp, unitSigned int - exists, err := config.MainEng.Table("common_unit_m").Join("LEFT", "unit_m", "common_unit_m.unit_id = unit_m.unit_id"). - Where("unit_m.unit_number = ?", team.Cardid). - Cols("common_unit_m.unit_id,common_unit_m.exp,unit_m.rarity,common_unit_m.max_hp,common_unit_m.is_signed"). - Get(&unitId, &unitExp, &unitRarity, &unitHp, &unitSigned) - CheckErr(err) - - if !exists { - panic("no such unit") - } - - if unitRarity != 4 { - panic("only support UR") - } - - var diffExp, diffSmile, diffPure, diffCool int - _, err = config.MainEng.Table("unit_level_limit_pattern_m").Where("unit_level_limit_id = 1 AND unit_level = 350"). - Cols("next_exp,smile_diff,pure_diff,cool_diff").Get(&diffExp, &diffSmile, &diffPure, &diffCool) - CheckErr(err) - - isSigned := false - if unitSigned == 1 { - isSigned = true - } - - var skillExp int - if team.Skilllevel != 8 { - skillExp = 0 - } else { - skillExp = 29900 - } - - unitData := tools.UnitData{ - UserID: 1681205441, // Replace with your own - UnitID: unitId, - Exp: unitExp + diffExp, - NextExp: 0, - Level: 350, - MaxLevel: 350, - LevelLimitID: 1, - Rank: 2, - MaxRank: 2, - Love: 1000, - MaxLove: 1000, - UnitSkillExp: skillExp, - UnitSkillLevel: team.Skilllevel, - MaxHp: unitHp, - UnitRemovableSkillCapacity: 8, - FavoriteFlag: false, - DisplayRank: 2, - IsRankMax: true, - IsLoveMax: true, - IsLevelMax: true, - IsSigned: isSigned, - IsSkillLevelMax: true, - IsRemovableSkillCapacityMax: true, - InsertDate: "2023-04-20 10:00:00", - } - - _, err = session.Table("user_unit_m").Insert(&unitData) - if err != nil { - session.Rollback() - panic(err) - } - fmt.Println("Insert ID:", unitData.UnitOwningUserID) - - if err = session.Commit(); err != nil { - session.Rollback() - panic(err) - } - } -} diff --git a/main.go b/main.go index b2ebaca..272202f 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "honoka-chan/handler" - _ "honoka-chan/llhelper" "honoka-chan/middleware" _ "honoka-chan/tools" "net/http" @@ -137,12 +136,13 @@ func main() { "url": strings.Split(ctx.Request.URL.String(), "?")[0], }) }) - w.GET("/deck", func(ctx *gin.Context) { - ctx.HTML(http.StatusOK, "admin/card.html", gin.H{ + w.GET("/upload", func(ctx *gin.Context) { + ctx.HTML(http.StatusOK, "admin/upload.html", gin.H{ "menu": 1, "url": strings.Split(ctx.Request.URL.String(), "?")[0], }) }) + w.POST("/upload", handler.Upload) } r.Run(":80") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") diff --git a/middleware/webui.go b/middleware/webui.go index c39c629..40b94e4 100644 --- a/middleware/webui.go +++ b/middleware/webui.go @@ -11,7 +11,7 @@ import ( func WebAuth(ctx *gin.Context) { session := sessions.Default(ctx) requestUrl := strings.Split(ctx.Request.URL.String(), "?")[0] // 过滤 GET 参数 - _, ok := session.Get("username").(string) + userId, ok := session.Get("userid").(int) if ok { if requestUrl == "/admin/login" { ctx.Redirect(http.StatusFound, "/admin/index") @@ -23,6 +23,7 @@ func WebAuth(ctx *gin.Context) { ctx.Abort() } } + ctx.Set("userid", userId) ctx.Next() } diff --git a/llhelper/model.go b/model/llhelper.go similarity index 97% rename from llhelper/model.go rename to model/llhelper.go index 4245ff3..4e327d4 100644 --- a/llhelper/model.go +++ b/model/llhelper.go @@ -1,4 +1,4 @@ -package llhelper +package model type Data struct { Version int `json:"version"` diff --git a/static/templates/admin/deck.html b/static/templates/admin/deck.html deleted file mode 100644 index cf2ec8a..0000000 --- a/static/templates/admin/deck.html +++ /dev/null @@ -1,27 +0,0 @@ -{{ define "admin/deck.html" }} - - - -
- - -