百万棋盘
One Million Chessboards

原始链接: https://eieio.games/blog/one-million-chessboards/

“百万棋盘”是一个新的网站,上面展示了百万个棋盘,在任意一个棋盘上移动棋子都会立即影响所有玩家。这款游戏的设计目标是大规模并发,是其开发者之前项目“百万复选框”的升级版。整个游戏,包括所有棋盘,都运行在一台服务器上,并使用二维数组存储在内存中。后端使用Go语言编写,采用单写多读线程模型,并使用互斥锁管理棋盘访问。一个值得注意的功能是前端的乐观锁式棋步应用和“回滚”机制,这需要大量的开发工作。玩家的移动会立即显示,但如果与服务器的实际状态发生冲突,前端会智能地撤销玩家的移动。开发者期待着提供更详细的技术实现解读,并鼓励大家尝试这款游戏。


原文

I made a website. It’s called One Million Chessboards. It has one million chessboards on it.

Moving a piece moves it for everyone, instantly. There are no turns. You can move between boards.

moving some pieces

What

Well last year I made this game called One Million Checkboxes.

It was a pretty fun time! So I thought I’d do something like this again.

I worked really hard on this one. I hope you like it.

How

This was the most technically challenging thing that I’ve worked on in a long time. I’m going to save a full technical writeup until I see how my decisions pan out, since I think there’s a decent chance I’ll need to make a lot of changes.

But I’ll summarize a few things for you.

  • Unlike One Million Checkboxes, I designed this for scale
  • The game runs on a single server (!)
  • The board is stored fully in-memory; it’s a 2D array of 64 million uint64s
  • The backend is written in go. This is my first go project.
  • I use a single writer thread, tons of reader threads, and coordinate access to the board with a mutex
  • The frontend optimistically applies all moves you make immediately. It then builds up a dependency graph of the moves you’ve made, and backs them out if it receives a conflicting update before the server acks your move.

That last part - optimistic move application with what games people sometimes call “rollback” - is about 1,600 lines of code that took me a ~7 days of fulltime work to write. I don’t remember the last time I wrestled with a problem that hard!

Can I play

Yes! Play it here.

I really hope you like this one. More updates to come :)

联系我们 contact @ memedata.com