|
| 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 | +} |
0 commit comments