Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

国际象棋 #720

Merged
merged 16 commits into from
Sep 1, 2023
Merged

国际象棋 #720

merged 16 commits into from
Sep 1, 2023

Conversation

aimerneige
Copy link
Contributor

应该能跑吧,看下 lint

@aimerneige
Copy link
Contributor Author

aimerneige commented Aug 30, 2023

image

测试了下,跑得妥妥的

删了 inkscape 跑不起来了 😭😭😭😭

plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/chess.go Show resolved Hide resolved
plugin/chess/core.go Show resolved Hide resolved
plugin/chess/core.go Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
Copy link
Member

@fumiama fumiama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

总的来说感觉不错,比莲宝 @lianhong2758 好太多了,莲宝你多学学🤣👉

plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/chess.go Show resolved Hide resolved
plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/chess.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Show resolved Hide resolved
// generateBoardSVG 生成棋盘 SVG 图片
func generateBoardSVG(svgFilePath, fenStr string, gameTurn chess.Color, sqs ...chess.Square) error {
os.Remove(svgFilePath)
f, err := os.Create(svgFilePath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意反并发

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

群反并发并非就万事大吉了,一样要做文件的反并发。简单的方法是只在程序启动时才清理/重建一次目录,然后就不管了。

@lianhong2758
Copy link
Contributor

总的来说感觉不错,比莲宝 @lianhong2758 好太多了,莲宝你多学学🤣👉

你是?

@aimerneige
Copy link
Contributor Author

剩下的不会改了

@aimerneige
Copy link
Contributor Author

需要添加字体 unifont 到 zbpdata

Copy link
Member

@fumiama fumiama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好还是不要用cmd.exec,我偏向于纯go实现。 @kanrichan 你那个能用吗?

plugin/chess/chess.go Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
highlightSquare = append(highlightSquare, lastMove.S2())
}
// 生成棋盘 svg 文件
svgFilePath := path.Join(tempFileDir, fmt.Sprintf("%d.svg", groupCode))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你文件反并发呢,最简单的办法是给每个文件名加一个随机值区分。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单个群内同时只允许同时存在一个对局,使用群号作为区分即可解决并发问题。

只有当用户发请求走棋时才需要读取图片,而棋盘也应该同步更新,实际操作为使用新的图片覆盖旧的图片,并立即读取,同一个群/即同一个对局中,图片读取完成后会立即失效,后续不再用到,使用新的图片覆盖也不会存在问题。不同的群/即不同的对局读写的文件路径是完全不同的,相互之间不会存在并发错误。

如果所有文件都使用随机值而不加有效区分的话,会生成大量的临时文件占用磁盘空间。每一次走棋会生成 30KB 的 svg 图片和 80KB 的 png 图片。一盘游戏平均要走 40 步,耗费 20 分钟,即约 4MB 的空间浪费。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那可否不存这个图片,直接作为base64发送,svg渲染用 @kanrichanhttps://github.com/kanrichan/resvg-go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我试着改一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可否不存这个图片

不可以,调用inkscape必须指定文件路径

svg渲染用 @kanrichanhttps://github.com/kanrichan/resvg-go

不会用

plugin/chess/core.go Outdated Show resolved Hide resolved
// generateBoardSVG 生成棋盘 SVG 图片
func generateBoardSVG(svgFilePath, fenStr string, gameTurn chess.Color, sqs ...chess.Square) error {
os.Remove(svgFilePath)
f, err := os.Create(svgFilePath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

群反并发并非就万事大吉了,一样要做文件的反并发。简单的方法是只在程序启动时才清理/重建一次目录,然后就不管了。

plugin/chess/core.go Outdated Show resolved Hide resolved
plugin/chess/core.go Outdated Show resolved Hide resolved
@aimerneige
Copy link
Contributor Author

help wanted

@aimerneige
Copy link
Contributor Author

aimerneige commented Sep 1, 2023

目前可行的解决方案:

  1. 尝试使用 kanrichan/resvg-go 渲染 svg 图
  2. 删除 inkscape 相关代码,只使用等宽字体渲染的棋盘
  3. 直接使用绘图库绘制棋盘,不使用 notnil/chess 提供的 svg

@@ -52,7 +54,7 @@ func blindfold(groupCode, senderUin int64, senderName string) message.Message {
// abort 中断对局
func abort(groupCode int64) message.Message {
if room, ok := chessRoomMap.Load(groupCode); ok {
return abortGame(room, groupCode, "对局已被管理员中断,游戏结束。")
return abortGame(*room, groupCode, "对局已被管理员中断,游戏结束。")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这函数,同步改成指针啊,算了我改吧。

@fumiama fumiama merged commit ba0ef37 into FloatTech:master Sep 1, 2023
7 checks passed
@aimerneige
Copy link
Contributor Author

aimerneige commented Sep 2, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants