Skip to content

Commit 3a8615e

Browse files
committed
feat: 背包清理只保留一天的数据
1 parent 02ee892 commit 3a8615e

File tree

4 files changed

+206
-44
lines changed

4 files changed

+206
-44
lines changed

bgiStatus/BagTools.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package bgiStatus
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strings"
10+
"time"
11+
)
12+
13+
func compareDate(a, b string) int {
14+
ta, _ := time.Parse("2006/1/2", a)
15+
tb, _ := time.Parse("2006/1/2", b)
16+
if ta.Before(tb) {
17+
return -1
18+
} else if ta.After(tb) {
19+
return 1
20+
}
21+
return 0
22+
}
23+
24+
func DeleteBag() {
25+
filePath := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\背包材料统计\\latest_record.txt", Config.BetterGIAddress))
26+
27+
// 打开文件读取内容
28+
file, err := os.Open(filePath)
29+
if err != nil {
30+
panic(err)
31+
}
32+
defer file.Close()
33+
34+
timeRegex := regexp.MustCompile(`^(\d{4}/\d{1,2}/\d{1,2}) \d{2}:\d{2}:\d{2}$`)
35+
36+
var blocks [][]string
37+
var currentBlock []string
38+
var currentDate string
39+
var latestDate string
40+
41+
scanner := bufio.NewScanner(file)
42+
43+
for scanner.Scan() {
44+
line := scanner.Text()
45+
46+
// 匹配时间戳行
47+
if match := timeRegex.FindStringSubmatch(line); match != nil {
48+
// 保存上一个 block
49+
if len(currentBlock) > 0 {
50+
blocks = append(blocks, append([]string{}, currentBlock...))
51+
currentBlock = nil
52+
}
53+
currentDate = match[1]
54+
55+
if latestDate == "" || compareDate(currentDate, latestDate) > 0 {
56+
latestDate = currentDate
57+
}
58+
}
59+
currentBlock = append(currentBlock, line)
60+
}
61+
// 最后一块
62+
if len(currentBlock) > 0 {
63+
blocks = append(blocks, currentBlock)
64+
}
65+
66+
// 过滤只保留最新日期的数据
67+
var result []string
68+
for _, block := range blocks {
69+
for _, line := range block {
70+
if match := timeRegex.FindStringSubmatch(line); match != nil {
71+
if match[1] == latestDate {
72+
result = append(result, strings.Join(block, "\n"))
73+
}
74+
break
75+
}
76+
}
77+
}
78+
79+
// 直接覆盖原文件
80+
err = os.WriteFile(filePath, []byte(strings.Join(result, "\n\n")), 0644)
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
fmt.Printf("成功:已将 %s 更新为仅包含 %s 的数据。\n", filePath, latestDate)
86+
87+
}
88+
89+
func DeleteMoLa() {
90+
91+
filePath := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\OCR读取当前摩拉记录并发送通知\\mora_log.txt", Config.BetterGIAddress))
92+
93+
file, err := os.Open(filePath)
94+
if err != nil {
95+
panic(err)
96+
}
97+
defer file.Close()
98+
99+
scanner := bufio.NewScanner(file)
100+
101+
// 日期行正则(匹配形如:2025/7/5 10:33:45)
102+
timeRegex := regexp.MustCompile(`^(\d{4}/\d{1,2}/\d{1,2}) \d{2}:\d{2}:\d{2}`)
103+
104+
var allLines []string
105+
var dateMap = make(map[string][]string)
106+
var latestDate string
107+
108+
for scanner.Scan() {
109+
line := scanner.Text()
110+
allLines = append(allLines, line)
111+
112+
if match := timeRegex.FindStringSubmatch(line); match != nil {
113+
date := match[1]
114+
dateMap[date] = append(dateMap[date], line)
115+
116+
if latestDate == "" || compareDate(date, latestDate) > 0 {
117+
latestDate = date
118+
}
119+
}
120+
}
121+
122+
// 写回文件,仅保留最新日期
123+
if lines, ok := dateMap[latestDate]; ok {
124+
output := strings.Join(lines, "\n") + "\n"
125+
err = os.WriteFile(filePath, []byte(output), 0644)
126+
if err != nil {
127+
panic(err)
128+
}
129+
fmt.Printf("成功:%s 中只保留了 %s 的记录。\n", filePath, latestDate)
130+
} else {
131+
fmt.Println("未找到匹配的记录")
132+
}
133+
134+
}
135+
136+
func DeleteYuanShi() {
137+
filePath := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\OCR读取当前抽卡资源并发送通知\\Resources_log.txt", Config.BetterGIAddress))
138+
file, err := os.Open(filePath)
139+
if err != nil {
140+
panic(err)
141+
}
142+
defer file.Close()
143+
144+
scanner := bufio.NewScanner(file)
145+
146+
// 日期行正则(匹配形如:2025/7/5 10:33:45)
147+
timeRegex := regexp.MustCompile(`^(\d{4}/\d{1,2}/\d{1,2}) \d{2}:\d{2}:\d{2}`)
148+
149+
var allLines []string
150+
var dateMap = make(map[string][]string)
151+
var latestDate string
152+
153+
for scanner.Scan() {
154+
line := scanner.Text()
155+
allLines = append(allLines, line)
156+
157+
if match := timeRegex.FindStringSubmatch(line); match != nil {
158+
date := match[1]
159+
dateMap[date] = append(dateMap[date], line)
160+
161+
if latestDate == "" || compareDate(date, latestDate) > 0 {
162+
latestDate = date
163+
}
164+
}
165+
}
166+
167+
// 写回文件,仅保留最新日期
168+
if lines, ok := dateMap[latestDate]; ok {
169+
output := strings.Join(lines, "\n") + "\n"
170+
err = os.WriteFile(filePath, []byte(output), 0644)
171+
if err != nil {
172+
panic(err)
173+
}
174+
fmt.Printf("成功:%s 中只保留了 %s 的记录。\n", filePath, latestDate)
175+
} else {
176+
fmt.Println("未找到匹配的记录")
177+
}
178+
}

bgiStatus/BgiStatus.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -467,33 +467,17 @@ func MorasStatistics() ([]Material, error) {
467467
// 删除背包统计
468468
func DeleteBagStatistics() string {
469469

470-
autoLog.Sugar.Infof("删除背包统计")
471-
filePath := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\背包材料统计\\latest_record.txt", Config.BetterGIAddress))
472-
// 删除文件
473-
err := os.Remove(filePath)
474-
if err != nil {
475-
fmt.Println("背包统计删除文件失败:", err)
476-
477-
}
478-
479-
autoLog.Sugar.Infof("删除摩拉统计")
480-
filePath2 := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\OCR读取当前摩拉记录并发送通知\\mora_log.txt", Config.BetterGIAddress))
481-
// 删除文件
482-
err2 := os.Remove(filePath2)
483-
if err2 != nil {
484-
autoLog.Sugar.Errorf("删除摩拉统计失败")
470+
autoLog.Sugar.Infof("清理背包统计")
471+
DeleteBag()
485472

486-
}
473+
autoLog.Sugar.Infof("清理摩拉统计")
474+
DeleteMoLa()
487475

488-
autoLog.Sugar.Infof("删除原石统计")
489-
filePath3 := filepath.Clean(fmt.Sprintf("%s\\User\\JsScript\\OCR读取当前抽卡资源并发送通知\\Resources_log.txt", Config.BetterGIAddress))
490-
err3 := os.Remove(filePath3)
491-
if err3 != nil {
492-
autoLog.Sugar.Errorf("删除原石统计失败")
493-
}
476+
autoLog.Sugar.Infof("清理原石统计")
477+
DeleteYuanShi()
494478

495-
autoLog.Sugar.Infof("文件删除成功")
496-
return "文件删除成功"
479+
autoLog.Sugar.Infof("清理成功")
480+
return "清理成功"
497481
}
498482

499483
type DogFood struct {

html/bg.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
<header>
156156
<div class="btn-container">
157157
<button class="btn" onclick="window.location.href='/'">返回主页</button>
158-
<button class="btn" onclick="deleteBagBtn()">清空统计</button>
158+
<button class="btn" onclick="deleteBagBtn()">清理统计,只保留一天</button>
159159
</div>
160160
<h1>{{ .title }}</h1>
161161
</header>
@@ -199,6 +199,8 @@ <h1>{{ .title }}</h1>
199199
.catch(err => {
200200
alert("请求出错:" + err);
201201
});
202+
//刷新
203+
location.reload();
202204
}
203205

204206
window.onload = function () {

main.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -673,26 +673,24 @@ func main() {
673673
context.JSON(http.StatusOK, gin.H{"status": "success", "data": results})
674674
})
675675

676-
//go bgiStatus.LogAnalysis2("better-genshin-impact20250717.log")
677-
678676
//检查BGI状态
679-
//go bgiStatus.CheckBetterGIStatus()
680-
////更新仓库
681-
//go func() {
682-
// err := bgiStatus.GitPull()
683-
// if err != nil {
684-
// autoLog.Sugar.Errorf("更新仓库失败:%v", err)
685-
// }
686-
//}()
687-
//go task.UpdateCode()
688-
//
689-
//if Config.IsMysSignIn {
690-
// //米游社自动签到
691-
// go task.MysSignIn()
692-
// autoLog.Sugar.Infof("米游社自动签到开启状态")
693-
//} else {
694-
// autoLog.Sugar.Infof("米游社自动签到关闭状态")
695-
//}
677+
go bgiStatus.CheckBetterGIStatus()
678+
//更新仓库
679+
go func() {
680+
err := bgiStatus.GitPull()
681+
if err != nil {
682+
autoLog.Sugar.Errorf("更新仓库失败:%v", err)
683+
}
684+
}()
685+
go task.UpdateCode()
686+
687+
if Config.IsMysSignIn {
688+
//米游社自动签到
689+
go task.MysSignIn()
690+
autoLog.Sugar.Infof("米游社自动签到开启状态")
691+
} else {
692+
autoLog.Sugar.Infof("米游社自动签到关闭状态")
693+
}
696694

697695
//服务器端口
698696
post := Config.Post

0 commit comments

Comments
 (0)