# MG2: Messages ## State Machine ``` +-------------+ placementPhase +------------------+ | Lobby state +--------------------->| Placement state | +-------------+ +-------+----------+ ^ | | | | | | | gameStart | | | | | | | v | gameOver +---------------+ +-----------------------------+ Running state | +---------------+ ``` ## Datatypes ### ServerState (Struct) | Field | Type | Explanation | | - | - | - | | teamShips | ShipState[] | | | actionPoints | int | Action points per player | | visibleHostileShips | Coordinate[] | Visible hostile ships | ### Config (Struct) | Field | Type | Explanation | | - | - | - | | serverName | String | Server Name | shipBalancings | ShipBalancing[2][] | | | shipsPerTeam | ShipType[2][] | | | N | int | Board size | | actionPointsGain | int | Action point gain per turn | | teamSizes | int[2] | Team sizes | ### ShipState (Struct) | Field | Type | Explanation | | - | - | - | | shipType | ShipType | | | position | Coord | | | currentHealth | int | Current health of the ship | | remainingCooldown | int | Remaining cooldown of the ship | | owner | String | Owner of the ship | ### Coordinate (Struct) | Field | Type | Explanation | | - | - | - | | x | int | x coordinate | | y | int | y coodinate | ### PlayerLobbyState (Struct) | Field | Type | Explanation | | - | - | - | | ready | bool | Is player ready | | playerID | int | Unique player ID | | name | String | Player name | ### Costs (Struct) | Field | Type | Explanation | | - | - | - | | cooldown | int | Number of rounds needed for cooldown | | actionPoints | int | Action points the action requires | ### CommonBalancing (Struct) | Field | Type | Explanation | | - | - | - | | shootCosts | Costs | | | shootRange | int | | | movementCosts | Costs | | | movementSpeed | int | | | abilityCosts | Costs | | | visionRange | int | | | initialHealth | int | | ### ShipBalancing (Struct) | Field | Type | Explanation | | - | - | - | | shipType | ShipTypeWithBalancing | - | | commonBalancing | CommonBalancing | - | ### ShipType (Struct) | Values | | - | | CARRIER | | BATTLESHIP| | CRUISER | | SUBMARINE| | DESTROYER| ### ShipTypeWithBalancing (Enum) | Values | | - | | CARRIER(ability: ScoutBalancing) | | BATTLESHIP (ability: MultiMissleBalancing)| | CRUISER(ability: EngineBoostBalancing) | | SUBMARINE(ability: TorpedoBalancing) | | DESTROYER(ability: PredatorMissileBalancing) | ### ScoutBalancing (Enum) | Field | Type | Explanation | - | - | - | | range | int | Range of the scout ability | | radius | int | Radius of scout ability | ### MultiMissleBalancing (Enum) | Field | Type | Explanation | - | - | - | | radius | int | Radius of missle ability | | damage | int | The damage of each rocket | ### PredatorMissileBalancing (Enum) | Field | Type | Explanation | - | - | - | | range | int | Range of the scout ability | | radius | int | Radius of scout ability | | damage | int | Damage of predator missile ability | ### EngineBoostBalancing (Struct) | Field | Type | Explanation | - | - | - | | steps | int | The ammount of fields the ship moves | ### TorpedoBalancing (Struct) | Field | Type | Explanation | - | - | - | | range | int | Range of the torpedo ability | | damage | int | The damage of the torpedo ability | ### ShipAction (enum) | Values | | - | | MOVE(moveProps: MoveProperties) | | SHOOT(shootProps: ShootProperties) | | ROTATE(rotateProps: RotateProperties) | | TORPEDO(torpedoProps: TorpedoProperties) | | SCOUT_PLANE(scoutProps: ScoutPlaneProperties) | | MULTI_MISSILE(multiMissileProps: MultiMissileProperties) | | PREDATOR(predatorProps: PredatorProperties) | | ENGINE_BOOST | ### MoveProperties (Struct) | Field | Type | Explanation | | - | - | - | | direction | MoveDirection | Direction to move ### ShootProperties (Struct) | Field | Type | Explanation | | - | - | - | | target | Coordinate | Target of Attack | ### RotateProperties (Struct) | Field | Type | Explanation | | - | - | - | | roatation | RoatationDirection | | ### TorpedoProperties (Struct) | Field | Type | Explanation | | - | - | - | | direction | Direction | Direction to shoot torpedo towards | ### ScoutPlaneProperties (Struct) | Field | Type | Explanation | | - | - | - | | center | Coordinate | Center point of scout ability | ### MultiMissileProperties (Struct) | Field | Type | Explanation | | - | - | - | | positions | Coordinate[3] | Positions to shoot at | ### PredatorMissileProperties (Struct) | Field | Type | Explanation | | - | - | - | | center | Coordinate | center of missile impact | ### Direction (Enum) | Values | | - | | NORTH | | EAST | | SOUTH | | WEST | ### MoveDirection (Enum) | Values | | - | | FORWARD | | BACKWARD | ### RotationDirection (Enum) | Values | | - | | COUNTER_CLOCKWISE | | CLOCKWISE | ### GameEndReason (Enum) | Values | | - | | DISCONNECT | | REGULAR(is_winner: bool) | ### ShipAssignment (Struct) | Field | Type | Explanation | | - | - | - | | shipNumber | int | | | coordinate | Coordinate | coordinate of ship origin | | direction | Direction | direction of ship | ## Error Codes | Number | Reason | | - | - | ## Messages ### Errors Responses can either be: - A success - A failure with a error code and an optional error message A response always contains the response type and, iff successfull all fields of the specific response. If the response is non-successfull it instead contains an error code and a optional error message. The equivalent rust type would be: ```rust enum Response<T> { Ok(T), Error(int, Option<String>) } ``` ### Serverbound #### Discovery ##### serverConfigRequest Every client can request the config of the server, even before joining the lobby. | Field | Type | Explanation | | - | - | - | ##### joinRequest Sent by the client to get to the server lobby. | Field | Type | Explanation | | - | - | - | | username | String | | #### Lobby ##### teamSwitchRequest | Field | Type | Explanation | | - | - | - | ##### setReadyStateRequest | Field | Type | Explanation | | - | - | - | | readyState | bool | New ready state | #### Placement ##### setPlacementRequest | Field | Type | Explanation | | - | - | - | | assignments | ShipAssignment[] | Assignments | #### Game ##### serverStateRequest | Field | Type | Explanation | | - | - | - | ##### shipActionRequest | Field | Type | Explanation | | - | - | - | | shipNumber | int | Origin of action | ### Clientbound #### Discovey ##### serverAdvertisment Sent via multicast to every device in the local network at a regular interval while the server is in the lobby state. | Field | Type | Explanation | | - | - | - | | address | String | Adress of server | ##### joinResponse: Response to joinRequest Response to the client who requested to join the lobby. | Field | Type | Explanation | | - | - | - | | playerId | int | Unique server assigned player id | ##### serverConfigResponse: Response to serverConfigRequest Response to the client who requested the server config. | Field | Type | Explanation | | - | - | - | | config | Config | Configuration of the server | #### Lobby ##### teamSwitchResponse: Response to teamSwitchRequest Response to the client who requested to switch teams. | Field | Type | Explanation | | - | - | - | ##### setReadyStateResponse: Response to setReadyStateRequest Response to the client who requested a ready state change. | Field | Type | Explanation | | - | - | - | ##### lobbyChangeEvent Sent to every client connected to the lobby to inform them about a change in the members of a team / the ready state of players. | Field | Type | Explanation | | - | - | - | | teams | PlayerLobbyState[2][] | | #### Placement ##### placementPhase Sent to every client who is connected to the lobby when everyone is in the ready state and the team sizes are not violated. | Field | Type | Explanation | | - | - | - | | corner | Coordinate | Coordinate of the bottom left corner | ##### placementResponse: Response to placementRequest Sent to the client who requested to place his ships. | Field | Type | Explanation | | - | - | - | #### Game ##### gameStart Note: This method moves the client from the Placement Phase to the Game Phase Sent to every client connected to the server once every player has sent a valid ship placement. | Field | Type | Explanation | | - | - | - | | state | ServerState | | ##### serverStateResponse: Response to serverStateRequest Sent to the client who requested the state of the game the server has. | Field | Type | Explanation | | - | - | - | | state | ServerState | | ##### shipActionResponse: Response to shipActionRequest Sent to the client who requested one of the ship actions. | Field | Type | Explanation | | - | - | - | ##### nextTurn Sent to every player in the game to indicate that it is now the turn of another player. | Field | Type | Explanation | | - | - | - | | nextPlayerId | int | The id of the next Player | | positionInQueue | int | turns remaining | ##### hitEvent Sent to the team of the player who performed the hit and the team of the player who received the hit. | Field | Type | Explanation | | - | - | - | | coordinate | Coordinate | Coordinate of the hit | | damage | int | Damage dealt | ##### destructionEvent Sent to the team of the player who performed the destructive shot and the team of the player who received the destructive shot. | Field | Type | Explanation | | - | - | - | | coordinate | Coordinate | Coordinate of destructive hit | TODO: Ask if destruction should display whole ship - (shipType) - (position) - (orientation) ##### visionEvent Sent to the players of a team once their collective vision changes. | Field | Type | Explanation | | - | - | - | | isShip | bool | Whether a ship is visible at the coordinate. When a ship gets out of vision, server sends a isShip=false and the client decides whether to draw fog of war or water | | coordinate | Coordinate | Where the vision event occured | ##### shipActionEvent Sent to the team of the player who performed the ship action. | Field | Type | Explanation | | - | - | - | | action | ShipAction | Ship Action | | shipNumber | int | Origin of action | ##### gameOverEvent Sent to every player in the game once a team won or a player left the game. | Field | Type | Explanation | | - | - | - | | reason | GameEndReason | | ## Example P is the current player E is a enemy of P T is a teammate of P S is the server ### Successfull torpedo attack 1. (P -> S) shipActionRequest { action: TORPEDO::({Direction::NORTH}), ship_number: 2 } 2. (S -> P) Response::OK ({ shipActionResponse { } } 3. (S -> P,T) shipActionEvent { action: TORPEDO::({Direction::NORTH}), ship_number: 2 } 4. (S -> P,T,E) hitEvent { coordinate: (21, 16), damage: 400 } 5. (S -> P,T,E) destructionEvent { coordinate: (21, 16) } ### Failed torpedo attack 1. (P -> S) shipActionRequest { action: TORPEDO::({Direction::NORTH}), ship_number: 2 } 2. (S -> P) Response::Error { error_code: 501, error_msg: "Cruiser cant shoot torpedo" } ## Tasks | Tasks | Assigned | - | - | Consider all Request Response Pairs and add Error Codes to the Error table for possible errors| | Explain the Error Mechanism for Responses| | Specify all Client-Bound Discovery Messages in the RFC| | Specify all Client-Bound Lobby Messages in the RFC| | Specify all Client-Bound Placement Messages in the RFC|MG2 | Specify all Client-Bound Game Messages in the RFC | | Specify all Server-Bound Discovery Messages in the RFC| | Specify all Server-Bound Lobby Messages in the RFC| | Specify all Server-Bound Placement Messages in the RFC| | Specify all Server-Bound Game Messages in the RFC | |ServerState, ShipState, ShipType |ShipBalancing, ShipTypeWithBalancing, CommonBalancing, Costs |Config, PlayerLobbyState, Coordinate, ShipAssignment |ScoutBalancing, MultiMissileBalancing, PredatorBalancing, EngineBoostBalancing, TorpedoBalancing |MoveProperties, ShootProperties, RotateProperties, ScoutProperties, MultiMissileProperties, PredatorProperties, EngineBoostProperties, TorpedoProperties |Direction, MoveDirection, RotationDirection, GameEndReason| MG2
{"metaMigratedAt":"2023-06-17T16:18:42.665Z","metaMigratedFrom":"Content","title":"MG2: Messages","breaks":true,"contributors":"[{\"id\":\"e3c1f485-7680-409c-9f79-a172360c4c0f\",\"add\":13845,\"del\":8269},{\"id\":\"0943d44d-0cad-414b-b706-a89910e877be\",\"add\":5989,\"del\":1247},{\"id\":null,\"add\":3747,\"del\":1380}]"}
    176 views