Skip to content

Commit

Permalink
Replaced the add all games with drops remaining button with two new…
Browse files Browse the repository at this point in the history
… options in the `settings` screen

Fixed an issue where SGI would become temporarily unresponsive when stopping the `card farming` feature if there were a lot of games in the `card farming list`
  • Loading branch information
zevnda committed Aug 20, 2024
1 parent abe57a2 commit bbef4b0
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 140 deletions.
61 changes: 39 additions & 22 deletions components/automation/CardFarming.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, useRef } from 'react';
import { checkDrops, logEvent, startIdler, stopIdler } from '@/utils/utils';
import { checkDrops, getAllGamesWithDrops, logEvent, startIdler, stopIdler } from '@/utils/utils';
import StopButton from './StopButton';
import { Button, Skeleton, Spinner } from '@nextui-org/react';
import { IoCheckmark } from 'react-icons/io5';
Expand Down Expand Up @@ -50,31 +50,44 @@ export default function CardFarming({ setActivePage }) {
const cardFarming = JSON.parse(localStorage.getItem('cardFarming')) || [];
const steamCookies = JSON.parse(localStorage.getItem('steamCookies')) || {};
const userSummary = JSON.parse(localStorage.getItem('userSummary')) || {};
const settings = JSON.parse(localStorage.getItem('settings')) || {};

const gameDataArr = cardFarming.map(game => JSON.parse(game));
const gamesSet = new Set();
let totalDrops = 0;

const dropCheckPromises = gameDataArr.map(async (gameData) => {
try {
const dropsRemaining = await checkDrops(userSummary.steamId, gameData.game.id, steamCookies.sid, steamCookies.sls);
if (dropsRemaining > 0) {
logEvent(`[Card Farming] ${dropsRemaining} drops remaining for ${gameData.game.name} - starting`);
try {
if (settings.cardFarming.allGames) {
const gamesWithDrops = await getAllGamesWithDrops(userSummary.steamId, steamCookies.sid, steamCookies.sls);
for (const gameData of gamesWithDrops) {
if (gamesSet.size < 30) {
console.log(gameData);
gamesSet.add({ appId: gameData.game.id, appName: gameData.game.name });
totalDrops += dropsRemaining;
totalDrops += gameData.game.remaining;
logEvent(`[Card Farming] ${gameData.game.remaining} drops remaining for ${gameData.game.name} - starting`);
} else {
break;
}
} else {
logEvent(`[Card Farming] ${dropsRemaining} drops remaining for ${gameData.game.name} - removed from list`);
removeGameFromFarmingList(gameData.game.id);
}
} catch (error) {
logEvent(`[Error] [Card Farming] ${error}`);
} else {
const dropCheckPromises = gameDataArr.map(async (gameData) => {
if (gamesSet.size >= 30) return Promise.resolve();
const dropsRemaining = await checkDrops(userSummary.steamId, gameData.game.id, steamCookies.sid, steamCookies.sls);
if (dropsRemaining > 0) {
if (gamesSet.size < 30) {
gamesSet.add({ appId: gameData.game.id, appName: gameData.game.name });
totalDrops += dropsRemaining;
logEvent(`[Card Farming] ${dropsRemaining} drops remaining for ${gameData.game.name} - starting`);
}
} else {
logEvent(`[Card Farming] ${dropsRemaining} drops remaining for ${gameData.game.name} - removed from list`);
removeGameFromFarmingList(gameData.game.id);
}
});
await Promise.all(dropCheckPromises);
}
});

await Promise.all(dropCheckPromises);
} catch (error) {
logEvent(`[Error] [Card Farming] ${error}`);
}

return { totalDrops, gamesSet };
};
Expand Down Expand Up @@ -133,13 +146,17 @@ export default function CardFarming({ setActivePage }) {
localStorage.setItem('cardFarming', JSON.stringify(updatedCardFarming));
};

const handleCancel = () => {
for (const game of gamesWithDrops) {
stopIdler(game.appId);
}
isMountedRef.current = false;
abortControllerRef.current.abort();
const handleCancel = async () => {
setActivePage('games');
try {
const stopPromises = Array.from(gamesWithDrops).map(game => stopIdler(game.appId));
await Promise.all(stopPromises);
} catch (error) {
console.error('Error stopping games:', error);
} finally {
isMountedRef.current = false;
abortControllerRef.current.abort();
}
};

return (
Expand Down
21 changes: 11 additions & 10 deletions components/automation/StopButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ export default function StopButton({ setActivePage, isMountedRef, abortControlle
...Array.from({ length: 500 }, (_, i) => 6 - i * 0.01),
];

const handleStop = () => {
if (screen === 'card-farming') {
for (const game of gamesWithDrops) {
stopIdler(game.appId);
const handleStop = async () => {
setActivePage('games');
try {
if (screen === 'card-farming') {
const stopPromises = Array.from(gamesWithDrops).map(game => stopIdler(game.appId));
await Promise.all(stopPromises);
} else {
await stopIdler(currentGame.appId);
}
} catch (error) {
console.error('Error stopping games:', error);
} finally {
isMountedRef.current = false;
abortControllerRef.current.abort();
setActivePage('games');
} else {
isMountedRef.current = false;
abortControllerRef.current.abort();
setActivePage('games');
stopIdler(currentGame.appId);
}
};

Expand Down
2 changes: 1 addition & 1 deletion components/gameslist/Achievements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export default function Achievements({ steamId, appId, setShowAchievements }) {
</div>
</div>
</div>
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={3000} />
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={5000} />
</React.Fragment>
);
}
7 changes: 4 additions & 3 deletions components/gameslist/Automate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function Automate({ setActivePage }) {
const startCardFarming = async () => {
const steamRunning = await invoke('check_status');
const steamCookies = JSON.parse(localStorage.getItem('steamCookies')) || {};
const settings = JSON.parse(localStorage.getItem('settings')) || {};
if (!steamRunning) {
return toast.error('Steam is not running');
}
Expand All @@ -27,8 +28,8 @@ export default function Automate({ setActivePage }) {
return toast.error('Steam credentials need to be updated');
}
const cardFarming = JSON.parse(localStorage.getItem('cardFarming')) || [];
if (cardFarming.length < 1) {
return toast.error('No games in card farming list');
if (!settings.cardFarming.allGames && cardFarming.length < 1) {
return toast.error('Enable the "All games" setting or add some games to your card farming list');
}
setActivePage('card-farming');
};
Expand Down Expand Up @@ -89,7 +90,7 @@ export default function Automate({ setActivePage }) {
</DropdownItem>
</DropdownMenu>
</Dropdown>
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={3000} />
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={5000} />
</React.Fragment>
);
}
2 changes: 1 addition & 1 deletion components/gameslist/GameAchievementList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export default function GameAchievementList({ gameList, favorites, cardFarming,
</div>
);
})}
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={3000} />
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={5000} />
</div>
</React.Fragment>
);
Expand Down
3 changes: 1 addition & 2 deletions components/gameslist/GameIdleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default function GameIdleList({ gameList, favorites, cardFarming, achieve
setTimeout(() => {
let cardFarming = JSON.parse(localStorage.getItem('cardFarming')) || [];
cardFarming.push(JSON.stringify(item));
console.log(cardFarming.map(JSON.parse));
localStorage.setItem('cardFarming', JSON.stringify(cardFarming));
const newCardFarming = (localStorage.getItem('cardFarming') && JSON.parse(localStorage.getItem('cardFarming'))) || [];
setCardFarming(newCardFarming.map(JSON.parse));
Expand Down Expand Up @@ -158,7 +157,7 @@ export default function GameIdleList({ gameList, favorites, cardFarming, achieve
</div>
);
})}
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={3000} />
<ToastContainer toastStyle={{ fontSize: 12 }} position='bottom-right' theme='dark' transition={Slide} pauseOnFocusLoss={false} pauseOnHover={false} autoClose={5000} />
</div>
</React.Fragment>
);
Expand Down
2 changes: 0 additions & 2 deletions components/gameslist/GamesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ export default function GamesList({ steamId, inputValue, isQuery, activePage, se
showStats={showStats}
handleShowStats={handleShowStats}
filteredGames={filteredGames}
setFilteredGames={setFilteredGames}
visibleGames={visibleGames}
setVisibleGames={setVisibleGames}
/>
)}

Expand Down
65 changes: 0 additions & 65 deletions components/gameslist/GamesWithDrops.jsx

This file was deleted.

7 changes: 1 addition & 6 deletions components/gameslist/PageHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Button, Select, SelectItem, Tooltip } from '@nextui-org/react';
import { IoIosStats } from 'react-icons/io';
import { MdSort } from 'react-icons/md';
import Automate from './Automate';
import GamesWithDrops from './GamesWithDrops';

export default function PageHeader({ activePage, setActivePage, sortStyle, setSortStyle, showStats, handleShowStats, filteredGames, setFilteredGames, visibleGames, setVisibleGames }) {
export default function PageHeader({ activePage, setActivePage, sortStyle, setSortStyle, showStats, handleShowStats, filteredGames, visibleGames }) {
const sortOptions = [
{ key: 'a-z', label: 'Title Ascending' },
{ key: 'z-a', label: 'Title Descending' },
Expand Down Expand Up @@ -48,10 +47,6 @@ export default function PageHeader({ activePage, setActivePage, sortStyle, setSo
</div>

<div className='flex justify-end items-center gap-2'>
{sortStyle === 'cardFarming' && (
<GamesWithDrops setFilteredGames={setFilteredGames} setVisibleGames={setVisibleGames} />
)}

<div className='flex items-center gap-1'>
<Automate setActivePage={setActivePage} />
</div>
Expand Down
3 changes: 1 addition & 2 deletions components/settings/AchievementSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function AchievementSettings({ settings, setSettings }) {

<Slider
label={
<div className='flex justify-between items-center gap-1 min-w-[300px]'>
<div className='flex justify-between items-center gap-1 min-w-[350px]'>
<p className='text-xs'>
Unlock interval
</p>
Expand All @@ -92,7 +92,6 @@ export default function AchievementSettings({ settings, setSettings }) {
</p>
</div>
}
isDisabled={localSettings.achievementUnlocker.random}
size='sm'
step={5}
minValue={5}
Expand Down
Loading

0 comments on commit bbef4b0

Please sign in to comment.