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

Function play

plugin/chess/core.go:171–312  ·  view source on GitHub ↗

play 走棋

(groupCode, senderUin int64, moveStr string)

Source from the content-addressed store, hash-verified

169
170// play 走棋
171func play(groupCode, senderUin int64, moveStr string) (msg message.Message, err error) {
172 msg = message.Message{message.At(senderUin)}
173 // 检查对局是否存在
174 room, ok := chessRoomMap.Load(groupCode)
175 if !ok {
176 return nil, errNotExist
177 }
178 // 不是对局中的玩家, 忽略消息
179 if (senderUin != room.whitePlayer) && (senderUin != room.blackPlayer) && !isAprilFoolsDay() {
180 return
181 }
182 // 对局未建立
183 if (room.whitePlayer == 0) || (room.blackPlayer == 0) {
184 msg = append(msg, message.Text("请等候其他玩家加入游戏。"))
185 return
186 }
187 // 需要对手走棋
188 if ((senderUin == room.whitePlayer) && (room.chessGame.Position().Turn() != chess.White)) || ((senderUin == room.blackPlayer) && (room.chessGame.Position().Turn() != chess.Black)) {
189 msg = append(msg, message.Text("请等待对手走棋。"))
190 return
191 }
192 room.lastMoveTime = time.Now().Unix()
193 // 走棋
194 if err = room.chessGame.MoveStr(moveStr); err != nil {
195 // 指令错误时检查
196 if !room.isBlindfold {
197 // 未开启盲棋, 提示指令错误
198 msg = append(msg, message.Text("移动「", moveStr, "」违规, 请检查, 格式请参考「代数记谱法」(Algebraic notation)。"))
199 return
200 }
201 // 开启盲棋, 判断违例情况
202 var currentPlayerColor chess.Color
203 if senderUin == room.whitePlayer {
204 currentPlayerColor = chess.White
205 } else {
206 currentPlayerColor = chess.Black
207 }
208 // 第一次违例, 提示
209 _flag := false
210 if (currentPlayerColor == chess.White) && !room.whiteErr {
211 room.whiteErr = true
212 chessRoomMap.Store(groupCode, room)
213 _flag = true
214 }
215 if (currentPlayerColor == chess.Black) && !room.blackErr {
216 room.blackErr = true
217 chessRoomMap.Store(groupCode, room)
218 _flag = true
219 }
220 if _flag {
221 msg = append(msg, message.Text("移动「", moveStr, "」违规, 再次违规会立即判负。"))
222 return
223 }
224 // 出现多次违例, 判负
225 room.chessGame.Resign(currentPlayerColor)
226 chessString := getChessString(*room)
227 msg = append(msg, message.Text("违规两次,游戏结束。\n", chessString))
228

Callers 1

initFunction · 0.85

Calls 7

isAprilFoolsDayFunction · 0.85
getChessStringFunction · 0.85
getBoardElementFunction · 0.85
newDBServiceFunction · 0.85
getELOStringFunction · 0.85
createPGNMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected