diff --git a/.gitignore b/.gitignore index 5a93bd1..de1eac4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ publickey.pem data logs -assets/account.db +assets/data.db assets/api1.json assets/api2.json diff --git a/assets/account.example.db b/assets/account.example.db deleted file mode 100644 index d07137c..0000000 Binary files a/assets/account.example.db and /dev/null differ diff --git a/assets/data.example.db b/assets/data.example.db new file mode 100644 index 0000000..1a38038 Binary files /dev/null and b/assets/data.example.db differ diff --git a/assets/main.db b/assets/main.db index 448c456..050417d 100644 Binary files a/assets/main.db and b/assets/main.db differ diff --git a/config/config.go b/config/config.go index 124b945..49c280f 100644 --- a/config/config.go +++ b/config/config.go @@ -4,21 +4,32 @@ package config import ( + "honoka-chan/utils" + "os" + _ "github.com/mattn/go-sqlite3" "xorm.io/xorm" ) var ( - ConfName = "config.yml" - Conf = &AppConfigs{} - MainEng *xorm.Engine - UserEng *xorm.Engine + ConfName = "config.yml" + Conf = &AppConfigs{} + ExampleDb = "assets/data.example.db" + MainDb = "assets/main.db" + UserDb = "assets/data.db" + MainEng *xorm.Engine + UserEng *xorm.Engine ) func init() { Conf = Load(ConfName) - eng, err := xorm.NewEngine("sqlite3", "assets/main.db") + _, err := os.Stat(UserDb) + if err != nil { + utils.WriteAllText(UserDb, utils.ReadAllText(ExampleDb)) + } + + eng, err := xorm.NewEngine("sqlite3", MainDb) if err != nil { panic(err) } @@ -30,7 +41,7 @@ func init() { MainEng.SetMaxOpenConns(50) MainEng.SetMaxIdleConns(10) - eng, err = xorm.NewEngine("sqlite3", "assets/account.db") + eng, err = xorm.NewEngine("sqlite3", UserDb) if err != nil { panic(err) } diff --git a/handler/api.go b/handler/api.go index 15d246b..11b118e 100644 --- a/handler/api.go +++ b/handler/api.go @@ -9,8 +9,10 @@ import ( "honoka-chan/database" "honoka-chan/encrypt" "honoka-chan/model" + "honoka-chan/tools" "honoka-chan/utils" "net/http" + "strings" "time" "github.com/gin-gonic/gin" @@ -19,6 +21,11 @@ import ( func ApiHandler(ctx *gin.Context) { reqTime := time.Now().Unix() // fmt.Println(ctx.PostForm("request_data")) + userId := ctx.Request.Header[http.CanonicalHeaderKey("User-ID")] + if len(userId) == 0 { + ctx.String(http.StatusForbidden, ErrorMsg) + return + } var formdata []model.SifApi err := json.Unmarshal([]byte(ctx.PostForm("request_data")), &formdata) if err != nil { @@ -38,120 +45,1034 @@ func ApiHandler(ctx *gin.Context) { if v.Action == "topInfo" { // fmt.Println("topInfo") key = "login_topinfo_result" + topInfoResp := model.TopInfoResp{ + // _ = model.TopInfoResp{ + Result: model.TopInfoResult{ + FriendActionCnt: 0, + FriendGreetCnt: 0, + FriendVarietyCnt: 0, + FriendNewCnt: 0, + PresentCnt: 0, + SecretBoxBadgeFlag: false, + ServerDatetime: time.Now().Format("2006-01-02 15:04:05"), + ServerTimestamp: time.Now().Unix(), + NoticeFriendDatetime: time.Now().Format("2006-01-02 15:04:05"), + NoticeMailDatetime: "2000-01-01 12:00:00", + FriendsApprovalWaitCnt: 0, + FriendsRequestCnt: 0, + IsTodayBirthday: false, + LicenseInfo: model.TopInfoLicenseInfo{ + LicenseList: []interface{}{}, + LicensedInfo: []interface{}{}, + ExpiredInfo: []interface{}{}, + BadgeFlag: false, + }, + UsingBuffInfo: []interface{}{}, + IsKlabIDTaskFlag: false, + KlabIDTaskCanSync: false, + HasUnreadAnnounce: false, + ExchangeBadgeCnt: []int{0, 0, 0}, + AdFlag: false, + HasAdReward: false, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(topInfoResp) + CheckErr(err) } else if v.Action == "topInfoOnce" { // fmt.Println("topInfoOnce") key = "login_topinfo_once_result" + topInfoOnceResp := model.TopInfoOnceResp{ + // _ = model.TopInfoOnceResp{ + Result: model.TopInfoOnceResult{ + NewAchievementCnt: 0, + UnaccomplishedAchievementCnt: 0, + LiveDailyRewardExist: false, + TrainingEnergy: 10, + TrainingEnergyMax: 10, + Notification: model.TopInfoOnceNotification{ + Push: false, + Lp: false, + UpdateInfo: false, + Campaign: false, + Live: false, + Lbonus: false, + Event: false, + Secretbox: false, + Birthday: true, + }, + OpenArena: true, + CostumeStatus: true, + OpenAccessory: true, + ArenaSiSkillUniqueCheck: true, + OpenV98: true, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(topInfoOnceResp) + CheckErr(err) } case "live": if v.Action == "liveStatus" { // fmt.Println("liveStatus") key = "live_status_result" + var liveDifficultyId int + normalLives := []model.NormalLiveStatusList{} + sql := `SELECT live_difficulty_id FROM normal_live_m ORDER BY live_difficulty_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + for rows.Next() { + err = rows.Scan(&liveDifficultyId) + CheckErr(err) + + normalLive := model.NormalLiveStatusList{ + LiveDifficultyID: liveDifficultyId, + Status: 1, + HiScore: 0, + HiComboCount: 0, + ClearCnt: 0, + AchievedGoalIDList: []int{}, + } + normalLives = append(normalLives, normalLive) + } + + specialLives := []model.SpecialLiveStatusList{} + sql = `SELECT live_difficulty_id FROM special_live_m ORDER BY live_difficulty_id ASC` + rows, err = MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + for rows.Next() { + err = rows.Scan(&liveDifficultyId) + CheckErr(err) + + specialLive := model.SpecialLiveStatusList{ + LiveDifficultyID: liveDifficultyId, + Status: 1, + HiScore: 0, + HiComboCount: 0, + ClearCnt: 0, + AchievedGoalIDList: []int{}, + } + specialLives = append(specialLives, specialLive) + } + + LiveStatusResp := model.LiveStatusResp{ + // _ = model.LiveStatusResp{ + Result: model.LiveStatusResult{ + NormalLiveStatusList: normalLives, + SpecialLiveStatusList: specialLives, + TrainingLiveStatusList: []model.TrainingLiveStatusList{}, + MarathonLiveStatusList: []interface{}{}, + FreeLiveStatusList: []interface{}{}, + CanResumeLive: false, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(LiveStatusResp) + CheckErr(err) } else if v.Action == "schedule" { // fmt.Println("schedule") key = "live_list_result" + var liveDifficultyId int + specialLives := []model.SpecialLiveStatusList{} + sql := `SELECT live_difficulty_id FROM special_live_m ORDER BY live_difficulty_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + for rows.Next() { + err = rows.Scan(&liveDifficultyId) + CheckErr(err) + + specialLive := model.SpecialLiveStatusList{ + LiveDifficultyID: liveDifficultyId, + Status: 1, + HiScore: 0, + HiComboCount: 0, + ClearCnt: 0, + AchievedGoalIDList: []int{}, + } + specialLives = append(specialLives, specialLive) + } + + livesList := []model.LiveList{} + for _, v := range specialLives { + livesList = append(livesList, model.LiveList{ + LiveDifficultyID: v.LiveDifficultyID, + StartDate: "2023-01-01 00:00:00", + EndDate: "2037-01-01 00:00:00", + IsRandom: false, + }) + } + liveListResp := model.LiveScheduleResp{ + // _ = model.LiveScheduleResp{ + Result: model.LiveScheduleResult{ + EventList: []interface{}{}, + LiveList: livesList, + LimitedBonusList: []interface{}{}, + LimitedBonusCommonList: []model.LimitedBonusCommonList{}, // 特效道具 + RandomLiveList: []model.RandomLiveList{}, // 随机歌曲 + FreeLiveList: []interface{}{}, + TrainingLiveList: []model.TrainingLiveList{}, // 挑战歌曲 + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(liveListResp) + CheckErr(err) } case "unit": switch v.Action { case "unitAll": // fmt.Println("unitAll") key = "unit_list_result" + unitsData := []model.Active{} + err = MainEng.Table("common_unit_m").Select("*").Find(&unitsData) + if err != nil { + panic(err) + } + + userUnits := []model.Active{} + err = UserEng.Table("user_unit_m").Select("*").Find(&userUnits) + if err != nil { + panic(err) + } + + unitsData = append(unitsData, userUnits...) + + unitListResp := model.UnitAllResp{ + // _ = model.UnitAllResp{ + Result: model.UnitAllResult{ + Active: unitsData, + Waiting: []model.Waiting{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(unitListResp) + CheckErr(err) case "deckInfo": // fmt.Println("deckInfo") key = "unit_deck_result" + userDeck := []tools.UserDeckData{} + err = UserEng.Table("user_deck_m").Where("user_id = ?", userId[0]).Asc("deck_id").Find(&userDeck) + CheckErr(err) + + unitDeckInfo := []model.UnitDeckInfo{} + for _, deck := range userDeck { + deckUnit := []tools.UnitDeckData{} + err = UserEng.Table("deck_unit_m").Where("user_deck_id = ?", deck.ID).Asc("position").Find(&deckUnit) + CheckErr(err) + + oUids := []model.UnitOwningUserIds{} + for _, unit := range deckUnit { + oUids = append(oUids, model.UnitOwningUserIds{ + Position: unit.Position, + UnitOwningUserID: unit.UnitOwningUserID, + }) + } + + mainFlag := false + if deck.MainFlag == 1 { + mainFlag = true + } + unitDeckInfo = append(unitDeckInfo, model.UnitDeckInfo{ + UnitDeckID: deck.DeckID, + MainFlag: mainFlag, + DeckName: deck.DeckName, + UnitOwningUserIds: oUids, + }) + } + unitDeckResp := model.UnitDeckInfoResp{ + // _ = model.UnitDeckInfoResp{ + Result: unitDeckInfo, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(unitDeckResp) + CheckErr(err) case "supporterAll": // fmt.Println("supporterAll") key = "unit_support_result" + unitSupportResp := model.UnitSupportResp{ + // _ = model.UnitSupportResp{ + Result: model.UnitSupportResult{ + UnitSupportList: []model.UnitSupportList{}, + }, // 练习道具 + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(unitSupportResp) + CheckErr(err) case "removableSkillInfo": // fmt.Println("removableSkillInfo") key = "owning_equip_result" + rmSkillResp := model.RemovableSkillResp{ + // _ = model.RemovableSkillResp{ + Result: model.RemovableSkillResult{ + OwningInfo: []model.OwningInfo{}, + EquipmentInfo: []interface{}{}, + }, // 宝石 + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(rmSkillResp) + CheckErr(err) case "accessoryAll": // fmt.Println("accessoryAll") key = "unit_accessory_result" + unitAccResp := model.UnitAccessoryAllResp{ + // _ = model.UnitAccessoryAllResp{ + Result: model.UnitAccessoryAllResult{ + AccessoryList: []interface{}{}, + WearingInfo: []interface{}{}, + EspecialCreateFlag: false, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(unitAccResp) + CheckErr(err) } case "costume": // fmt.Println("costumeList") key = "costume_list_result" + costumeListResp := model.CostumeListResp{ + // _ = model.CostumeListResp{ + Result: model.CostumeListResult{ + CostumeList: []model.CostumeList{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(costumeListResp) + CheckErr(err) case "album": // fmt.Println("albumAll") key = "album_unit_result" + albumLists := []model.AlbumResult{} + sql := `SELECT unit_id,rarity FROM unit_m ORDER BY unit_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + for rows.Next() { + albumList := model.AlbumResult{ + RankMaxFlag: true, + LoveMaxFlag: true, + RankLevelMaxFlag: true, + AllMaxFlag: true, + FavoritePoint: 1000, + } + var uid, rit int + err = rows.Scan(&uid, &rit) + CheckErr(err) + albumList.UnitID = uid + if rit != 4 { + albumList.SignFlag = false + if rit == 1 { + albumList.HighestLovePerUnit = 50 + albumList.TotalLove = 50 + } else if rit == 2 { + albumList.HighestLovePerUnit = 200 + albumList.TotalLove = 200 + } else if rit == 3 { + albumList.HighestLovePerUnit = 500 + albumList.TotalLove = 500 + } else if rit == 5 { + albumList.HighestLovePerUnit = 750 + albumList.TotalLove = 750 + } + } else { + albumList.HighestLovePerUnit = 1000 + albumList.TotalLove = 1000 + + stmt, err := MainEng.DB().Prepare("SELECT COUNT(*) AS ct FROM unit_sign_asset_m WHERE unit_id = ?") + CheckErr(err) + defer stmt.Close() + + var count int + err = stmt.QueryRow(uid).Scan(&count) + CheckErr(err) + + if count > 0 { + albumList.SignFlag = true + } else { + albumList.SignFlag = false + } + } + albumLists = append(albumLists, albumList) + } + + albumResp := model.AlbumResp{ + // _ = model.AlbumResp{ + Result: albumLists, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(albumResp) + CheckErr(err) case "scenario": // fmt.Println("scenarioStatus") key = "scenario_status_result" + sql := `SELECT scenario_id FROM scenario_m ORDER BY scenario_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + scenarioLists := []model.ScenarioStatusList{} + for rows.Next() { + var sid int + err = rows.Scan(&sid) + CheckErr(err) + scenarioLists = append(scenarioLists, model.ScenarioStatusList{ + ScenarioID: sid, + Status: 2, + }) + } + scenarioResp := model.ScenarioStatusResp{ + // _ = model.ScenarioStatusResp{ + Result: model.ScenarioStatusResult{ + ScenarioStatusList: scenarioLists, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(scenarioResp) + CheckErr(err) case "subscenario": // fmt.Println("subscenarioStatus") key = "subscenario_status_result" + sql := `SELECT subscenario_id FROM subscenario_m ORDER BY subscenario_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + subScenarioLists := []model.SubscenarioStatusList{} + for rows.Next() { + var sid int + err = rows.Scan(&sid) + CheckErr(err) + subScenarioLists = append(subScenarioLists, model.SubscenarioStatusList{ + SubscenarioID: sid, + Status: 2, + }) + } + subScenarioResp := model.SubscenarioStatusResp{ + // _ = model.SubscenarioStatusResp{ + Result: model.SubscenarioStatusResult{ + SubscenarioStatusList: subScenarioLists, + UnlockedSubscenarioIds: []interface{}{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(subScenarioResp) + CheckErr(err) case "eventscenario": // fmt.Println("status") key = "event_scenario_result" + eventsList := []model.EventScenarioList{} + sql := `SELECT event_id FROM event_scenario_m GROUP BY event_id ORDER BY event_id DESC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + for rows.Next() { + var eventId int + err = rows.Scan(&eventId) + CheckErr(err) + + sql = `SELECT event_scenario_id,chapter,chapter_asset,open_date FROM event_scenario_m WHERE event_id = ? ORDER BY chapter DESC` + chaps, err := MainEng.DB().Query(sql, eventId) + CheckErr(err) + defer chaps.Close() + chapsList := []model.EventScenarioChapterList{} + var open_date string + for chaps.Next() { + var event_scenario_id, chapter int + var chapter_asset interface{} + err = chaps.Scan(&event_scenario_id, &chapter, &chapter_asset, &open_date) + CheckErr(err) + chapList := model.EventScenarioChapterList{ + EventScenarioID: event_scenario_id, + Chapter: chapter, + Status: 2, + OpenFlashFlag: 0, + IsReward: false, + CostType: 1000, + ItemID: 1200, + Amount: 1, + } + if chapter_asset != nil { + chapList.ChapterAsset = chapter_asset.(string) + } + + chapsList = append(chapsList, chapList) + } + + eventList := model.EventScenarioList{ + EventID: eventId, + OpenDate: strings.ReplaceAll(open_date, "/", "-"), + ChapterList: chapsList, + } + + // HACK event_scenario_btn_asset + if eventId == 10001 { + eventList.EventScenarioBtnAsset = "assets/image/ui/eventscenario/38_se_ba_t.png" + } else if eventId == 221 { + eventList.EventScenarioBtnAsset = "assets/image/ui/eventscenario/215_se_ba_t.png" + } else { + eventList.EventScenarioBtnAsset = fmt.Sprintf("assets/image/ui/eventscenario/%d_se_ba_t.png", eventId) + } + + eventsList = append(eventsList, eventList) + } + eventScenarioResp := model.EventScenarioStatusResp{ + // _ = model.EventScenarioStatusResp{ + Result: model.EventScenarioStatusResult{ + EventScenarioList: eventsList, // + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(eventScenarioResp) + CheckErr(err) case "multiunit": // fmt.Println("multiunitscenarioStatus") key = "multi_unit_scenario_result" + sql := `SELECT multi_unit_id FROM multi_unit_scenario_m GROUP BY multi_unit_id ORDER BY multi_unit_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + var mId int + multiUnitsList := []model.MultiUnitScenarioStatusList{} + for rows.Next() { + err = rows.Scan(&mId) + CheckErr(err) + + sql = `SELECT multi_unit_scenario_btn_asset,open_date,multi_unit_scenario_id,chapter FROM multi_unit_scenario_m a LEFT JOIN multi_unit_scenario_open_m b ON a.multi_unit_id = b.multi_unit_id WHERE a.multi_unit_id = ?` + units, err := MainEng.DB().Query(sql, mId) + CheckErr(err) + defer units.Close() + var multi_unit_scenario_id, chapter int + var multi_unit_scenario_btn_asset, open_date string + for units.Next() { + err = units.Scan(&multi_unit_scenario_btn_asset, &open_date, &multi_unit_scenario_id, &chapter) + CheckErr(err) + } + + multiUnitsList = append(multiUnitsList, model.MultiUnitScenarioStatusList{ + MultiUnitID: mId, + Status: 2, + MultiUnitScenarioBtnAsset: multi_unit_scenario_btn_asset, + OpenDate: strings.ReplaceAll(open_date, "/", "-"), + ChapterList: []model.MultiUnitScenarioChapterList{ + { + MultiUnitScenarioID: multi_unit_scenario_id, + Chapter: chapter, + Status: 2, + }, + }, + }) + } + unitsResp := model.MultiUnitScenarioStatusResp{ + // _ = model.MultiUnitScenarioStatusResp{ + Result: model.MultiUnitScenarioStatusResult{ + MultiUnitScenarioStatusList: multiUnitsList, + UnlockedMultiUnitScenarioIds: []interface{}{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(unitsResp) + CheckErr(err) case "payment": // fmt.Println("productList") key = "product_result" + productResp := model.ProductListResp{ + // _ = model.ProductListResp{ + Result: model.ProductListResult{ + RestrictionInfo: model.RestrictionInfo{ + Restricted: false, + }, + UnderAgeInfo: model.UnderAgeInfo{ + BirthSet: false, + HasLimit: false, + LimitAmount: nil, + MonthUsed: 0, + }, + SnsProductList: []model.SnsProductList{}, + ProductList: []model.ProductList{}, + SubscriptionList: []model.SubscriptionList{}, + ShowPointShop: false, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(productResp) + CheckErr(err) case "banner": // fmt.Println("bannerList") key = "banner_result" + bannerResp := model.BannerListResp{ + // _ = model.BannerListResp{ + Result: model.BannerListResult{ + TimeLimit: "2037-12-31 23:59:59", + BannerList: []model.BannerList{ + { + BannerType: 1, + TargetID: 1743, + AssetPath: "assets/image/secretbox/icon/s_ba_1743_1.png", + FixedFlag: false, + BackSide: false, + BannerID: 101151, + StartDate: "2013-04-15 00:00:00", + EndDate: "2037-12-31 23:59:59", + AddUnitStartDate: "2022-01-01 00:00:00", + }, + { + BannerType: 2, + TargetID: 1, + AssetPath: "assets/image/webview/wv_ba_01.png", + WebviewURL: "/manga", + FixedFlag: false, + BackSide: true, + BannerID: 200001, + StartDate: "2016-10-15 15:00:00", + EndDate: "2037-12-31 23:59:59", + }, + }, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(bannerResp) + CheckErr(err) case "notice": // fmt.Println("noticeMarquee") key = "item_marquee_result" + marqueeResp := model.NoticeMarqueeResp{ + // _ = model.NoticeMarqueeResp{ + Result: model.NoticeMarqueeResult{ + ItemCount: 0, + MarqueeList: []interface{}{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(marqueeResp) + CheckErr(err) case "user": // fmt.Println("getNavi") key = "user_intro_result" + userIntroResp := model.UserNaviResp{ + // _ = model.UserNaviResp{ + Result: model.UserNaviResult{ + User: model.User{ + UserID: 9999999, + UnitOwningUserID: 41674, + }, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(userIntroResp) + CheckErr(err) case "navigation": // fmt.Println("specialCutin") key = "special_cutin_result" + cutinResp := model.SpecialCutinResp{ + // _ = model.SpecialCutinResp{ + Result: model.SpecialCutinResult{ + SpecialCutinList: []interface{}{}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(cutinResp) + CheckErr(err) case "award": // fmt.Println("awardInfo") key = "award_result" + sql := `SELECT award_id FROM award_m ORDER BY award_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + awardsList := []model.AwardInfo{} + for rows.Next() { + var aId int + err = rows.Scan(&aId) + CheckErr(err) + isSet := false + if aId == 113 { // 极推穗乃果 + isSet = true + } + awardsList = append(awardsList, model.AwardInfo{ + AwardID: aId, + IsSet: isSet, + InsertDate: time.Now().Format("2006-01-02 03:04:05"), + }) + } + awardResp := model.AwardInfoResp{ + // _ = model.AwardInfoResp{ + Result: model.AwardInfoResult{ + AwardInfo: awardsList, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(awardResp) + CheckErr(err) case "background": // fmt.Println("backgroundInfo") key = "background_result" + sql := `SELECT background_id FROM background_m ORDER BY background_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + backgroundsList := []model.BackgroundInfo{} + for rows.Next() { + var bId int + err = rows.Scan(&bId) + CheckErr(err) + isSet := false + if bId == 143 { // 穗乃果的房间[情人节] + isSet = true + } + backgroundsList = append(backgroundsList, model.BackgroundInfo{ + BackgroundID: bId, + IsSet: isSet, + InsertDate: time.Now().Format("2006-01-02 03:04:05"), + }) + } + backgroundResp := model.BackgroundInfoResp{ + // _ = model.BackgroundInfoResp{ + Result: model.BackgroundInfoResult{ + BackgroundInfo: backgroundsList, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(backgroundResp) + CheckErr(err) case "stamp": // fmt.Println("stampInfo") key = "stamp_result" + stampResp := utils.ReadAllText("assets/stamp.json") + var mStampResp interface{} + err = json.Unmarshal([]byte(stampResp), &mStampResp) + CheckErr(err) + res, err = json.Marshal(mStampResp) + CheckErr(err) case "exchange": // fmt.Println("owningPoint") key = "exchange_point_result" + sql := `SELECT exchange_point_id FROM exchange_point_m ORDER BY exchange_point_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + exPointsList := []model.ExchangePointList{} + for rows.Next() { + var eId int + err = rows.Scan(&eId) + CheckErr(err) + exPointsList = append(exPointsList, model.ExchangePointList{ + Rarity: eId, + ExchangePoint: 9999, + }) + } + exPointsResp := model.ExchangePointResp{ + // _ = model.ExchangePointResp{ + Result: model.ExchangePointResult{ + ExchangePointList: exPointsList, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(exPointsResp) + CheckErr(err) case "livese": // fmt.Println("liveseInfo") key = "live_se_result" + liveSeResp := model.LiveSeInfoResp{ + // _ = model.LiveSeInfoResp{ + Result: model.LiveSeInfoResult{ + LiveSeList: []int{1, 2, 3}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(liveSeResp) + CheckErr(err) case "liveicon": // fmt.Println("liveiconInfo") key = "live_icon_result" + liveIconResp := model.LiveIconInfoResp{ + // _ = model.LiveIconInfoResp{ + Result: model.LiveIconInfoResult{ + LiveNotesIconList: []int{1, 2, 3}, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(liveIconResp) + CheckErr(err) case "item": // fmt.Println("list") key = "item_list_result" + itemResp := utils.ReadAllText("assets/item.json") + // _ = utils.ReadAllText("assets/item.json") + var mItemResp interface{} + err = json.Unmarshal([]byte(itemResp), &mItemResp) + CheckErr(err) + res, err = json.Marshal(mItemResp) + CheckErr(err) case "marathon": // fmt.Println("marathonInfo") key = "marathon_result" + marathonResp := model.MarathonInfoResp{ + // _ = model.MarathonInfoResp{ + Result: []interface{}{}, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(marathonResp) + CheckErr(err) case "challenge": // fmt.Println("challengeInfo") key = "challenge_result" + challengeResp := model.ChallengeInfoResp{ + // _ = model.ChallengeInfoResp{ + Result: []interface{}{}, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(challengeResp) + CheckErr(err) case "museum": // fmt.Println("info") key = "museum_result" + sql := `SELECT museum_contents_id,smile_buff,pure_buff,cool_buff FROM museum_contents_m ORDER BY museum_contents_id ASC` + rows, err := MainEng.DB().Query(sql) + CheckErr(err) + defer rows.Close() + var smileBuf, pureBuf, coolBuf int + var mIds []int + for rows.Next() { + var museum_contents_id, smile_buff, pure_buff, cool_buff int + err = rows.Scan(&museum_contents_id, &smile_buff, &pure_buff, &cool_buff) + CheckErr(err) + smileBuf += smile_buff + pureBuf += pure_buff + coolBuf += cool_buff + mIds = append(mIds, museum_contents_id) + } + + museumInfoResp := model.MuseumInfoResp{ + Result: model.MuseumInfoResult{ + MuseumInfo: model.MuseumInfo{ + Parameter: model.MuseumInfoParameter{ + Smile: smileBuf, + Pure: pureBuf, + Cool: coolBuf, + }, + ContentsIDList: mIds, + }, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(museumInfoResp) + CheckErr(err) case "profile": if v.Action == "liveCnt" { // fmt.Println("liveCnt") key = "profile_livecnt_result" + difficultyResp := tools.DifficultyResp{ + // _ = DifficultyResp{ + Result: []tools.DifficultyResult{ + { + Difficulty: 1, + ClearCnt: 315, + }, + { + Difficulty: 2, + ClearCnt: 310, + }, + { + Difficulty: 3, + ClearCnt: 314, + }, + { + Difficulty: 4, + ClearCnt: 455, + }, + { + Difficulty: 6, + ClearCnt: 233, + }, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(difficultyResp) + CheckErr(err) } else if v.Action == "cardRanking" { // fmt.Println("cardRanking") key = "profile_card_ranking_result" + var result []interface{} + love := utils.ReadAllText("assets/love.json") + err := json.Unmarshal([]byte(love), &result) + CheckErr(err) + loveResp := tools.LoveResp{ + // _ = LoveResp{ + Result: result, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(loveResp) + CheckErr(err) } else if v.Action == "profileInfo" { // fmt.Println("profileInfo") key = "profile_info_result" + profileResp := tools.ProfileResp{ + Result: tools.ProfileResult{ + UserInfo: tools.UserInfo{ + UserID: 9999999, // 3241988 + Name: "\u68a6\u8def @\u65c5\u7acb\u3061\u306e\u65e5\u306b", + Level: 1028, + CostMax: 100, + UnitMax: 5000, + EnergyMax: 417, + FriendMax: 99, + UnitCnt: 3898, + InviteCode: "377385143", + ElapsedTimeFromLogin: "14\u5c0f\u65f6\u524d", + Introduction: "\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d \u65f6\u5149\u4ece\u4e0d\u505c\u7559\\n\u5982\u4eca\u7684\u6211\u4eec \u6bd5\u4e1a\u518d\u56de\u9996\\n\u8ffd\u68a6\u7684\u670b\u53cb\u4eec\u4e00\u540c \u8e0f\u4e0a\u4e86\u65b0\u7684\u5f81\u9014\\n\u5728\u4e0d\u4e45\u7684\u4eca\u540e \u5728\u672a\u77e5\u7684\u67d0\u5904\\n \u6211\u4eec\u4e00\u5b9a\u4f1a\u518d\u4e00\u6b21\u9082\u9005\\n \u8bf7\u4e0d\u8981\u5fd8\u8bb0\u6211\u4eec \u66fe\u7ecf\u7684\u7b11\u5bb9\r", + }, + CenterUnitInfo: tools.CenterUnitInfo{ + UnitOwningUserID: 41674, + UnitID: 3927, + Exp: 79700, + NextExp: 0, + Level: 100, + LevelLimitID: 2, + MaxLevel: 100, + Rank: 2, + MaxRank: 2, + Love: 1000, + MaxLove: 1000, + UnitSkillLevel: 8, + MaxHp: 6, + FavoriteFlag: true, + DisplayRank: 2, + UnitSkillExp: 29900, + UnitRemovableSkillCapacity: 8, + Attribute: 1, + Smile: 1, + Cute: 1, + Cool: 1, + IsLoveMax: true, + IsLevelMax: true, + IsRankMax: true, + IsSigned: true, + IsSkillLevelMax: true, + SettingAwardID: 113, + RemovableSkillIds: []int{}, + AccessoryInfo: tools.AccessoryInfo{}, + Costume: tools.Costume{}, + TotalSmile: 1, + TotalCute: 1, + TotalCool: 1, + TotalHp: 6, + }, + NaviUnitInfo: tools.NaviUnitInfo{ + UnitOwningUserID: 41674, + UnitID: 3927, + Exp: 79700, + NextExp: 0, + Level: 100, + MaxLevel: 100, + LevelLimitID: 2, + Rank: 2, + MaxRank: 2, + Love: 1000, + MaxLove: 1000, + UnitSkillExp: 29900, + UnitSkillLevel: 8, + MaxHp: 6, + UnitRemovableSkillCapacity: 8, + FavoriteFlag: true, + DisplayRank: 2, + IsLoveMax: true, + IsLevelMax: true, + IsRankMax: true, + IsSigned: true, + IsSkillLevelMax: true, + IsRemovableSkillCapacityMax: true, + InsertDate: "2016-10-11 10:33:03", + TotalSmile: 1, + TotalCute: 1, + TotalCool: 1, + TotalHp: 6, + RemovableSkillIds: []int{}, + }, + IsAlliance: false, + FriendStatus: 0, + SettingAwardID: 113, + SettingBackgroundID: 143, + }, + Status: 200, + CommandNum: false, + TimeStamp: time.Now().Unix(), + } + res, err = json.Marshal(profileResp) + CheckErr(err) } default: // fmt.Println(ErrorMsg) fmt.Println(v) err = errors.New("invalid option") + CheckErr(err) } - CheckErr(err) + // fmt.Println(key) + _ = key - res, err = database.LevelDb.Get([]byte(key)) - CheckErr(err) + // if key != "login_topinfo_result" && key != "login_topinfo_once_result" { + // res, err = database.LevelDb.Get([]byte(key)) + // CheckErr(err) + // } var result interface{} err = json.Unmarshal([]byte(res), &result) @@ -176,11 +1097,6 @@ func ApiHandler(ctx *gin.Context) { ctx.String(http.StatusForbidden, ErrorMsg) return } - userId := ctx.Request.Header[http.CanonicalHeaderKey("User-ID")] - if len(userId) == 0 { - ctx.String(http.StatusForbidden, ErrorMsg) - return - } if !database.MatchTokenUid(authToken, userId[0]) { ctx.String(http.StatusForbidden, ErrorMsg) diff --git a/handler/live.go b/handler/live.go index b6d54d0..8b3482a 100644 --- a/handler/live.go +++ b/handler/live.go @@ -107,6 +107,11 @@ func PlayLiveHandler(ctx *gin.Context) { CheckErr(err) deckId := playReq.UnitDeckID + // Save Deck Id for /live/reward + key := "live_deck_" + userId[0] + err = database.LevelDb.Put([]byte(key), []byte(strconv.Itoa(deckId))) + CheckErr(err) + // Song type: normal / special // sqlite3 doesn't support FULL OUTER JOIN so use UNION ALL here. sql := `SELECT notes_setting_asset,c_rank_score,b_rank_score,a_rank_score,s_rank_score,ac_flag,swing_flag FROM live_setting_m WHERE live_setting_id IN (SELECT live_setting_id FROM normal_live_m WHERE live_difficulty_id = ? UNION ALL SELECT live_setting_id FROM special_live_m WHERE live_difficulty_id = ?)` @@ -119,7 +124,7 @@ func PlayLiveHandler(ctx *gin.Context) { // fmt.Println(c_rank_score, b_rank_score, a_rank_score, s_rank_score) notes := []model.NotesList{} - fmt.Println("./assets/notes/" + notes_setting_asset) + // fmt.Println("./assets/notes/" + notes_setting_asset) notes_list := utils.ReadAllText("./assets/notes/" + notes_setting_asset) err = json.Unmarshal([]byte(notes_list), ¬es) CheckErr(err) @@ -312,7 +317,7 @@ func PlayScoreHandler(ctx *gin.Context) { // fmt.Println(c_rank_score, b_rank_score, a_rank_score, s_rank_score) notes := []model.NotesList{} - fmt.Println("./assets/notes/" + notes_setting_asset) + // fmt.Println("./assets/notes/" + notes_setting_asset) notes_list := utils.ReadAllText("./assets/notes/" + notes_setting_asset) err = json.Unmarshal([]byte(notes_list), ¬es) CheckErr(err) @@ -434,10 +439,13 @@ func PlayRewardHandler(ctx *gin.Context) { err = MainEng.DB().QueryRow(sql, difficultyId, difficultyId).Scan(&c_rank_score, &b_rank_score, &a_rank_score, &s_rank_score, &c_rank_combo, &b_rank_combo, &a_rank_combo, &s_rank_combo, &ac_flag, &swing_flag) CheckErr(err) - unitsList := []model.PlayRewardUnitList{} - deck, err := database.LevelDb.Get([]byte("deck_info")) + key := "live_deck_" + userId[0] + deckId, err := database.LevelDb.Get([]byte(key)) CheckErr(err) - err = json.Unmarshal(deck, &unitsList) + unitsList := []model.PlayRewardUnitList{} + err = UserEng.Table("deck_unit_m").Select("*"). + Join("LEFT", "user_deck_m", "deck_unit_m.user_deck_id = user_deck_m.id"). + Where("user_id = ? AND deck_id = ?", userId[0], string(deckId)).Find(&unitsList) CheckErr(err) totalScore := playRewardReq.ScoreSmile + playRewardReq.ScoreCool + playRewardReq.ScoreCute diff --git a/handler/private.go b/handler/private.go index d8e5d5a..c550b47 100644 --- a/handler/private.go +++ b/handler/private.go @@ -6,6 +6,7 @@ import ( "fmt" "honoka-chan/database" "honoka-chan/encrypt" + "honoka-chan/tools" "honoka-chan/utils" "io" "net/http" @@ -318,8 +319,13 @@ func AccountLoginHandler(ctx *gin.Context) { defer stmt.Close() var pass, autokey, ticket, userid string - err = stmt.QueryRow(phone).Scan(&pass, &autokey, &ticket, &userid) + rows, err := stmt.Query(phone) CheckErr(err) + defer rows.Close() + for rows.Next() { + err = rows.Scan(&pass, &autokey, &ticket, &userid) + CheckErr(err) + } loginResp := LoginResp{} loginCode := 0 @@ -372,6 +378,8 @@ func AccountLoginHandler(ctx *gin.Context) { panic(err) } + tools.InitUserData(int(loginTime)) + // Login Response loginResp.Autokey = autokey loginResp.HasRealInfo = 1 diff --git a/middleware/middleware.go b/middleware/middleware.go index 89f469a..e88962f 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -18,6 +18,7 @@ func CommonMid(ctx *gin.Context) { res, _ := database.LevelDb.Get([]byte(userId[0])) if authStr != string(res) { ctx.String(http.StatusForbidden, handler.ErrorMsg) + ctx.Abort() return } } @@ -26,4 +27,6 @@ func CommonMid(ctx *gin.Context) { ctx.Header("server_version", config.Conf.Server.VersionDate) ctx.Header("version_up", config.Conf.Server.VersionUp) ctx.Header("status_code", "200") + + ctx.Next() } diff --git a/model/live.go b/model/live.go index b2b0382..824834b 100644 --- a/model/live.go +++ b/model/live.go @@ -397,22 +397,42 @@ type EffortPoint struct { Rewards []Rewards `json:"rewards"` } +// type PlayRewardUnitList struct { +// UnitOwningUserID int `json:"unit_owning_user_id"` +// UnitID int `json:"unit_id"` +// Position int `json:"position"` +// Level int `json:"level"` +// LevelLimitID int `json:"level_limit_id"` +// DisplayRank int `json:"display_rank"` +// Love int `json:"love"` +// UnitSkillLevel int `json:"unit_skill_level"` +// IsRankMax bool `json:"is_rank_max"` +// IsLoveMax bool `json:"is_love_max"` +// IsLevelMax bool `json:"is_level_max"` +// IsSigned bool `json:"is_signed"` +// BeforeLove int `json:"before_love"` +// MaxLove int `json:"max_love"` +// // Costume Costume `json:"costume,omitempty"` +// } + type PlayRewardUnitList struct { - UnitOwningUserID int `json:"unit_owning_user_id"` - UnitID int `json:"unit_id"` - Position int `json:"position"` - Level int `json:"level"` - LevelLimitID int `json:"level_limit_id"` - DisplayRank int `json:"display_rank"` - Love int `json:"love"` - UnitSkillLevel int `json:"unit_skill_level"` - IsRankMax bool `json:"is_rank_max"` - IsLoveMax bool `json:"is_love_max"` - IsLevelMax bool `json:"is_level_max"` - IsSigned bool `json:"is_signed"` - BeforeLove int `json:"before_love"` - MaxLove int `json:"max_love"` - // Costume Costume `json:"costume,omitempty"` + ID int `xorm:"id pk autoincr" json:"-"` + UserDeckID int `xorm:"user_deck_id" json:"-"` + UnitOwningUserID int `xorm:"unit_owning_user_id" json:"unit_owning_user_id"` + UnitID int `xorm:"unit_id" json:"unit_id"` + Position int `xorm:"position" json:"position"` + Level int `xorm:"level" json:"level"` + LevelLimitID int `xorm:"level_limit_id" json:"level_limit_id"` + DisplayRank int `xorm:"display_rank" json:"display_rank"` + Love int `xorm:"love" json:"love"` + UnitSkillLevel int `xorm:"unit_skill_level" json:"unit_skill_level"` + IsRankMax bool `xorm:"is_rank_max" json:"is_rank_max"` + IsLoveMax bool `xorm:"is_love_max" json:"is_love_max"` + IsLevelMax bool `xorm:"is_level_max" json:"is_level_max"` + IsSigned bool `xorm:"is_signed" json:"is_signed"` + BeforeLove int `xorm:"before_love" json:"before_love"` + MaxLove int `xorm:"max_love" json:"max_love"` + InsertData int64 `xorm:"insert_date" json:"-"` } type BeforeUserInfo struct { diff --git a/model/unit.go b/model/unit.go index 0721d62..45de4c0 100644 --- a/model/unit.go +++ b/model/unit.go @@ -21,30 +21,31 @@ type Costume struct { } type Active struct { - UnitOwningUserID int `json:"unit_owning_user_id"` - UnitID int `json:"unit_id"` - Exp int `json:"exp"` - NextExp int `json:"next_exp"` - Level int `json:"level"` - MaxLevel int `json:"max_level"` - LevelLimitID int `json:"level_limit_id"` - Rank int `json:"rank"` - MaxRank int `json:"max_rank"` - Love int `json:"love"` - MaxLove int `json:"max_love"` - UnitSkillExp int `json:"unit_skill_exp"` - UnitSkillLevel int `json:"unit_skill_level"` - MaxHp int `json:"max_hp"` - UnitRemovableSkillCapacity int `json:"unit_removable_skill_capacity"` - FavoriteFlag bool `json:"favorite_flag"` - DisplayRank int `json:"display_rank"` - IsRankMax bool `json:"is_rank_max"` - IsLoveMax bool `json:"is_love_max"` - IsLevelMax bool `json:"is_level_max"` - IsSigned bool `json:"is_signed"` - IsSkillLevelMax bool `json:"is_skill_level_max"` - IsRemovableSkillCapacityMax bool `json:"is_removable_skill_capacity_max"` - InsertDate string `json:"insert_date"` + UnitOwningUserID int `xorm:"unit_owning_user_id pk autoincr" json:"unit_owning_user_id"` + UserID int `xorm:"user_id" json:"-"` + UnitID int `xorm:"unit_id" json:"unit_id"` + Exp int `xorm:"exp" json:"exp"` + NextExp int `xorm:"next_exp" json:"next_exp"` + Level int `xorm:"level" json:"level"` + MaxLevel int `xorm:"max_level" json:"max_level"` + LevelLimitID int `xorm:"level_limit_id" json:"level_limit_id"` + Rank int `xorm:"rank" json:"rank"` + MaxRank int `xorm:"max_rank" json:"max_rank"` + Love int `xorm:"love" json:"love"` + MaxLove int `xorm:"max_love" json:"max_love"` + UnitSkillExp int `xorm:"unit_skill_exp" json:"unit_skill_exp"` + UnitSkillLevel int `xorm:"unit_skill_level" json:"unit_skill_level"` + MaxHp int `xorm:"max_hp" json:"max_hp"` + UnitRemovableSkillCapacity int `xorm:"unit_removable_skill_capacity" json:"unit_removable_skill_capacity"` + FavoriteFlag bool `xorm:"favorite_flag" json:"favorite_flag"` + DisplayRank int `xorm:"display_rank" json:"display_rank"` + IsRankMax bool `xorm:"is_rank_max" json:"is_rank_max"` + IsLoveMax bool `xorm:"is_love_max" json:"is_love_max"` + IsLevelMax bool `xorm:"is_level_max" json:"is_level_max"` + IsSigned bool `xorm:"is_signed" json:"is_signed"` + IsSkillLevelMax bool `xorm:"is_skill_level_max" json:"is_skill_level_max"` + IsRemovableSkillCapacityMax bool `xorm:"is_removable_skill_capacity_max" json:"is_removable_skill_capacity_max"` + InsertDate string `xorm:"insert_date" json:"insert_date"` // Costume Costume `json:"costume,omitempty"` } diff --git a/tools/create.go b/tools/create.go new file mode 100644 index 0000000..00c4d06 --- /dev/null +++ b/tools/create.go @@ -0,0 +1,273 @@ +package tools + +import ( + "encoding/json" + "fmt" + "honoka-chan/model" + "time" + + _ "github.com/mattn/go-sqlite3" +) + +type UnitData struct { + UnitOwningUserID int `xorm:"unit_owning_user_id pk autoincr" json:"unit_owning_user_id"` + UserID int `xorm:"user_id" json:"-"` + UnitID int `xorm:"unit_id" json:"unit_id"` + Exp int `xorm:"exp" json:"exp"` + NextExp int `xorm:"next_exp" json:"next_exp"` + Level int `xorm:"level" json:"level"` + MaxLevel int `xorm:"max_level" json:"max_level"` + LevelLimitID int `xorm:"level_limit_id" json:"level_limit_id"` + Rank int `xorm:"rank" json:"rank"` + MaxRank int `xorm:"max_rank" json:"max_rank"` + Love int `xorm:"love" json:"love"` + MaxLove int `xorm:"max_love" json:"max_love"` + UnitSkillExp int `xorm:"unit_skill_exp" json:"unit_skill_exp"` + UnitSkillLevel int `xorm:"unit_skill_level" json:"unit_skill_level"` + MaxHp int `xorm:"max_hp" json:"max_hp"` + UnitRemovableSkillCapacity int `xorm:"unit_removable_skill_capacity" json:"unit_removable_skill_capacity"` + FavoriteFlag bool `xorm:"favorite_flag" json:"favorite_flag"` + DisplayRank int `xorm:"display_rank" json:"display_rank"` + IsRankMax bool `xorm:"is_rank_max" json:"is_rank_max"` + IsLoveMax bool `xorm:"is_love_max" json:"is_love_max"` + IsLevelMax bool `xorm:"is_level_max" json:"is_level_max"` + IsSigned bool `xorm:"is_signed" json:"is_signed"` + IsSkillLevelMax bool `xorm:"is_skill_level_max" json:"is_skill_level_max"` + IsRemovableSkillCapacityMax bool `xorm:"is_removable_skill_capacity_max" json:"is_removable_skill_capacity_max"` + InsertDate string `xorm:"insert_date" json:"insert_date"` +} + +type UserDeckData struct { + ID int `xorm:"id pk autoincr"` + DeckID int `xorm:"deck_id"` + MainFlag int `xorm:"main_flag"` + DeckName string `xorm:"deck_name"` + UserID int `xorm:"user_id"` + InsertDate int64 `xorm:"insert_date"` +} + +type UnitDeckData struct { + ID int `xorm:"id pk autoincr" json:"-"` + UserDeckID int `xorm:"user_deck_id" json:"-"` + UnitOwningUserID int `xorm:"unit_owning_user_id" json:"unit_owning_user_id"` + UnitID int `xorm:"unit_id" json:"unit_id"` + Position int `xorm:"position" json:"position"` + Level int `xorm:"level" json:"level"` + LevelLimitID int `xorm:"level_limit_id" json:"level_limit_id"` + DisplayRank int `xorm:"display_rank" json:"display_rank"` + Love int `xorm:"love" json:"love"` + UnitSkillLevel int `xorm:"unit_skill_level" json:"unit_skill_level"` + IsRankMax bool `xorm:"is_rank_max" json:"is_rank_max"` + IsLoveMax bool `xorm:"is_love_max" json:"is_love_max"` + IsLevelMax bool `xorm:"is_level_max" json:"is_level_max"` + IsSigned bool `xorm:"is_signed" json:"is_signed"` + BeforeLove int `xorm:"before_love" json:"before_love"` + MaxLove int `xorm:"max_love" json:"max_love"` + InsertData int64 `xorm:"insert_date" json:"-"` +} + +type AlbumSeries struct { + UnitID int `json:"unit_id"` + AlbumSeriesID int `json:"album_series_id"` + Rarity int `json:"rarity"` + RankMin int `json:"rank_min"` + RankMax int `json:"rank_max"` + HpMax int `json:"hp_max"` + DefaultRemovableSkillCapacity int `json:"default_removable_skill_capacity"` +} + +func GenCommonUnitData2() { + sql := `SELECT unit_id,album_series_id,rarity,rank_min,rank_max,hp_max,default_removable_skill_capacity FROM unit_m WHERE unit_id NOT IN (SELECT unit_id FROM unit_m WHERE unit_type_id IN (SELECT unit_type_id FROM unit_type_m WHERE image_button_asset IS NULL AND (background_color = 'dcdbe3' AND original_attribute_id IS NULL) OR (unit_type_id IN (10,110,127,128,129) OR unit_type_id BETWEEN 131 AND 140))) ORDER BY unit_number ASC` + rows, err := MainEng.QueryInterface(sql) + CheckErr(err) + + rb, err := json.Marshal(rows) + CheckErr(err) + // fmt.Println(string(rb)) + + // 默认卡组 + userDeck := UserDeckData{ + DeckID: 1, + MainFlag: 1, + DeckName: "Default deck", + UserID: 1, + InsertDate: time.Now().Unix(), + } + _, err = UserEng.Table("user_deck_m").Insert(&userDeck) + CheckErr(err) + userDeckId := userDeck.ID + fmt.Println("userDeckId:", userDeckId) + + position := 1 + + albums := []AlbumSeries{} + if err = json.Unmarshal(rb, &albums); err != nil { + panic(err) + } + + startTime := time.Now().Add(-time.Hour * 24 * 30) + for _, v := range albums { + unitData := UnitData{} + startTime = startTime.Add(time.Second * 3) + + unitData.UnitID = v.UnitID + unitData.NextExp = 0 + unitData.Rank = v.RankMin + unitData.MaxRank = v.RankMax + unitData.MaxHp = v.HpMax + unitData.DisplayRank = 2 + unitData.UnitSkillLevel = 8 + unitData.FavoriteFlag = true + unitData.IsRankMax = true + unitData.IsLevelMax = true + unitData.IsLoveMax = true + unitData.IsSkillLevelMax = true + unitData.IsRemovableSkillCapacityMax = true + unitData.InsertDate = startTime.Local().Format("2006-01-02 03:04:05") + if v.Rarity != 4 { + unitData.LevelLimitID = 0 + unitData.UnitRemovableSkillCapacity = v.DefaultRemovableSkillCapacity + unitData.IsSigned = false + switch v.Rarity { + case 1: + // N + unitData.Exp = 8000 + unitData.Level = 40 + unitData.MaxLevel = 40 + unitData.Love = 50 + unitData.MaxLove = 50 + unitData.UnitSkillExp = 0 + unitData.UnitSkillLevel = 0 + unitData.FavoriteFlag = false + unitData.IsRankMax = false + unitData.IsLevelMax = false + unitData.IsLoveMax = false + unitData.IsSkillLevelMax = false + unitData.IsRemovableSkillCapacityMax = false + case 2: + // R + unitData.Exp = 13500 + unitData.Level = 60 + unitData.MaxLevel = 60 + unitData.Love = 200 + unitData.MaxLove = 200 + unitData.UnitSkillExp = 490 + case 3: + // SR + unitData.Exp = 36800 + unitData.Level = 80 + unitData.MaxLevel = 80 + unitData.Love = 500 + unitData.MaxLove = 500 + unitData.UnitSkillExp = 4900 + case 5: + // SSR + unitData.Exp = 56657 + unitData.Level = 90 + unitData.MaxLevel = 90 + unitData.Love = 750 + unitData.MaxLove = 750 + unitData.UnitSkillExp = 12700 + } + } else { + // UR + unitData.Exp = 79700 + unitData.Level = 100 + unitData.MaxLevel = 100 + unitData.LevelLimitID = 2 + unitData.Love = 1000 + unitData.MaxLove = 1000 + unitData.UnitSkillExp = 29900 + unitData.UnitRemovableSkillCapacity = 8 + + stmt, err := MainEng.DB().Prepare("SELECT COUNT(*) AS ct FROM unit_sign_asset_m WHERE unit_id = ?") + CheckErr(err) + defer stmt.Close() + + var ct int + err = stmt.QueryRow(v.UnitID).Scan(&ct) + CheckErr(err) + + if ct > 0 { + unitData.IsSigned = true + } else { + unitData.IsSigned = false + } + } + + _, err = MainEng.Table("common_unit_m").Insert(&unitData) + CheckErr(err) + // fmt.Println("Inserted ID:", unitData.UnitOwningUserID) + + // 默认卡组 + if v.AlbumSeriesID == 615 { // 仆光套 + unitDeckData := UnitDeckData{ + UserDeckID: userDeckId, + UnitOwningUserID: unitData.UnitOwningUserID, + UnitID: v.UnitID, + Position: position, + Level: 100, + LevelLimitID: 2, + DisplayRank: 2, + Love: 1000, + UnitSkillLevel: 8, + IsRankMax: true, + IsLoveMax: true, + IsLevelMax: true, + IsSigned: unitData.IsSigned, + BeforeLove: 1000, + MaxLove: 1000, + InsertData: time.Now().Unix(), + } + + // 入库 + _, err = UserEng.Table("deck_unit_m").Insert(&unitDeckData) + CheckErr(err) + deckUnitId := unitDeckData.ID + fmt.Println("deckUnitId:", deckUnitId) + + // + position++ + } + } +} + +func GenCommonUnitData3() { + uId := 1681205441 // Hardcode for now + userDeck := []UserDeckData{} + err := UserEng.Table("user_deck_m").Where("user_id = ?", uId).Asc("deck_id").Find(&userDeck) + CheckErr(err) + + unitDeckInfo := []model.UnitDeckInfo{} + for _, deck := range userDeck { + deckUnit := []UnitDeckData{} + err = UserEng.Table("deck_unit_m").Where("user_deck_id = ?", deck.ID).Asc("position").Find(&deckUnit) + CheckErr(err) + + oUids := []model.UnitOwningUserIds{} + for _, unit := range deckUnit { + oUids = append(oUids, model.UnitOwningUserIds{ + Position: unit.Position, + UnitOwningUserID: unit.UnitOwningUserID, + }) + } + + mainFlag := false + if deck.MainFlag == 1 { + mainFlag = true + } + unitDeckInfo = append(unitDeckInfo, model.UnitDeckInfo{ + UnitDeckID: deck.DeckID, + MainFlag: mainFlag, + DeckName: deck.DeckName, + UnitOwningUserIds: oUids, + }) + } + + b, err := json.Marshal(unitDeckInfo) + CheckErr(err) + fmt.Println(string(b)) +} + +func GenCommonUnitData() { +} diff --git a/tools/gen.go b/tools/gen.go index c731577..0587116 100644 --- a/tools/gen.go +++ b/tools/gen.go @@ -3,7 +3,6 @@ package tools import ( "encoding/json" "fmt" - "honoka-chan/database" "honoka-chan/model" "honoka-chan/utils" "strings" @@ -149,7 +148,7 @@ type ProfileResp struct { } var ( - CenterId int + CenterId = 41674 ) func GenApi1Data() { @@ -242,144 +241,20 @@ func GenApi1Data() { respAll = append(respAll, liveListResp) // unit_list_result - sql = `SELECT unit_id,album_series_id,rarity,rank_min,rank_max,hp_max,default_removable_skill_capacity FROM unit_m WHERE unit_id NOT IN (SELECT unit_id FROM unit_m WHERE unit_type_id IN (SELECT unit_type_id FROM unit_type_m WHERE image_button_asset IS NULL AND (background_color = 'dcdbe3' AND original_attribute_id IS NULL) OR (unit_type_id IN (10,110,127,128,129) OR unit_type_id BETWEEN 131 AND 140))) ORDER BY unit_number ASC;` - rows, err = MainEng.DB().Query(sql) - CheckErr(err) - unitsData := []model.Active{} - oId := 3000000000 // 起始卡片 ID,随意设置 - sdt := time.Now().Add(-time.Hour * 24 * 30) - - defaultDeckUnitList := []model.PlayRewardUnitList{} - - for rows.Next() { - unitData := model.Active{} - var uid, rit, rank, max_rank, hp_max, skill_capacity int - var album_series_id interface{} - err = rows.Scan(&uid, &album_series_id, &rit, &rank, &max_rank, &hp_max, &skill_capacity) - if err != nil { - fmt.Println(err) - continue - } - oId++ - sdt = sdt.Add(time.Second * 3) - - unitData.UnitOwningUserID = oId - unitData.UnitID = uid - unitData.NextExp = 0 - unitData.Rank = rank - unitData.MaxRank = max_rank - unitData.MaxHp = hp_max - unitData.DisplayRank = 2 - unitData.UnitSkillLevel = 8 - unitData.FavoriteFlag = true - unitData.IsRankMax = true - unitData.IsLevelMax = true - unitData.IsLoveMax = true - unitData.IsSkillLevelMax = true - unitData.IsRemovableSkillCapacityMax = true - unitData.InsertDate = sdt.Local().Format("2006-01-02 03:04:05") - if rit != 4 { - unitData.LevelLimitID = 0 - unitData.UnitRemovableSkillCapacity = skill_capacity - unitData.IsSigned = false - switch rit { - case 1: - // N - unitData.Exp = 8000 - unitData.Level = 40 - unitData.MaxLevel = 40 - unitData.Love = 50 - unitData.MaxLove = 50 - unitData.UnitSkillExp = 0 - unitData.UnitSkillLevel = 0 - unitData.FavoriteFlag = false - unitData.IsRankMax = false - unitData.IsLevelMax = false - unitData.IsLoveMax = false - unitData.IsSkillLevelMax = false - unitData.IsRemovableSkillCapacityMax = false - case 2: - // R - unitData.Exp = 13500 - unitData.Level = 60 - unitData.MaxLevel = 60 - unitData.Love = 200 - unitData.MaxLove = 200 - unitData.UnitSkillExp = 490 - case 3: - // SR - unitData.Exp = 36800 - unitData.Level = 80 - unitData.MaxLevel = 80 - unitData.Love = 500 - unitData.MaxLove = 500 - unitData.UnitSkillExp = 4900 - case 5: - // SSR - unitData.Exp = 56657 - unitData.Level = 90 - unitData.MaxLevel = 90 - unitData.Love = 750 - unitData.MaxLove = 750 - unitData.UnitSkillExp = 12700 - } - } else { - // UR - unitData.Exp = 79700 - unitData.Level = 100 - unitData.MaxLevel = 100 - unitData.LevelLimitID = 2 - unitData.Love = 1000 - unitData.MaxLove = 1000 - unitData.UnitSkillExp = 29900 - unitData.UnitRemovableSkillCapacity = 8 - - stmt, err := MainEng.DB().Prepare("SELECT COUNT(*) AS ct FROM unit_sign_asset_m WHERE unit_id = ?") - CheckErr(err) - defer stmt.Close() - - var count int - err = stmt.QueryRow(uid).Scan(&count) - CheckErr(err) - - if count > 0 { - unitData.IsSigned = true - } else { - unitData.IsSigned = false - } - } - - unitsData = append(unitsData, unitData) - - // 中心位成员 - if uid == 3927 { // AC15 果 - CenterId = oId - } - - switch album_series_id := album_series_id.(type) { - case int64: - if album_series_id == 615 { - // 仆光套 - defaultDeckUnitList = append(defaultDeckUnitList, model.PlayRewardUnitList{ - UnitOwningUserID: oId, - UnitID: uid, - Level: 100, - LevelLimitID: 2, - DisplayRank: 2, - Love: 1000, - UnitSkillLevel: 8, - IsRankMax: true, - IsLoveMax: true, - IsLevelMax: true, - IsSigned: unitData.IsSigned, - BeforeLove: 1000, - MaxLove: 1000, - }) - } - } + err = MainEng.Table("common_unit_m").Select("*").Find(&unitsData) + if err != nil { + panic(err) } + userUnits := []model.Active{} + err = UserEng.Table("user_unit_m").Select("*").Find(&userUnits) + if err != nil { + panic(err) + } + + unitsData = append(unitsData, userUnits...) + unitListResp := model.UnitAllResp{ // _ = model.UnitAllResp{ Result: model.UnitAllResult{ @@ -393,37 +268,45 @@ func GenApi1Data() { respAll = append(respAll, unitListResp) // unit_deck_result - oUids := []model.UnitOwningUserIds{} - for k, v := range defaultDeckUnitList[0:9] { - position := k + 1 - defaultDeckUnitList[k].Position = position - oUids = append(oUids, model.UnitOwningUserIds{ - Position: position, - UnitOwningUserID: v.UnitOwningUserID, + uId := 1681205441 // Hardcode for now + userDeck := []UserDeckData{} + err = UserEng.Table("user_deck_m").Where("user_id = ?", uId).Asc("deck_id").Find(&userDeck) + CheckErr(err) + + unitDeckInfo := []model.UnitDeckInfo{} + for _, deck := range userDeck { + deckUnit := []UnitDeckData{} + err = UserEng.Table("deck_unit_m").Where("user_deck_id = ?", deck.ID).Asc("position").Find(&deckUnit) + CheckErr(err) + + oUids := []model.UnitOwningUserIds{} + for _, unit := range deckUnit { + oUids = append(oUids, model.UnitOwningUserIds{ + Position: unit.Position, + UnitOwningUserID: unit.UnitOwningUserID, + }) + } + + mainFlag := false + if deck.MainFlag == 1 { + mainFlag = true + } + unitDeckInfo = append(unitDeckInfo, model.UnitDeckInfo{ + UnitDeckID: deck.DeckID, + MainFlag: mainFlag, + DeckName: deck.DeckName, + UnitOwningUserIds: oUids, }) } unitDeckResp := model.UnitDeckInfoResp{ // _ = model.UnitDeckInfoResp{ - Result: []model.UnitDeckInfo{ - { - UnitDeckID: 1, - MainFlag: true, - DeckName: "Future style", - UnitOwningUserIds: oUids, - }, - }, + Result: unitDeckInfo, Status: 200, CommandNum: false, TimeStamp: time.Now().Unix(), } respAll = append(respAll, unitDeckResp) - // HACK 保存卡组信息 - deck, err := json.Marshal(defaultDeckUnitList) - CheckErr(err) - err = database.LevelDb.Put([]byte("deck_info"), deck) - CheckErr(err) - // unit_support_result unitSupportResp := model.UnitSupportResp{ // _ = model.UnitSupportResp{ @@ -969,7 +852,7 @@ func GenApi2Data() { FriendVarietyCnt: 0, FriendNewCnt: 0, PresentCnt: 0, - SecretBoxBadgeFlag: true, + SecretBoxBadgeFlag: false, ServerDatetime: time.Now().Format("2006-01-02 15:04:05"), ServerTimestamp: time.Now().Unix(), NoticeFriendDatetime: time.Now().Format("2006-01-02 15:04:05"), diff --git a/tools/init.go b/tools/init.go new file mode 100644 index 0000000..a6f0f3d --- /dev/null +++ b/tools/init.go @@ -0,0 +1,151 @@ +package tools + +import ( + "fmt" + "time" +) + +type UserData struct { + ID int `xorm:"id pk autoincr"` + Phone string `xorm:"phone"` + Password string `xorm:"password"` + Autokey string `xorm:"autokey"` + Ticket string `xorm:"ticket"` + UserID int `xorm:"userid"` + LastLoginTime int64 `xorm:"last_login_time"` +} + +type UserPref struct { + ID int `xorm:"id pk autoincr"` + UserID int `xorm:"user_id"` + AwardID int `xorm:"award_id"` + BackgroundID int `xorm:"background_id"` + UnitOwningUserID int `xorm:"unit_owning_user_id"` + UserName string `xorm:"user_name"` + UserLevel int `xorm:"user_level"` + UserDesc string `xorm:"user_desc"` + UpdateTime int64 `xorm:"update_time"` +} + +func InitUserData(userId int) { + userList := []UserData{} + if userId != 0 { + err := UserEng.Table("users").Where("userid = ?", userId).Find(&userList) + CheckErr(err) + } else { + err := UserEng.Table("users").Asc("id").Find(&userList) + CheckErr(err) + } + + session := UserEng.NewSession() + defer session.Close() + + for _, user := range userList { + // 检查用户配置 + exists, err := UserEng.Table("user_preference_m").Where("user_id = ?", user.UserID).Exist() + CheckErr(err) + + if !exists { + // 默认中心成员() + var oId int + _, err = MainEng.Table("common_unit_m").Cols("unit_owning_user_id").Where("unit_id = ?", 31).Get(&oId) + CheckErr(err) + fmt.Println("Center UnitOwningUserID:", oId) + userPref := UserPref{ + UserID: user.UserID, + AwardID: 1, // 音乃木坂学生 + BackgroundID: 1, // 初始背景 + UnitOwningUserID: oId, + UserName: "音乃木坂学生", + UserLevel: 1, + UserDesc: "你好。", + UpdateTime: time.Now().Unix(), + } + _, err = UserEng.Table("user_preference_m").Insert(&userPref) + CheckErr(err) + fmt.Println("UserPref ID", userPref.ID) + } else { + userPref := UserPref{} + _, err = UserEng.Table("user_preference_m").Where("user_id = ?", user.UserID).Get(&userPref) + CheckErr(err) + } + + // 检查用户卡组配置 + exists, err = UserEng.Table("user_deck_m").Where("user_id = ?", user.UserID).Asc("deck_id").Exist() + CheckErr(err) + fmt.Println("UserDeck exists:", exists) + + if !exists { + userDeck := UserDeckData{ + DeckID: 1, + MainFlag: 1, + DeckName: "队伍A", + UserID: user.UserID, + InsertDate: time.Now().Unix(), + } + + if err = session.Begin(); err != nil { + panic(err) + } + + // 默认队伍 + _, err = session.Table("user_deck_m").Insert(&userDeck) + if err != nil { + session.Rollback() + panic(err) + } + userDeckId := userDeck.ID + fmt.Println("New UserDeck:", userDeckId) + + // 默认卡组 + unitIds := []int{} + err = MainEng.Table("unit_m").Cols("unit_id").Where("album_series_id = ?", 615).Find(&unitIds) + if err != nil { + session.Rollback() + panic(err) + } + + unitData := []UnitData{} + err = MainEng.Table("common_unit_m").In("unit_id", unitIds).Find(&unitData) + if err != nil { + session.Rollback() + panic(err) + } + // fmt.Println(unitData) + + position := 1 + for _, unit := range unitData { + unitDeckData := UnitDeckData{ + UserDeckID: userDeckId, + UnitOwningUserID: unit.UnitOwningUserID, + UnitID: unit.UnitID, + Position: position, + Level: 100, + LevelLimitID: 2, + DisplayRank: 2, + Love: 1000, + UnitSkillLevel: 8, + IsRankMax: true, + IsLoveMax: true, + IsLevelMax: true, + IsSigned: unit.IsSigned, + BeforeLove: 1000, + MaxLove: 1000, + InsertData: time.Now().Unix(), + } + _, err = session.Table("deck_unit_m").Insert(&unitDeckData) + if err != nil { + session.Rollback() + panic(err) + } + fmt.Println("New DeckUnit:", unitDeckData.ID) + + position++ + } + + if err = session.Commit(); err != nil { + panic(err) + } + } + } +} diff --git a/tools/tools.go b/tools/tools.go index e9b4e25..56877f4 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -15,15 +15,17 @@ func init() { MainEng = config.MainEng UserEng = config.UserEng - GenApi1Data() - GenApi2Data() - GenApi3Data() - LoadApi1Data("assets/api1.json") - LoadApi2Data("assets/api2.json") - LoadApi3Data("assets/api3.json") + // GenApi1Data() + // GenApi2Data() + // GenApi3Data() + // LoadApi1Data("assets/api1.json") + // LoadApi2Data("assets/api2.json") + // LoadApi3Data("assets/api3.json") // ListUnitData() // go SyncNotesList() // GenDownloadDb() + // GenCommonUnitData() + InitUserData(0) } func CheckErr(err error) {