From db84731d9dbfe9a8d3e1e526554be01ec036a5a4 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Jul 2025 00:34:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=E5=B9=B6=E5=8F=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=20Best30=20=E5=92=8C=20Recent10=20=E7=9A=84=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/scoresaber/bs50.go | 59 +++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/handler/scoresaber/bs50.go b/handler/scoresaber/bs50.go index bea618b..cce9289 100644 --- a/handler/scoresaber/bs50.go +++ b/handler/scoresaber/bs50.go @@ -638,31 +638,50 @@ func getAllScoreSaberScores(playerID string) ([]SongData, error) { if err != nil { return nil, fmt.Errorf("获取Best30失败: %v", err) } - for i, score := range topScores { - songData, err := convertScoreSaberToSongData(score, cwd) - if err != nil { - log.Printf("转换分数失败: %v", err) - continue - } - songData.IsBest30 = i < 30 - allSongs = append(allSongs, songData) - } - - // 获取Recent10 recentScores, err := getRecent10Scores(playerID) if err != nil { return nil, fmt.Errorf("获取Recent10失败: %v", err) } - for _, score := range recentScores { - songData, err := convertScoreSaberToSongData(score, cwd) - if err != nil { - log.Printf("转换分数失败: %v", err) - continue + wgAll := sync.WaitGroup{} + wgAll.Add(2) + go func() { + defer wgAll.Done() + wg := sync.WaitGroup{} + for i, score := range topScores { + wg.Add(1) + go func() { + defer wg.Done() + songData, err := convertScoreSaberToSongData(score, cwd) + if err != nil { + log.Printf("转换分数失败: %v", err) + return + } + songData.IsBest30 = i < 30 + allSongs[i] = songData + }() } - songData.IsBest30 = false - allSongs = append(allSongs, songData) - } - + wg.Wait() + }() + // 获取Recent10 + go func() { + defer wgAll.Done() + wg := sync.WaitGroup{} + for _, score := range recentScores { + wg.Add(1) + go func() { + defer wg.Done() + songData, err := convertScoreSaberToSongData(score, cwd) + if err != nil { + log.Printf("转换分数失败: %v", err) + return + } + songData.IsBest30 = false + allSongs = append(allSongs, songData) + }() + wg.Wait() + } + }() + wgAll.Wait() return allSongs, nil } From 101dba4f9ebdb2904c973ff1e3650267d08216e7 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Jul 2025 02:00:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E5=B0=86=20Blackjack=20?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8=E4=B8=AD=E7=9A=84=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E4=BB=8E=20util=20=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E5=88=B0=20utiljack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/blackjack/controller/simulator.go | 22 ++++++++++---------- handler/blackjack/util/status.go | 10 --------- handler/blackjack/{util => utiljack}/dict.go | 2 +- handler/blackjack/utiljack/status.go | 10 +++++++++ util/split.go | 6 ++++++ 5 files changed, 28 insertions(+), 22 deletions(-) delete mode 100644 handler/blackjack/util/status.go rename handler/blackjack/{util => utiljack}/dict.go (90%) create mode 100644 handler/blackjack/utiljack/status.go diff --git a/handler/blackjack/controller/simulator.go b/handler/blackjack/controller/simulator.go index 8aa63c5..1f65d87 100644 --- a/handler/blackjack/controller/simulator.go +++ b/handler/blackjack/controller/simulator.go @@ -4,7 +4,7 @@ import ( "strconv" "git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/model" - "git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/util" + "git.lxtend.com/lixiangwuxian/qqbot/handler/blackjack/utiljack" ) type BackJackSimulator struct { @@ -13,7 +13,7 @@ type BackJackSimulator struct { playerCards *model.Deck dealerScore int playerScore int - status util.Status + status utiljack.Status } func NewBlackJackSimulator() *BackJackSimulator { @@ -77,13 +77,13 @@ func (simulator *BackJackSimulator) Init() *BackJackSimulator { simulator.playerCards = initPlayerCards(simulator.playerCards, simulator.deck) simulator.dealerScore, simulator.playerScore = 0, 0 - simulator.status = util.INITIALIZED + simulator.status = utiljack.INITIALIZED return simulator } func (simulator *BackJackSimulator) Hit() *BackJackResponse { - if simulator.status == util.FAILED || simulator.status == util.WINNED || simulator.status == util.DRAW { + if simulator.status == utiljack.FAILED || simulator.status == utiljack.WINNED || simulator.status == utiljack.DRAW { return NewBlackJackResponse(400, "[🐧] You have failed in this Blackjack game !\n") } @@ -96,7 +96,7 @@ func (simulator *BackJackSimulator) Hit() *BackJackResponse { } if simulator.playerScore > 21 { - simulator.status = util.FAILED + simulator.status = utiljack.FAILED return NewBlackJackResponse(400, "[🐧] Your total score exceeds 21 ! You lose !\n") } } @@ -105,7 +105,7 @@ func (simulator *BackJackSimulator) Hit() *BackJackResponse { } func (simulator *BackJackSimulator) Stand() *BackJackResponse { - if simulator.status == util.FAILED || simulator.status == util.WINNED || simulator.status == util.DRAW { + if simulator.status == utiljack.FAILED || simulator.status == utiljack.WINNED || simulator.status == utiljack.DRAW { return NewBlackJackResponse(400, "[🐧] You have failed in this Blackjack game !\n") } @@ -150,20 +150,20 @@ func (simulator *BackJackSimulator) Stand() *BackJackResponse { } if minDealerScore > 21 { - simulator.status = util.WINNED + simulator.status = utiljack.WINNED return NewBlackJackResponse(400, "[🐧] The dealer burst and you have won the game !\n") } } } if maxDealerScore > maxPlayerScore { - simulator.status = util.FAILED + simulator.status = utiljack.FAILED return NewBlackJackResponse(400, "[🐧] The dealer wins the game !\n") } else if maxDealerScore < maxPlayerScore { - simulator.status = util.WINNED + simulator.status = utiljack.WINNED return NewBlackJackResponse(400, "[🐧] You have won the game !\n") } else if maxDealerScore == maxPlayerScore { - simulator.status = util.DRAW + simulator.status = utiljack.DRAW return NewBlackJackResponse(200, "[🐧] It is a draw !\n") } @@ -178,6 +178,6 @@ func (simulator *BackJackSimulator) GetPlayerCards() *model.Deck { return simulator.playerCards } -func (simulator *BackJackSimulator) GetStatus() util.Status { +func (simulator *BackJackSimulator) GetStatus() utiljack.Status { return simulator.status } diff --git a/handler/blackjack/util/status.go b/handler/blackjack/util/status.go deleted file mode 100644 index fab2822..0000000 --- a/handler/blackjack/util/status.go +++ /dev/null @@ -1,10 +0,0 @@ -package util - -type Status int - -const ( - INITIALIZED Status = iota - WINNED Status = iota - FAILED Status = iota - DRAW Status = iota -) diff --git a/handler/blackjack/util/dict.go b/handler/blackjack/utiljack/dict.go similarity index 90% rename from handler/blackjack/util/dict.go rename to handler/blackjack/utiljack/dict.go index 04c01f4..43c772b 100644 --- a/handler/blackjack/util/dict.go +++ b/handler/blackjack/utiljack/dict.go @@ -1,4 +1,4 @@ -package util +package utiljack var Dict map[string]string diff --git a/handler/blackjack/utiljack/status.go b/handler/blackjack/utiljack/status.go new file mode 100644 index 0000000..5606ebd --- /dev/null +++ b/handler/blackjack/utiljack/status.go @@ -0,0 +1,10 @@ +package utiljack + +type Status int + +const ( + INITIALIZED Status = iota + WINNED Status = iota + FAILED Status = iota + DRAW Status = iota +) diff --git a/util/split.go b/util/split.go index c5243d6..83a9e4f 100644 --- a/util/split.go +++ b/util/split.go @@ -4,6 +4,12 @@ import ( "regexp" ) +/* +按空格分割字符串,返回分割后的字符串数组 +@param text 要分割的字符串 +@param n 分割的次数 +@return 分割后的字符串数组 +*/ func SplitN(text string, n int) []string { re := regexp.MustCompile(`\s+`) tokens := re.Split(text, n) From bffb3b897518b17ead36b26cd8be24cc6b5a0fb8 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Jul 2025 22:40:45 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0bs30=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/scoresaber/{bs50.go => bs30.go} | 4 ++++ 1 file changed, 4 insertions(+) rename handler/scoresaber/{bs50.go => bs30.go} (99%) diff --git a/handler/scoresaber/bs50.go b/handler/scoresaber/bs30.go similarity index 99% rename from handler/scoresaber/bs50.go rename to handler/scoresaber/bs30.go index cce9289..b0e688a 100644 --- a/handler/scoresaber/bs50.go +++ b/handler/scoresaber/bs30.go @@ -645,11 +645,13 @@ func getAllScoreSaberScores(playerID string) ([]SongData, error) { wgAll := sync.WaitGroup{} wgAll.Add(2) go func() { + defer util.ReportPanicToDev() defer wgAll.Done() wg := sync.WaitGroup{} for i, score := range topScores { wg.Add(1) go func() { + defer util.ReportPanicToDev() defer wg.Done() songData, err := convertScoreSaberToSongData(score, cwd) if err != nil { @@ -664,11 +666,13 @@ func getAllScoreSaberScores(playerID string) ([]SongData, error) { }() // 获取Recent10 go func() { + defer util.ReportPanicToDev() defer wgAll.Done() wg := sync.WaitGroup{} for _, score := range recentScores { wg.Add(1) go func() { + defer util.ReportPanicToDev() defer wg.Done() songData, err := convertScoreSaberToSongData(score, cwd) if err != nil { From e32f4c13feede4066819ed4f5ba4300fa694936c Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Jul 2025 22:52:49 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20getAllScoreSabe?= =?UTF-8?q?rScores=20=E5=87=BD=E6=95=B0=E4=B8=AD=E7=9A=84=20allSongs=20?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=96=B9=E5=BC=8F=EF=BC=8C=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E6=AD=A3=E7=A1=AE=E5=88=9B=E5=BB=BA=20SongData=20?= =?UTF-8?q?=E5=88=87=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/scoresaber/bs30.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/handler/scoresaber/bs30.go b/handler/scoresaber/bs30.go index b0e688a..7cec0fa 100644 --- a/handler/scoresaber/bs30.go +++ b/handler/scoresaber/bs30.go @@ -630,7 +630,8 @@ func getRecent10Scores(playerID string) ([]ScoreSaberPlayerScore, error) { // 获取玩家的所有分数并转换为 SongData func getAllScoreSaberScores(playerID string) ([]SongData, error) { - var allSongs []SongData + // var allSongs []SongData + allSongs := make([]SongData, 40) cwd, _ := os.Getwd() // 获取Best30 @@ -669,7 +670,7 @@ func getAllScoreSaberScores(playerID string) ([]SongData, error) { defer util.ReportPanicToDev() defer wgAll.Done() wg := sync.WaitGroup{} - for _, score := range recentScores { + for i, score := range recentScores { wg.Add(1) go func() { defer util.ReportPanicToDev() @@ -680,7 +681,7 @@ func getAllScoreSaberScores(playerID string) ([]SongData, error) { return } songData.IsBest30 = false - allSongs = append(allSongs, songData) + allSongs[i+30] = songData }() wg.Wait() }