Hi guys,
I've been struggling with this for quite some time now and I don't know what is going on.. I don't see it 😞
I build a PowerApp that needs to distribute players between 1 and 4 collections of teams (it's a game).
So I have 4 teams, with matching collections:
- colBlueTeam
- colPurpleTeam
- colGreenTeam
- colOrangeTeam
First I select how many teams it needs to create via a dropdown menu. In the OnSelect I create all the collections and remove the first blank record in the collection and I give the variable "varNextColor" the value: "blue" since the blue team is always the first team who gets filled with players.
ClearCollect(
colBlueTeam,
{PlayerBlue: Blank()}
);
ClearCollect(
colPurpleTeam,
{PlayerPurple: Blank()}
);
ClearCollect(
colGreenTeam,
{PlayerGreen: Blank()}
);
ClearCollect(
colOrangeTeam,
{PlayerOrange: Blank()}
);
Remove(
colBlueTeam,
First(colBlueTeam)
);
Remove(
colPurpleTeam,
First(colPurpleTeam)
);
Remove(
colGreenTeam,
First(colGreenTeam)
);
Remove(
colOrangeTeam,
First(colOrangeTeam)
);
UpdateContext({varNextColor: "blue"})
I then add members using a textbox and add them to a collection "colAllPlayers"
Then I press a button: "Divide players randomly". Which does the following:
UpdateContext({varRandomPlayer: First(Shuffle(ColAllPlayers).Name)});
Remove(
ColAllPlayers,
{Name: varRandomPlayer.Name}
);
Switch(
varNumberOfTeams.Value,
2,
If(
varNextColor = "blue",
UpdateContext({varNextColor: "purple"});
Collect(
colBlueTeam,
{PlayerBlue: varRandomPlayer.Name}
),
UpdateContext({varNextColor: "blue"});
Collect(
colPurpleTeam,
{PlayerPurple: varRandomPlayer.Name}
)
),
3,
If(
varNextColor = "blue",
UpdateContext({varNextColor: "purple"});
Collect(
colBlueTeam,
{PlayerBlue: varRandomPlayer.Name}
);
);
If(
varNextColor = "purple",
UpdateContext({varNextColor: "green"});
Collect(
colPurpleTeam,
{PlayerPurple: varRandomPlayer.Name}
);
);
If(
varNextColor = "green",
UpdateContext({varNextColor: "blue"});
Collect(
colGreenTeam,
{PlayerGreen: varRandomPlayer.Name}
);
),
4,
If(
varNextColor = "blue",
Collect(
colBlueTeam,
{PlayerBlue: varRandomPlayer.Name}
);
UpdateContext({varNextColor: "purple"})
);
If(
varNextColor = "purple",
Collect(
colPurpleTeam,
{PlayerPurple: varRandomPlayer.Name}
);
UpdateContext({varNextColor: "green"})
);
If(
varNextColor = "green",
Collect(
colGreenTeam,
{PlayerGreen: varRandomPlayer.Name}
);
UpdateContext({varNextColor: "orange"})
);
If(
varNextColor = "orange",
Collect(
colOrangeTeam,
{PlayerOrange: varRandomPlayer.Name}
);
UpdateContext({varNextColor: "blue"})
)
)
I then get a random record from the collection colAllPlayers and I remove the name from the collection since it's going to be assigned to a team.
So I use a switch to determine how many teams are selected (minimal is 2 teams, maximum is 4). If in the dropdown menu the value: 3 is selected. I want to add someone to the blue team first since that is the default first team. Then I update the varNextColor value with purple, so if I click on the divide button again (you have to keep pressing it till all players have been assigned) it needs to:
- skip the first IF statement since the color is purple not blue.
- enter the second IF statement since the color is purple, add a player to the purple team. Change the variable varNextColor to green. etc.... etc...
But what is actually happening is:
- I press the divide button, the first user is being added to all the collections.... Instead of the collection I am specifying. I checked via File -> Collections that there 4 TeamCollections, and each of them have the same users added.. Also the varNextColor does not get updated, it keeps on blue. So there is something going wrong....
How?!
**** I fixed it by using another switch statement instead of all those if statements. But I still would like to know why it wasn't working with the if statements.