From 19eda728c92acbeeb46ae8939093c120244d8c24 Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Sat, 2 Sep 2023 14:22:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8F=AA=E6=9C=89=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=AE=89=E8=A3=85=20inkscape=20=E6=97=B6=E6=89=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=B8=85=E7=90=86=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/chess/core.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/plugin/chess/core.go b/plugin/chess/core.go index a5a85260ab..88d5f5ed98 100644 --- a/plugin/chess/core.go +++ b/plugin/chess/core.go @@ -103,9 +103,11 @@ func draw(groupCode, senderUin int64) message.Message { eloString = elo } replyMsg := textWithAt(senderUin, "接受和棋,游戏结束。\n"+eloString+chessString) - if err := cleanTempFiles(groupCode); err != nil { - log.Debugln("[chess]", "Fail to clean temp files", err) - return message.Message{message.Text("ERROR: ", err)} + if commandExists("inkscape") { + if err := cleanTempFiles(groupCode); err != nil { + log.Debugln("[chess]", "Fail to clean temp files", err) + return message.Message{message.Text("ERROR: ", err)} + } } chessRoomMap.Delete(groupCode) return replyMsg @@ -169,9 +171,11 @@ func resign(groupCode, senderUin int64) message.Message { replyMsg = textWithAt(senderUin, "对手认输,游戏结束,你胜利了。\n"+eloString+chessString) } // 删除临时文件 - if err := cleanTempFiles(groupCode); err != nil { - log.Debugln("[chess]", "Fail to clean temp files", err) - return message.Message{message.Text("ERROR: ", err)} + if commandExists("inkscape") { + if err := cleanTempFiles(groupCode); err != nil { + log.Debugln("[chess]", "Fail to clean temp files", err) + return message.Message{message.Text("ERROR: ", err)} + } } chessRoomMap.Delete(groupCode) return replyMsg @@ -231,9 +235,11 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message { chessString := getChessString(*room) replyMsg := textWithAt(senderUin, "违例两次,游戏结束。\n"+chessString) // 删除临时文件 - if err := cleanTempFiles(groupCode); err != nil { - log.Debugln("[chess]", "Fail to clean temp files", err) - return message.Message{message.Text("ERROR: ", err)} + if commandExists("inkscape") { + if err := cleanTempFiles(groupCode); err != nil { + log.Debugln("[chess]", "Fail to clean temp files", err) + return message.Message{message.Text("ERROR: ", err)} + } } chessRoomMap.Delete(groupCode) return replyMsg @@ -309,9 +315,11 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message { if !room.isBlindfold { replyMsg = append(replyMsg, boardImgEle) } - if err := cleanTempFiles(groupCode); err != nil { - log.Debugln("[chess]", "Fail to clean temp files", err) - return message.Message{message.Text("ERROR: ", err)} + if commandExists("inkscape") { + if err := cleanTempFiles(groupCode); err != nil { + log.Debugln("[chess]", "Fail to clean temp files", err) + return message.Message{message.Text("ERROR: ", err)} + } } chessRoomMap.Delete(groupCode) return replyMsg @@ -445,9 +453,11 @@ func abortGame(room chessRoom, groupCode int64, hint string) message.Message { return message.Message{message.Text("ERROR: ", err)} } } - if err := cleanTempFiles(groupCode); err != nil { - log.Debugln("[chess]", "Fail to clean temp files", err) - return message.Message{message.Text("ERROR: ", err)} + if commandExists("inkscape") { + if err := cleanTempFiles(groupCode); err != nil { + log.Debugln("[chess]", "Fail to clean temp files", err) + return message.Message{message.Text("ERROR: ", err)} + } } chessRoomMap.Delete(groupCode) msg := simpleText(hint) From 2bb15c1fb27c8855dc1b8c344d7648f16f417efb Mon Sep 17 00:00:00 2001 From: Aimer Neige Date: Sat, 2 Sep 2023 21:29:30 +0800 Subject: [PATCH 2/2] fix lint && bug --- plugin/chess/core.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugin/chess/core.go b/plugin/chess/core.go index 88d5f5ed98..a2940d6a55 100644 --- a/plugin/chess/core.go +++ b/plugin/chess/core.go @@ -16,6 +16,7 @@ import ( "github.com/FloatTech/floatbox/file" "github.com/FloatTech/gg" "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/img/text" "github.com/RomiChan/syncx" "github.com/jinzhu/gorm" "github.com/notnil/chess" @@ -103,7 +104,7 @@ func draw(groupCode, senderUin int64) message.Message { eloString = elo } replyMsg := textWithAt(senderUin, "接受和棋,游戏结束。\n"+eloString+chessString) - if commandExists("inkscape") { + if inkscapeExists() { if err := cleanTempFiles(groupCode); err != nil { log.Debugln("[chess]", "Fail to clean temp files", err) return message.Message{message.Text("ERROR: ", err)} @@ -171,7 +172,7 @@ func resign(groupCode, senderUin int64) message.Message { replyMsg = textWithAt(senderUin, "对手认输,游戏结束,你胜利了。\n"+eloString+chessString) } // 删除临时文件 - if commandExists("inkscape") { + if inkscapeExists() { if err := cleanTempFiles(groupCode); err != nil { log.Debugln("[chess]", "Fail to clean temp files", err) return message.Message{message.Text("ERROR: ", err)} @@ -235,7 +236,7 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message { chessString := getChessString(*room) replyMsg := textWithAt(senderUin, "违例两次,游戏结束。\n"+chessString) // 删除临时文件 - if commandExists("inkscape") { + if inkscapeExists() { if err := cleanTempFiles(groupCode); err != nil { log.Debugln("[chess]", "Fail to clean temp files", err) return message.Message{message.Text("ERROR: ", err)} @@ -315,7 +316,7 @@ func play(senderUin int64, groupCode int64, moveStr string) message.Message { if !room.isBlindfold { replyMsg = append(replyMsg, boardImgEle) } - if commandExists("inkscape") { + if inkscapeExists() { if err := cleanTempFiles(groupCode); err != nil { log.Debugln("[chess]", "Fail to clean temp files", err) return message.Message{message.Text("ERROR: ", err)} @@ -453,7 +454,7 @@ func abortGame(room chessRoom, groupCode int64, hint string) message.Message { return message.Message{message.Text("ERROR: ", err)} } } - if commandExists("inkscape") { + if inkscapeExists() { if err := cleanTempFiles(groupCode); err != nil { log.Debugln("[chess]", "Fail to clean temp files", err) return message.Message{message.Text("ERROR: ", err)} @@ -480,7 +481,7 @@ func getBoardElement(groupCode int64) (message.MessageSegment, bool, string) { } // 未安装 inkscape 直接返回对局字符串 // TODO: 使用原生 go 库渲染 svg - if !commandExists("inkscape") { + if !inkscapeExists() { boardString := room.chessGame.Position().Board().Draw() boardImageB64, err := generateCharBoardImage(boardString) if err != nil { @@ -628,8 +629,7 @@ func generateCharBoardImage(boardString string) (string, error) { dc.SetRGB(1, 1, 1) dc.Clear() dc.SetRGB(0, 0, 0) - // fnt := text.GNUUnifontFontFile - fontdata, err := file.GetLazyData("text.GNUUnifontFontFile", control.Md5File, true) + fontdata, err := file.GetLazyData(text.GNUUnifontFontFile, control.Md5File, true) if err != nil { // TODO: err solve panic(err) @@ -704,9 +704,9 @@ func getELORate(whiteUin, blackUin int64, dbService *chessDBService) (whiteRate return } -// commandExists 判断 指令是否存在 -func commandExists(cmd string) bool { - _, err := exec.LookPath(cmd) +// inkscapeExists 判断 inkscape 是否存在 +func inkscapeExists() bool { + _, err := exec.LookPath("inkscape") return err == nil }