From 7db71ccd2a95347bffcade6e95e417e7c2d316cb Mon Sep 17 00:00:00 2001 From: Henricus Louwhoff Date: Wed, 20 Dec 2023 19:52:02 +0100 Subject: [PATCH] Add player spawned check This adds an extra check to see if the player has already spawned before firing the spawn ship event. This will prevent the error that an entity with that same ID has already been added in case the player decides to refresh the page. --- lib/ship_web/live/game_live.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/ship_web/live/game_live.ex b/lib/ship_web/live/game_live.ex index fd57b36..485a1fb 100644 --- a/lib/ship_web/live/game_live.ex +++ b/lib/ship_web/live/game_live.ex @@ -20,9 +20,11 @@ defmodule ShipWeb.GameLive do |> assign_loading_state() if connected?(socket) do - ECSx.ClientEvents.add(player.id, :spawn_ship) - # The first load will now have additional responsibilities - send(self(), :first_load) + unless PlayerSpawned.exists?(player.id) do + ECSx.ClientEvents.add(player.id, :spawn_ship) + # The first load will now have additional responsibilities + send(self(), :first_load) + end end {:ok, socket}