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

Function getBoardElement

plugin/chess/core.go:445–529  ·  view source on GitHub ↗

getBoardElement 获取棋盘图片的消息内容

(groupCode int64)

Source from the content-addressed store, hash-verified

443
444// getBoardElement 获取棋盘图片的消息内容
445func getBoardElement(groupCode int64) (imgMsg message.Segment, err error) {
446 fontdata, err := file.GetLazyData(text.GNUUnifontFontFile, control.Md5File, true)
447 if err != nil {
448 return
449 }
450 room, ok := chessRoomMap.Load(groupCode)
451 if !ok {
452 return imgMsg, errNotExist
453 }
454 // 获取高亮方块
455 highlightSquare := make([]chess.Square, 0, 2)
456 moves := room.chessGame.Moves()
457 if len(moves) != 0 {
458 lastMove := moves[len(moves)-1]
459 highlightSquare = append(highlightSquare, lastMove.S1())
460 highlightSquare = append(highlightSquare, lastMove.S2())
461 }
462 // 生成棋盘 svg 文件
463 buf := bytes.NewBuffer([]byte{})
464 fenStr := room.chessGame.FEN()
465 gameTurn := room.chessGame.Position().Turn()
466 pos := &chess.Position{}
467 if err = pos.UnmarshalText(binary.StringToBytes(fenStr)); err != nil {
468 return
469 }
470 yellow := color.RGBA{255, 255, 0, 1}
471 mark := cimage.MarkSquares(yellow, highlightSquare...)
472 board := pos.Board()
473 fromBlack := cimage.Perspective(gameTurn)
474 err = cimage.SVG(buf, board, fromBlack, mark)
475 if err != nil {
476 return
477 }
478
479 worker, err := resvg.NewDefaultWorker(context.Background())
480 if err != nil {
481 return
482 }
483 defer worker.Close()
484
485 tree, err := worker.NewTreeFromData(buf.Bytes(), &resvg.Options{
486 Dpi: 96,
487 FontFamily: "Unifont",
488 FontSize: 24.0,
489 })
490 if err != nil {
491 return
492 }
493 defer tree.Close()
494
495 fontdb, err := worker.NewFontDBDefault()
496 if err != nil {
497 return
498 }
499 defer fontdb.Close()
500
501 err = fontdb.LoadFontData(fontdata)
502 if err != nil {

Callers 2

playFunction · 0.85
createGameFunction · 0.85

Calls 1

CloseMethod · 0.45

Tested by

no test coverage detected