socha.api.networking.game_client¶
This module handles the communication with the api and the students‘ logic.
Module Contents¶
Classes¶
The PlayerClient handles all incoming and outgoing objects accordingly to their types. |
- class socha.api.networking.game_client.IClientHandler¶
- history: List[List[socha._socha.GameState | socha.api.protocol.protocol.Error | socha.api.protocol.protocol.Result]] = []¶
- calculate_move() socha._socha.Move¶
Calculates a move that the logic wants the server to perform in the game room.
- on_update(state: socha._socha.GameState) None¶
If the server _send an update on the current state of the game this method is called. :param state: The current state that server sent.
- on_game_over(roomMessage: socha.api.protocol.protocol.Result) None¶
If the game has ended the server will _send a result message. This method will call if this happens.
- Parameter:
roomMessage – The Result the server has sent.
- on_error(logMessage: str) None¶
If error occurs, for instance when the logic sent a move that is not rule conform, the server will _send an error message and closes the connection. If this happens, this method is called.
- Parameter:
logMessage – The message, that server sent.
- on_room_message(data) None¶
If the server sends a message that cannot be handheld by anny other method, this will be called.
- Parameter:
data – The data the Server sent.
- on_game_prepared(message) None¶
If the game has been prepared by the server this method will be called.
- Parameter:
message – The message that server sends with the response.
- on_game_joined(room_id) None¶
If the client has successfully joined a game room this method will be called.
- Parameter:
room_id – The room id the client has joined.
- on_game_left() None¶
If the server left the room, this method will be called. If the client is running on survive mode it’ll be running until shut downed manually.
- while_disconnected(player_client: GameClient) None¶
The client loop will keep calling this method while there is no active connection to a game server. This can be used to do tasks after a game is finished and the server left. Please be aware, that the client has to be shut down manually if it is in survive mode.
- Rückgabe:
True if the client should shut down. False if the client should continue to run.
- on_create_game(game_client: GameClient) None¶
This method will be called if the client is in admin mode and the client has authenticated with the server. The client can now create a game.
- Parameter:
game_client – The client that is in admin mode.
- on_prepared(game_client: GameClient, room_id: str, reservations: List[str]) None¶
This method will be called if the client is in admin mode and the client has created a game.
- Parameter:
game_client – The client that is in admin mode.
room_id – The room id of the game.
reservations – The reservations of the game.
- on_observed(game_client: GameClient, room_id: str)¶
This method will be called if the client is in admin mode and the client is observing a game.
- Parameter:
game_client – The client that is in admin mode.
room_id – The room id of the game.
- while_waiting() None¶
This method will be called while the client is waiting for the server to send a new message. This method is running threaded, which will be terminated without warning if the client receives a new message.
- class socha.api.networking.game_client.GameClient(host: str, port: int, handler: IClientHandler, reservation: str, room_id: str, password: str, auto_reconnect: bool, survive: bool, headless: bool)¶
Bases:
socha.api.networking.xml_protocol_interface.XMLProtocolInterfaceThe PlayerClient handles all incoming and outgoing objects accordingly to their types.
- join_game()¶
- join_game_room(room_id: str)¶
- join_game_with_reservation(reservation: str)¶
- authenticate(password: str)¶
- create_game(player_1: socha.api.protocol.protocol.Slot, player_2: socha.api.protocol.protocol.Slot, game_type: str, pause: bool)¶
- observe(room_id: str)¶
- cancel(room_id: str)¶
- step(room_id: str)¶
- pause(room_id: str, pause: bool)¶
- send_message_to_room(room_id: str, message)¶
- _on_object(message)¶
Process various types of messages related to a game.
- Args:
message: The message object containing information about the game.
- Returns:
None
- _on_move_request(room_id)¶
- _on_state(message)¶
- start()¶
Starts the client loop.
- join() None¶
Tries to join a game with a connected server. It uses the reservation, or room id to join. If they are not present it joins just without, as fallback.
- _handle_left()¶
- _client_loop()¶
The client loop is the main loop, where the client waits for messages from the server and handles them accordingly.
- stop()¶
Disconnects from the server and stops the client loop.