Skip to content

Commit

Permalink
fix(calculator): rebase error μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
κΉ€μ„±μš± authored and 42sungwook committed Oct 13, 2023
1 parent b8e3975 commit 1d26113
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 57 deletions.
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;
};

0 comments on commit 1d26113

Please sign in to comment.