Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Calculator rebase error 수정 #315

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/Calculator/atoms/subjectListAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const emptySubject = (index: number) => {
return {
id: index,
name: '',
exp: null,
expEdited: null,
exp: 0,
expEdited: 0,
score: 100,
blackhole: 0,
bonus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
subjectListAtomInitialValue,
} from '@/Calculator/atoms/subjectListAtom';
import { useSubjectList } from '@/Calculator/hooks/useSubjectList';
import { currentOpenSpotlightIndexAtom } from '@/Calculator/atoms/currentOpenSpotlightIndexAtom';
import { calculatorDialogAtom } from '@core/atoms/calculatorDialogAtom';
import styled from '@emotion/styled';
import { Button, CaptionText, H2BoldText, HStack } from '@shared/ui-kit';
Expand All @@ -16,6 +17,9 @@ export const CalculatorInputHeader = () => {
const theme = useTheme();
const subjectList = useAtomValue(subjectListAtom);
const setCalculatorDialog = useSetAtom(calculatorDialogAtom);
const setCurrentOpenSpotlightIndex = useSetAtom(
currentOpenSpotlightIndexAtom,
);
const { updateSubjectList } = useSubjectList();

const handleResetButtonClick = () => {
Expand All @@ -32,6 +36,13 @@ export const CalculatorInputHeader = () => {
return;
}

const firstEmptyIndex = subjectList.findIndex(
(subject) => subject.name === '',
);

setCurrentOpenSpotlightIndex(
firstEmptyIndex === -1 ? subjectList.length : firstEmptyIndex,
);
updateSubjectList([...subjectList, emptySubject(subjectList.length)]);
};

Expand Down
2 changes: 1 addition & 1 deletion app/src/Calculator/components/ProjectSpotlight/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const ProjectSpotlight = ({
spotlightWidth = '100%',
}: ProjectSpotlightProps) => {
const [search, searchResult] = useLazyQuery(GET_PROJECTS);
const LIMIT = 4;
const LIMIT = 5;
const [currentOpenSpotlightIndex, setCurrentOpenSpotlightIndex] = useAtom(
currentOpenSpotlightIndexAtom,
);
Expand Down
131 changes: 77 additions & 54 deletions app/src/Calculator/hooks/useSubjectList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,100 @@ export const useSubjectList = () => {
calculatorUserInfoAtom,
);

// FIXME: 로직 가독성
const updateSubjectList = useCallback(
(subjectList: Subject[]) => {
let newStartLevel = -1;
let sum = daysFromStart + currentBlackhole;
let finalLevel = currentLevel;

const updatedList = subjectList.map((subject) => {
const updatedList = subjectList.map((subject, index) => {
const { score, bonus, exp } = subject;

if (exp === null) {
return subject;
}

/**
* FIXME:
* [{
* id: 0,
* name: '',
* exp: 0,
* expEdited: 0,
* score: 100,
* blackhole: 0,
* bonus: false,
* startLevel: 0,
* finishLevel: 0,
* }]
*
* 위 입력일 때 NaN 발생.
*/

if (newStartLevel === -1) newStartLevel = currentLevel;
const editStartLevel = newStartLevel;
const decimalLevel = Math.floor(newStartLevel);
const currentExp = Math.floor(
expMaxTable[decimalLevel] +
expReqTable[decimalLevel + 1] * (newStartLevel - decimalLevel),
);
const newExp = bonus
? Math.floor(((exp * score) / 100) * 1.042)
: Math.floor((exp * score) / 100);
const calculateSubjectExp = (
exp: number,
score: number,
bonus: boolean,
) => {
const expWithScore = exp * score;
const expWithBonus = bonus
? Math.floor((expWithScore / 100) * 1.042)
: Math.floor(expWithScore / 100);

if (isNaN(expWithBonus)) return 0;
return expWithBonus;
};

const calculateSubjectLevel = (newCurrentExp: number) => {
const newDecimalLevel = expMaxTable.findIndex(
(exp) => exp > newCurrentExp,
);

//레벨 소숫점 2자리까지 Math.round로 계산
const newLevel =
Math.round(
(newDecimalLevel +
(newCurrentExp - expMaxTable[newDecimalLevel]) /
expReqTable[newDecimalLevel]) *
100,
) / 100;

if (isNaN(newLevel)) return 0;
return newLevel;
};

const calculateCurrentExp = (newStartLevel: number) => {
const decimalLevel = Math.floor(newStartLevel);
const currentTotalExp = Math.floor(
expMaxTable[decimalLevel] +
expReqTable[decimalLevel + 1] * (newStartLevel - decimalLevel),
);

if (isNaN(currentTotalExp)) return 0;
return currentTotalExp;
};

const calculateBlackhole = (
startExp: number,
endExp: number,
sum: number,
) => {
if (startExp >= MAX_EXP_VALUE) return 0;
if (endExp >= MAX_EXP_VALUE) endExp = MAX_EXP_VALUE;

//계산식 참조: https://medium.com/@benjaminmerchin/42-black-hole-deep-dive-cbc4b343c6b2
const blackhole = Math.floor(
((endExp / 49980) ** 0.45 - (startExp / 49980) ** 0.45) * 483,
);

//받을 수 있는 총 블랙홀 수를 넘는 경우
if (blackhole + sum > MAX_BLACKHOLE_DAYS) {
const newBlackhole = Math.max(MAX_BLACKHOLE_DAYS - sum, 0);

if (isNaN(newBlackhole)) return 0;
return newBlackhole;
}

if (isNaN(blackhole)) return 0;
return blackhole;
};

const newStartLevel = finalLevel;
const newExp = calculateSubjectExp(exp, score, bonus);
const currentExp = calculateCurrentExp(newStartLevel);
const newCurrentExp = currentExp + newExp;
const newDecimalLevel = expMaxTable.findIndex(
(exp) => exp > newCurrentExp,
);
const newLevel =
Math.round(
(newDecimalLevel +
(newCurrentExp - expMaxTable[newDecimalLevel]) /
expReqTable[newDecimalLevel]) *
100,
) / 100;
const newLevel = calculateSubjectLevel(newCurrentExp);
const newBlackhole = calculateBlackhole(currentExp, newCurrentExp, sum);
sum += newBlackhole;
newStartLevel = newLevel;
finalLevel = newLevel;

return {
...subject,
id: index,
expEdited: newExp,
startLevel: editStartLevel,
startLevel: newStartLevel,
finishLevel: newLevel,
blackhole: newBlackhole,
};
Expand All @@ -92,15 +127,3 @@ export const useSubjectList = () => {

return { updateSubjectList };
};

const calculateBlackhole = (startExp: number, endExp: number, sum: number) => {
if (startExp >= MAX_EXP_VALUE) return 0;
if (endExp >= MAX_EXP_VALUE) endExp = MAX_EXP_VALUE;
const blackhole = Math.floor(
((endExp / 49980) ** 0.45 - (startExp / 49980) ** 0.45) * 483,
);
if (blackhole + sum > MAX_BLACKHOLE_DAYS) {
return Math.max(MAX_BLACKHOLE_DAYS - sum, 0);
}
return blackhole;
};