Makes map vote and roundstart smart about player count. (#5418)
* Makes map vote and roundstart smart about player count. No more Saltern with 30 players, or Knight Ship with 50. * a typo * Address reviews. * Localized.
This commit is contained in:
120
Content.Server/Maps/GameMapManager.cs
Normal file
120
Content.Server/Maps/GameMapManager.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.Chat.Managers;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
{
|
||||
public class GameMapManager : IGameMapManager
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IChatManager _chatManager = default!;
|
||||
|
||||
private GameMapPrototype _currentMap = default!;
|
||||
private bool _currentMapForced;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_configurationManager.OnValueChanged(CCVars.GameMap, value =>
|
||||
{
|
||||
if (TryLookupMap(value, out var map))
|
||||
_currentMap = map;
|
||||
else
|
||||
throw new ArgumentException($"Unknown map prototype {value} was selected!");
|
||||
}, true);
|
||||
_configurationManager.OnValueChanged(CCVars.GameMapForced, value => _currentMapForced = value, true);
|
||||
}
|
||||
|
||||
public IEnumerable<GameMapPrototype> CurrentlyEligibleMaps()
|
||||
{
|
||||
var maps = AllVotableMaps().Where(IsMapEligible).ToArray();
|
||||
|
||||
return maps.Length == 0 ? AllMaps().Where(x => x.Fallback) : maps;
|
||||
}
|
||||
|
||||
public IEnumerable<GameMapPrototype> AllVotableMaps()
|
||||
{
|
||||
return _prototypeManager.EnumeratePrototypes<GameMapPrototype>().Where(x => x.Votable);
|
||||
}
|
||||
|
||||
public IEnumerable<GameMapPrototype> AllMaps()
|
||||
{
|
||||
return _prototypeManager.EnumeratePrototypes<GameMapPrototype>();
|
||||
}
|
||||
|
||||
public bool TrySelectMap(string gameMap)
|
||||
{
|
||||
if (!TryLookupMap(gameMap, out var map) || !IsMapEligible(map)) return false;
|
||||
|
||||
_currentMap = map;
|
||||
_currentMapForced = false;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public void ForceSelectMap(string gameMap)
|
||||
{
|
||||
if (!TryLookupMap(gameMap, out var map))
|
||||
throw new ArgumentException($"The map \"{gameMap}\" is invalid!");
|
||||
_currentMap = map;
|
||||
_currentMapForced = true;
|
||||
}
|
||||
|
||||
public void SelectRandomMap()
|
||||
{
|
||||
var maps = CurrentlyEligibleMaps().ToList();
|
||||
_random.Shuffle(maps);
|
||||
_currentMap = maps[0];
|
||||
_currentMapForced = false;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMap()
|
||||
{
|
||||
return _currentMap;
|
||||
}
|
||||
|
||||
public GameMapPrototype GetSelectedMapChecked(bool loud = false)
|
||||
{
|
||||
if (!_currentMapForced && !IsMapEligible(GetSelectedMap()))
|
||||
{
|
||||
var oldMap = GetSelectedMap().MapName;
|
||||
SelectRandomMap();
|
||||
if (loud)
|
||||
{
|
||||
_chatManager.DispatchServerAnnouncement(
|
||||
Loc.GetString("gamemap-could-not-use-map-error",
|
||||
("oldMap", oldMap), ("newMap", GetSelectedMap().MapName)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return GetSelectedMap();
|
||||
}
|
||||
|
||||
public bool CheckMapExists(string gameMap)
|
||||
{
|
||||
return TryLookupMap(gameMap, out _);
|
||||
}
|
||||
|
||||
private bool IsMapEligible(GameMapPrototype map)
|
||||
{
|
||||
return map.MaxPlayers >= _playerManager.PlayerCount && map.MinPlayers <= _playerManager.PlayerCount;
|
||||
}
|
||||
|
||||
private bool TryLookupMap(string gameMap, [NotNullWhen(true)] out GameMapPrototype? map)
|
||||
{
|
||||
return _prototypeManager.TryIndex(gameMap, out map);
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Content.Server/Maps/GameMapPrototype.cs
Normal file
53
Content.Server/Maps/GameMapPrototype.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
{
|
||||
/// <summary>
|
||||
/// Prototype data for a game map.
|
||||
/// </summary>
|
||||
[Prototype("gameMap")]
|
||||
public class GameMapPrototype : IPrototype
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[ViewVariables, DataField("id", required: true)]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Minimum players for the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("minPlayers", required: true)]
|
||||
public uint MinPlayers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum players for the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("maxPlayers")]
|
||||
public uint MaxPlayers { get; } = uint.MaxValue;
|
||||
|
||||
/// <summary>
|
||||
/// Name of the given map.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("mapName", required: true)]
|
||||
public string MapName { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Relative directory path to the given map, i.e. `Maps/saltern.yml`
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("mapPath", required: true)]
|
||||
public string MapPath { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Controls if the map can be used as a fallback if no maps are eligible.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("fallback")]
|
||||
public bool Fallback { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Controls if the map can be voted for.
|
||||
/// </summary>
|
||||
[ViewVariables, DataField("votable")]
|
||||
public bool Votable { get; } = true;
|
||||
}
|
||||
}
|
||||
68
Content.Server/Maps/IGameMapManager.cs
Normal file
68
Content.Server/Maps/IGameMapManager.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Content.Server.Maps
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages which station map will be used for the next round.
|
||||
/// </summary>
|
||||
public interface IGameMapManager
|
||||
{
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps eligible to be played right now.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> CurrentlyEligibleMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps that can be voted for.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllVotableMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all maps.
|
||||
/// </summary>
|
||||
/// <returns>enumerator of map prototypes</returns>
|
||||
IEnumerable<GameMapPrototype> AllMaps();
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to select the given map.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
bool TrySelectMap(string gameMap);
|
||||
|
||||
/// <summary>
|
||||
/// Forces the given map, making sure the game map manager won't reselect if conditions are no longer met at round restart.
|
||||
/// </summary>
|
||||
/// <param name="gameMap">map prototype</param>
|
||||
/// <returns>success or failure</returns>
|
||||
void ForceSelectMap(string gameMap);
|
||||
|
||||
/// <summary>
|
||||
/// Selects a random map.
|
||||
/// </summary>
|
||||
void SelectRandomMap();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, without double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMap();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the currently selected map, double-checking if it can be used.
|
||||
/// </summary>
|
||||
/// <returns>selected map</returns>
|
||||
GameMapPrototype GetSelectedMapChecked(bool loud = false);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the given map exists
|
||||
/// </summary>
|
||||
/// <param name="gameMap">name of the map</param>
|
||||
/// <returns>existence</returns>
|
||||
bool CheckMapExists(string gameMap);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user