This repository has been archived on 2024-01-29. You can view files and clone it, but cannot push or open issues or pull requests.
distributed_system_coursework/README.md

246 lines
8.7 KiB
Markdown
Raw Normal View History

Andre Goncalves Henriques (URN: 6644818), Seoeun Lee (URN: 6595203)
2024-01-19 21:25:30 +00:00
# Cooperative Application "Atomas" Game
2024-01-19 21:25:30 +00:00
The application that this project implements is a cooperative version of the game [Atomas](https://play.google.com/store/apps/details?id=com.sirnic.atomas&hl=en&gl=US&pli=1).
Atomas is a simple single player mobile phone game which allows players to combine atoms to make new atoms with higher atomic numbers.
2024-01-19 21:25:30 +00:00
The Server application implements a cooperative and simplified version of this game that allows players to play a game of Atomas in the same board at the same time.
2024-01-19 21:25:30 +00:00
The Server uses Paxos to guarantee that only one user can play at a time and that a player cannot make a move on a different game state than another player.
The `server.ex` file provides both `Server` Module that runs this service and a `Client` Module that can be used to display the game state in the terminal with nicer representation.
2024-01-19 21:25:30 +00:00
# API
The server provides the following API:
```elixir
{:start_game, participants, pid_to_inform}
{:get_game_state, game_id, pid_to_inform}
{:make_move, game_id, move, pid_to_inform}
```
## Important Concepts
2024-01-20 22:57:44 +00:00
The `game_id` is a number and is an identifier for the game. This can be a number as paxos guarantees that there cannot be two games with same number, as the server would have learned about that decision and would have incremented the number.
2024-01-19 21:25:30 +00:00
The `game_state` is a representation of a game state that corresponds to an array of numbers, and some special atoms, where the number represents the atomic number. The maximum length of this array is 16, so when an array gets bigger than that the game is over.
2024-01-19 21:25:30 +00:00
## Start Game
The `:start_game` message causes the server to create a new game.
The `:start_game` message takes the arguments `participants` and `pid_to_inform`.
- `participants` is a list of names of the servers that are participating in the game.
- `pid_to_inform` is a pid of the process that needs to be informed of the success or failure of this action.
2024-01-19 21:25:30 +00:00
For example, to request the start of a game, a user might do somethig like:
2024-01-19 21:25:30 +00:00
```elixir
send(server_pid, {:start_game, [:p0, :p1], self()})
# use recieve do to wait for the message
# Or, assuming the server was started with the name :p0
Server.start_game(:p0, [:p0, :p1])
```
The server will answer with:
```
{:start_game_ans, game_id}
```
This request will always create a new game (with the exception of crashes).
## Get Game State
The `:get_game_state` message causes the server to return the current state of a game.
The `:get_game_state` message takes the arguments as `game_id` and `pid_to_inform`.
- `game_id` is the game_id returned by the `:start_game` request
- `pid_to_inform` is a pid of the process that needs to be informed of the success or failure of this action.
2024-01-19 21:25:30 +00:00
For example, to request the current state of a game:
2024-01-19 21:25:30 +00:00
```elixir
send(server_pid, {:get_game_state, 1, self()})
# use 'recieve do' to wait for the message
2024-01-19 21:25:30 +00:00
# Or, assuming the server was started with the name ':p0'
2024-01-19 21:25:30 +00:00
Server.get_game_state(:p0, 1)
```
The server will answer with one of the following:
```elixir
{:game_state, game_id, :not_playing} # When the player is not playing in the game
{:game_state, game_id, :game_does_not_exist} # When the game was never created
{:game_state, game_id, :game_finished, score} # When the game is already over
{:game_state, game_id, game_state, hand} # When the player is in the game, and the game has started
2024-01-19 21:25:30 +00:00
# Returns the current game state of the game,
# and the current "atom" on the hand of the player
```
## Make a move
The `:make_move` message causes the server to try to play the move that the player is requesting.
The `:make_move` message takes the arguments as `game_id`, `move`, and `pid_to_inform`.
- `game_id` is the game_id returned by the `:start_game` request
- `move` is the position in the game state array where the play wants to insert their "atom"
- `pid_to_inform` is a pid of the process that needs to be informed of the success or failure of this action.
2024-01-19 21:25:30 +00:00
For example, to request the current state of a agame:
2024-01-19 21:25:30 +00:00
```elixir
send(server_pid, {:make_move, 1, 3, self()})
# use recieve do to wait for the message
# Or, assuming the server was started with the name :p0
Server.make_move(:p0, 1, 3)
```
The server will answer with one of the following:
```elixir
{:make_move, game_id, :not_playing} # When the player is not playing in the game
{:make_move, game_id, :game_does_not_exist} # When the game was never created
{:make_move, game_id, :game_finished, score} # When the game is already over
{:make_move, game_id, :invalid_move} # When the move requested by the player is an invalid move
{:make_move, game_id, :player_moved_before, game_state, hand} # When the player is in the game and the game has started,
# but another player has played the game before this request was made
2024-01-19 21:25:30 +00:00
# Returns the current game state of the game,
# and the current "atom" on the player's hand
2024-01-19 21:25:30 +00:00
{:make_move, game_id, game_state, hand} # When the player is in the game and the game has not finished
# and the move is valid
# Returns the current game state of the game,
# and the current "atom" on the hand of the player
```
# Properties
- If a game exists, then there was a process that creates it.
- Eventually, all processes will be in the same game state
- A player cannot make a move in a game state unless it's not updated to the last game state
- If two players make a move at the same time in the same game, only one of the moves happens and the other player eventually gets notified
- If a player makes a valid move it will be evetually played
2024-01-19 21:25:30 +00:00
# Assumptions
- Processes won't behave in a byzantine way
2024-01-20 18:51:54 +00:00
- Only a minority of processes can fail
- Will run in a partial synchronous system
- When the game is created, the game state will have atoms in it, and the participants will have atoms in their hands
2024-01-19 21:25:30 +00:00
# Usage instructions
There are two ways of interacting with the server: via the API or the `Client` Module
2024-01-20 17:50:23 +00:00
## Notes
By default, all the logs are disabled to enable all the logs inside the paxos file. There is a module variable called `@min_print_level`.
Setting it to 0 enables all the logs
2024-01-20 17:50:23 +00:00
## API
Start a new iex session and compilie `paxos.ex` and `server.ex`:
```elixir
c "paxos.ex"; c "server.ex"
```
** Paxos needs to be compiled before the server as the server requires macros written in the paxos file **
Now start 3 instances of the server:
```elixir
procs = Enum.to_list(1..3) |> Enum.map(fn x -> :"p#{x}" end)
pids = procs |> Enum.map(fn x -> Server.start(x, procs) end)
```
After the 3 instances have been started, you can use Server helper functions to communicate with the Server:
2024-01-20 17:50:23 +00:00
```elixir
Server.start_game(:p1, [:p1, :p2])
Server.get_game_state(:p1, 1)
Server.make_move(:p1, 1, 0)
```
## Using the Client
Open 2 different terminals. One terminal will display the game, and the other one will be used to control the game
2024-01-20 17:50:23 +00:00
In the first terminal, start an iex session with a session name:
2024-01-20 17:50:23 +00:00
```
iex --sname c1
```
In the second terminal, start an iex session with a session name:
2024-01-20 17:50:23 +00:00
```
iex --sname c2
```
On the first terminal, connect the first terminal to the second terminal:
2024-01-20 17:50:23 +00:00
```elixir
# In c1
Node.connect(:"c2@<computer_name>")
```
On both terminals compile paxos and server:
```elixir
# In both run
c "paxos.ex"; c "server.ex"
```
Start the servers on the first terminal, create a game, and enter display mode:
2024-01-20 17:50:23 +00:00
```elixir
# In c1
procs = Enum.to_list(1..3) |> Enum.map(fn x -> :"p#{x}" end)
pids = procs |> Enum.map(fn x -> Server.start(x, procs) end)
Server.start_game(:p1, [:p1, :p2])
Client.display_game(:p1, 1)
```
You now should see something like:
```
0 H
1
Be
H
5 2
Be
Be Li
4 3
Be
```
The Center "Atom" is your hand, while the Letters arround are the "game_state" or "board",
2024-01-20 17:50:23 +00:00
the number represents where you can put the "Atom" in your hand
Start control mode on the second terminal by entering:
2024-01-20 17:50:23 +00:00
```elixir
# In c2
Client.control_game(:p1, 1)
```
You should see:
```
Type the number you want to play or q to exit:
```
On the second terminal, insert the number in where you want to make your move and hit enter!
2024-01-20 17:50:23 +00:00
The map should update on the top screen and now you can play the game!