Skip to content

Commit

Permalink
Engine update.
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaTR committed Apr 9, 2023
1 parent f40cf62 commit 5e8a479
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 88 deletions.
88 changes: 62 additions & 26 deletions OpenRA.Mods.SS/ServerTraits/LobbyCommandsSS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class LobbyCommandsSS : ServerTrait, IInterpretCommand, INotifyServerStar
[TranslationReference]
const string KickNone = "notification-kick-none";

[TranslationReference]
const string NoKickSelf = "notification-kick-self";

[TranslationReference]
const string NoKickGameStarted = "notification-no-kick-game-started";

Expand Down Expand Up @@ -255,6 +258,26 @@ static void CheckAutoStart(S server)
if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
return;

// Don't start without any players
if (server.LobbyInfo.Slots.All(sl => server.LobbyInfo.ClientInSlot(sl.Key) == null))
return;

// Does the host have the map installed?
if (server.Type != ServerType.Dedicated && server.ModData.MapCache[server.Map.Uid].Status != MapStatus.Available)
{
// Client 0 will always be the Host
// In some cases client 0 doesn't exist, so we untick all players
var host = server.LobbyInfo.Clients.FirstOrDefault(c => c.Index == 0);
if (host != null)
host.State = Session.ClientState.NotReady;
else
foreach (var client in server.LobbyInfo.Clients)
client.State = Session.ClientState.NotReady;

server.SyncLobbyClients();
return;
}

server.StartGame();
}
}
Expand Down Expand Up @@ -286,20 +309,25 @@ static bool StartGame(S server, Connection conn, Session.Client client, string s
{
if (!client.IsAdmin)
{
server.SendOrderTo(conn, "Message", OnlyHostStartGame);
server.SendLocalizedMessageTo(conn, OnlyHostStartGame);
return true;
}

if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
{
server.SendLocalizedMessageTo(conn, NoStartUntilRequiredSlotsFull);
return true;
}

if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required &&
server.LobbyInfo.ClientInSlot(sl.Key) == null))
if (server.LobbyInfo.Slots.All(sl => server.LobbyInfo.ClientInSlot(sl.Key) == null))
{
server.SendOrderTo(conn, "Message", NoStartUntilRequiredSlotsFull);
server.SendOrderTo(conn, "Message", NoStartWithoutPlayers);
return true;
}

if (!server.LobbyInfo.GlobalSettings.EnableSingleplayer && server.LobbyInfo.NonBotPlayers.Count() < 2)
{
server.SendOrderTo(conn, "Message", TwoHumansRequired);
server.SendLocalizedMessageTo(conn, TwoHumansRequired);
return true;
}

Expand Down Expand Up @@ -492,10 +520,11 @@ static bool SlotBot(S server, Connection conn, Session.Client client, string s)
};

// Pick a random color for the bot
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<IColorPickerManagerInfo>();
var terrainColors = server.ModData.DefaultTerrainInfo[server.Map.TileSet].RestrictedPlayerColors;
var playerColors = server.LobbyInfo.Clients.Select(c => c.Color)
.Concat(server.Map.Players.Players.Values.Select(p => p.Color));

bot.Color = bot.PreferredColor = colorManager.RandomPresetColor(server.Random, terrainColors, playerColors);

server.LobbyInfo.Clients.Add(bot);
Expand Down Expand Up @@ -526,7 +555,7 @@ static bool Map(S server, Connection conn, Session.Client client, string s)
}

var lastMap = server.LobbyInfo.GlobalSettings.Map;
Action<MapPreview> selectMap = map =>
void SelectMap(MapPreview map)
{
lock (server.LobbyInfo)
{
Expand Down Expand Up @@ -612,28 +641,28 @@ static bool Map(S server, Connection conn, Session.Client client, string s)
if (briefing != null)
server.SendMessage(briefing);
}
};
}

Action queryFailed = () => server.SendLocalizedMessageTo(conn, UnknownMap);
void QueryFailed() => server.SendLocalizedMessageTo(conn, UnknownMap);

var m = server.ModData.MapCache[s];
if (m.Status == MapStatus.Available || m.Status == MapStatus.DownloadAvailable)
selectMap(m);
SelectMap(m);
else if (server.Settings.QueryMapRepository)
{
server.SendLocalizedMessageTo(conn, SearchingMap);
var mapRepository = server.ModData.Manifest.Get<WebServices>().MapRepository;
var reported = false;
server.ModData.MapCache.QueryRemoteMapDetails(mapRepository, new[] { s }, selectMap, _ =>
server.ModData.MapCache.QueryRemoteMapDetails(mapRepository, new[] { s }, SelectMap, _ =>
{
if (!reported)
queryFailed();
QueryFailed();
reported = true;
});
}
else
queryFailed();
QueryFailed();

return true;
}
Expand Down Expand Up @@ -750,31 +779,37 @@ static bool Kick(S server, Connection conn, Session.Client client, string s)
return true;
}

Exts.TryParseIntegerInvariant(split[0], out var kickClientID);
var kickConn = Exts.TryParseIntegerInvariant(split[0], out var kickClientID)
? server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == kickClientID) : null;

var kickConn = server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == kickClientID);
if (kickConn == null)
{
server.SendLocalizedMessageTo(conn, KickNone);
return true;
}

var kickClient = server.GetClient(kickConn);
if (server.State == ServerState.GameStarted && !kickClient.IsObserver)
if (client == kickClient)
{
server.SendLocalizedMessageTo(conn, NoKickSelf);
return true;
}

if (!server.CanKickClient(kickClient))
{
server.SendLocalizedMessageTo(conn, NoKickGameStarted);
return true;
}

Log.Write("server", $"Kicking client {kickClientID}.");
server.SendLocalizedMessage(Kicked, Translation.Arguments("admin", client.Name, "client", kickClient.Name));
server.SendLocalizedMessage(Kicked, Translation.Arguments("admin", client.Name, "player", kickClient.Name));
server.SendOrderTo(kickConn, "ServerError", YouWereKicked);
server.DropClient(kickConn);

if (bool.TryParse(split[1], out var tempBan) && tempBan)
{
Log.Write("server", $"Temporarily banning client {kickClientID} ({kickClient.IPAddress}).");
server.SendLocalizedMessage(TempBan, Translation.Arguments("admin", client.Name, "client", kickClient.Name));
server.SendLocalizedMessage(TempBan, Translation.Arguments("admin", client.Name, "player", kickClient.Name));
server.TempBans.Add(kickClient.IPAddress);
}

Expand All @@ -795,8 +830,8 @@ static bool MakeAdmin(S server, Connection conn, Session.Client client, string s
return true;
}

Exts.TryParseIntegerInvariant(s, out var newAdminId);
var newAdminConn = server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == newAdminId);
var newAdminConn = Exts.TryParseIntegerInvariant(s, out var newAdminId)
? server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == newAdminId) : null;

if (newAdminConn == null)
{
Expand Down Expand Up @@ -832,8 +867,9 @@ static bool MakeSpectator(S server, Connection conn, Session.Client client, stri
return true;
}

Exts.TryParseIntegerInvariant(s, out var targetId);
var targetConn = server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == targetId);
var targetConn = Exts.TryParseIntegerInvariant(s, out var targetId)
? server.Conns.SingleOrDefault(c => server.GetClient(c)?.Index == targetId) : null;

if (targetConn == null)
{
server.SendLocalizedMessageTo(conn, EmptySlot);
Expand Down Expand Up @@ -1195,20 +1231,20 @@ static Color SanitizePlayerColor(S server, Color askedColor, int playerIndex, Co
{
lock (server.LobbyInfo)
{
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
var colorManager = server.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<IColorPickerManagerInfo>();
var askColor = askedColor;

Action<string> onError = message =>
void OnError(string message)
{
if (connectionToEcho != null && message != null)
server.SendLocalizedMessageTo(connectionToEcho, message);
};
}

var terrainColors = server.ModData.DefaultTerrainInfo[server.Map.TileSet].RestrictedPlayerColors;
var playerColors = server.LobbyInfo.Clients.Where(c => c.Index != playerIndex).Select(c => c.Color)
.Concat(server.Map.Players.Players.Values.Select(p => p.Color)).ToList();

return colorManager.MakeValid(askColor, server.Random, terrainColors, playerColors, onError);
return colorManager.MakeValid(askColor, server.Random, terrainColors, playerColors, OnError);
}
}

Expand Down
14 changes: 7 additions & 7 deletions OpenRA.Mods.SS/Traits/World/SpawnSSUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ public class SpawnSSUnit : IWorldLoaded
readonly SpawnSSUnitInfo info;

WorldRenderer wr;
HashSet<(string[] Actors, int Amount, int Inner, int Outer)> bases = new HashSet<(string[] actors, int amount, int inner, int outer)>();
HashSet<(string[] Actors, int Amount, int Inner, int Outer)> bases = new();

public bool TeamSpawns;
public bool QuickClassChange;
public bool ClassChanging = true;
public bool ClassChangingPaused = false;
public Dictionary<Player, CPos> PlayerSpawnPoints = new Dictionary<Player, CPos>();
public Dictionary<Player, Player> TeamLeaders = new Dictionary<Player, Player>();
public Dictionary<Player, int> Teams = new Dictionary<Player, int>();
public Dictionary<Player, string> Classes = new Dictionary<Player, string>();
public Dictionary<Player, Actor> Units = new Dictionary<Player, Actor>();
Dictionary<CPos, bool> spawnPointOccupation = new Dictionary<CPos, bool>();
public Dictionary<Player, CPos> PlayerSpawnPoints = new();
public Dictionary<Player, Player> TeamLeaders = new();
public Dictionary<Player, int> Teams = new();
public Dictionary<Player, string> Classes = new();
public Dictionary<Player, Actor> Units = new();
Dictionary<CPos, bool> spawnPointOccupation = new();

public SpawnSSUnit(SpawnSSUnitInfo info)
{
Expand Down
Loading

0 comments on commit 5e8a479

Please sign in to comment.