MCPcopy Index your code
hub / github.com/FloatTech/ZeroBot-Plugin / draw

Function draw

plugin/chess/core.go:65–108  ·  view source on GitHub ↗

draw 和棋

(groupCode, senderUin int64)

Source from the content-addressed store, hash-verified

63
64// draw 和棋
65func draw(groupCode, senderUin int64) (msg message.Message, err error) {
66 msg = message.Message{message.At(senderUin)}
67 // 检查对局是否存在
68 room, ok := chessRoomMap.Load(groupCode)
69 if !ok {
70 return nil, errNotExist
71 }
72 // 检查消息发送者是否为对局中的玩家
73 if senderUin != room.whitePlayer && senderUin != room.blackPlayer {
74 return
75 }
76 // 处理和棋逻辑
77 room.lastMoveTime = time.Now().Unix()
78 if room.drawPlayer == 0 {
79 room.drawPlayer = senderUin
80 chessRoomMap.Store(groupCode, room)
81 msg = append(msg, message.Text("请求和棋, 发送「和棋」或「draw」接受和棋。走棋视为拒绝和棋。"))
82 return
83 }
84 if room.drawPlayer == senderUin {
85 return
86 }
87 err = room.chessGame.Draw(chess.DrawOffer)
88 if err != nil {
89 return
90 }
91 chessString := getChessString(*room)
92 eloString := ""
93 if len(room.chessGame.Moves()) > 4 {
94 // 若走子次数超过 4 认为是有效对局, 存入数据库
95 dbService := newDBService()
96 if err = dbService.createPGN(chessString, room.whitePlayer, room.blackPlayer, room.whiteName, room.blackName); err != nil {
97 return
98 }
99 whiteScore, blackScore := 0.5, 0.5
100 eloString, err = getELOString(*room, whiteScore, blackScore)
101 if err != nil {
102 return
103 }
104 }
105 msg = append(msg, message.Text("接受和棋, 游戏结束。\n", eloString, chessString))
106 chessRoomMap.Delete(groupCode)
107 return
108}
109
110// resign 认输
111func resign(groupCode, senderUin int64) (msg message.Message, err error) {

Callers 1

initFunction · 0.70

Calls 4

getChessStringFunction · 0.85
newDBServiceFunction · 0.85
getELOStringFunction · 0.85
createPGNMethod · 0.80

Tested by

no test coverage detected