Skip to content

Commit

Permalink
Stability++
Browse files Browse the repository at this point in the history
  • Loading branch information
starswaitforus authored and solcloud committed Oct 9, 2024
1 parent 205450d commit 51d97ab
Show file tree
Hide file tree
Showing 48 changed files with 214 additions and 124 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
run: |
sudo apt-get update && sudo apt-get install -y \
composer
sudo sh -c 'echo "zend.assertions = 1" >> /etc/php/8.1/cli/php.ini'
- name: "Install Composer dependencies"
timeout-minutes: 1
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"@composer dumpautoload --optimize --classmap-authoritative",
"@stan",
"@unit"
],
"checkfull": [
"@check",
"@infection"
]
},
"require": {
Expand Down
30 changes: 19 additions & 11 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,21 @@
"@default": true,
"@conditional_boundary": false,
"@conditional_negotiation": false,
"Break_": false,
"CastInt": false,
"Continue_": false,
"DecrementInteger": false,
"FalseValue": false,
"IfNegation": false,
"Increment": false,
"IncrementInteger": false,
"InstanceOf_": false,
"IntegerNegation": false,
"LogicalAnd": false,
"LogicalAndAllSubExprNegation": false,
"LogicalAndNegation": false,
"LogicalAndSingleSubExprNegation": false,
"LogicalOrAllSubExprNegation": false,
"LogicalOr": false,
"LogicalOrAllSubExprNegation": false,
"Minus": false,
"Modulus": false,
"MulEqual": false,
"Multiplication": false,
"Plus": false,
"PlusEqual": false,
"RoundingFamily": false,
"TrueValue": false,
"ArrayItem": {
Expand All @@ -51,17 +44,32 @@
"cs\\Event\\*::serialize",
],
},
"Break_": {
"ignore": [
"cs\\Traits\\Player\\JumpTrait::jump",
],
},
"Coalesce": {
"ignoreSourceCodeByRegex": [
".+\\(\\$skipPlayerIds\\[\\$playerId\\].+",
".+SpeedMultiplier-\\{\\$itemId\\}.+",
],
},
"Division": {
ignoreSourceCodeByRegex: [
"ignoreSourceCodeByRegex": [
".+rand\\(.+",
],
},
"InstanceOf_": {
"ignoreSourceCodeByRegex": [
"if\\s*\\(\\$this->ball->getResolutionAngleVertical\\(\\) > 0 && \\(.+",
],
},
"LogicalAndNegation": {
"ignoreSourceCodeByRegex": [
"if\\s*\\(\\$count > 10 && \\$count % 2 === 0\\)\\s\\{",
],
},
"MatchArmRemoval": {
"ignoreSourceCodeByRegex": [
".+GameException::invalid\\(.+",
Expand All @@ -80,13 +88,13 @@
"\\$this->addSoundEvent\\(.+\\);",
"\\$bullet->addPlayerIdSkip\\(\\$playerId\\);",
"\\$this->convertToNavMeshNode\\(\\$navmesh\\);",
]
],
},
"Ternary": {
"ignore": [
"cs\\Core\\Player::serialize",
],
ignoreSourceCodeByRegex: [
"ignoreSourceCodeByRegex": [
".+rand\\(.+",
],
},
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ parameters:
- server/src/
- test/
level: max
checkMissingCallableSignature: true
3 changes: 2 additions & 1 deletion server/src/Core/Bullet.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function getOrigin(): Point

public function lowerDamage(int $amount): void
{
$this->damage -= abs($amount);
assert($amount >= 0);
$this->damage -= $amount;
}

public function getDamage(): int
Expand Down
28 changes: 18 additions & 10 deletions server/src/Core/Collision.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public static function pointWithBox(Point $point, Box $box): bool

public static function pointWithBoxBoundary(Point $point, Point $boxMin, Point $boxMax): bool
{
assert($boxMin->x <= $boxMax->x);
assert($boxMin->y <= $boxMax->y);
assert($boxMin->z <= $boxMax->z);

if ($point->y > $boxMax->y || $point->y < $boxMin->y) {
return false;
}
Expand All @@ -194,20 +198,24 @@ public static function pointWithBoxBoundary(Point $point, Point $boxMin, Point $

public static function boxWithBox(Point $boundaryAMin, Point $boundaryAMax, Point $boundaryBMin, Point $boundaryBMax): bool
{
assert($boundaryAMin->x <= $boundaryAMax->x);
assert($boundaryAMin->y <= $boundaryAMax->y);
assert($boundaryAMin->z <= $boundaryAMax->z);

assert($boundaryBMin->x <= $boundaryBMax->x);
assert($boundaryBMin->y <= $boundaryBMax->y);
assert($boundaryBMin->z <= $boundaryBMax->z);

if ($boundaryAMin->x > $boundaryBMax->x || $boundaryBMin->x > $boundaryAMax->x) {
return false;
}
if ($boundaryAMin->y > $boundaryBMax->y || $boundaryBMin->y > $boundaryAMax->y) {
return false;
}

if (
$boundaryAMax->x >= $boundaryBMin->x
&& $boundaryAMin->x <= $boundaryBMax->x
&& $boundaryAMax->z >= $boundaryBMin->z
&& $boundaryAMin->z <= $boundaryBMax->z
) {
return true;
if ($boundaryAMin->z > $boundaryBMax->z || $boundaryBMin->z > $boundaryAMax->z) {
return false;
}

return false;
return true;
}

}
4 changes: 2 additions & 2 deletions server/src/Core/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Game
private array $players = [];
/** @var Event[] */
private array $events = [];
/** @var Event[] */
/** @var list<Event> */
private array $tickEvents = [];

protected int $tick = 0;
Expand Down Expand Up @@ -228,7 +228,7 @@ public function getWorld(): World
}

/**
* @return Event[]
* @return list<Event>
*/
public function consumeTickEvents(): array
{
Expand Down
2 changes: 1 addition & 1 deletion server/src/Core/PathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function findTile(Point $pointOnFloor, int $radius): Point

for ($distance = 1; $distance <= $maxDistance; $distance++) {
$candidate->addPart(...$this->moves[$angle]);
if (!$this->canFullyMoveTo($candidate, $angle, 1, $radius, $this->colliderHeight)) {
if (!$this->canFullyMoveTo($candidate, $angle, 1, $radius, $this->colliderHeight)) { // @infection-ignore-all
break;
}

Expand Down
7 changes: 6 additions & 1 deletion server/src/Core/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,13 @@ public function lowerArmor(int $armorDamage): void

public function lowerHealth(int $healthDamage): void
{
assert($healthDamage >= 0);
if ($healthDamage <= 0) {
return;
}

$this->addEvent(new TimeoutEvent(null, 70), $this->eventIdShotSlowdown);
$this->health -= abs($healthDamage);
$this->health -= $healthDamage;
if ($this->health <= 0) {
$this->health = 0;
$this->onPlayerDied();
Expand Down
4 changes: 3 additions & 1 deletion server/src/Core/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Setting
'flyingMovementSpeedMultiplier' => 0.8,
'throwSpeed' => 40,

'playerVelocity' => 0,
'playerVelocity' => 100,
'playerHeadRadius' => 10,
'playerBoundingRadius' => 60,
'playerJumpHeight' => 150,
Expand Down Expand Up @@ -52,6 +52,8 @@ public static function loadConstants(array $constants): void
*/
private static function fixBackwardCompatible(array &$constants): void
{
// BC code
$constants['playerVelocity'] = ($constants['playerVelocity'] ?? 0);
foreach (self::defaultConstant as $key => $defaultValue) {
if (isset($constants[$key])) {
continue;
Expand Down
4 changes: 1 addition & 3 deletions server/src/Core/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,7 @@ public function playerDiedToFallDamage(Player $playerDead): void

private function playerDiedToFlame(Player $playerCulprit, Player $playerDead, Flammable $item): void
{
if (false === ($item instanceof Grenade)) {
throw new GameException("New flammable non grenade type?"); // @codeCoverageIgnore
}
assert($item instanceof Grenade, "New flammable non grenade type?");
$this->game->playerGrenadeKilledEvent($playerCulprit, $playerDead, $item);
}

Expand Down
3 changes: 2 additions & 1 deletion server/src/Equipment/Kevlar.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function repairArmor(): void

public function lowerArmor(int $armorDamage): void
{
$this->armor -= abs($armorDamage);
assert($armorDamage >= 0);
$this->armor -= $armorDamage;
if ($this->armor <= 0) {
$this->armor = 0;
$this->type = ArmorType::NONE;
Expand Down
3 changes: 2 additions & 1 deletion server/src/Event/CallbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class CallbackEvent extends Event
{
public function __construct(private Closure $callback)
/** @param Closure(static,int):void $callback */
public function __construct(private readonly Closure $callback)
{
}

Expand Down
1 change: 1 addition & 0 deletions server/src/Event/CrouchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class CrouchEvent extends TickEvent
{
public readonly int $moveOffset;

/** @param Closure(static,int):void $callback */
public function __construct(public bool $directionDown, Closure $callback)
{
parent::__construct($callback, Setting::tickCountCrouch());
Expand Down
2 changes: 1 addition & 1 deletion server/src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class Event implements NetSerializable
{
protected int $tickCount = 0;
public int $customId = 0;
/** @var Closure[] function(Event $event):void{} */
/** @var list<Closure(static):void> function(Event $event):void{} */
public array $onComplete = [];

abstract public function process(int $tick): void;
Expand Down
2 changes: 1 addition & 1 deletion server/src/Event/GameOverEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use cs\Enum\GameOverReason;

final class GameOverEvent extends TickEvent
final class GameOverEvent extends NoTickEvent
{

public function __construct(public readonly GameOverReason $reason)
Expand Down
8 changes: 4 additions & 4 deletions server/src/Event/GameStartEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use cs\Core\Player;
use cs\Net\ServerSetting;

final class GameStartEvent extends TickEvent
final class GameStartEvent extends NoTickEvent
{

public function __construct(
private Player $player,
private ServerSetting $setting,
private GameProperty $gameSetting,
private readonly Player $player,
private readonly ServerSetting $setting,
private readonly GameProperty $gameSetting,
)
{
}
Expand Down
1 change: 1 addition & 0 deletions server/src/Event/KillEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function __construct(
private bool $headShot,
)
{
parent::__construct();
}

public function getPlayerDead(): Player
Expand Down
15 changes: 15 additions & 0 deletions server/src/Event/NoTickEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace cs\Event;

use cs\Core\GameException;

abstract class NoTickEvent extends Event
{

public function process(int $tick): void
{
GameException::invalid(get_class($this)); // @codeCoverageIgnore
}

}
1 change: 1 addition & 0 deletions server/src/Event/PauseStartEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
final class PauseStartEvent extends TimeoutEvent
{

/** @param Closure(static,int):void $callback */
public function __construct(private Game $game, private PauseReason $reason, Closure $callback, int $timeoutMs)
{
parent::__construct($callback, $timeoutMs);
Expand Down
1 change: 1 addition & 0 deletions server/src/Event/PlantEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
final class PlantEvent extends TimeoutEvent
{

/** @param Closure(static,int):void $callback */
public function __construct(Closure $callback, int $timeoutMs, private Point $position)
{
parent::__construct($callback, $timeoutMs);
Expand Down
1 change: 1 addition & 0 deletions server/src/Event/RoundEndEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class RoundEndEvent extends TickEvent

public function __construct(private Game $game, public readonly bool $attackersWins, public readonly RoundEndReason $reason)
{
parent::__construct();
$this->roundNumberEnded = $game->getRoundNumber();
}

Expand Down
1 change: 1 addition & 0 deletions server/src/Event/RoundStartEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
final class RoundStartEvent extends TickEvent
{

/** @param Closure(static,int):void $callback */
public function __construct(private int $aliveAttackers, private int $aliveDefenders, Closure $callback)
{
parent::__construct($callback);
Expand Down
1 change: 1 addition & 0 deletions server/src/Event/SoundEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class SoundEvent extends TickEvent

public function __construct(public readonly Point $position, public readonly SoundType $type)
{
parent::__construct();
}

public function setItem(?Item $item): self
Expand Down
7 changes: 2 additions & 5 deletions server/src/Event/TickEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

class TickEvent extends Event
{
protected ?Closure $callback = null;
protected int $maxTickCount = 1;

public function __construct(?Closure $callback = null, int $maxTickCount = 1)
/** @param ?Closure(static,int):void $callback */
public function __construct(protected ?Closure $callback = null, protected int $maxTickCount = 1)
{
$this->callback = $callback;
$this->maxTickCount = $maxTickCount;
}

final public function process(int $tick): void
Expand Down
7 changes: 3 additions & 4 deletions server/src/Event/TimeoutEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

class TimeoutEvent extends Event
{
protected int $tickCountTimeout = 0;
protected ?Closure $callback = null;
protected int $tickCountTimeout;

public function __construct(?Closure $callback, protected int $timeoutMs)
/** @param ?Closure(static,int):void $callback */
public function __construct(protected ?Closure $callback, protected int $timeoutMs)
{
$this->callback = $callback;
$this->tickCountTimeout = $this->timeMsToTick($this->timeoutMs);
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/Net/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Server

/** @var Client[] [playerId => Client] */
private array $clients = [];
/** @var array<int,callable> [playerId => fn(PlayerControl $control):void {} */
/** @var array<int,callable(PlayerControl):void> [playerId => fn(PlayerControl $control):void {} */
private array $tickCommands = [];
private LoggerInterface $logger;

Expand Down
Loading

0 comments on commit 51d97ab

Please sign in to comment.