Skip to content

Question about creating a server for each game session #33

@henry-hz

Description

@henry-hz

Dear @iacobson , thanks for sharing the amazing Ecspanse lib! In case we want to create a server for each game session, in order to optimize the latency in case we have many game sessions simultaneously, let me know if this approach [AI gen] would be fine:

Yes, if you want to have one server per group of players, you would need to modify the architecture to dynamically spawn Demo servers.
Currently, the Demo module is started as a single instance in the supervision tree.

Here's a high-level approach to support multiple game instances:

1 Create a DynamicSupervisor to manage multiple game instances:

defmodule Demo.GameSupervisor do                                                                                                     
  use DynamicSupervisor                                                                                                              
                                                                                                                                     
  def start_link(_) do                                                                                                               
    DynamicSupervisor.start_link(__MODULE__, nil, name: __MODULE__)                                                                  
  end                                                                                                                                
                                                                                                                                     
  def init(_) do                                                                                                                     
    DynamicSupervisor.init(strategy: :one_for_one)                                                                                   
  end                                                                                                                                
                                                                                                                                     
  def start_game(game_id) do                                                                                                         
    # Each game would need its own unique name                                                                                       
    child_spec = %{                                                                                                                  
      id: Demo,                                                                                                                      
      start: {Demo, :start_link, [game_id]},                                                                                         
      restart: :transient                                                                                                            
    }                                                                                                                                
    DynamicSupervisor.start_child(__MODULE__, child_spec)                                                                            
  end                                                                                                                                
end                                                                                                                                  

2 Modify the application supervision tree:

 defmodule Demo.Application do                                                                                                        
   use Application                                                                                                                    
                                                                                                                                      
   @impl true                                                                                                                         
   def start(_type, _args) do                                                                                                         
     children = [                                                                                                                     
       Demo.GameSupervisor                                                                                                            
       # Other supervisors/workers...                                                                                                 
     ]                                                                                                                                
                                                                                                                                      
     opts = [strategy: :one_for_one, name: Demo.Supervisor]                                                                           
     Supervisor.start_link(children, opts)                                                                                            
   end                                                                                                                                
 end                                                                                                                                  

3 Modify the Demo module to accept a game_id and use it for naming:

defmodule Demo do                                                                                                                    
  use Ecspanse                                                                                                                       
                                                                                                                                     
  def start_link(game_id) do                                                                                                         
    Ecspanse.start_link(__MODULE__, game_id, name: via_tuple(game_id))                                                               
  end                                                                                                                                
                                                                                                                                     
  # Use Registry to track game instances                                                                                             
  defp via_tuple(game_id) do                                                                                                         
    {:via, Registry, {Demo.GameRegistry, game_id}}                                                                                   
  end                                                                                                                                
                                                                                                                                     
  # Rest of the module...                                                                                                            
end                                                                                                                                  

4 Then you could start new game instances like:

# Start a new game for group "group_1"                                                                                               
{:ok, pid} = Demo.GameSupervisor.start_game("group_1")                                                                               
                                                                                                                                     
# Start another game for group "group_2"                                                                                             
{:ok, pid2} = Demo.GameSupervisor.start_game("group_2")                                                                              

You would also need to:

1 Add a Registry to track the game instances
2 Modify your game logic to scope operations to specific game instances
3 Consider how to clean up/stop game instances when they're no longer needed
4 Handle game state persistence if needed
5 Consider how to route player actions to the correct game instance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions