Help for bedwars auto-ending and ranking

So i made a really good bedwars map but it couldn't manage tu end the game when 3 teams did ey ther beds were destroyed and ranking all of them by their time alive , so can everyone help ?