Skip to content

Commit 5485cc3

Browse files
authored
[BUG FIX] 国际象棋插件 bug 修复 (#722)
* 只有正确安装 inkscape 时才需要清理临时文件 * fix lint && bug
1 parent ba0ef37 commit 5485cc3

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

plugin/chess/core.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/FloatTech/floatbox/file"
1717
"github.com/FloatTech/gg"
1818
"github.com/FloatTech/zbputils/control"
19+
"github.com/FloatTech/zbputils/img/text"
1920
"github.com/RomiChan/syncx"
2021
"github.com/jinzhu/gorm"
2122
"github.com/notnil/chess"
@@ -103,9 +104,11 @@ func draw(groupCode, senderUin int64) message.Message {
103104
eloString = elo
104105
}
105106
replyMsg := textWithAt(senderUin, "接受和棋,游戏结束。\n"+eloString+chessString)
106-
if err := cleanTempFiles(groupCode); err != nil {
107-
log.Debugln("[chess]", "Fail to clean temp files", err)
108-
return message.Message{message.Text("ERROR: ", err)}
107+
if inkscapeExists() {
108+
if err := cleanTempFiles(groupCode); err != nil {
109+
log.Debugln("[chess]", "Fail to clean temp files", err)
110+
return message.Message{message.Text("ERROR: ", err)}
111+
}
109112
}
110113
chessRoomMap.Delete(groupCode)
111114
return replyMsg
@@ -169,9 +172,11 @@ func resign(groupCode, senderUin int64) message.Message {
169172
replyMsg = textWithAt(senderUin, "对手认输,游戏结束,你胜利了。\n"+eloString+chessString)
170173
}
171174
// 删除临时文件
172-
if err := cleanTempFiles(groupCode); err != nil {
173-
log.Debugln("[chess]", "Fail to clean temp files", err)
174-
return message.Message{message.Text("ERROR: ", err)}
175+
if inkscapeExists() {
176+
if err := cleanTempFiles(groupCode); err != nil {
177+
log.Debugln("[chess]", "Fail to clean temp files", err)
178+
return message.Message{message.Text("ERROR: ", err)}
179+
}
175180
}
176181
chessRoomMap.Delete(groupCode)
177182
return replyMsg
@@ -231,9 +236,11 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message {
231236
chessString := getChessString(*room)
232237
replyMsg := textWithAt(senderUin, "违例两次,游戏结束。\n"+chessString)
233238
// 删除临时文件
234-
if err := cleanTempFiles(groupCode); err != nil {
235-
log.Debugln("[chess]", "Fail to clean temp files", err)
236-
return message.Message{message.Text("ERROR: ", err)}
239+
if inkscapeExists() {
240+
if err := cleanTempFiles(groupCode); err != nil {
241+
log.Debugln("[chess]", "Fail to clean temp files", err)
242+
return message.Message{message.Text("ERROR: ", err)}
243+
}
237244
}
238245
chessRoomMap.Delete(groupCode)
239246
return replyMsg
@@ -309,9 +316,11 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message {
309316
if !room.isBlindfold {
310317
replyMsg = append(replyMsg, boardImgEle)
311318
}
312-
if err := cleanTempFiles(groupCode); err != nil {
313-
log.Debugln("[chess]", "Fail to clean temp files", err)
314-
return message.Message{message.Text("ERROR: ", err)}
319+
if inkscapeExists() {
320+
if err := cleanTempFiles(groupCode); err != nil {
321+
log.Debugln("[chess]", "Fail to clean temp files", err)
322+
return message.Message{message.Text("ERROR: ", err)}
323+
}
315324
}
316325
chessRoomMap.Delete(groupCode)
317326
return replyMsg
@@ -445,9 +454,11 @@ func abortGame(room chessRoom, groupCode int64, hint string) message.Message {
445454
return message.Message{message.Text("ERROR: ", err)}
446455
}
447456
}
448-
if err := cleanTempFiles(groupCode); err != nil {
449-
log.Debugln("[chess]", "Fail to clean temp files", err)
450-
return message.Message{message.Text("ERROR: ", err)}
457+
if inkscapeExists() {
458+
if err := cleanTempFiles(groupCode); err != nil {
459+
log.Debugln("[chess]", "Fail to clean temp files", err)
460+
return message.Message{message.Text("ERROR: ", err)}
461+
}
451462
}
452463
chessRoomMap.Delete(groupCode)
453464
msg := simpleText(hint)
@@ -470,7 +481,7 @@ func getBoardElement(groupCode int64) (message.MessageSegment, bool, string) {
470481
}
471482
// 未安装 inkscape 直接返回对局字符串
472483
// TODO: 使用原生 go 库渲染 svg
473-
if !commandExists("inkscape") {
484+
if !inkscapeExists() {
474485
boardString := room.chessGame.Position().Board().Draw()
475486
boardImageB64, err := generateCharBoardImage(boardString)
476487
if err != nil {
@@ -618,8 +629,7 @@ func generateCharBoardImage(boardString string) (string, error) {
618629
dc.SetRGB(1, 1, 1)
619630
dc.Clear()
620631
dc.SetRGB(0, 0, 0)
621-
// fnt := text.GNUUnifontFontFile
622-
fontdata, err := file.GetLazyData("text.GNUUnifontFontFile", control.Md5File, true)
632+
fontdata, err := file.GetLazyData(text.GNUUnifontFontFile, control.Md5File, true)
623633
if err != nil {
624634
// TODO: err solve
625635
panic(err)
@@ -694,9 +704,9 @@ func getELORate(whiteUin, blackUin int64, dbService *chessDBService) (whiteRate
694704
return
695705
}
696706

697-
// commandExists 判断 指令是否存在
698-
func commandExists(cmd string) bool {
699-
_, err := exec.LookPath(cmd)
707+
// inkscapeExists 判断 inkscape 是否存在
708+
func inkscapeExists() bool {
709+
_, err := exec.LookPath("inkscape")
700710
return err == nil
701711
}
702712

0 commit comments

Comments
 (0)