bool TryCreateGps(GridGpsSource source, out LocalGpsSource gps) { if (!VRageUtils.TryGetCubeGridById(source.GridId, out var grid)) { Log.Trace($"broadcast: grid not found: {source.GridId}"); gps = default; return(false); } var playerName = (string)null; if (!grid.BigOwners.TryGetFirst(out var playerId)) { Log.Trace($"grid no owner: \"{grid.DisplayName}\""); } else if (!MySession.Static.Players.TryGetPlayerById(playerId, out var player)) { Log.Trace($"player not found for grid: \"{grid.DisplayName}\": {playerId}"); } else { playerName = player.DisplayName; } var faction = MySession.Static.Factions.GetPlayerFaction(playerId); var factionTag = faction?.Tag; var name = Format(_config.GpsNameFormat); var description = Format(_config.GpsDescriptionFormat); gps = new LocalGpsSource { Id = grid.EntityId, Name = name, Color = ColorUtils.TranslateColor(_config.GpsColorCode), Description = description, Position = grid.PositionComp.GetPosition(), Radius = 0, EntityId = grid.EntityId, PromoteLevel = (int)_config.GpsVisiblePromoteLevel, ExcludedPlayers = _config.GpsMutedPlayers.ToArray(), }; return(true); string Format(string format) { return(format .Replace("{grid}", grid.DisplayName) .Replace("{player}", playerName ?? "<none>") .Replace("{faction}", factionTag ?? "<none>") .Replace("{ratio}", $"{source.LongLagNormal * 100:0}%") .Replace("{rank}", GpsUtils.RankToString(source.Rank)) .Replace("{time}", GpsUtils.RemainingTimeToString(source.RemainingTime))); } }
public async Task Update(IReadOnlyDictionary <long, PunishSource> lags) { // move to the game loop so we can synchronously operate on blocks await VRageUtils.MoveToGameLoop(); foreach (var(gridId, lag) in lags) { if (!lag.IsPinned) { continue; } if (!VRageUtils.TryGetCubeGridById(gridId, out var grid)) { Log.Warn($"grid not found for grid id: {gridId}"); continue; } if (!_punishedIds.Contains(gridId)) { _punishedIds.Add(gridId); Log.Info($"Punished: \"{grid.DisplayName}\" type: {_config.PunishType}"); } await PunishGrid(grid); Log.Debug($"block punish: \"{grid.Name}\" <{lag.GridId}> {_config.PunishType}"); // move to the next frame so we won't lag the server await VRageUtils.MoveToGameLoop(); } foreach (var existingId in _punishedIds) { if (!lags.ContainsKey(existingId)) { var name = VRageUtils.TryGetCubeGridById(existingId, out var g) ? $"\"{g.DisplayName}\"" : $"<{existingId}>"; Log.Info($"Done punishment: {name}"); } } _punishedIds.ExceptWith(lags.Keys); // back to some worker thread await TaskUtils.MoveToThreadPool(); }