local g=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local w=g:WaitForChild("\077\111\100\117\108\101\115")local v=require(w.src.utils.Logger)local A=require(w.src.actions.rooms.addPlayerFinishToRoom)local C=require(w.src.actions.toClient.clientFinishGame)local Q=require(w.src.actions.toClient.clientSendNotification)local t=require(w.M)local c=require(w.src.assets)local U=require(w.src.Transporter)local B=require(w.src.Leaderboards)local S=require(w.src.GameDatastore)local T=require(w.src.objects.InventoryObjects)local R=T.RoomObjects local function O(g,w)local function A(g)return g.finishTime~=nil end local C=t.select(w,A)local Q=t.count(C)v:d("\080\108\097\121\101\114\115 \102\105\110\105\115\104\101\100",Q,w)if Q==0 and t.count(w)==1 then return g/2 elseif Q==0 then return g end local c=(g/(Q+1))*(Q*1.75)return math.floor(c/10)*10 end local function W(g,w)return function(t)local T=(t:getState()).rooms[w]local W=R[w]local F=W.config local H=T.playersPlaying[g.UserId]if H and H.finishTime then v:d("\080\108\097\121\101\114  \097\108\114\101\097\100\121 \102\105\110\105\115\104\101\100 \114\111\111\109\058"..g.Name)else local R=O(F.prizeCoins,T.playersPlaying)v:d("\080\108\097\121\101\114 \102\105\110\105\115\104\101\100 \114\111\111\109\058 "..(g.Name..("\046 \084\114\097\110\115\112\111\114\116 \116\111 \108\111\098\098\121\046 \071\105\118\101 \109\111\110\101\121\058 "..(R.." \099\111\105\110\115\046"))))U:placePlayersToHomeSpawn({g})t:dispatch(A(g,w,os.time(),R))t:dispatch(C(g,w,os.time(),R))S:incrementCoins(g,R)t:dispatch(Q(g,"\089\111\117 \119\111\110 "..(R.." \099\111\105\110\115"),c.money["\099\111\105\110\115-\112\105\108\101"]))B:updateMostPlayed(g)B:updateMostPlayed(g,w)end end end return W
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)

local addPlayerFinishToRoom = require(Modules.src.actions.rooms.addPlayerFinishToRoom)
local clientFinishGame = require(Modules.src.actions.toClient.clientFinishGame)
local clientSendNotification = require(Modules.src.actions.toClient.clientSendNotification)
local M = require(Modules.M)
local assets = require(Modules.src.assets)
local Transporter = require(Modules.src.Transporter)
local Leaderboards = require(Modules.src.Leaderboards)
local GameDatastore = require(Modules.src.GameDatastore)

local InventoryObjects = require(Modules.src.objects.InventoryObjects)
local RoomObjects = InventoryObjects.RoomObjects

local function calulatePrize(prizeCoins, playersPlaying)
	local function isPlayerFinished(player)
		return player.finishTime ~= nil
	end

	local playersFinished = M.select(playersPlaying, isPlayerFinished)

	local count = M.count(playersFinished)

	logger:d('Players finished', count, playersPlaying)

	if count == 0 and M.count(playersPlaying) == 1 then
		-- solo run gives always half
		return prizeCoins / 2
	elseif count == 0 then
		return prizeCoins
	end

	local newPrize = prizeCoins / (count + 1) * (count * 1.75)
	return math.floor(newPrize / 10) * 10
end

local function playerFinishedRoom(player, roomId)
	return function(store)
		local room = store:getState().rooms[roomId]

		local roomObject = RoomObjects[roomId]
		local config = roomObject.config

		local playerObj = room.playersPlaying[player.UserId]
		if playerObj and playerObj.finishTime then
			logger:d('Player  already finished room:' .. player.Name)
		else
			local coins = calulatePrize(config.prizeCoins, room.playersPlaying)
			logger:d(
				'Player finished room: ' .. player.Name .. '. Transport to lobby. Give money: ' .. coins .. ' coins.'
			)

			Transporter:placePlayersToHomeSpawn({ player })

			store:dispatch(addPlayerFinishToRoom(player, roomId, os.time(), coins))
			store:dispatch(clientFinishGame(player, roomId, os.time(), coins))

			GameDatastore:incrementCoins(player, coins)
			store:dispatch(
				clientSendNotification(
					player,
					'You won ' .. coins .. ' coins',
					assets.money['coins-pile']
				)
			)
			Leaderboards:updateMostPlayed(player)
			Leaderboards:updateMostPlayed(player, roomId)
		end
	end
end

return playerFinishedRoom