Skip to content

Commit

Permalink
display time according to user setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapenbr committed Jul 31, 2024
1 parent a5e681f commit 0e2d36b
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 37 deletions.
24 changes: 22 additions & 2 deletions src/components/live/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
CarStint,
StintInfo,
} from "@buf/mpapenbr_iracelog.community_timostamm-protobuf-ts/iracelog/analysis/v1/car_stint_pb";

import _ from "lodash";
import { Comparator } from "semver";
import { SessionState } from "../../stores";
import { ICarBaseData as ICarBaseDataGrpc } from "../../stores/grpc/slices/availableCarsSlice";
import { sortCarNumberStr } from "../../utils/output";
import { secAsHHMMSS, sortCarNumberStr } from "../../utils/output";

export interface ICarFilterData {
carNum: string;
Expand Down Expand Up @@ -177,3 +177,23 @@ export const isInSelectedRange = (si: StintInfo, range: [number, number]): boole
export const supportsCarData = (raceloggerVersion: string): boolean => {
return new Comparator(">=0.4.4").test(raceloggerVersion);
};

export const hocDisplayTimeByUserSettings =
(sessionData: SessionState, timeMode: string, formatter?: (d: number) => string) =>
(d: number): string => {
const toAdd = sessionData.session.timeOfDay - sessionData.session.sessionTime;
const myFormatter = formatter ?? secAsHHMMSS;
switch (timeMode) {
case "sim":
return myFormatter(d + toAdd);
case "real":
const ref: Date = sessionData.recordDate;
// // JS hell when calculating days. In order to use own formatter we need the seconds.
// // Note: TZ-offset -60 on GMT+0100 ;)
const val = myFormatter((ref.getTime() / 1000 + d - ref.getTimezoneOffset() * 60) % 86400);
return val;
case "session":
default:
return myFormatter(d);
}
};
5 changes: 3 additions & 2 deletions src/components/nivo/carPitstops.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CarPit,
PitInfo,
} from "@buf/mpapenbr_iracelog.community_timostamm-protobuf-ts/iracelog/analysis/v1/car_pit_pb";
import { secAsHHMMSS, secAsMMSS } from "../../utils/output";
import { secAsMMSS } from "../../utils/output";

const { Option } = Select;
const { useToken } = theme;
Expand All @@ -16,6 +16,7 @@ interface MyProps {
showCars: string[];
hideLongPitstops: boolean;
hideThreshold: number;
rangeTimeFormatter: (sec: number) => string;
}
const CarPitstopsNivo: React.FC<MyProps> = (props: MyProps) => {
// const cars = useSelector((state: ApplicationState) => state.raceData.availableCars);
Expand Down Expand Up @@ -83,7 +84,7 @@ value: 77.66666666553647
#{pitInfo[pitIdx].carNum} {data.id}
</strong>
<br />
Lap {pitInfo[pitIdx].lapEnter} at {secAsHHMMSS(pitInfo[pitIdx].enterTime)}
Lap {pitInfo[pitIdx].lapEnter} at {props.rangeTimeFormatter(pitInfo[pitIdx].enterTime)}
</div>
);
};
Expand Down
5 changes: 3 additions & 2 deletions src/components/nivo/carStints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
CarStint,
StintInfo,
} from "@buf/mpapenbr_iracelog.community_timostamm-protobuf-ts/iracelog/analysis/v1/car_stint_pb";
import { secAsHHMMSS, secAsMMSS } from "../../utils/output";
import { secAsMMSS } from "../../utils/output";

const { useToken } = theme;
interface MyProps {
carStints: CarStint[];
carOccs: CarOccupancy[];
showCars: string[];
showAsLabel: string;
rangeTimeFormatter: (sec: number) => string;
}
const CarStintsNivo: React.FC<MyProps> = (props: MyProps) => {
const { token } = useToken();
Expand Down Expand Up @@ -103,7 +104,7 @@ value: 77.66666666553647
<br />
Lap {item.lapExit} - {item.lapEnter} ({item.lapEnter - item.lapExit + 1})
<br />
{secAsHHMMSS(item.exitTime)} - {secAsHHMMSS(item.enterTime)}
{props.rangeTimeFormatter(item.exitTime)} - {props.rangeTimeFormatter(item.enterTime)}
</div>
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/nivo/stintTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ interface MyProps {
stintInfo: StintInfo;
driver: string;
avgLap?: number;
displayTime?: (sec: number) => string;
}
const StintTooltip: React.FC<MyProps> = (props: MyProps) => {
const dispTime = props.displayTime ?? secAsHHMMSS;
return (
<div className="iracelog-stint-tooltip">
<strong>
Expand All @@ -21,7 +23,7 @@ const StintTooltip: React.FC<MyProps> = (props: MyProps) => {
{props.stintInfo.lapEnter - props.stintInfo.lapExit + 1})
{props.avgLap ? ` (${lapTimeString(props.avgLap)})` : ""}
<br />
{secAsHHMMSS(props.stintInfo.exitTime)} - {secAsHHMMSS(props.stintInfo.enterTime)}
{dispTime(props.stintInfo.exitTime)} - {dispTime(props.stintInfo.enterTime)}
</div>
);
};
Expand Down
10 changes: 8 additions & 2 deletions src/components/nivo/stintsummary/stintstretch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Tooltip, theme } from "antd";
import React from "react";
import { useAppSelector } from "../../../stores";
import { secAsMMSS } from "../../../utils/output";
import { findDriverByStint } from "../../live/util";
import { findDriverByStint, hocDisplayTimeByUserSettings } from "../../live/util";
import StintTooltip from "../stintTooltip";
import { CombinedStintData } from "./commons";

Expand All @@ -27,8 +27,9 @@ const { useToken } = theme;
const StintStretch: React.FC<MyProps> = (props: MyProps) => {
const carOccs = useAppSelector((state) => state.carOccupancies);
const carStints = useAppSelector((state) => state.carStints);

const carStint = carStints.find((v) => v.carNum === props.carNum);
const sessionData = useAppSelector((state) => state.session);
const stateGlobalSettings = useAppSelector((state) => state.userSettings.global);
if (!props.carNum || !carStint) {
return <></>;
// return <Empty />;
Expand All @@ -46,6 +47,10 @@ const StintStretch: React.FC<MyProps> = (props: MyProps) => {
const textHeight = Math.min(12, barHeight);
const carNumLabel = props.showCarNum ? 40 : 0;

const displayTimeFromSettings = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
);
const step = (w - carNumLabel) / (maxTime - minTime);
// console.log(`min: ${minTime}, max: ${maxTime}, step: ${step}`);
const InternalGraph = (
Expand Down Expand Up @@ -98,6 +103,7 @@ const StintStretch: React.FC<MyProps> = (props: MyProps) => {
driver={
findDriverByStint(currentCarInfo, c.data as StintInfo)?.name ?? "n.a."
}
displayTime={displayTimeFromSettings}
/>
}
>
Expand Down
22 changes: 17 additions & 5 deletions src/components/stintRanking/rankingSvg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { StintInfo } from "@buf/mpapenbr_iracelog.community_timostamm-protobuf-t
import { Empty, Tooltip, theme } from "antd";
import _ from "lodash";
import { useAppSelector } from "../../stores";
import { lapTimeString } from "../../utils/output";
import { lapTimeString, secAsHHMM, secAsHHMMSS } from "../../utils/output";
import { boxPlotDataFor } from "../live/statsutil";
import { findDriverByStint } from "../live/util";
import { findDriverByStint, hocDisplayTimeByUserSettings } from "../live/util";
import StintTooltip from "../nivo/stintTooltip";
import { ICarCombinedStintData } from "../nivo/stintsummary/commons";

Expand All @@ -21,7 +21,6 @@ interface MyProps {
showCars: string[];
hightlightCars: string[];
toggleHighlightCar: (carNum: string) => void;
displayTime: (sec: number) => string;
}

interface IStintInfoExt extends StintInfo {
Expand All @@ -30,7 +29,8 @@ interface IStintInfoExt extends StintInfo {
const { useToken } = theme;
const StintRankingSvg: React.FC<MyProps> = (props: MyProps) => {
const carOccs = useAppSelector((state) => state.carOccupancies);
const globalSettings = useAppSelector((state) => state.userSettings.globalSettings);
const stateGlobalSettings = useAppSelector((state) => state.userSettings.globalSettings);
const sessionData = useAppSelector((state) => state.session);
const { token } = useToken();
const width = props.width ?? 800;
const height = props.height ?? 600;
Expand Down Expand Up @@ -82,6 +82,17 @@ const StintRankingSvg: React.FC<MyProps> = (props: MyProps) => {
}
return 1.0;
};
const displayTimeX = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
secAsHHMM,
);
const displayTimeTT = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
secAsHHMMSS,
);

const carRow = (carData: ICarCombinedStintData) => {
return carData.data
.filter((d) => d.type == "stint")
Expand All @@ -105,6 +116,7 @@ const StintRankingSvg: React.FC<MyProps> = (props: MyProps) => {
no={d.idx}
driver={findDriverByStint(currentCarInfo, d.data as StintInfo)?.name ?? "n.a."}
avgLap={d.avgTime}
displayTime={displayTimeTT}
/>
}
>
Expand Down Expand Up @@ -154,7 +166,7 @@ const StintRankingSvg: React.FC<MyProps> = (props: MyProps) => {
textAnchor="middle"
alignmentBaseline="hanging"
>
{props.displayTime(d)}
{displayTimeX(d)}
</text>
))}
</g>
Expand Down
12 changes: 9 additions & 3 deletions src/components/stintSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ColumnsType } from "antd/lib/table";
import React from "react";
import { useAppSelector } from "../stores";
import { lapTimeString, secAsHHMMSS } from "../utils/output";
import { findDriverByStint, getCarStints } from "./live/util";
import { findDriverByStint, getCarStints, hocDisplayTimeByUserSettings } from "./live/util";

interface MyProps {
carNum?: string;
Expand All @@ -27,6 +27,8 @@ const StintSummary: React.FC<MyProps> = (props: MyProps) => {
const carInfo = useAppSelector((state) => state.carOccupancies);
const carStints = useAppSelector((state) => state.carStints);
const carLaps = useAppSelector((state) => state.carLaps);
const sessionData = useAppSelector((state) => state.session);
const stateGlobalSettings = useAppSelector((state) => state.userSettings.global);

const carStint = carStints.find((v) => v.carNum === props.carNum);
if (!props.carNum || !carStint) {
Expand Down Expand Up @@ -92,11 +94,15 @@ const StintSummary: React.FC<MyProps> = (props: MyProps) => {
dataIndex: "avgLapTime",
},
];
const displayTimeFromSettings = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
);
const data: IStintSummary[] = getCarStints(carStints, props.carNum).map((v, idx) => ({
no: idx + 1,
driver: findDriverByStint(currentCarInfo, v)?.name ?? "n.a.",
startStint: secAsHHMMSS(v.exitTime),
endStint: secAsHHMMSS(v.enterTime),
startStint: displayTimeFromSettings(v.exitTime),
endStint: displayTimeFromSettings(v.enterTime),
laps: v.numLaps,
stintTime: secAsHHMMSS(v.stintTime),
avgLapTime: stintAvg(v),
Expand Down
8 changes: 7 additions & 1 deletion src/container/CarPitstopsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SettingOutlined } from "@ant-design/icons";
import { Button, Col, Popover, Row } from "antd";
import * as React from "react";
import MultiSelectCarFilter from "../components/live/multiCarSelectFilter";
import { hocDisplayTimeByUserSettings } from "../components/live/util";
import CarPitstopsNivo from "../components/nivo/carPitstops";
import PitstopControl from "../components/pitstopControl";
import { useAppDispatch, useAppSelector } from "../stores";
Expand All @@ -17,6 +18,7 @@ export const CarPitstopsContainer: React.FC = () => {

const stateGlobalSettings = useAppSelector((state) => state.userSettings.global);
const raceOrder = useAppSelector((state) => state.raceOrder);
const sessionData = useAppSelector((state) => state.session);
const dispatch = useAppDispatch();

const inputData: InputData = {
Expand Down Expand Up @@ -55,12 +57,16 @@ export const CarPitstopsContainer: React.FC = () => {
}
},
};

const displayTimeFromSettings = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
);
const graphProps = {
showCars: filterProps.selectedCars,
carPits: carPits,
hideLongPitstops: userSettings.hideLongPitstops,
hideThreshold: userSettings.hideThreshold,
rangeTimeFormatter: displayTimeFromSettings,
};
return (
<>
Expand Down
8 changes: 7 additions & 1 deletion src/container/CarStintsContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Col, Radio, RadioChangeEvent, Row } from "antd";
import * as React from "react";
import MultiSelectCarFilter from "../components/live/multiCarSelectFilter";
import { hocDisplayTimeByUserSettings } from "../components/live/util";
import CarStintsNivo from "../components/nivo/carStints";
import { useAppDispatch, useAppSelector } from "../stores";
import { IMultiCarSelectFilterSettings } from "../stores/grpc/slices/types";
Expand All @@ -15,6 +16,7 @@ export const CarStintsContainer: React.FC = () => {
const carOccs = useAppSelector((state) => state.carOccupancies);
const stateGlobalSettings = useAppSelector((state) => state.userSettings.global);
const raceOrder = useAppSelector((state) => state.raceOrder);
const sessionData = useAppSelector((state) => state.session);
const dispatch = useAppDispatch();

const inputData: InputData = {
Expand Down Expand Up @@ -53,12 +55,16 @@ export const CarStintsContainer: React.FC = () => {
}
},
};

const displayTimeFromSettings = hocDisplayTimeByUserSettings(
sessionData,
stateGlobalSettings.timeMode,
);
const graphProps = {
showCars: filterProps.selectedCars,
carStints: carStints,
carOccs: carOccs,
showAsLabel: userSettings.showAsLabel,
rangeTimeFormatter: displayTimeFromSettings,
};
const onShowModeChange = (e: RadioChangeEvent) => {
dispatch(updateStints({ ...userSettings, showAsLabel: e.target.value }));
Expand Down
20 changes: 2 additions & 18 deletions src/container/StintRankingContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
updateStintRankings,
} from "../stores/grpc/slices/userSettingsSlice";

import { secAsHHMM, secAsHHMMSS } from "../utils/output";
import { secAsHHMMSS } from "../utils/output";
import { InputData, prepareFilterData } from "./multiCarSelectFilterHelper";

const { Option } = Select;
Expand Down Expand Up @@ -80,21 +80,7 @@ export const StintRankingContainer: React.FC = () => {
}
},
};
const displayTimeFromSettings = (d: number) => {
const toAdd = sessionData.session.timeOfDay - sessionData.session.sessionTime;
switch (stateGlobalSettings.timeMode) {
case "session":
return secAsHHMM(d);
case "sim":
return secAsHHMM(d + toAdd);
case "real":
const ref: Date = sessionData.recordDate;
// // JS hell when calculating days. In order to use own formatter we need the seconds.
// // Note: TZ-offset -60 on GMT+0100 ;)
const val = secAsHHMM((ref.getTime() / 1000 + d - ref.getTimezoneOffset() * 60) % 86400);
return val;
}
};

const filterProps = prepareFilterData(inputData);
const props = {
...filterProps,
Expand All @@ -109,7 +95,6 @@ export const StintRankingContainer: React.FC = () => {
toggleHighlightCar: (carNum: string) => {
dispatch(toggleHighlightCar(carNum));
},
displayTime: displayTimeFromSettings,
};

const carColors = assignCarColors(availableCars);
Expand Down Expand Up @@ -176,7 +161,6 @@ export const StintRankingContainer: React.FC = () => {
showCars={props.selectedCars}
hightlightCars={props.highlightCars}
toggleHighlightCar={props.toggleHighlightCar}
displayTime={props.displayTime}
/>
</Row>
{globalWamp.currentLiveId === undefined ? (
Expand Down
1 change: 1 addition & 0 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const store = configureStore({
middleware: (getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: false }),
});
export type RootState = ReturnType<typeof store.getState>;
export type SessionState = ReturnType<typeof sessionSlice.reducer>;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();

0 comments on commit 0e2d36b

Please sign in to comment.