Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
fix: make code more compact and less repetitive
Browse files Browse the repository at this point in the history
  • Loading branch information
dollannn committed Apr 9, 2024
1 parent 42415c3 commit 96b48eb
Showing 1 changed file with 43 additions and 62 deletions.
105 changes: 43 additions & 62 deletions Models/Arena.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal class Arena
private RoundType _roundType;
private ILogger _logger;

private bool _isArenaActive;

public ArenaPlayer? _player1;
private int _player1Kills;
private bool _player1HasLastKill;
Expand All @@ -25,6 +27,7 @@ public Arena(ILogger logger, Tuple<SpawnPoint, SpawnPoint> spawns)
_spawns = spawns;
_rank = 0;
_logger = logger;
_isArenaActive = false;
_player1Kills = 0;
_player1HasLastKill = true;
_player2Kills = 0;
Expand Down Expand Up @@ -89,6 +92,8 @@ public void AddPlayers(ArenaPlayer? player1, ArenaPlayer? player2, int rank = -1

public void OnPlayerSpawn(CCSPlayerController playerController)
{
_isArenaActive = anyPValid();

bool wasPlayer1 = isPValid(_player1) && _player1!.PlayerController == playerController;
bool wasPlayer2 = isPValid(_player2) && _player2!.PlayerController == playerController;

Expand All @@ -112,39 +117,30 @@ public void OnPlayerSpawn(CCSPlayerController playerController)
p2Spawn = _spawns.Item1;
}

if (isPValid(_player1))
{
// Get spawnpoint from the arena
Vector? pos = p1Spawn.AbsOrigin;
QAngle? angle = p1Spawn.AbsRotation;
Vector? velocity = new Vector(0, 0, 0);

// Teleport player there
if (pos != null && angle != null)
{
_player1?.PlayerController?.Pawn.Value?.Teleport(pos, angle, velocity);
}

// Reset weapons and health
_player1!.ResetPlayerWeapons(_roundType);
_player1!.PlayerController!.Pawn!.Value!.Health = 100;
}
// Prepare the players
PreparePlayer(_player1, p1Spawn, _roundType);
PreparePlayer(_player2, p2Spawn, _roundType);
}
}

private void PreparePlayer(ArenaPlayer? player, SpawnPoint spawnPoint, RoundType roundType)
{
if (isPValid(player))
{
// Get spawnpoint from the arena
Vector? pos = spawnPoint.AbsOrigin;
QAngle? angle = spawnPoint.AbsRotation;
Vector? velocity = new Vector(0, 0, 0);

if (isPValid(_player2))
// Teleport player there
if (pos != null && angle != null)
{
// Get spawnpoint from the arena
Vector? pos = p2Spawn.AbsOrigin;
QAngle? angle = p2Spawn.AbsRotation;
Vector? velocity = new Vector(0, 0, 0);

// Teleport player there
if (pos != null && angle != null)
{
_player2!.PlayerController!.Pawn.Value?.Teleport(pos, angle, velocity);
}
_player2!.ResetPlayerWeapons(_roundType);
_player2!.PlayerController!.Pawn!.Value!.Health = 100;
player?.PlayerController?.Pawn.Value?.Teleport(pos, angle, velocity);
}

// Reset weapons and health
player!.ResetPlayerWeapons(roundType);
player!.PlayerController!.Pawn!.Value!.Health = 100;
}
}

Expand Down Expand Up @@ -180,7 +176,7 @@ public void OnPlayerDeath(CCSPlayerController playerController)

public void LogCurrentInfo()
{
if (isPValid(_player1) || isPValid(_player2))
if (_isArenaActive)
{
_logger.LogInformation($"------ ARENA {_rank} -----");
if (isPValid(_player1)) _logger.LogInformation($"Player1: {_player1?.PlayerController.PlayerName}");
Expand All @@ -191,29 +187,15 @@ public void LogCurrentInfo()

public void OnRoundEnd()
{
// Notify player of win/loss
if (isPValid(_player1) && isPValid(_player2))
{
if (_player1Kills > _player2Kills)
{
_player1!.PrintToChat($"{ChatColors.Green}You won!");
_player2!.PrintToChat($"{ChatColors.Red}You lost!");
}
else if (_player2Kills > _player1Kills)
{
_player2!.PrintToChat($"{ChatColors.Green}You won!");
_player1!.PrintToChat($"{ChatColors.Red}You lost!");
}
else if (_player1HasLastKill)
{
_player1!.PrintToChat($"{ChatColors.Green}You won!");
_player2!.PrintToChat($"{ChatColors.Red}You lost!");
}
else
{
_player2!.PrintToChat($"{ChatColors.Green}You won!");
_player1!.PrintToChat($"{ChatColors.Red}You lost!");
}
string loserMsg = $"{ChatColors.Red}You lost!";
string winnerMsg = $"{ChatColors.Green}You won!";
string winnerMessage = _player1Kills > _player2Kills ? winnerMsg : loserMsg;
string loserMessage = _player1Kills > _player2Kills ? loserMsg : winnerMsg;

_player1!.PrintToChat(winnerMessage);
_player2!.PrintToChat(loserMessage);
}
}

Expand All @@ -240,18 +222,12 @@ public ArenaResult GetArenaResult()
}
}

// If player1 was valid, give them the win
if (isPValid(_player1))
{
return new ArenaResult(ArenaResultType.NoOpponent, _player1, null);
}

// If player2 was valid, give them the win
if (isPValid(_player2))
// If player1 or player2 was valid, give them the win
if (_isArenaActive)
{
return new ArenaResult(ArenaResultType.NoOpponent, _player2, null);
ArenaPlayer? winner = isPValid(_player1) ? _player1 : _player2;
return new ArenaResult(ArenaResultType.NoOpponent, winner, null);
}

// If this point is reached, the arena either started with or now has no players
return new ArenaResult(ArenaResultType.Empty, null, null);
}
Expand All @@ -260,4 +236,9 @@ private bool isPValid(ArenaPlayer? player)
{
return player != null && player.PlayerController.IsValid && player.PlayerController.Connected == PlayerConnectedState.PlayerConnected && player.PlayerController.Pawn.IsValid;
}

private bool anyPValid()
{
return isPValid(_player1) || isPValid(_player2);
}
}

0 comments on commit 96b48eb

Please sign in to comment.