Files
tbd-station-14/Content.Server/GameTicking/GameTicker.Player.cs
Moony 36181334b5 StationSystem/jobs/partial spawning refactor (#7580)
* Partial work on StationSystem refactor.

* WIP station jobs API.

* forgor to fire off grid events.

* Partial implementation of StationSpawningSystem

* whoops infinite loop.

* Spawners should work now.

* it compiles.

* tfw

* Vestigial code cleanup.

* fix station deletion.

* attempt to make tests go brr

* add latejoin spawnpoints to test maps.

* make sure the station still exists while destructing spawners.

* forgot an exists check.

* destruction order check.

* hopefully fix final test.

* fail-safe radstorm.

* Deep-clean job code further. This is bugged!!!!!

* Fix job bug. (init order moment)

* whooo cleanup

* New job selection algorithm that tries to distribute fairly across stations.

* small nitpicks

* Give the heads their weights to replace the head field.

* make overflow assign take a station list.

* moment

* Fixes and test #1 of many.

* please fix nullspace

* AssignJobs should no longer even consider showing up on a trace.

* add comment.

* Introduce station configs, praying i didn't miss something.

* in one small change stations are now fully serializable.

* Further doc comments.

* whoops.

* Solve bug where assignjobs didn't account for roundstart.

* Fix spawning, improve the API.
Caught an oversight in stationsystem that should've broke everything but didn't, whoops.

* Goodbye JobController.

* minor fix..

* fix test fail, remove debug logs.

* quick serialization fixes.

* fixes..

* sus

* partialing

* Update Content.Server/Station/Systems/StationJobsSystem.Roundstart.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Use dirtying to avoid rebuilding the list 2,100 times.

* add a bajillion more lines of docs (mostly in AssignJobs so i don't ever forget how it works)

* Update Content.IntegrationTests/Tests/Station/StationJobsTest.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* Add the Mysteriously Missing Captain Check.

* Put maprender back the way it belongs.

* I love addressing reviews.

* Update Content.Server/Station/Systems/StationJobsSystem.cs

Co-authored-by: Kara <lunarautomaton6@gmail.com>

* doc cleanup.

* Fix bureaucratic error, add job slot tests.

* zero cost abstractions when

* cri

* saner error.

* Fix spawning failing certain tests due to gameticker not handling falliability correctly.
Can't fix this until I refactor the rest of spawning code.

* submodule gaming

* Packedenger.

* Documentation consistency.

Co-authored-by: Kara <lunarautomaton6@gmail.com>
2022-05-10 13:43:30 -05:00

167 lines
5.5 KiB
C#

using Content.Server.Players;
using Content.Shared.GameTicking;
using Content.Shared.GameWindow;
using Content.Shared.Preferences;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.GameTicking
{
[UsedImplicitly]
public sealed partial class GameTicker
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
private void InitializePlayer()
{
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
}
private void PlayerStatusChanged(object? sender, SessionStatusEventArgs args)
{
var session = args.Session;
switch (args.NewStatus)
{
case SessionStatus.Connecting:
// Cancel shutdown update timer in progress.
_updateShutdownCts?.Cancel();
break;
case SessionStatus.Connected:
{
AddPlayerToDb(args.Session.UserId.UserId);
// Always make sure the client has player data. Mind gets assigned on spawn.
if (session.Data.ContentDataUncast == null)
session.Data.ContentDataUncast = new PlayerData(session.UserId, args.Session.Name);
// Make the player actually join the game.
// timer time must be > tick length
Timer.Spawn(0, args.Session.JoinGame);
_chatManager.SendAdminAnnouncement(Loc.GetString("player-join-message", ("name", args.Session.Name)));
if (LobbyEnabled && _roundStartCountdownHasNotStartedYetDueToNoPlayers)
{
_roundStartCountdownHasNotStartedYetDueToNoPlayers = false;
_roundStartTime = _gameTiming.CurTime + LobbyDuration;
}
break;
}
case SessionStatus.InGame:
{
_prefsManager.OnClientConnected(session);
var data = session.ContentData();
DebugTools.AssertNotNull(data);
if (data!.Mind == null)
{
if (LobbyEnabled)
{
PlayerJoinLobby(session);
return;
}
SpawnWaitPrefs();
}
else
{
if (data.Mind.CurrentEntity == null)
{
SpawnWaitPrefs();
}
else
{
session.AttachToEntity(data.Mind.CurrentEntity);
PlayerJoinGame(session);
}
}
break;
}
case SessionStatus.Disconnected:
{
if (_playersInLobby.ContainsKey(session)) _playersInLobby.Remove(session);
_chatManager.SendAdminAnnouncement(Loc.GetString("player-leave-message", ("name", args.Session.Name)));
ServerEmptyUpdateRestartCheck();
_prefsManager.OnClientDisconnected(session);
break;
}
}
//When the status of a player changes, update the server info text
UpdateInfoText();
async void SpawnWaitPrefs()
{
await _prefsManager.WaitPreferencesLoaded(session);
SpawnPlayer(session, EntityUid.Invalid);
}
async void AddPlayerToDb(Guid id)
{
if (RoundId != 0 && _runLevel != GameRunLevel.PreRoundLobby)
{
await _db.AddRoundPlayers(RoundId, id);
}
}
}
private HumanoidCharacterProfile GetPlayerProfile(IPlayerSession p)
{
return (HumanoidCharacterProfile) _prefsManager.GetPreferences(p.UserId).SelectedCharacter;
}
public void PlayerJoinGame(IPlayerSession session)
{
_chatManager.DispatchServerMessage(session, Loc.GetString("game-ticker-player-join-game-message"));
if (_playersInLobby.ContainsKey(session))
_playersInLobby.Remove(session);
_playersInGame.Add(session.UserId);
RaiseNetworkEvent(new TickerJoinGameEvent(), session.ConnectedClient);
}
private void PlayerJoinLobby(IPlayerSession session)
{
_playersInLobby[session] = LobbyPlayerStatus.NotReady;
_playersInGame.Remove(session.UserId);
var client = session.ConnectedClient;
RaiseNetworkEvent(new TickerJoinLobbyEvent(), client);
RaiseNetworkEvent(GetStatusMsg(session), client);
RaiseNetworkEvent(GetInfoMsg(), client);
RaiseNetworkEvent(GetPlayerStatus(), client);
RaiseLocalEvent(new PlayerJoinedLobbyEvent(session));
}
private void ReqWindowAttentionAll()
{
RaiseNetworkEvent(new RequestWindowAttentionEvent());
}
}
public sealed class PlayerJoinedLobbyEvent : EntityEventArgs
{
public readonly IPlayerSession PlayerSession;
public PlayerJoinedLobbyEvent(IPlayerSession playerSession)
{
PlayerSession = playerSession;
}
}
}