Ropasci is a P2P application for playing the ROck-PAper-SCIssors game among peers.
Rock beats scissors. Scissors beat paper. Paper covers rock.
Press the buttons to make your move.
This game is written in Java and relies on JavaFX for the GUI. Internally, the game logic is maintained separately from the multithreaded GUI and networking code and responds to events generated by them. Thus, the GUI drives events being pushed to the peers, and the networking code bubbles up the P2P state changes.
Peers connect to each other and gossip their own connected peers to build a fully-connected P2P topology. In each gameplay round, only the connected participants contributeto the game's progress.
Each peer spins up a NetworkManager
responsible for orchestrating events between the P2P network and the GUI. A simple TCP-based ServerSocket
listens for connecting peers, with new peers being added to a thread-safe list contained in a Supervisor
.
A simple handshake-based binary protocol is used. When two peers connect, they exchange a length-prefixed payload containing a handshake byte and their peer ID. Upon successful handshaking, the two peers can communicate their usernames to each other and commence sending game state updates.
Each subsequent message is a length-prefixed snippet of the message type followed by an optional data segment, depending on the message type.
When messages are successfully decoded from their binary format into their Java representations, they're bubbled up to the UI and game state logic.