KhanalAmsul / tictactoe

0 stars 0 forks source link

Multiplayer #1

Open Jeremy-Barnes opened 4 years ago

Jeremy-Barnes commented 4 years ago

A quick note about how hosts and clients (servers and players) get along:

A player cannot invoke a method on another player's machine, not directly. This is partly for network topology reasons, but also because that would be dangerous and stupid. So when I want to SendMessage to you, I can't just call your ReceiveMessage method. Instead, I have to call a server side method - SendChat - that can validate and, if needed, reject my message. If the server permits it, my message is sent to everyone. This is done by the server invoking methods on all client machines. So any operation that is client to client is actually client -> server, server -> client.

Currently, the engine (Game) assumes that the input that comes from the console will alternate between X and O. In order to convert to multiplayer, we need a few things:

1) A way to communicate the game state over the wire to other players (and spectators). 2) A way to assign X or O status to an arbitrary player 3) The game engine must know who's turn it is and whether or not it needs to consult the Console for input or a networked player for input.

For #1 move the game state into its own class. That object should track who's turn it is (X or O) and it should also contain the board. This can then be serialized to JSON, creating an easy format to send to other players.

Do this before pursuing #2 and #3 - make local tictactoe use a custom game state object rather than storing the board and who's turn it is in the engine.

For all of these, we will need a way for the netcode to update the local player's game state - the engine will have to create a way for the NetworkPlayer to access the game's state.

For #2 NetworkPlayer needs a new method that is invoked by the server (like ReceiveMessage) where the server assigns a side (X or O) to the player. This can receive a 0 or 1, true or false, X or O, whatever, so long as it knows how to tell one player they are X or O. When the new method is invoked, it should update that player's engine (this should not be stored in the game state class) so that it knows who plays which turn.

For #3 the engine should know if X or O is local (or neither - spectator), and when it is

local's turn: the game state should be updated based on Console input (as normal), and then the new game state should be pushed out to everyone via a new method in NetworkPlayer that is invoked by the local machine (like SendMessage) - call it SendGameState(). This method should invoke a new NetworkHost method that sends all players and spectators the new game state - BroadcastGameState(). This new server method will invoke another new method on every client, ReceiveGameState(), which in turn will update that machine's local game state (as described before #1), to reflect the new board state and that it is now the other player's turn (X or O)

network player's turn: the engine should enter a loop inside the main game loop, and the thread should do nothing for 1 second. The loop should execute forever until the game state reflects that it is local's turn again, when the board will rerender and show the result of the network player's move.

Remember, each player is local player on their own machine.

Jeremy-Barnes commented 4 years ago

@KhanalAmsul poke

Jeremy-Barnes commented 4 years ago

@KhanalAmsul prod

Jeremy-Barnes commented 4 years ago

@KhanalAmsul start thinking about item #3, it will involve small changes to the main game loop and some new methods in NetworkHost and NetworkPlayer.

Jeremy-Barnes commented 4 years ago

beep

KhanalAmsul commented 4 years ago

beep beep. definitely going to need your help understanding the networkplayer and networkhost files as I think that's a little beyond me right now :).

but basically thought process is this right:

at start of game detect if it is local turn or network turn:

if local turn proceed as if local game -> then change to network turn -> then network player goes and it reverts back to local turn?

Jeremy-Barnes commented 4 years ago

Yep! I'm free to help pretty much whenever, just hit me on Discord.