Chapter 10

Afterword

At first glance, the project we built in this book can look surprisingly simple: connect to PlayOK, receive a chess move, ask an engine for a reply, and send that move back. But once we actually started building it, the number of different programming skills required became much more obvious.

The final bot may be relatively small. Getting from an unexplained WebSocket connection to a working automated chess player is not.

One Small Project, Many Skills

The first skill we needed was not programming at all. It was observation.

Before writing our own client, we had to watch what the browser was doing. We opened developer tools, inspected network traffic, filtered WebSocket connections, captured messages, and performed individual actions while comparing the resulting traffic.

That is a different way of thinking from simply following an API document. Instead of asking, "What does the documentation say?", we asked, "What does the application actually do?"

From there, the project quickly became a combination of many different areas of programming:

None of these subjects is particularly unusual by itself. What makes the project interesting is that they all have to work together.

Simple Goal, Complicated Path

Many programming projects look simple when described as a single sentence. The complexity often comes from the number of different systems that must cooperate to make that sentence true.

Learning by Connecting Different Areas

This is one of the reasons I like projects such as this one.

You cannot really learn the WebSocket client without touching networking. You cannot understand the chess messages without understanding chess notation. You cannot connect the chess engine without learning something about UCI. You cannot reproduce the browser behavior without reverse engineering. And you cannot keep everything manageable without learning how to structure the code.

Each subject becomes a reason to learn another subject.

For example, the line:

move = source | (target << 6)

looks like a tiny piece of Python. But understanding why it works requires several ideas at once: square numbering, binary representation, bit shifting, bit fields, the PlayOK protocol, and the relationship between the server's representation and python-chess.

Likewise, this apparently simple call:

self.ws.recv()

opens the door to a much larger subject. We have to think about blocking I/O, background threads, asynchronous events, connection lifetime, server state, message ordering, and what happens when the connection or the remote application behaves differently from our expectations.

That is how programming knowledge grows in real projects. One problem leads naturally to another.

Why Reverse Engineering Is Such a Useful Skill

One of the most valuable lessons from this project is learning how to work when the documentation is incomplete.

In real software development, we often receive clean documentation and well-defined APIs. But not every system gives us that luxury. Sometimes the documentation is incomplete, outdated, unavailable, or simply not detailed enough for what we want to build.

In those situations, observation becomes a technical skill.

We learned to capture traffic, change one thing at a time, compare messages, identify constants, look for changing values, inspect the browser's JavaScript, and form hypotheses from repeated observations.

The important part is that we did not need to understand everything.

We only needed to understand enough of the protocol to reproduce the behavior required by our application.

You Do Not Need the Whole Protocol

When reverse engineering an application, resist the temptation to decode everything. Start with the smallest useful subset. Once that subset works, expand it only when the project requires something new.

From Browser Behavior to Software Design

Another interesting part of the project was the transition from reverse engineering into ordinary software engineering.

At the beginning, we were simply looking at messages:

{"i":[72,575]}

Eventually that became:

self.send_command("join", table)

We moved from thinking about raw protocol details to thinking about useful abstractions.

The same thing happened with chess moves. Instead of making the rest of the application care about PlayOK's packed integer format, the engine module handles the conversion at the boundary. The rest of the program can continue thinking in terms of chess moves and board positions.

This is an important software-engineering lesson hidden inside a reverse-engineering project: once you understand an external system, hide its complexity behind a clean interface.

Testing Taught Us Something Different

The final tests also showed why real-world testing matters.

Our bot played two complete games. The first opponent disconnected. The second opponent resigned. After the first game, the bot continued running and found another game instead of requiring a restart.

Those results tested much more than the chess engine.

They tested the WebSocket connection, the lobby logic, table selection, state tracking, game initialization, chess-board synchronization, engine communication, move encoding, message sending, and the transition from one game back to the lobby.

At the same time, the test exposed functionality that we had not implemented yet: the program did not clearly report why the games ended.

That is exactly what a good test should do. It should demonstrate what works and reveal what still needs work.

The Project Is Bigger Than the Bot

The finished bot is the visible result of this book, but the code itself is not the most important result.

The more useful result is the collection of skills required to build it.

If you learned how to inspect a WebSocket connection, that skill can be used on another application.

If you learned how to analyze an undocumented message format, that skill can be used on another protocol.

If you learned how to communicate with a UCI engine, you can connect a different chess application to an engine.

If you learned how to use threads to separate network activity from the main application, that knowledge applies far beyond chess.

If you learned how to convert an unexplained stream of numbers into a useful abstraction, you have learned one of the fundamental habits of practical programming.

The PlayOK bot is simply the project that gives us a reason to practice all of these skills together.

A Free Book for Everyone

I have decided to release this book for free because I want the knowledge and the project to be accessible to anyone who wants to learn from it.

You do not need to be a professional programmer to follow the project. You can start with the individual chapters that interest you and gradually connect the ideas together. If you are learning Python, you can study the code. If you are interested in networking, you can study the WebSocket protocol. If you are interested in chess programming, you can follow the UCI and python-chess integration. If you are interested in reverse engineering, you can repeat the traffic-analysis process yourself.

And you do not have to stop at this project.

Take the techniques, change the application, experiment with the protocol, build your own client, break the code, fix it, and see what you discover.

Follow the Project, Then Go Further

The best way to learn from a project like this is not simply to read the finished code. Run it, inspect it, change it, add features, and try to explain to yourself why every part works. Then build something different with the same ideas.

Supporting the Work

Although the book is free, creating and maintaining projects like this takes a considerable amount of time.

There is the research itself, experimentation with the software, writing and rewriting the explanations, preparing examples, testing the code, maintaining the project, and answering questions from people who are following along.

If this book has helped you learn something useful, and you would like to support the work, you can make a donation via PayPal:

PayPal: maksymkorzh@gmail.com

A donation is completely optional. The book remains free, and everyone is welcome to use the material and follow the projects regardless of whether they contribute financially.

If you do choose to donate, thank you. Your support helps make it possible to continue creating free programming books, experiments, tutorials, and open educational projects.

Keep Building

We started this book with a simple question: how can we make our chess engine play on an online chess server?

The answer turned out to involve much more than chess.

We learned how to watch a browser communicate with a server. We learned how to recognize a WebSocket protocol hidden behind a web application. We discovered how numeric protocol fields could be mapped to meaningful commands. We connected Python to an external chess engine. We translated between different coordinate systems. We worked with binary representations and bit fields. We managed state across asynchronous messages. We tested the complete system against real opponents.

And eventually, the bot played.

That is the part I want you to remember.

Programming becomes much more interesting when we stop treating technologies as isolated subjects and start combining them to build something that actually works.

A WebSocket is one subject. Python is another. Chess programming is another. Reverse engineering is another. Threading is another. Protocol analysis is another.

But when we put them together, they become a working system.

That is what this project was really about.

So take the code, follow the experiments, ask questions, break things, fix them, and build something of your own.

The next interesting project is always waiting to be reverse engineered.