local A=game:GetService("\080\108\097\121\101\114\115")local v=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local Y=v:WaitForChild("\108\105\098")local i=v:WaitForChild("\099\111\109\109\111\110")local C=require(Y:WaitForChild("\080\105\122\122\097\065\108\112\097\099\097"))local R=require(Y:WaitForChild("\080\114\111\109\105\115\101"))local M=require(i:WaitForChild("\083\101\108\101\099\116\111\114\115"))local k=require(i:WaitForChild("\065\099\116\105\111\110\115"))local H=require(i:WaitForChild("\084\104\117\110\107\115"))local c=require(Y:WaitForChild("\083\105\103\110\097\108"))local p=C.GameModule:extend("\080\108\097\121\101\114\072\097\110\100\108\101\114")p.playerLoaded=c.new()local function f(A,v,Y)print("\076\111\097\100\105\110\103 \100\097\116\097 \102\111\114\058 ",A)wait(2)v:dispatch(H.PLAYER_JOINED(A,Y))v:dispatch(H.ASSET_TRYGIVE(A,"\098\097\115\101\098\097\108\108\050\048\048\055"))v:dispatch(H.ASSET_TRYGIVE(A,"\098\097\110\100\105\116"))v:dispatch(H.ASSET_TRYGIVE(A,"\098\097\099\111\110\104\097\105\114"))v:dispatch(H.ASSET_TRYGIVE(A,"\097\114\114\111\119"))v:dispatch(H.ASSET_TRYGIVE(A,"\102\111\111\116\098\097\108\108\072\101\108\109\101\116"))v:dispatch(H.ASSET_TRYGIVE(A,"\109\097\116\101\114\105\097\108\095\112\097\115\116\101\108\114\101\100"))v:dispatch(H.ASSET_TRYGIVE(A,"\109\097\116\101\114\105\097\108\095\112\097\115\116\101\108\100\101\101\112\103\114\101\101\110"))v:dispatch(H.ASSET_TRYGIVE(A,"\109\097\116\101\114\105\097\108\095\112\097\115\116\101\108\099\121\097\110"))v:dispatch(H.ASSET_TRYGIVE(A,"\109\097\116\101\114\105\097\108\095\112\097\115\116\101\108\121\101\108\108\111\119"))Y:initialPlayerState(A,v:getState())p.playerLoaded:fire(A)print("\076\111\097\100\101\100\033")end local function P(A,v)v:dispatch(H.PLAYER_LEAVING(A))end function p.playerReady(v,A)local Y=v.core:getModule("\083\101\114\118\101\114\065\112\105")local i=v.core:getModule("\083\116\111\114\101\067\111\110\116\097\105\110\101\114");(R.all({i:getStore();Y:getApi()})):andThen(function(v)return R.async(function(Y,i)local C,R=unpack(v)f(A,C,R)end)end)end function p.onStoreAndApi(i,v,Y)A.PlayerRemoving:Connect(function(A)P(A,v)end)end function p.postInit(A)local v=A.core:getModule("\083\101\114\118\101\114\065\112\105")local Y=A.core:getModule("\083\116\111\114\101\067\111\110\116\097\105\110\101\114");(R.all({Y:getStore(),v:getApi()})):andThen(function(v)return R.async(function(Y,i)A:onStoreAndApi(unpack(v))end)end)end function p.getLoadedPlayers(Y,v)local i={}for A,Y in pairs(A:GetPlayers())do local C=M.getPlayerState(v:getState(),Y)if C then table.insert(i,Y)end end return i end return p
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local lib = ReplicatedStorage:WaitForChild("lib")
local common = ReplicatedStorage:WaitForChild("common")

local PizzaAlpaca = require(lib:WaitForChild("PizzaAlpaca"))
local Promise = require(lib:WaitForChild("Promise"))
local Selectors = require(common:WaitForChild("Selectors"))
local Actions = require(common:WaitForChild("Actions"))
local Thunks = require(common:WaitForChild("Thunks"))

local Signal = require(lib:WaitForChild("Signal"))

local PlayerHandler = PizzaAlpaca.GameModule:extend("PlayerHandler")
PlayerHandler.playerLoaded = Signal.new()

local function playerAdded(player,store,api)
    print("Loading data for: ", player)
    wait(2)
    store:dispatch(Thunks.PLAYER_JOINED(player,api))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"baseball2007"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"bandit"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"baconhair"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"arrow"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"footballHelmet"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"material_pastelred"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"material_pasteldeepgreen"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"material_pastelcyan"))
    store:dispatch(Thunks.ASSET_TRYGIVE(player,"material_pastelyellow"))
    --store:dispatch(Thunks.ASSET_TRYGIVE(player,"10mil"))
    --store:dispatch(Thunks.ASSET_TRYGIVE(player,"pet_partyball"))
    api:initialPlayerState(player,store:getState())
    PlayerHandler.playerLoaded:fire(player)
    print("Loaded!")
end

local function playerLeaving(player,store)
	store:dispatch(Thunks.PLAYER_LEAVING(player))
end

function PlayerHandler:playerReady(player)
    local apiWrapper = self.core:getModule("ServerApi")
    local storeContainer = self.core:getModule("StoreContainer")

    Promise.all({
        storeContainer:getStore(),
        apiWrapper:getApi()
    }):andThen(function(resolved)
        return Promise.async(function(resolve,reject)
            local store, api = unpack(resolved)
            playerAdded(player, store, api)
        end)
    end)
end

function PlayerHandler:onStoreAndApi(store,api)
    Players.PlayerRemoving:Connect(function(player)
        playerLeaving(player,store)
    end)
end

function PlayerHandler:postInit()
    local apiWrapper = self.core:getModule("ServerApi")
    local storeContainer = self.core:getModule("StoreContainer")

    Promise.all({
        storeContainer:getStore(),
        apiWrapper:getApi()
    }):andThen(function(resolved)
        return Promise.async(function(resolve,reject)
            self:onStoreAndApi(unpack(resolved))
        end)
    end)
end

function PlayerHandler:getLoadedPlayers(store)
    local loadedPlayers = {}


    for _,player in pairs(Players:GetPlayers()) do
        local playerState = Selectors.getPlayerState(store:getState(), player)
        if playerState then
            table.insert(loadedPlayers,player)
        end
    end

    return loadedPlayers
end

return PlayerHandler