Skip to content

Commit

Permalink
🐛 Experimental checkin success returns "possible infinite points" (#2266
Browse files Browse the repository at this point in the history
)
  • Loading branch information
HerrLevin authored Jan 13, 2024
1 parent 10809e1 commit 29c2c80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
abstract class PointsCalculationController extends Controller
{

private const REDUCED_POINTS = [
PointReason::NOT_SUFFICIENT,
PointReason::FORCED,
PointReason::MANUAL_TRIP,
];

public static function calculatePoints(
int $distanceInMeter,
HafasTravelType $hafasTravelType,
Expand Down Expand Up @@ -42,41 +48,28 @@ private static function calculatePointsWithReason(
float $distancePoints,
PointReason $pointReason
): PointCalculation {
if ($pointReason === PointReason::MANUAL_TRIP) {
return new PointCalculation(
points: 0,
basePoints: $basePoints,
distancePoints: $distancePoints,
reason: $pointReason,
factor: 0,
);
}
if ($pointReason === PointReason::NOT_SUFFICIENT || $pointReason === PointReason::FORCED) {
return new PointCalculation(
points: 1,
basePoints: $basePoints,
distancePoints: $distancePoints,
reason: $pointReason,
factor: 0,
);
}
$factor = self::getFactorByReason($pointReason);

$basePoints *= $factor;
$distancePoints *= $factor;

return new PointCalculation(
points: ceil($basePoints + $distancePoints),
points: self::getPointsByReason($pointReason, ($basePoints + $distancePoints), $factor),
basePoints: $basePoints,
distancePoints: $distancePoints,
reason: $pointReason,
factor: $factor,
);
}

public static function getPointsByReason(PointReason $pointReason, int $points, float $factor): int {
if (in_array($pointReason, self::REDUCED_POINTS)) {
return $pointReason === PointReason::MANUAL_TRIP ? 0 : 1;
}

return ceil($points * $factor);
}

#[Pure]
public static function getFactorByReason(PointReason $pointReason): float|int {
if ($pointReason === PointReason::NOT_SUFFICIENT || $pointReason === PointReason::FORCED) {
if (in_array($pointReason, self::REDUCED_POINTS)) {
return 0;
}
if ($pointReason === PointReason::GOOD_ENOUGH) {
Expand Down
2 changes: 1 addition & 1 deletion resources/vue/components/CheckinSuccessHelper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
<p>{{ transChoice("checkin.points.earned", points.points, { points: points.points.toString() }) }}</p>
<p v-if="points.calculation.reason === 1 || points.calculation.reason === 2" class="text-muted">
{{ trans("checkin.points.could-have") }}
{{ trans("checkin.points.full", { points: (points.points / points.calculation.factor).toString() }) }}
{{ trans("checkin.points.full", { points: (points.calculation.base + points.calculation.distance ).toString() }) }}
</p>
<p v-if="points.calculation.reason === 3" class="text-danger">{{ trans("checkin.points.forced") }}</p>
Expand Down

0 comments on commit 29c2c80

Please sign in to comment.