Chapter 2

Project Structure

Before we start reverse engineering individual WebSocket messages, it is useful to decide how the final program will be organized. The goal is not to reproduce the entire PlayOK website. We only need to build the small part of the browser client that is necessary for an external chess engine to play games through PlayOK.

The project will therefore be split into four main Python files. Each file has a specific responsibility, and keeping those responsibilities separate will make the reverse engineering process much easier to follow.

The Four Parts

At a high level, the application will look like this:

The important idea is that these components communicate with each other rather than becoming one large program. The PlayOK client should not need to know how the chess engine searches for moves, and the chess engine interface should not need to know how PlayOK's WebSocket protocol works internally.

config.py

The config.py file will contain configuration that is shared by the rest of the application.

This includes the credentials required to connect as a PlayOK user, connection-related settings, and constants representing the different WebSocket commands and message types that we discover during the reverse engineering process.

One reason for keeping the WebSocket command constants here is readability. Reverse engineered protocols often contain messages identified by numbers. When we are looking through the code, a raw integer does not tell us much about its purpose. Giving these values human-readable names allows the rest of the program to describe what it is doing instead of forcing us to remember what every encoded value means.

The configuration file will therefore act as a central place for values that describe the environment and the protocol, while the actual behavior of the client will remain in the other modules.

engine.py

The engine.py file will be responsible for communicating with the UCI chess engine.

Our chess engine already understands chess and already knows how to calculate a move. What it does not understand is PlayOK. From the engine's point of view, it is simply communicating through the UCI protocol.

This module will provide the bridge between those two worlds. It will start and communicate with the UCI engine, maintain the chess position required by the engine, request moves, and receive the engine's responses.

It will also handle the conversion between the representation used by UCI and the representation expected by PlayOK. This separation is important because the two protocols have completely different purposes. UCI is designed for communication between chess software, while PlayOK's WebSocket protocol is designed for communication between the PlayOK browser client and its server.

The engine module therefore becomes the chess-specific side of our bridge. It knows about chess positions and UCI moves, but it does not need to know how a WebSocket connection is established or how PlayOK tables are managed.

playok.py

The playok.py file will contain our custom PlayOK client.

PlayOK already has a complete JavaScript client running inside the browser. That client handles many things that are useful to a human player: displaying the interface, managing menus, handling chat, updating controls, displaying tables, processing user interaction, and communicating with the PlayOK server.

We do not need to reproduce all of that.

Our client will be a much smaller implementation containing only the functionality required for the chess bot. Its job is to establish the WebSocket connection, authenticate or identify the user, understand the relevant server messages, detect games and challenges, join the appropriate game, receive opponent moves, and send our own moves.

In other words, we are building a minimal PlayOK client specifically for a chess engine. It will not have a graphical interface and it will not attempt to reproduce the entire PlayOK browser application. It only needs to reproduce the protocol behavior that matters to our bot.

This is an important principle in reverse engineering. We are not trying to clone the original application. We are trying to understand enough of its communication protocol to implement the subset of behavior that our own application requires.

main.py

The main.py file will be the entry point of the application.

It will provide a simple console-based interface through which we can control the bot and issue user-defined commands. The main program will connect the different components together without containing the implementation details of either the PlayOK protocol or the chess engine.

This gives us a clean separation between the interface and the underlying systems. The console can tell the PlayOK client what to do, while the PlayOK client can communicate with the engine when a chess move is required.

Why Keep It Simple?

There is a temptation when building a project like this to create a large framework around it. We could introduce many classes, abstractions, configuration systems, and helper modules before the bot even plays its first game.

That would work against the purpose of this project.

The interesting part of this book is understanding the protocol and building the bridge between PlayOK and the chess engine. The code should therefore remain as simple as possible while still keeping the major responsibilities separated.

As we reverse engineer more of the protocol, the individual files will naturally become more sophisticated. New message types will appear, more game states will need to be handled, and additional conversion logic will be required. Having a clear project structure from the beginning will allow that complexity to grow without turning the entire program into one large block of code.

By the end of the project, these four components will work together as a small custom client: main.py controls the application, playok.py communicates with PlayOK, engine.py communicates with the UCI engine, and config.py keeps the configuration and protocol definitions in one place.

Separate Responsibilities

Each module should have one clear responsibility. Configuration describes the protocol, the engine handles chess, the PlayOK client handles communication and server state, and the main program provides the user interface. Keeping these responsibilities separate makes the reverse-engineered code much easier to change later.