GameAppWeb.PlayerLive.Index (game_app v0.1.0)

LiveView module for listing players.

Summary

Functions

Overview

Handles the "delete" event by attempting to remove a player based on the provided ID, updating the client's interface and internal state as necessary.

Handles incoming messages for updating player information in the LiveView.

Handle the LiveView's URL params.

Mount callback for the LiveView.

Callback implementation for Phoenix.LiveView.render/1.

Functions

Link to this function

handle_event(binary, map, socket)

Overview

Handles the "delete" event by attempting to remove a player based on the provided ID, updating the client's interface and internal state as necessary.

Parameters

  • "delete": Event name indicating a deletion request.
  • %{"id" => id}: Payload containing the ID of the player to be deleted.
  • socket: Current LiveView socket.

Functionality

  1. Player Retrieval: Attempts to retrieve the player by ID from the accounts system.
  2. Deletion Process:
  • Successful Player Retrieval:
    • Successful Deletion: If the player is successfully deleted, broadcasts a deletion update and maintains the socket state.
    • Failed Deletion: If the deletion fails, displays an error message to the user without altering the socket's assigned data.
    • Player Not Found: If no player is found, assumes the player has already been deleted or does not exist and broadcasts a deletion message to ensure UI consistency.
Link to this function

handle_info(arg, socket)

Handles incoming messages for updating player information in the LiveView.

This function has three clauses:

Handling :saved Message

Overview

Handles the :saved message from the PlayerLive.FormComponent, broadcasting an update and maintaining the socket state.

Parameters

  • {GameAppWeb.PlayerLive.FormComponent, {:saved, player}}: Tuple indicating the source of the message and the action, along with the player data.
  • socket: Current LiveView socket.

Functionality

  • Broadcast Update: Sends a broadcast about the updated player using the broadcast_update/2 function, notifying other parts of the application about the change.
  • Socket Maintenance: No changes are made to the socket, and it simply passes through to maintain the current state.

Processing Player Updates

Overview

Processes updates for players, either adding new ones or updating existing ones, unless the player is marked as deleted.

Parameters

  • {:update_player, new_player}: Tuple containing the action and the player data.
  • socket: Current LiveView socket.

Functionality

  1. Check Deletion Status: Skips updates if the player ID is found in the deleted_player_ids set.
  2. Player Update Handling:
  • New Player: Adds the player to the players map and updates the client view if the player does not exist.
  • Existing Player: Updates the player data using get_latest/2 if the player already exists, ensuring only the most recent data is used.
  1. Stream Updates:
  • Uses stream_insert to send real-time updates to the client, reflecting changes in the player list.

Handling Player Deletion

Overview

Handles the deletion of a player by updating the application's state and client view accordingly.

Parameters

  • {:delete_player, player}: Tuple indicating the action and the player to be deleted.
  • socket: Current LiveView socket.

Functionality

  1. Remove Player: Deletes the player from the players map using the player's ID.
  2. Track Deletion: Adds the player's ID to the deleted_player_ids set to prevent future operations on this player.
  3. Update Client View:
  • Assigns the updated players map and deletion set back to the socket.
  • Initiates a stream_delete to remove the player from the client's display in real-time.
Link to this function

handle_params(params, url, socket)

Handle the LiveView's URL params.

Link to this function

mount(params, session, socket)

Mount callback for the LiveView.

Overview

Initializes the LiveView for managing quiz topics, setting up real-time updates and data relevant to the user's session.

Parameters

  • _params: Query strings or POST parameters (unused).
  • _session: Session data (unused).
  • socket: Current LiveView socket.

Functionality

  1. User and Account Data:
  • Extracts account_id and user_id from the socket.
  1. PubSub Subscriptions:
  • Subscribes to personalized topics based on user_id and account_id for receiving real-time updates on questions and topic changes.
  1. Data Assignment:
  • Retrieves and assigns topic visibility options and topics accessible to the user.
  • Assigns additional flags like is_admin based on user roles for client-side use.
  1. Socket Updates:
  • Streams topics to the client and updates the socket with necessary visibility options and statuses.
Link to this function

render(assigns)

Callback implementation for Phoenix.LiveView.render/1.