Skip to content

Commit

Permalink
🚸 don't give points for manual trips (#2170)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Nov 29, 2023
1 parent af34cbd commit e03b406
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 77 deletions.
11 changes: 8 additions & 3 deletions app/Enum/PointReason.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

enum PointReason: int
{
case IN_TIME = 0;
case GOOD_ENOUGH = 1;
case IN_TIME = 0;
case GOOD_ENOUGH = 1;
case NOT_SUFFICIENT = 2;
case FORCED = 3;
case FORCED = 3;

/**
* Trip was manually created by the user => no points.
*/
case MANUAL_TRIP = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Dto\PointCalculation;
use App\Enum\HafasTravelType;
use App\Enum\PointReason;
use App\Enum\TripSource;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use JetBrains\PhpStorm\Pure;
Expand All @@ -17,6 +18,7 @@ public static function calculatePoints(
HafasTravelType $hafasTravelType,
Carbon $departure,
Carbon $arrival,
TripSource $tripSource,
bool $forceCheckin = false,
Carbon $timestampOfView = null
): PointCalculation {
Expand All @@ -30,7 +32,7 @@ public static function calculatePoints(
return self::calculatePointsWithReason(
basePoints: $base,
distancePoints: $distance,
pointReason: self::getReason($departure, $arrival, $forceCheckin, $timestampOfView),
pointReason: self::getReason($departure, $arrival, $forceCheckin, $tripSource, $timestampOfView),
);
}

Expand All @@ -40,6 +42,15 @@ 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,
Expand Down Expand Up @@ -75,14 +86,18 @@ public static function getFactorByReason(PointReason $pointReason): float|int {
}

public static function getReason(
Carbon $departure,
Carbon $arrival,
bool $forceCheckin,
Carbon $timestampOfView
Carbon $departure,
Carbon $arrival,
bool $forceCheckin,
TripSource $tripSource,
Carbon $timestampOfView
): PointReason {
if ($forceCheckin) {
return PointReason::FORCED;
}
if ($tripSource === TripSource::USER) {
return PointReason::MANUAL_TRIP;
}

/**
* Full points, 20min before the departure time or during the ride
Expand Down
39 changes: 21 additions & 18 deletions app/Http/Controllers/Backend/Transport/TrainCheckinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ private static function createTrainCheckin(
hafasTravelType: $trip->category,
departure: $firstStop->departure,
arrival: $lastStop->arrival,
forceCheckin: $force
tripSource: $trip->source,
forceCheckin: $force,
);
try {
$trainCheckin = TrainCheckin::create([
Expand Down Expand Up @@ -221,7 +222,7 @@ private static function createTrainCheckin(
}

public static function changeDestination(
TrainCheckin $checkin,
TrainCheckin $checkin,
TrainStopover $newDestinationStopover
): PointReason {
if ($newDestinationStopover->arrival_planned->isBefore($checkin->origin_stopover->arrival_planned)
Expand All @@ -232,13 +233,14 @@ public static function changeDestination(
}

$newDistance = (new LocationController($checkin->HafasTrip, $checkin->origin_stopover, $newDestinationStopover))
->calculateDistance();
->calculateDistance();

$pointsResource = PointsCalculationController::calculatePoints(
distanceInMeter: $newDistance,
hafasTravelType: $checkin->HafasTrip->category,
departure: $checkin->origin_stopover->departure,
arrival: $newDestinationStopover->arrival,
tripSource: $checkin->HafasTrip->source
);

$checkin->update([
Expand Down Expand Up @@ -286,42 +288,43 @@ public static function getHafasTrip(string $tripId, string $lineName, int $start
* @throws DistanceDeviationException
*/
public static function refreshDistanceAndPoints(Status $status, bool $resetPolyline = false): void {
$trainCheckin = $status->trainCheckin;
$checkin = $status->trainCheckin;
if ($resetPolyline) {
$trainCheckin->HafasTrip->update(['polyline_id' => null]);
$checkin->HafasTrip->update(['polyline_id' => null]);
}
$firstStop = $trainCheckin->origin_stopover;
$lastStop = $trainCheckin->destination_stopover;
$firstStop = $checkin->origin_stopover;
$lastStop = $checkin->destination_stopover;
$distance = (new LocationController(
hafasTrip: $trainCheckin->HafasTrip,
hafasTrip: $checkin->HafasTrip,
origin: $firstStop,
destination: $lastStop
))->calculateDistance();
$oldPoints = $trainCheckin->points;
$oldDistance = $trainCheckin->distance;
$oldPoints = $checkin->points;
$oldDistance = $checkin->distance;

if ($distance === 0 || $oldDistance !== 0 && $distance / $oldDistance >= 1.15) {
if ($distance === 0 || ($oldDistance !== 0 && $distance / $oldDistance >= 1.15)) {
Log::warning(sprintf(
'Distance deviation for status #%d is greater than 15 percent. Original: %d, new: %d',
$status->id,
$oldDistance,
$distance
));
'Distance deviation for status #%d is greater than 15 percent. Original: %d, new: %d',
$status->id,
$oldDistance,
$distance
));
throw new DistanceDeviationException();
}

$pointsResource = PointsCalculationController::calculatePoints(
distanceInMeter: $distance,
hafasTravelType: $trainCheckin->HafasTrip->category,
hafasTravelType: $checkin->HafasTrip->category,
departure: $firstStop->departure,
arrival: $lastStop->arrival,
tripSource: $checkin->HafasTrip->source,
timestampOfView: $status->created_at
);
$payload = [
'distance' => $distance,
'points' => $pointsResource->points,
];
$trainCheckin->update($payload);
$checkin->update($payload);
Log::debug(sprintf('Updated distance and points of status #%d: Old: %dm %dp New: %dm %dp',
$status->id,
$oldDistance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function edit(Request $request): RedirectResponse {
hafasTravelType: $status->trainCheckin->HafasTrip->category,
departure: $newDeparture,
arrival: $newArrival,
tripSource: $status->trainCheckin->HafasTrip->source,
timestampOfView: $newDeparture,
);

Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/HafasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enum\HafasTravelType as HTT;
use App\Enum\TravelType;
use App\Enum\TripSource;
use App\Exceptions\HafasException;
use App\Models\HafasOperator;
use App\Models\HafasTrip;
Expand Down Expand Up @@ -164,7 +165,7 @@ public static function fetchDepartures(
bool $skipTimeShift = false
) {
$client = self::getHttpClient();
$time = $skipTimeShift ? $when : (clone $when)->shiftTimezone("Europe/Berlin");
$time = $skipTimeShift ? $when : (clone $when)->shiftTimezone("Europe/Berlin");
$query = [
'when' => $time->toIso8601String(),
'duration' => $duration,
Expand Down Expand Up @@ -413,7 +414,8 @@ public static function fetchHafasTrip(string $tripID, string $lineName): HafasTr
'polyline_id' => $polyline->id,
'departure' => $tripJson->plannedDeparture,
'arrival' => $tripJson->plannedArrival,
'delay' => $tripJson->arrivalDelay ?? null
'delay' => $tripJson->arrivalDelay ?? null,
'source' => TripSource::HAFAS,
]);

//Save TrainStations
Expand Down
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions database/factories/HafasTripFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories;

use App\Enum\HafasTravelType;
use App\Enum\TripSource;
use App\Http\Controllers\TransportController;
use App\Models\HafasOperator;
use App\Models\HafasTrip;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function definition(): array {
'departure' => now()->subMinutes(15)->format('c'),
'arrival' => now()->addMinutes(80)->format('c'),
'delay' => 0, //TODO: is deprecated? used?
'source' => TripSource::HAFAS,
];
}

Expand Down
3 changes: 2 additions & 1 deletion resources/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@
"overlapping-checkin.description": "Deine Fahrt konnte nicht gespeichert werden, da es mit deinem Check-In in :lineName überlappt.",
"overlapping-checkin.description2": "Möchtest du den Check-In jetzt erzwingen?",
"no-points-warning": "Dafür bekommst du nicht die vollen Punkte.",
"no-points-message": "Du hast nicht die vollen Punkte bekommen, da du den Check-In erzwungen hast.",
"no-points-message.forced": "Du hast nicht die vollen Punkte bekommen, da du den Check-In erzwungen hast.",
"no-points-message.manual": "Du hast keine Punkte bekommen, da diese Fahrt manuell erstellt wurde.",
"overlapping-checkin.force-yes": "Ja, erzwingen.",
"overlapping-checkin.force-no": "Nein, nichts machen.",
"report-bug": "Fehler melden",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/de_by.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@
"merry-christmas": "Mir wünschat dir a frohe Weihnachtszeit!",
"overlapping-checkin.description2": "Meachtsch dean Check-In jetz erzwinga?",
"no-points-warning": "Dofiar kriagsch id dia volla Punkte.",
"no-points-message": "Du hosch id dia volla Punkte kriagt, weil dean Check-In erzwunga hosch.",
"no-points-message.forced": "Du hosch id dia volla Punkte kriagt, weil dean Check-In erzwunga hosch.",
"overlapping-checkin.force-yes": "Jo, erzwinga.",
"overlapping-checkin.force-no": "Nei, nix doa.",
"report-bug": "Feahler melda",
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@
"overlapping-checkin.description": "Your trip could not be saved because it overlapped with your check-in in :lineName.",
"overlapping-checkin.description2": "Do you want to force check-in now?",
"no-points-warning": "You won't get full points for that.",
"no-points-message": "You didn't get the full points because you forced the check-in.",
"no-points-message.forced": "You didn't get the full points because you forced the check-in.",
"no-points-message.manual": "You didn't get points because this trip was added manually.",
"overlapping-checkin.force-yes": "Yes, enforce.",
"overlapping-checkin.force-no": "No, do nothing.",
"report-bug": "Report bug",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@
"request-feature": "Demander une fonction",
"email.verification.too-many-requests": "Veuillez patienter quelques minutes avant de demander un autre mail de confirmation.",
"overlapping-checkin": "Chevauchement de trajet",
"no-points-message": "Vous n'avez pas obtenu tous les points pour avoir forcé l'enregistrement.",
"no-points-message.forced": "Vous n'avez pas obtenu tous les points pour avoir forcé l'enregistrement.",
"about.who0": "Träwelling est un projet Open-Sources.",
"about.who1": "Depuis 2013 diverses personnes ont développé le projet. Quelques corrections de bugs et aussi quelques changements plus importants.",
"about.who2": "Vous pouvez voir la liste des contributeurs au code sur <a href=\":link\" target=\"_blank\">GitHub</a>.",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@
"empty-en-route": "Attualmente non ci sono utenti Träwelling in giro.",
"overlapping-checkin.description2": "Vuoi forzare il check-in?",
"no-points-warning": "Non otterrai il massimo dei punti.",
"no-points-message": "Non hai ottenuto il massimo dei punti perché hai forzato il check-in.",
"no-points-message.forced": "Non hai ottenuto il massimo dei punti perché hai forzato il check-in.",
"overlapping-checkin.force-yes": "Si, forzare.",
"report-bug": "Segnalare un bug",
"about.events": "Cosa sono gli eventi?",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/nb_NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@
"merry-christmas": "Vi ønsker deg en riktig god jul!",
"overlapping-checkin.description2": "Vil du tvinge innsjekk nå?",
"no-points-warning": "Du får ikke fulle poeng for det.",
"no-points-message": "Du fikk ikke hele poeng for å tvinge inn innsjekkingen.",
"no-points-message.forced": "Du fikk ikke hele poeng for å tvinge inn innsjekkingen.",
"overlapping-checkin.force-yes": "Ja, tvinger.",
"overlapping-checkin": "Overlappende tur",
"overlapping-checkin.description": "Reisen din kunne ikke lagres fordi den overlapper med innsjekkingen din i :lineName.",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@
"settings.ics.descriptor": "Hier kun je je ics-Link beheren. Met deze links kun je eerder gemaakte ritten in een agenda met ics-ondersteuning zetten.",
"experimental-feature": "Experimentele functie",
"settings.revoke-token.success": "De toegang is ingetrokken",
"no-points-message": "Je hebt niet het volledige aantal punten verdiend omdat je de check-in geforceerd hebt.",
"no-points-message.forced": "Je hebt niet het volledige aantal punten verdiend omdat je de check-in geforceerd hebt.",
"settings.visibility.disclaimer": "Als je profiel privé is, kunnen alleen volgers je statussen zien, óók je openbare statussen.",
"about.events.description1": "Träwelling brengt mensen samen die samen gebruikmaken van het openbaar vervoer. Soms gaan ze zelfs naar hetzelfde evenement zonder dat ze dat van elkaar weten!",
"about.events.description2": "Daarom hebben we de evenmentfunctie gemaakt. Een ieder kan <a href=\":link\">via ons formulier</a> een evenement voor een vastgelegde periode aanbevelen, en alle gebruikers van Träwellig kunnen de check-ins aan het evenement in kwestie koppelen.",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@
"overlapping-checkin.description": "Din resa kunde inte sparas eftersom den överlappar din incheckning i :lineName.",
"overlapping-checkin.description2": "Vill du tvinga incheckning nu?",
"no-points-warning": "Du får inte full poäng för det.",
"no-points-message": "Du fick inte hela poängen för att tvinga fram incheckningen.",
"no-points-message.forced": "Du fick inte hela poängen för att tvinga fram incheckningen.",
"overlapping-checkin.force-yes": "Ja, tvinga.",
"overlapping-checkin.force-no": "Nej, gör ingenting.",
"about.who0": "Träwelling är ett projekt med öppen källkod.",
Expand Down
9 changes: 8 additions & 1 deletion resources/views/components/checkin-success.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@
</a>
</span>
@endif
@if($points === 0 && $pointReason === PointReason::MANUAL_TRIP)
<br/>
<span class="text-danger">
<i class="fa-solid fa-triangle-exclamation"></i>
{{__('no-points-message.manual')}}
</span>
@endif
@if($points === 1 && $forced)
<br/>
<span class="text-danger">
<i class="fa-solid fa-triangle-exclamation"></i>
{{__('no-points-message')}}
{{__('no-points-message.forced')}}
</span>
@endif
</p>
Expand Down
Loading

0 comments on commit e03b406

Please sign in to comment.