Replace add bots / assign with a combined slot options dropdown.

This commit is contained in:
Paul Chote
2013-06-30 13:31:55 +12:00
parent 0ad9e65577
commit a49bf40413
4 changed files with 94 additions and 88 deletions

View File

@@ -335,46 +335,35 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
int teams; int teamCount;
if (!int.TryParse(s, out teams)) if (!int.TryParse(s, out teamCount))
{ {
server.SendOrderTo(conn, "Message", "Number of teams could not be parsed: {0}".F(s)); server.SendOrderTo(conn, "Message", "Number of teams could not be parsed: {0}".F(s));
return true; return true;
} }
teams = teams.Clamp(1, 8);
var maxTeams = (server.lobbyInfo.Clients.Count(c => c.Slot != null) + 1) / 2;
teamCount = teamCount.Clamp(0, maxTeams);
var players = server.lobbyInfo.Slots var players = server.lobbyInfo.Slots
.Select(slot => server.lobbyInfo.Clients.SingleOrDefault(c => c.Slot == slot.Key)) .Select(slot => server.lobbyInfo.ClientInSlot(slot.Key))
.Where(c => c != null && !server.lobbyInfo.Slots[c.Slot].LockTeam).ToArray(); .Where(c => c != null && !server.lobbyInfo.Slots[c.Slot].LockTeam);
if (players.Length < 2)
var assigned = 0;
var playerCount = players.Count();
foreach (var player in players)
{ {
server.SendOrderTo(conn, "Message", "Not enough players to assign teams"); // Free for all
return true; if (teamCount == 0)
} player.Team = 0;
if (teams > players.Length)
{ // Humans vs Bots
server.SendOrderTo(conn, "Message", "Too many teams for the number of players"); else if (teamCount == 1)
return true; player.Team = player.Bot == null ? 1 : 2;
else
player.Team = assigned++ * teamCount / playerCount + 1;
} }
var teamSizes = new int[players.Length];
for (var i = 0; i < players.Length; i++)
teamSizes[i % teams]++;
var playerIndex = 0;
for (var team = 1; team <= teams; team++)
{
for (var teamPlayerIndex = 0; teamPlayerIndex < teamSizes[team - 1]; playerIndex++, teamPlayerIndex++)
{
var cl = players[playerIndex];
if (cl.Bot == null)
cl.State = Session.ClientState.NotReady;
cl.Team = team;
}
}
// All vs Host
if (teams == 1)
client.Team = 2;
server.SyncLobbyInfo(); server.SyncLobbyInfo();
return true; return true;
}}, }},

View File

@@ -165,49 +165,83 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}; };
mapButton.IsVisible = () => mapButton.Visible && Game.IsHost; mapButton.IsVisible = () => mapButton.Visible && Game.IsHost;
var assignTeams = lobby.GetOrNull<DropDownButtonWidget>("ASSIGNTEAMS_DROPDOWNBUTTON"); var slotsButton = lobby.GetOrNull<DropDownButtonWidget>("SLOTS_DROPDOWNBUTTON");
if (assignTeams != null) if (slotsButton != null)
{ {
assignTeams.IsVisible = () => Game.IsHost; slotsButton.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null ||
assignTeams.IsDisabled = () => gameStarting || orderManager.LobbyInfo.Clients.Count(c => c.Slot != null) < 2 orderManager.LocalClient.IsReady || !orderManager.LobbyInfo.Slots.Values.Any(s => s.AllowBots || !s.LockTeam);
|| orderManager.LocalClient == null || orderManager.LocalClient.IsReady;
assignTeams.OnMouseDown = _ => var aiModes = Rules.Info["player"].Traits.WithInterface<IBotInfo>().Select(t => t.Name);
slotsButton.OnMouseDown = _ =>
{ {
var options = Enumerable.Range(1, orderManager.LobbyInfo.Clients.Count(c => c.Slot != null).Clamp(1, 8) - 1).Select(d => new DropDownOption var options = new Dictionary<string, IEnumerable<DropDownOption>>();
var botController = orderManager.LobbyInfo.Clients.Where(c => c.IsAdmin).FirstOrDefault();
if (orderManager.LobbyInfo.Slots.Values.Any(s => s.AllowBots))
{ {
Title = (d == 1 ? "All vs Host" : "{0} Teams".F(d)), var botOptions = new List<DropDownOption>(){ new DropDownOption()
{
Title = "Add",
IsSelected = () => false,
OnClick = () =>
{
foreach (var slot in orderManager.LobbyInfo.Slots)
{
var bot = aiModes.Random(Game.CosmeticRandom);
var c = orderManager.LobbyInfo.ClientInSlot(slot.Key);
if (slot.Value.AllowBots == true && (c == null || c.Bot != null))
orderManager.IssueOrder(Order.Command("slot_bot {0} {1} {2}".F(slot.Key, botController.Index, bot)));
}
}
}};
if (orderManager.LobbyInfo.Clients.Any(c => c.Bot != null))
{
botOptions.Add(new DropDownOption()
{
Title = "Remove",
IsSelected = () => false,
OnClick = () =>
{
foreach (var slot in orderManager.LobbyInfo.Slots)
{
var c = orderManager.LobbyInfo.ClientInSlot(slot.Key);
if (c != null && c.Bot != null)
orderManager.IssueOrder(Order.Command("slot_open "+slot.Value.PlayerReference));
}
}
});
}
options.Add("Bots", botOptions);
}
var teamCount = (orderManager.LobbyInfo.Slots.Count(s => !s.Value.LockTeam && orderManager.LobbyInfo.ClientInSlot(s.Key) != null) + 1) / 2;
if (teamCount >= 1)
{
var teamOptions = Enumerable.Range(0, teamCount + 1).Reverse().Select(d => new DropDownOption
{
Title = (d > 1 ? "{0} Teams".F(d) : d == 1 ? "Humans vs Bots" : "Free for all"),
IsSelected = () => false, IsSelected = () => false,
OnClick = () => orderManager.IssueOrder(Order.Command("assignteams {0}".F(d.ToString()))) OnClick = () => orderManager.IssueOrder(Order.Command("assignteams {0}".F(d.ToString())))
}); });
options.Add("Teams", teamOptions);
}
Func<DropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) => Func<DropDownOption, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{ {
var item = ScrollItemWidget.Setup(template, option.IsSelected, option.OnClick); var item = ScrollItemWidget.Setup(template, option.IsSelected, option.OnClick);
item.Get<LabelWidget>("LABEL").GetText = () => option.Title; item.Get<LabelWidget>("LABEL").GetText = () => option.Title;
return item; return item;
}; };
assignTeams.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", options.Count() * 30, options, setupItem); slotsButton.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 175, options, setupItem);
}; };
} }
var disconnectButton = lobby.Get<ButtonWidget>("DISCONNECT_BUTTON"); var disconnectButton = lobby.Get<ButtonWidget>("DISCONNECT_BUTTON");
disconnectButton.OnClick = () => { CloseWindow(); onExit(); }; disconnectButton.OnClick = () => { CloseWindow(); onExit(); };
var addBotsButton = lobby.Get<ButtonWidget>("ADD_BOTS");
addBotsButton.IsVisible = () => Game.IsHost;
addBotsButton.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null
|| orderManager.LocalClient.IsReady || !orderManager.LobbyInfo.Slots.Values.Any(s => s.AllowBots);
addBotsButton.OnClick = () => {
var aiModes = Rules.Info["player"].Traits.WithInterface<IBotInfo>().Select(t => t.Name);
foreach (var slot in orderManager.LobbyInfo.Slots)
{
var bot = aiModes.Random(Game.CosmeticRandom);
var c = orderManager.LobbyInfo.ClientInSlot(slot.Key);
if (slot.Value.AllowBots == true && (c == null || c.Bot != null))
orderManager.IssueOrder(Order.Command("slot_bot {0} {1} {2}".F(slot.Key,0,bot)));
}
};
var allowCheats = lobby.Get<CheckboxWidget>("ALLOWCHEATS_CHECKBOX"); var allowCheats = lobby.Get<CheckboxWidget>("ALLOWCHEATS_CHECKBOX");
allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats; allowCheats.IsChecked = () => orderManager.LobbyInfo.GlobalSettings.AllowCheats;
allowCheats.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null allowCheats.IsDisabled = () => !Game.IsHost || gameStarting || orderManager.LocalClient == null

View File

@@ -94,28 +94,25 @@ Container@SERVER_LOBBY:
Y:257 Y:257
Width:130 Width:130
Height:20 Height:20
Text: Debug Menu Text:Cheats
Checkbox@CRATES_CHECKBOX: Checkbox@CRATES_CHECKBOX:
X:160 X:120
Y:257 Y:257
Width:80 Width:80
Height:20 Height:20
Text: Crates Text:Crates
Button@ADD_BOTS: DropDownButton@SLOTS_DROPDOWNBUTTON:
X:398-120-10 X:213
Y:255 Y:255
Width:120 Width:150
Height:25 Height:25
Text:Add Bots Text:Slot Options
Font:Bold Button@CHANGEMAP_BUTTON:
DropDownButton@ASSIGNTEAMS_DROPDOWNBUTTON: X:368
X:398
Y:255 Y:255
Width:120 Width:150
Height:25 Height:25
Font:Bold Text:Change Map
Visible:false
Text:Assign
ScrollPanel@CHAT_DISPLAY: ScrollPanel@CHAT_DISPLAY:
X:15 X:15
Y:285 Y:285
@@ -170,12 +167,6 @@ Container@SERVER_LOBBY:
Width:140 Width:140
Height:35 Height:35
Text:Music Text:Music
Button@CHANGEMAP_BUTTON:
X:450
Y:499
Width:140
Height:35
Text:Change Map
Button@START_GAME_BUTTON: Button@START_GAME_BUTTON:
X:600 X:600
Y:499 Y:499

View File

@@ -125,21 +125,13 @@ Background@SERVER_LOBBY:
Height:25 Height:25
Text:Change Map Text:Change Map
Font:Bold Font:Bold
Button@ADD_BOTS: DropDownButton@SLOTS_DROPDOWNBUTTON:
X:PARENT_RIGHT-154
Y:PARENT_BOTTOM-(289-25-5)
Width:120
Height:25
Text:Add Bots
Font:Bold
DropDownButton@ASSIGNTEAMS_DROPDOWNBUTTON:
X:PARENT_RIGHT-154 X:PARENT_RIGHT-154
Y:PARENT_BOTTOM-229 Y:PARENT_BOTTOM-229
Width:120 Width:120
Height:25 Height:25
Font:Bold Font:Bold
Visible:false Text:Admin
Text:Assign
DropDownButton@DIFFICULTY_DROPDOWNBUTTON: DropDownButton@DIFFICULTY_DROPDOWNBUTTON:
X:PARENT_RIGHT-154 X:PARENT_RIGHT-154
Y:PARENT_BOTTOM-199 Y:PARENT_BOTTOM-199