-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
321 lines (286 loc) · 7.41 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"fmt"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/vtb-link/bianka/basic"
"image/color"
"math/rand"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"time"
"golang.org/x/exp/slog"
"github.com/vtb-link/bianka/live"
"github.com/vtb-link/bianka/proto"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
)
//go:embed Resource/Wx.jpg
var WxJpg []byte
//go:embed Resource/Alipay.jpg
var AliPayJpg []byte
//go:embed Resource/AlipayRedPack.jpg
var AliPayRedPack []byte
func CalculateTimeDifference(timeString string) time.Duration {
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
return 0
}
layout := "2006-01-02 15:04:05"
t, err := time.ParseInLocation(layout, timeString, location)
if err != nil {
panic(err)
}
// 计算当前时间与给定时间之间的差异
diff := time.Since(t)
return diff
}
func RemoveTags(str string) string {
// 创建正则表达式匹配模式
re := regexp.MustCompile(`<.*?>`)
// 使用空字符串替换匹配到的部分
result := re.ReplaceAllString(str, "")
return result
}
func SendLineToWs(NormalLine Line, Gift GiftLine, LineType int) {
switch {
case len(NormalLine.OpenID) > 0:
Send := WsPack{
OpMessage: OpAdd,
LineType: LineType,
Line: NormalLine,
}
SendWsJson, err := json.Marshal(Send)
if err != nil {
return
}
QueueChatChan <- SendWsJson
case len(Gift.OpenID) > 0:
Send := WsPack{
OpMessage: OpAdd,
LineType: LineType,
GiftLine: Gift,
}
SendWsJson, err := json.Marshal(Send)
if err != nil {
return
}
QueueChatChan <- SendWsJson
}
}
func SendDmToWs(Dm *proto.CmdDanmuData) {
SendDmWsJson, err := json.Marshal(Dm)
if err != nil {
return
}
DmChatChan <- SendDmWsJson
}
func SendMusicServer(Path, Keyword string) {
for i := 0; i < 3; i++ {
get, err := http.Get("http://127.0.0.1:99/" + Path + "?keyword=" + Keyword)
if err != nil {
return
}
if get.StatusCode == 200 {
break
}
}
}
func SendDelToWs(LineType, index int, OpenId string) {
Send := WsPack{
OpMessage: OpDelete,
Index: index,
LineType: LineType,
Line: Line{
OpenID: OpenId,
},
}
SendWsJson, err := json.Marshal(Send)
if err != nil {
return
}
QueueChatChan <- SendWsJson
}
func SendWhereToWs(OpenId string) {
Send := WsPack{
OpMessage: OpWhere,
Line: Line{
OpenID: OpenId,
},
}
SendWsJson, err := json.Marshal(Send)
if err != nil {
return
}
QueueChatChan <- SendWsJson
}
func DeleteLine(OpenId string) {
switch {
case line.GuardIndex[OpenId] != 0:
line.GuardLine = append(line.GuardLine[:line.GuardIndex[OpenId]-1],
line.GuardLine[line.GuardIndex[OpenId]:]...)
SendDelToWs(GuardLineType, line.GuardIndex[OpenId]-1, OpenId)
delete(line.GuardIndex, OpenId)
line.UpdateIndex(GuardLineType)
SetLine(line)
case line.GiftIndex[OpenId] != 0:
line.GiftLine = append(line.GiftLine[:line.GiftIndex[OpenId]-1],
line.GiftLine[line.GiftIndex[OpenId]:]...)
SendDelToWs(GiftLineType, line.GiftIndex[OpenId]-1, OpenId)
delete(line.GiftIndex, OpenId)
line.UpdateIndex(GiftLineType)
SetLine(line)
case line.CommonIndex[OpenId] != 0:
line.CommonLine = append(line.CommonLine[:line.CommonIndex[OpenId]-1],
line.CommonLine[line.CommonIndex[OpenId]:]...)
SendDelToWs(CommonLineType, line.CommonIndex[OpenId]-1, OpenId)
delete(line.CommonIndex, OpenId)
line.UpdateIndex(CommonLineType)
SetLine(line)
}
}
func DeleteFirst() {
if len(line.GuardLine) != 0 {
DeleteLine(line.GuardLine[0].OpenID)
} else if len(line.GiftLine) != 0 {
DeleteLine(line.GiftLine[0].OpenID)
} else if len(line.CommonLine) != 0 {
DeleteLine(line.CommonLine[0].OpenID)
}
}
func assistUI() *fyne.Container {
Wx := canvas.NewImageFromReader(bytes.NewReader(WxJpg), "Wx.jpg")
Wx.FillMode = canvas.ImageFillOriginal
AliPay := canvas.NewImageFromReader(bytes.NewReader(AliPayJpg), "Alipay.jpg")
AliPay.FillMode = canvas.ImageFillOriginal
AliPayRed := canvas.NewImageFromReader(bytes.NewReader(AliPayRedPack), "AliPayRedPack.jpg")
AliPayRed.FillMode = canvas.ImageFillOriginal
Cont := container.NewHBox(Wx, AliPay, AliPayRed)
return Cont
}
func DisplaySpecialUserListUI() *fyne.Container {
SpecialUserBoxItem := make(map[string]*fyne.Container)
Cont := container.NewVBox()
for k, v := range SpecialUserList {
var timeCanvas = canvas.NewText(time.Unix(v.EndTime, 0).Format("2006-01-02 15:04:05"), color.White)
SpecialUserBoxItem[k] = container.NewHBox(
canvas.NewText(v.UserName, color.White),
timeCanvas,
widget.NewButton("删除", func() {
delete(SpecialUserList, k)
globalConfiguration.SpecialUserList = SpecialUserList
SetConfig(globalConfiguration)
Cont.Remove(SpecialUserBoxItem[k])
}),
widget.NewButton("修改截止时间", func() {
var selectedYear, selectedMonth, selectedDay string
dialog.ShowCustomConfirm("选择截止日期", "确定", "取消", NewDatePicker(&selectedYear, &selectedMonth, &selectedDay), func(b bool) {
timestamp, err := ConvertToTimestamp(selectedYear, selectedMonth, selectedDay)
if err != nil {
dialog.ShowError(errors.New("时间选择错误"), CtrlWindows)
}
SpecialUserList[k] = SpecialUserStruct{
EndTime: timestamp,
UserName: v.UserName,
}
globalConfiguration.SpecialUserList = SpecialUserList
SetConfig(globalConfiguration)
timeCanvas.Text = time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
Cont.Refresh()
}, SpecialUserSetWindows)
}),
)
Cont.Add(SpecialUserBoxItem[k])
}
return Cont
}
func randomInt(min, max int) int {
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Intn(max-min+1) + min
}
func CleanOldVersion() {
_, err := os.Stat("./Version " + NowVersion)
if err != nil {
_ = os.Remove("./line.json")
_ = os.Remove("./lineConfig.json")
_, _ = os.Create("./Version " + NowVersion)
return
}
}
// AgreeOpenUrl 尝试函数名过检测使用的抽象函数名,实际作用只是调用命令行打开链接
func AgreeOpenUrl(url string) error {
var (
cmd string
args []string
)
switch runtime.GOOS {
case "windows":
cmd, args = "cmd", []string{"/c", "start"}
case "darwin":
cmd = "open"
case "Agree":
cmd = "Agree"
os.Exit(0)
default:
// "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func Restart() {
exePath, err := os.Executable()
if err != nil {
fmt.Println("无法获取可执行文件路径:", err)
return
}
// 启动新进程来替换当前进程
cmd := exec.Command(exePath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
}
func NewHeartbeat(client *live.Client, GameId string, CloseChan chan bool) {
tk := time.NewTicker(time.Second * 10)
go func() {
for {
select {
case <-tk.C:
if err := client.AppHeartbeat(GameId); err != nil {
slog.Error("Heartbeat fail", err)
} else {
slog.Info("Heartbeat Success", GameId)
}
case <-CloseChan:
tk.Stop()
break
}
}
}()
}
func CloseRoomConnect(AppClient *live.Client, GameId string, WsClient *basic.WsClient, HeartbeatCloseChan chan bool) {
fmt.Println("触发关闭函数")
// Non-blocking send to HeartbeatCloseChan
if WsClient != nil {
WsClient.Close()
}
if AppClient != nil {
AppClient.AppEnd(GameId)
}
if HeartbeatCloseChan != nil {
select {
case HeartbeatCloseChan <- true:
default:
break
}
}
App.Quit()
}