Implement more endpoints & overhaul [2/n]

Drop LevelDB and move all the things to SQLite.

Implemented endpoints:
/api <unit, accessoryMaterialAll>
/api <unit, accessoryTab>
/live/continue
/live/partyList
/unit/favoriteAccessory (WIP)
/unit/sale

Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
2026-04-28 11:44:56 +08:00
parent 1500821f3e
commit 150ce674ad
218 changed files with 2993 additions and 2160 deletions
+144 -155
View File
@@ -2,183 +2,157 @@ package live
import (
"encoding/json"
"fmt"
"honoka-chan/internal/middleware"
unitmodel "honoka-chan/internal/model/unit"
"honoka-chan/internal/router"
"honoka-chan/internal/schema/live"
liveschema "honoka-chan/internal/schema/live"
"honoka-chan/internal/session"
"honoka-chan/pkg/db"
honokautils "honoka-chan/pkg/utils"
"math"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
)
func play(ctx *gin.Context) {
ss := session.Get(ctx)
defer ss.Finalize()
playReq := live.PlayReq{}
playReq := liveschema.PlayReq{}
err := json.Unmarshal([]byte(ctx.MustGet("request_data").(string)), &playReq)
if ss.CheckErr(err) {
return
}
// fmt.Println(ctx.MustGet("request_data").(string))
tDifficultyId := playReq.LiveDifficultyID
difficultyId, err := strconv.Atoi(tDifficultyId)
ss.RegisterLiveInProgress(playReq.UnitDeckID)
difficultyID, _ := strconv.Atoi(playReq.LiveDifficultyID)
// 歌曲类型: normal / special
// sqlite3 不支持 FULL OUTER JOIN 所以这里使用 UNION ALL
var liveSetting struct {
NotesSettingAsset string `xorm:"notes_setting_asset"`
ARankScore int `xorm:"a_rank_score"`
BRankScore int `xorm:"b_rank_score"`
CRankScore int `xorm:"c_rank_score"`
SRankScore int `xorm:"s_rank_score"`
AcFlag int `xorm:"ac_flag"`
SwingFlag int `xorm:"swing_flag"`
}
sql := `
SELECT notes_setting_asset,
a_rank_score,
b_rank_score,
c_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 = ?
)
`
_, err = ss.MainEng.SQL(sql, difficultyID, difficultyID).Get(&liveSetting)
if ss.CheckErr(err) {
return
}
deckId := playReq.UnitDeckID
// fmt.Println("liveSetting", liveSetting)
// Save Deck Id for /live/reward
key := "live_deck_" + strconv.Itoa(ss.UserID)
err = db.Ldb.Set([]byte(key), []byte(strconv.Itoa(deckId)))
if ss.CheckErr(err) {
return
}
// 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 = ?)`
var notes_setting_asset string
var c_rank_score, b_rank_score, a_rank_score, s_rank_score, ac_flag, swing_flag int
err = ss.MainEng.DB().QueryRow(sql, difficultyId, difficultyId).Scan(&notes_setting_asset, &c_rank_score, &b_rank_score, &a_rank_score, &s_rank_score, &ac_flag, &swing_flag)
if ss.CheckErr(err) {
return
}
// fmt.Println(notes_setting_asset)
// fmt.Println(c_rank_score, b_rank_score, a_rank_score, s_rank_score)
notes := []live.NotesList{}
// fmt.Println("./assets/serverdata/beatmaps/" + notes_setting_asset)
notes_list := honokautils.ReadAllText("./assets/serverdata/beatmaps/" + notes_setting_asset)
notes := []liveschema.NotesList{}
notes_list := honokautils.ReadAllText("./assets/serverdata/beatmaps/" + liveSetting.NotesSettingAsset)
err = json.Unmarshal([]byte(notes_list), &notes)
if ss.CheckErr(err) {
return
}
ranks := []live.RankInfo{}
ranks = append(ranks, live.RankInfo{
ranks := []liveschema.RankInfo{}
ranks = append(ranks, liveschema.RankInfo{
Rank: 5,
RankMin: 0,
RankMax: c_rank_score,
}, live.RankInfo{
RankMax: liveSetting.CRankScore,
}, liveschema.RankInfo{
Rank: 4,
RankMin: c_rank_score + 1,
RankMax: b_rank_score,
}, live.RankInfo{
RankMin: liveSetting.CRankScore + 1,
RankMax: liveSetting.BRankScore,
}, liveschema.RankInfo{
Rank: 3,
RankMin: b_rank_score + 1,
RankMax: a_rank_score,
}, live.RankInfo{
RankMin: liveSetting.BRankScore + 1,
RankMax: liveSetting.ARankScore,
}, liveschema.RankInfo{
Rank: 2,
RankMin: a_rank_score + 1,
RankMax: s_rank_score,
}, live.RankInfo{
RankMin: liveSetting.ARankScore + 1,
RankMax: liveSetting.SRankScore,
}, liveschema.RankInfo{
Rank: 1,
RankMin: s_rank_score + 1,
RankMin: liveSetting.SRankScore + 1,
RankMax: 0,
})
// ss.UserEng.ShowSQL(true)
// ss.MainEng.ShowSQL(true)
owningIdList := []int{}
err = ss.UserEng.Table("user_deck_unit").Join("LEFT", "user_deck", "user_deck_unit.user_deck_id = user_deck.id").
Where("user_id = ? AND deck_id = ?", ss.UserID, deckId).Cols("unit_owning_user_id").
OrderBy("user_deck_unit.position ASC").Find(&owningIdList)
err = ss.UserEng.Table("user_deck_unit").
Join("LEFT", "user_deck", "user_deck_unit.user_deck_id = user_deck.id").
Where("user_deck.user_id = ? AND user_deck.deck_id = ?", ss.UserID, playReq.UnitDeckID).
Cols("unit_owning_user_id").OrderBy("user_deck_unit.position ASC").Find(&owningIdList)
if ss.CheckErr(err) {
return
}
unitList := []live.UnitList{}
var totalSmile, totalPure, totalCool, maxLove float64
unitList := []liveschema.UnitList{}
var totalSmile, totalPure, totalCool float64
var totalHp int
for _, owningId := range owningIdList {
var uId int
exists, err := ss.MainEng.Table("common_unit_m").Where("unit_owning_user_id = ?", owningId).Cols("unit_id").Get(&uId)
// 卡片基础属性
var baseSmile, basePure, baseCool, smileMax, pureMax, coolMax float64
var unitData unitmodel.UnitDataMap
_, err = ss.GetBasicUnitInfo().Where("unit_owning_user_id = ?", owningId).Get(&unitData)
if ss.CheckErr(err) {
return
}
var maxHp, attrId, unitTypeId int
var baseSmile, basePure, baseCool, smileMax, pureMax, coolMax float64
if exists {
// 公共卡片仅为100级属性
_, err = ss.MainEng.Table("unit_m").Where("unit_id = ?", uId).
Join("LEFT", "unit_rarity_m", "unit_m.rarity = unit_rarity_m.rarity").
Cols("attribute_id,hp_max,smile_max,pure_max,cool_max,after_love_max,unit_type_id").
Get(&attrId, &maxHp, &baseSmile, &basePure, &baseCool, &maxLove, &unitTypeId)
if ss.CheckErr(err) {
return
}
} else {
// 用户卡片暂时固定为满级350级
exists, err := ss.UserEng.Table("user_unit").Where("unit_owning_user_id = ?", owningId).Cols("unit_id").Get(&uId)
if ss.CheckErr(err) {
return
}
if exists {
// 卡片100级基础属性
_, err = ss.MainEng.Table("unit_m").Where("unit_id = ?", uId).
Join("LEFT", "unit_rarity_m", "unit_m.rarity = unit_rarity_m.rarity").
Cols("attribute_id,hp_max,smile_max,pure_max,cool_max,after_love_max,unit_type_id").
Get(&attrId, &maxHp, &baseSmile, &basePure, &baseCool, &maxLove, &unitTypeId)
if ss.CheckErr(err) {
return
}
// 增量属性
var diffSmile, diffPure, diffCool float64
_, err = ss.MainEng.Table("unit_level_limit_pattern_m").Where("unit_level_limit_id = 1 AND unit_level = 350").
Cols("smile_diff,pure_diff,cool_diff").Get(&diffSmile, &diffPure, &diffCool)
if ss.CheckErr(err) {
return
}
// 更新卡片属性(注意这里是负数,要用减号)
baseSmile -= diffSmile
basePure -= diffPure
baseCool -= diffCool
} else {
err = fmt.Errorf("no such unit owning id: %d", owningId)
}
if ss.CheckErr(err) {
return
}
}
baseSmile = float64(unitData.Smile)
basePure = float64(unitData.Cute)
baseCool = float64(unitData.Cool)
// fmt.Println("================================")
// fmt.Println("基础属性:", baseSmile, basePure, baseCool)
// 饰品属性加成(满级)
var accessoryOwningId int
_, err = ss.UserEng.Table("user_accessory_wear").Where("unit_owning_user_id = ?", owningId).
exists, err := ss.UserEng.Table("user_accessory_wear").
Where("unit_owning_user_id = ?", owningId).
Cols("accessory_owning_user_id").Get(&accessoryOwningId)
if ss.CheckErr(err) {
return
}
var smileAccessory, pureAccessory, coolAccessory float64
_, err = ss.MainEng.Table("common_accessory_m").Join("LEFT", "accessory_m", "common_accessory_m.accessory_id = accessory_m.accessory_id").
Where("accessory_owning_user_id = ?", accessoryOwningId).Cols("smile_max,pure_max,cool_max").
Get(&smileAccessory, &pureAccessory, &coolAccessory)
if ss.CheckErr(err) {
return
}
// fmt.Println("基础属性:", baseSmile, basePure, baseCool)
if exists {
var smileAccessory, pureAccessory, coolAccessory float64
_, err = ss.MainEng.Table("common_accessory_m").
Join("LEFT", "accessory_m", "common_accessory_m.accessory_id = accessory_m.accessory_id").
Where("accessory_owning_user_id = ?", accessoryOwningId).Cols("smile_max,pure_max,cool_max").
Get(&smileAccessory, &pureAccessory, &coolAccessory)
if ss.CheckErr(err) {
return
}
// 饰品属性加成(该加成会影响个宝等百分比宝石属性加成的计算,故先计算。)
baseSmile += smileAccessory
basePure += pureAccessory
baseCool += coolAccessory
// fmt.Println("饰品属性加成:", smileAccessory, pureAccessory, coolAccessory)
// fmt.Println("饰品属性加成后的基础属性:", baseSmile, basePure, baseCool)
// 饰品属性加成(该加成会影响个宝等百分比宝石属性加成的计算,故先计算。)
baseSmile += smileAccessory
basePure += pureAccessory
baseCool += coolAccessory
// fmt.Println("饰品属性加成:", smileAccessory, pureAccessory, coolAccessory)
// fmt.Println("饰品属性加成后的基础属性:", baseSmile, basePure, baseCool)
}
// 回忆画廊属性加成(该加成会影响个宝等百分比宝石属性加成的计算,故先计算。)
var smileBuff, pureBuff, coolBuff float64
_, err = ss.MainEng.Table("museum_contents_m").Select("SUM(smile_buff),SUM(pure_buff),SUM(cool_buff)").Get(&smileBuff, &pureBuff, &coolBuff)
_, err = ss.MainEng.Table("museum_contents_m").
Select("SUM(smile_buff),SUM(pure_buff),SUM(cool_buff)").
Get(&smileBuff, &pureBuff, &coolBuff)
if ss.CheckErr(err) {
return
}
@@ -189,26 +163,25 @@ func play(ctx *gin.Context) {
// fmt.Println("回忆画廊属性加成后的基础属性:", baseSmile, basePure, baseCool)
// 绊属性加成(该加成会影响个宝等百分比宝石属性加成的计算,故先计算。)
switch attrId {
switch unitData.Attribute {
case 1:
baseSmile += maxLove
baseSmile += float64(unitData.MaxLove)
case 2:
basePure += maxLove
basePure += float64(unitData.MaxLove)
case 3:
baseCool += maxLove
baseCool += float64(unitData.MaxLove)
}
// fmt.Println("绊属性加成:", maxLove)
// fmt.Println("绊属性加成:", unitData.MaxLove)
// fmt.Println("绊属性加成后的基础属性:", baseSmile, basePure, baseCool)
// 宝石属性加成
var kissSmile, kissPure, kissCool float64
var skillSmile, skillPure, skillCool float64
// var mainCenterSmile, mainCenterPure, mainCenterCool float64
// var secCenterSmile, secCenterPure, secCenterCool float64
// 宝石加成(满级)
removableSkillIds := []int{}
err = ss.UserEng.Table("user_unit_skill_equip").Where("unit_owning_user_id = ? AND user_id = ?", owningId, ss.UserID).
err = ss.UserEng.Table("user_unit_skill_equip").
Where("unit_owning_user_id = ?", owningId).
Cols("unit_removable_skill_id").Find(&removableSkillIds)
if ss.CheckErr(err) {
return
@@ -218,7 +191,8 @@ func play(ctx *gin.Context) {
// 判断宝石效果类型(效果范围、效果类型、效果值、是否固定数值)
var effectRange, effectType, fixedValueFlag, refType int
var effectValue float64
_, err = ss.MainEng.Table("unit_removable_skill_m").Where("unit_removable_skill_id = ?", sk).
_, err = ss.MainEng.Table("unit_removable_skill_m").
Where("unit_removable_skill_id = ?", sk).
Cols("effect_range,effect_type,effect_value,fixed_value_flag,target_reference_type").
Get(&effectRange, &effectType, &effectValue, &fixedValueFlag, &refType)
if ss.CheckErr(err) {
@@ -276,7 +250,8 @@ func play(ctx *gin.Context) {
// 主唱技能加成
var myCenterUnitId int
_, err = ss.UserEng.Table("user_deck_unit").Join("LEFT", "user_deck", "user_deck_unit.user_deck_id = user_deck.id").
_, err = ss.UserEng.Table("user_deck_unit").
Join("LEFT", "user_deck", "user_deck_unit.user_deck_id = user_deck.id").
Where("user_deck.deck_id = ? AND user_deck.user_id = ? AND user_deck_unit.position = 5", playReq.UnitDeckID, ss.UserID).
Cols("user_deck_unit.unit_id").Get(&myCenterUnitId)
if ss.CheckErr(err) {
@@ -310,16 +285,18 @@ func play(ctx *gin.Context) {
_, err = ss.MainEng.Table("unit_m").
Join("LEFT", "unit_leader_skill_extra_m", "unit_m.default_leader_skill_id = unit_leader_skill_extra_m.unit_leader_skill_id").
Where("unit_m.unit_id = ?", myCenterUnitId).
Cols("unit_leader_skill_extra_m.effect_value,unit_leader_skill_extra_m.member_tag_id").Get(&mySubEffectValue, &myMemberTagId)
Cols("unit_leader_skill_extra_m.effect_value,unit_leader_skill_extra_m.member_tag_id").
Get(&mySubEffectValue, &myMemberTagId)
if ss.CheckErr(err) {
return
}
exists, err = ss.MainEng.Table("unit_type_member_tag_m").
Where("unit_type_id = ? AND member_tag_id = ?", unitTypeId, myMemberTagId).Exist()
Where("unit_type_id = ? AND member_tag_id = ?", unitData.UnitTypeID, myMemberTagId).Exist()
if ss.CheckErr(err) {
return
}
var mySubSmile, mySubPure, mySubCool float64
if exists {
switch myAttrId {
@@ -335,24 +312,26 @@ func play(ctx *gin.Context) {
// 好友主唱技能加成
// TODO 好友支援存入数据库
var tomoUnitId int64
partyList := gjson.Parse(honokautils.ReadAllText("assets/serverdata/partylist.json")).Get("response_data.party_list")
partyList.ForEach(func(key, value gjson.Result) bool {
if value.Get("user_info.user_id").Int() == playReq.PartyUserID {
tomoUnitId = value.Get("center_unit_info.unit_id").Int()
return false
}
return true
})
// var tomoUnitId int64
// partyList := gjson.Parse(honokautils.ReadAllText("assets/serverdata/partylist.json")).Get("response_data.party_list")
// partyList.ForEach(func(key, value gjson.Result) bool {
// if value.Get("user_info.user_id").Int() == playReq.PartyUserID {
// tomoUnitId = value.Get("center_unit_info.unit_id").Int()
// return false
// }
// return true
// })
// TODO: 好友功能实装前先使用自己的卡组助战
tomoUnitId := myCenterUnitId
// fmt.Println("好友UnitID:", tomoUnitId)
// 好友主唱技能加成:主C技能(这里不使用新C技能,即以某属性的百分比提升另一属性)
var tomoAttrId int
var tomoEffectValue float64
_, err = ss.MainEng.Table("unit_m").
Join("LEFT", "unit_leader_skill_m", "unit_m.default_leader_skill_id = unit_leader_skill_m.unit_leader_skill_id").
Where("unit_m.unit_id = ?", tomoUnitId).
Cols("unit_m.attribute_id,unit_leader_skill_m.effect_value").Get(&tomoAttrId, &tomoEffectValue)
Cols("unit_leader_skill_m.effect_value").
Get(&tomoEffectValue)
if ss.CheckErr(err) {
return
}
@@ -373,13 +352,14 @@ func play(ctx *gin.Context) {
_, err = ss.MainEng.Table("unit_m").
Join("LEFT", "unit_leader_skill_extra_m", "unit_m.default_leader_skill_id = unit_leader_skill_extra_m.unit_leader_skill_id").
Where("unit_m.unit_id = ?", tomoUnitId).
Cols("unit_leader_skill_extra_m.effect_value,unit_leader_skill_extra_m.member_tag_id").Get(&tomoSubEffectValue, &tomoMemberTagId)
Cols("unit_leader_skill_extra_m.effect_value,unit_leader_skill_extra_m.member_tag_id").
Get(&tomoSubEffectValue, &tomoMemberTagId)
if ss.CheckErr(err) {
return
}
exists, err = ss.MainEng.Table("unit_type_member_tag_m").
Where("unit_type_id = ? AND member_tag_id = ?", unitTypeId, tomoMemberTagId).Exist()
Where("unit_type_id = ? AND member_tag_id = ?", unitData.UnitTypeID, tomoMemberTagId).Exist()
if ss.CheckErr(err) {
return
}
@@ -400,7 +380,16 @@ func play(ctx *gin.Context) {
totalSmile += smileMax + myCenterSmile + mySubSmile + tomoCenterSmile + tomoSubSmile
totalPure += pureMax + myCenterPure + mySubPure + tomoCenterPure + tomoSubPure
totalCool += coolMax + myCenterCool + mySubCool + tomoCenterCool + tomoSubCool
totalHp += maxHp
totalHp += unitData.MaxHp
// fmt.Println("smileMax, myCenterSmile, mySubSmile, tomoCenterSmile, tomoSubSmile, totalSmile",
// smileMax, myCenterSmile, mySubSmile, tomoCenterSmile, tomoSubSmile,
// smileMax+myCenterSmile+mySubSmile+tomoCenterSmile+tomoSubSmile)
// fmt.Println("pureMax, myCenterPure, mySubPure, tomoCenterPure, tomoSubPure, totalPure",
// pureMax, myCenterPure, mySubPure, tomoCenterPure, tomoSubPure,
// pureMax+myCenterPure+mySubPure+tomoCenterPure+tomoSubPure)
// fmt.Println("coolMax, myCenterCool, mySubCool, tomoCenterCool, tomoSubCool, totalPure",
// coolMax, myCenterCool, mySubCool, tomoCenterCool, tomoSubCool,
// coolMax+myCenterCool+mySubCool+tomoCenterCool+tomoSubCool)
// 单卡属性计算结果取上取整
fixedSmileMax := int(smileMax)
@@ -408,7 +397,7 @@ func play(ctx *gin.Context) {
fixedCoolMax := int(coolMax)
// fmt.Println("单卡属性:", fixedSmileMax, fixedPureMax, fixedCoolMax)
unitList = append(unitList, live.UnitList{
unitList = append(unitList, liveschema.UnitList{
Smile: fixedSmileMax,
Cute: fixedPureMax,
Cool: fixedCoolMax,
@@ -421,17 +410,17 @@ func play(ctx *gin.Context) {
fixedTotalCool := int(math.Ceil(totalCool))
// fmt.Println("全卡组属性:", fixedTotalSmile, fixedTotalPure, fixedTotalCool)
lives := []live.PlayLiveList{}
lives = append(lives, live.PlayLiveList{
LiveInfo: live.LiveInfo{
LiveDifficultyID: difficultyId,
lives := []liveschema.PlayLiveList{}
lives = append(lives, liveschema.PlayLiveList{
LiveInfo: liveschema.LiveInfo{
LiveDifficultyID: difficultyID,
IsRandom: false,
AcFlag: ac_flag,
SwingFlag: swing_flag,
AcFlag: liveSetting.AcFlag,
SwingFlag: liveSetting.SwingFlag,
NotesList: notes,
},
DeckInfo: live.DeckInfo{
UnitDeckID: deckId,
DeckInfo: liveschema.DeckInfo{
UnitDeckID: playReq.UnitDeckID,
TotalSmile: fixedTotalSmile,
TotalCute: fixedTotalPure,
TotalCool: fixedTotalCool,
@@ -441,8 +430,8 @@ func play(ctx *gin.Context) {
},
})
playResp := live.PlayResp{
ResponseData: live.PlayData{
playResp := liveschema.PlayResp{
ResponseData: liveschema.PlayData{
RankInfo: ranks,
EnergyFullTime: "2023-03-20 01:28:55",
OverMaxEnergy: 0,