Skip to content

Commit

Permalink
fix issue with date cutoffs in lichess games download
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Oct 22, 2023
1 parent e3d5d1a commit bf0ddfb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
15 changes: 6 additions & 9 deletions src/components/common/AccountCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ function AccountCards({
setDatabases: React.Dispatch<React.SetStateAction<DatabaseInfo[]>>;
}) {
const [sessions, setSessions] = useAtom(sessionsAtom);
function countGames(stats: ChessComStats) {
let total = 0;
Object.values(stats).forEach((stat) => {
if (stat.record)
total += stat.record.win + stat.record.loss + stat.record.draw;
});
return total;
}
return (
<>
{sessions.map((session) => {
Expand Down Expand Up @@ -93,6 +85,11 @@ function AccountCards({
);
}
if (session.chessCom && session.chessCom.stats) {
let totalGames = 0;
Object.values(session.chessCom.stats).forEach((stat) => {
if (stat.record)
totalGames += stat.record.win + stat.record.loss + stat.record.draw;
});
return (
<AccountCard
key={session.chessCom.username}
Expand All @@ -105,7 +102,7 @@ function AccountCards({
) ?? null
}
updatedAt={session.updatedAt}
total={countGames(session.chessCom.stats)}
total={totalGames}
stats={getStats(session.chessCom.stats)}
logout={() => {
setSessions((sessions) =>
Expand Down
22 changes: 12 additions & 10 deletions src/components/home/AccountCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ export function AccountCard({
);
});
const [loading, setLoading] = useState(false);
const [lastGameDate, setLastGameDate] = useState<Date | null>(null);
const timestamp = lastGameDate?.getTime() ?? null;
const [lastGameDate, setLastGameDate] = useState<number | null>(null);

async function convert(filepath: string, timestamp: number | null) {
info("converting " + filepath);
info("converting " + filepath + " " + timestamp);
await invoke("convert_pgn", {
file: filepath,
timestamp,
Expand All @@ -114,10 +113,13 @@ export function AccountCard({
sort: "date",
direction: "desc",
}).then((games) => {
if (games.count > 0) {
setLastGameDate(
new Date(games.data[0].date + " " + games.data[0].time)
);
if (games.count > 0 && games.data[0].date && games.data[0].time) {
const [year, month, day] = games.data[0].date.split(".").map(Number);
const [hour, minute, second] = games.data[0].time
.split(":")
.map(Number);
const d = Date.UTC(year, month - 1, day, hour, minute, second);
setLastGameDate(d);
}
});
}
Expand Down Expand Up @@ -176,16 +178,16 @@ export function AccountCard({
onClick={async () => {
setLoading(true);
if (type === "lichess") {
await downloadLichess(title, timestamp, token);
await downloadLichess(title, lastGameDate, token);
} else {
await downloadChessCom(title, timestamp);
await downloadChessCom(title, lastGameDate);
}
const p = await resolve(
await appDataDir(),
"db",
`${title}_${type}.pgn`
);
convert(p, timestamp).catch(() => {
convert(p, lastGameDate).catch(() => {
setLoading(false);
});
}}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lichess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export async function downloadLichess(
) {
let url = `${baseURL}/games/user/${player}?perfType=ultraBullet,bullet,blitz,rapid,classical,correspondence&rated=true`;
if (timestamp) {
url += `?since=${timestamp}`;
url += `&since=${timestamp}`;
}
const path = await resolve(await appDataDir(), "db", player + "_lichess.pgn");
await invoke("download_file", {
Expand Down

0 comments on commit bf0ddfb

Please sign in to comment.