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
+53
View File
@@ -0,0 +1,53 @@
package session
import (
accessorymodel "honoka-chan/internal/model/accessory"
usermodel "honoka-chan/internal/model/user"
)
func (ss *Session) GetUserAccessoryWearByUnitOwningUserID(unitOwningUserID int) (bool, *usermodel.UserAccessoryWear) {
wearData := usermodel.UserAccessoryWear{}
has, err := ss.UserEng.Table(new(usermodel.UserAccessoryWear)).
Where("unit_owning_user_id = ?", unitOwningUserID).Get(&wearData)
if ss.CheckErr(err) {
return false, nil
}
return has, &wearData
}
func (ss *Session) GetAccessoryByAccessoryOwningUserID(accessoryOwningUserID int) (bool, *accessorymodel.Accessory) {
accessoryData := accessorymodel.Accessory{}
has, err := ss.MainEng.Table("common_accessory_m").Alias("a").
Join("LEFT", "accessory_m", "a.accessory_id = accessory_m.accessory_id").
Where("a.accessory_owning_user_id = ?", accessoryOwningUserID).
Cols("accessory_m.*,a.exp").Get(&accessoryData)
if ss.CheckErr(err) {
return false, nil
}
return has, &accessoryData
}
func (ss *Session) GetUserAccessoryInfoByUnitOwningUserID(unitOwningUserID int) (bool, *usermodel.AccessoryInfo) {
has, wearData := ss.GetUserAccessoryWearByUnitOwningUserID(unitOwningUserID)
if !has {
return false, nil
}
has, accessoryData := ss.GetAccessoryByAccessoryOwningUserID(wearData.AccessoryOwningUserID)
if !has {
return false, nil
}
return has, &usermodel.AccessoryInfo{
AccessoryOwningUserID: wearData.AccessoryOwningUserID,
AccessoryID: accessoryData.AccessoryID,
Exp: accessoryData.Exp,
NextExp: 0,
Level: accessoryData.MaxLevel,
MaxLevel: accessoryData.MaxLevel,
RankUpCount: accessoryData.MaxLevel - accessoryData.DefaultMaxLevel,
FavoriteFlag: true,
}
}
+32
View File
@@ -0,0 +1,32 @@
package session
import (
usermodel "honoka-chan/internal/model/user"
)
func (ss *Session) GetUserDeck(deckID int) (bool, *usermodel.UserDeck) {
deckData := usermodel.UserDeck{}
has, err := ss.UserEng.Table("user_deck").
Where("user_id = ? AND deck_id = ?", ss.UserID, deckID).Get(&deckData)
if ss.CheckErr(err) {
return false, nil
}
return has, &deckData
}
func (ss *Session) GetUserDeckUnit(deckID int) []usermodel.UserDeckUnit {
has, deckData := ss.GetUserDeck(deckID)
if !has {
return []usermodel.UserDeckUnit{}
}
unitData := []usermodel.UserDeckUnit{}
err := ss.UserEng.Table("user_deck_unit").
Where("user_deck_id = ?", deckData.ID).Find(&unitData)
if ss.CheckErr(err) {
return []usermodel.UserDeckUnit{}
}
return unitData
}
+39
View File
@@ -0,0 +1,39 @@
package session
import (
ghomemodel "honoka-chan/internal/model/ghome"
)
func (ss *Session) GetDeviceID() string {
return ss.Ctx.Request.Header.Get("X-DEVICEID")
}
func (ss *Session) GetRandKey() []byte {
deviceKey := ghomemodel.DeviceKey{}
has, err := ss.UserEng.Table(new(ghomemodel.DeviceKey)).
Where("device_id = ?", ss.GetDeviceID()).Get(&deviceKey)
if ss.CheckErr(err) {
return nil
}
if !has {
return nil
}
return []byte(deviceKey.RandKey)
}
func (ss *Session) SetRandKey(key string) {
var err error
if ss.GetRandKey() == nil {
_, err = ss.UserEng.Insert(&ghomemodel.DeviceKey{
DeviceID: ss.GetDeviceID(),
RandKey: key,
})
} else {
_, err = ss.UserEng.Table(new(ghomemodel.DeviceKey)).
Where("device_id = ?", ss.GetDeviceID()).Update(&ghomemodel.DeviceKey{RandKey: key})
}
if ss.CheckErr(err) {
return
}
}
+215
View File
@@ -0,0 +1,215 @@
package session
import (
"encoding/json"
"honoka-chan/internal/constant"
usermodel "honoka-chan/internal/model/user"
liveschema "honoka-chan/internal/schema/live"
"honoka-chan/pkg/utils"
)
// Move from LevelDB to SQLite
// https://github.com/YumeMichi/honoka-chan/blob/a749efad9fd0789668dedcb59948248da369d32c/handler/live.go#L554
// https://github.com/DarkEnergyProcessor/NPPS4/blob/29aaba6e7a3b4b80d414e58f4b511a9627cb0e24/npps4/system/live.py#L360
func (ss *Session) GetLiveInProgress() (bool, *usermodel.UserLiveInProgress) {
progress := usermodel.UserLiveInProgress{}
has, err := ss.UserEng.Table(new(usermodel.UserLiveInProgress)).
Where("user_id = ?", ss.UserID).Get(&progress)
if ss.CheckErr(err) {
return false, nil
}
return has, &progress
}
// Move from LevelDB to SQLite
// https://github.com/YumeMichi/honoka-chan/blob/a749efad9fd0789668dedcb59948248da369d32c/handler/live.go#L45
// https://github.com/DarkEnergyProcessor/NPPS4/blob/29aaba6e7a3b4b80d414e58f4b511a9627cb0e24/npps4/system/live.py#L372
func (ss *Session) RegisterLiveInProgress(deckID int) {
var err error
has, progress := ss.GetLiveInProgress()
if has {
progress.DeckID = deckID
_, err = ss.UserEng.Table(new(usermodel.UserLiveInProgress)).ID(progress.ID).Update(progress)
} else {
progress = &usermodel.UserLiveInProgress{
DeckID: deckID,
UserID: ss.UserID,
}
_, err = ss.UserEng.Table(new(usermodel.UserLiveInProgress)).Insert(progress)
}
if ss.CheckErr(err) {
return
}
}
// Move from LevelDB to SQLite
// https://github.com/YumeMichi/honoka-chan/blob/a749efad9fd0789668dedcb59948248da369d32c/handler/live.go#L554
// https://github.com/DarkEnergyProcessor/NPPS4/blob/29aaba6e7a3b4b80d414e58f4b511a9627cb0e24/npps4/system/live.py#L366
func (ss *Session) ClearLiveInProgress() {
_, err := ss.UserEng.Table(new(usermodel.UserLiveInProgress)).Where("user_id = ?", ss.UserID).Delete()
if ss.CheckErr(err) {
return
}
}
func (ss *Session) GetLiveInfo(LiveDifficultyID int) (bool, *liveschema.LiveInfo) {
var info struct {
LiveDifficultyID int `xorm:"live_difficulty_id"`
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"`
ARankCombo int `xorm:"a_rank_combo"`
BRankCombo int `xorm:"b_rank_combo"`
CRankCombo int `xorm:"c_rank_combo"`
SRankCombo int `xorm:"s_rank_combo"`
AcFlag int `xorm:"ac_flag"`
SwingFlag int `xorm:"swing_flag"`
}
has, err := ss.MainEng.Table("special_live_m").Alias("a").
Join("LEFT", "live_setting_m", "a.live_setting_id = live_setting_m.live_setting_id").
Where("live_difficulty_id = ?", LiveDifficultyID).
Cols("a.live_difficulty_id,live_setting_m.*").Get(&info)
if ss.CheckErr(err) {
return false, nil
}
if !has {
has, err = ss.MainEng.Table("special_live_m").Alias("a").
Join("LEFT", "live_setting_m", "a.live_setting_id = live_setting_m.live_setting_id").
Where("live_difficulty_id = ?", LiveDifficultyID).
Cols("a.live_difficulty_id,live_setting_m.*").Get(&info)
if ss.CheckErr(err) {
return false, nil
}
}
var notesList []liveschema.NotesList
if has {
noteData := utils.ReadAllText("./assets/serverdata/beatmaps/" + info.NotesSettingAsset)
err = json.Unmarshal([]byte(noteData), &notesList)
if ss.CheckErr(err) {
return false, nil
}
}
return has, &liveschema.LiveInfo{
LiveDifficultyID: info.LiveDifficultyID,
IsRandom: false,
ARankScore: info.ARankScore,
BRankScore: info.BRankScore,
CRankScore: info.CRankScore,
SRankScore: info.SRankScore,
ARankCombo: info.ARankCombo,
BRankCombo: info.BRankCombo,
CRankCombo: info.CRankCombo,
SRankCombo: info.SRankCombo,
AcFlag: info.AcFlag,
SwingFlag: info.SwingFlag,
NotesList: notesList,
}
}
func (ss *Session) GetDeckInfo(deckID int) (bool, *usermodel.DeckInfo) {
// 卡片信息
totalHp, totalSmile, totalCute, totalCool := 0, 0, 0, 0
unitData := []usermodel.UnitList{}
units := ss.GetUserDeckUnit(deckID)
for _, u := range units {
_, uData := ss.GetUnitInfo(u.UnitID)
tempUnitData := usermodel.UnitList{
UnitID: u.UnitID,
Position: u.Position,
Level: u.Level,
LevelLimitID: u.LevelLimitID,
DisplayRank: u.DisplayRank,
Love: u.MaxLove,
UnitSkillLevel: u.UnitSkillLevel,
IsRankMax: u.IsRankMax,
IsLoveMax: u.IsLoveMax,
IsLevelMax: u.IsLevelMax,
IsSigned: u.IsSigned,
Rank: uData.Rank,
MaxHp: uData.MaxHp,
UnitRemovableSkillCapacity: uData.UnitRemovableSkillCapacity,
TotalStatus: usermodel.TotalStatus{
Hp: uData.MaxHp,
Smile: uData.Smile,
Cute: uData.Cute,
Cool: uData.Cool,
},
SiBonus: usermodel.SiBonus{
Hp: 0,
Smile: 0,
Cute: 0,
Cool: 0,
},
}
// 技能宝石信息
skillData := ss.GetUserUnitSkillEquipID(u.UnitOwningUserID)
tempUnitData.RemovableSkillIds = skillData
// 饰品信息
has, accessoryData := ss.GetUserAccessoryInfoByUnitOwningUserID(u.UnitOwningUserID)
if has {
tempUnitData.AccessoryInfo = accessoryData
}
// 其他属性
totalHp += uData.MaxHp
totalSmile += uData.Smile
totalCute += uData.Cute
totalCool += uData.Cool
unitData = append(unitData, tempUnitData)
}
return true, &usermodel.DeckInfo{
TotalStatus: usermodel.TotalStatus{
Hp: totalHp,
Smile: totalSmile,
Cute: totalCute,
Cool: totalCool,
},
CenterBonus: usermodel.CenterBonus{ // TODO: 计算C位加成
Hp: 0,
Smile: 0,
Cute: 0,
Cool: 0,
},
SiBonus: usermodel.SiBonus{
Hp: 0,
Smile: 0,
Cute: 0,
Cool: 0,
},
UnitList: unitData,
}
}
func (ss *Session) GetLiveGoalList(LiveDifficultyID int) []constant.LiveGoalType {
var goal []constant.LiveGoalType
err := ss.MainEng.Table("live_goal_reward_m").
Where("live_difficulty_id = ?", LiveDifficultyID).Cols("live_goal_type").Find(&goal)
if ss.CheckErr(err) {
return []constant.LiveGoalType{}
}
return goal
}
func (ss *Session) GetUserLiveGoalList(LiveDifficultyID int) []constant.LiveGoalType {
var goal []constant.LiveGoalType
var goalData []usermodel.UserLiveGoal
err := ss.UserEng.Table(new(usermodel.UserLiveGoal)).
Where("user_id = ? AND live_difficulty_id = ?", ss.UserID, LiveDifficultyID).Find(&goalData)
if ss.CheckErr(err) {
return []constant.LiveGoalType{}
}
for _, g := range goalData {
goal = append(goal, g.GoalType)
}
return goal
}
+21
View File
@@ -0,0 +1,21 @@
package session
import loginmodel "honoka-chan/internal/model/login"
func (ss *Session) GetAuthKey(token string) (bool, *loginmodel.AuthKey) {
authKeyData := loginmodel.AuthKey{}
has, err := ss.UserEng.Table(new(loginmodel.AuthKey)).
Where("authorize_token = ?", token).Get(&authKeyData)
if ss.CheckErr(err) {
return false, nil
}
return has, &authKeyData
}
func (ss *Session) SetAuthKey(authKey *loginmodel.AuthKey) {
_, err := ss.UserEng.Insert(authKey)
if ss.CheckErr(err) {
return
}
}
+3 -23
View File
@@ -2,8 +2,7 @@ package session
import (
"encoding/json"
"errors"
"honoka-chan/internal/model/user"
usermodel "honoka-chan/internal/model/user"
"honoka-chan/internal/utils"
"honoka-chan/pkg/db"
"log"
@@ -19,7 +18,7 @@ type Session struct {
UserEng *xorm.Session
UserID int
UserPref user.UserPref
UserPref usermodel.UserPref
}
func New(ctx *gin.Context) *Session {
@@ -33,14 +32,7 @@ func New(ctx *gin.Context) *Session {
userID := ctx.GetString("userid")
if userID != "" {
exist, err := ss.UserEng.Table("user_pref").Where("user_id = ?", userID).Get(&ss.UserPref)
if ss.CheckErr(err) {
return nil
}
if !exist {
ss.Abort(errors.New("user not exist!"))
return nil
}
ss.UserPref = ss.GetUserPref(userID)
ss.UserID = ss.UserPref.UserID
}
@@ -77,18 +69,6 @@ func (ss *Session) CheckErr(err error) bool {
return false
}
func (ss *Session) GetDeviceID() string {
return ss.Ctx.Request.Header.Get("X-DEVICEID")
}
func (ss *Session) GetRandKey() []byte {
key, err := db.Ldb.Get([]byte(ss.GetDeviceID()))
if ss.CheckErr(err) {
return nil
}
return key
}
func (ss *Session) Respond(resp any) {
data, err := json.Marshal(resp)
if ss.CheckErr(err) {
+67
View File
@@ -0,0 +1,67 @@
package session
import (
unitmodel "honoka-chan/internal/model/unit"
usermodel "honoka-chan/internal/model/user"
"xorm.io/xorm"
)
func (ss *Session) GetBasicUnitInfo() *xorm.Session {
return ss.UserEng.Table("user_unit_data").Alias("a").
Join("LEFT", "common_unit_data", "a.unit_id = common_unit_data.unit_id").
Cols(`
a.unit_owning_user_id,
a.favorite_flag,
a.display_rank,
common_unit_data.*
`)
}
func (ss *Session) GetUnitInfo(unitID int) (bool, *unitmodel.CommonUnitData) {
unitInfo := unitmodel.CommonUnitData{}
has, err := ss.UserEng.Table(new(unitmodel.CommonUnitData)).
Where("unit_id = ?", unitID).Get(&unitInfo)
if ss.CheckErr(err) {
return false, nil
}
return has, &unitInfo
}
func (ss *Session) GetUserUnitInfo(unitOwningUserID int) (bool, *unitmodel.CommonUnitData) {
var unitID int
has, err := ss.UserEng.Table(new(usermodel.UserUnitData)).
Where("unit_owning_user_id = ?", unitOwningUserID).
Cols("unit_id").Get(&unitID)
if ss.CheckErr(err) {
return false, nil
}
if !has {
return false, nil
}
return ss.GetUnitInfo(unitID)
}
func (ss *Session) GetUserUnitSkillEquip(unitOwningUserID int) []usermodel.UserUnitSkillEquip {
skill := []usermodel.UserUnitSkillEquip{}
err := ss.UserEng.Table("user_unit_skill_equip").
Where("unit_owning_user_id = ?", unitOwningUserID).Find(&skill)
if ss.CheckErr(err) {
return []usermodel.UserUnitSkillEquip{}
}
return skill
}
func (ss *Session) GetUserUnitSkillEquipID(unitOwningUserID int) []int {
skillID := []int{}
skill := ss.GetUserUnitSkillEquip(unitOwningUserID)
for _, s := range skill {
skillID = append(skillID, s.UnitRemovableSkillID)
}
return skillID
}
+132
View File
@@ -0,0 +1,132 @@
package session
import (
"errors"
"honoka-chan/internal/constant"
usermodel "honoka-chan/internal/model/user"
userschema "honoka-chan/internal/schema/user"
)
func (ss *Session) GetUserPref(userID string) usermodel.UserPref {
pref := usermodel.UserPref{}
has, err := ss.UserEng.Table(new(usermodel.UserPref)).
Where("user_id = ?", userID).Get(&pref)
if ss.CheckErr(err) {
return usermodel.UserPref{}
}
if !has {
ss.Abort(errors.New("用户不存在!"))
return usermodel.UserPref{}
}
return pref
}
func (ss *Session) GetUserInfo() userschema.UserInfo {
return userschema.UserInfo{
UserID: ss.UserID,
Name: ss.UserPref.UserName,
Level: ss.UserPref.UserLevel,
Exp: 1089696,
PreviousExp: 0,
NextExp: 1207185,
GameCoin: 112124104,
SnsCoin: 10000,
FreeSnsCoin: 50000,
PaidSnsCoin: 50000,
SocialPoint: 1438165,
UnitMax: 5000,
WaitingUnitMax: 5000,
CurrentEnergy: 417,
EnergyMax: 417,
EnergyFullTime: "2023-03-20 01:28:55",
LicenseLiveEnergyRecoverlyTime: 60,
EnergyFullNeedTime: 0,
OverMaxEnergy: 417,
TrainingEnergy: 10,
TrainingEnergyMax: 10,
FriendMax: 99,
InviteCode: ss.UserPref.InviteCode,
InsertDate: "2015-08-10 18:58:30",
UpdateDate: "2018-08-09 18:13:12",
TutorialState: -1,
DiamondCoin: 0,
CrystalCoin: 0,
UnlockRandomLiveMuse: 1,
UnlockRandomLiveAqours: 1,
}
}
func (ss *Session) GetUserLiveRecord(liveDifficultyID int) []usermodel.UserLiveRecord {
liveRecord := []usermodel.UserLiveRecord{}
err := ss.UserEng.Table(new(usermodel.UserLiveRecord)).
Where("user_id = ? AND live_difficulty_id = ?",
ss.UserID, liveDifficultyID).
Find(&liveRecord)
if ss.CheckErr(err) {
return []usermodel.UserLiveRecord{}
}
return liveRecord
}
func (ss *Session) GetUserLiveRecordWithSkillOn(liveDifficultyID int, isSkillOn bool) (bool, *usermodel.UserLiveRecord) {
liveRecord := usermodel.UserLiveRecord{}
has, err := ss.UserEng.Table(new(usermodel.UserLiveRecord)).
Where("user_id = ? AND live_difficulty_id = ? AND is_skill_on = ?",
ss.UserID, liveDifficultyID, isSkillOn).
Get(&liveRecord)
if ss.CheckErr(err) {
return false, nil
}
return has, &liveRecord
}
func (ss *Session) UpdateUserLiveRecord(newLiveRecord *usermodel.UserLiveRecord, updateType constant.PreciseScoreUpdateType) {
// 获取原有记录
has, liveRecord := ss.GetUserLiveRecordWithSkillOn(newLiveRecord.LiveDifficultyID, newLiveRecord.IsSkillOn)
if !has {
// 没有记录则直接新增
ss.UpdateUserLiveRecordWrapper(newLiveRecord, false)
return
}
// 判断记录更新类型
switch updateType {
case constant.PreciseScoreUpdateTypePerfect:
// 根据 Perfect 数更新
// 判断 Perfect 数是否大于原有记录
if newLiveRecord.PerfectCnt >= liveRecord.PerfectCnt {
// 更新记录
ss.UpdateUserLiveRecordWrapper(newLiveRecord, true)
}
case constant.PreciseScoreUpdateTypeScore:
// 根据分数更新
// 判断分数是否大于原有记录
if newLiveRecord.TotalScore >= liveRecord.TotalScore {
// 更新记录
ss.UpdateUserLiveRecordWrapper(newLiveRecord, true)
}
default:
// 总是更新
ss.UpdateUserLiveRecordWrapper(newLiveRecord, true)
}
}
func (ss *Session) UpdateUserLiveRecordWrapper(newLiveRecord *usermodel.UserLiveRecord, isUpdate bool) {
if isUpdate {
_, err := ss.UserEng.Table(new(usermodel.UserLiveRecord)).
Where("user_id = ? AND live_difficulty_id = ? AND is_skill_on = ?",
ss.UserID, newLiveRecord.LiveDifficultyID, newLiveRecord.IsSkillOn).
Update(newLiveRecord)
if ss.CheckErr(err) {
return
}
} else {
_, err := ss.UserEng.Insert(newLiveRecord)
if ss.CheckErr(err) {
return
}
}
}