Skip to content

Commit 1ccfa39

Browse files
authored
Merge pull request #30 from Seol-JY/feature/imporve-calc-speed
feat: 공백 및 개행 오타에 패널티 부과
2 parents 2205e48 + c3ee50e commit 1ccfa39

File tree

8 files changed

+59
-8
lines changed

8 files changed

+59
-8
lines changed

client/src/components/Debug.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function Debug({
77
filestate,
88
Correctchr,
99
Wrongchr,
10+
Spacechr,
1011
fileLength,
1112
setFinishTrigger,
1213
}) {
@@ -43,7 +44,7 @@ function Debug({
4344
useInterval(() => {
4445
// useInterval custom Hook
4546
setCount(count + 0.05); //0.05씩 UPDATE
46-
setCpm(String(speed(count, Correctchr)).padStart(4, "0"));
47+
setCpm(String(speed(count, Correctchr, Spacechr)).padStart(4, "0"));
4748
}, terval); // Hook 실행조건, terval은 밀리초단위
4849

4950
return (
@@ -62,6 +63,7 @@ const mapStateToProps = (state) => {
6263
return {
6364
Correctchr: state.correct.Correctchr,
6465
Wrongchr: state.wrong.Wrongchr,
66+
Spacechr: state.space.Spacechr,
6567
};
6668
};
6769

client/src/components/Editor.jsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export default function Editor({
4646

4747
const userInputTabHandler = (event) => {
4848
// todo: 조건식 최적화
49-
//tab을 공백4칸으로
5049
if (event.key === "Escape") {
5150
setUseAutoComplete(false);
5251
} else if (
@@ -93,12 +92,20 @@ export default function Editor({
9392
}
9493
};
9594

95+
// const containsNonEnglish = (text) => /[^\x00-\x7F]/.test(text);
96+
9697
const userInputHandler = (event) => {
9798
//input창 내용을 userinput에 반영
99+
const newValue = event.currentTarget.value;
100+
// if (containsNonEnglish(newValue)) {
101+
// alert("영문만 입력 가능합니다.");
102+
// return;
103+
// }
104+
98105
const previousValue = userInput; // 직전 값 저장
99-
setUserInput(event.currentTarget.value); // replace(/[ㄱ-ㅎ|ㅏ-ㅣ|가-힣]/g,'')
100-
if (event.currentTarget.value.length - previousValue.length > 2) {
101-
alert("Careful. This might not do what you think, 잘못된 접근입니다.");
106+
setUserInput(newValue);
107+
if (newValue.length - previousValue.length > 2) {
108+
alert("잘못된 접근입니다.");
102109
window.location.reload();
103110
}
104111
};

client/src/components/Text.jsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
22
import { connect } from "react-redux";
33
import { setCorrectchr } from "../redux/correct/actions";
44
import { setWrongchr } from "../redux/wrong/actions";
5+
import { setSpacechr } from "../redux/space/actions";
56
import getFilecontents from "../utils/filecontents";
67
import AutoCompletion from "./AutoCompletion";
78

@@ -10,7 +11,8 @@ function Text(props) {
1011
const [textSplit, setTextSplit] = useState([]);
1112
const [themeColor, setThemeColor] = useState("");
1213
let wrong = 0,
13-
correct = 0;
14+
correct = 0,
15+
space = 0;
1416
const user = props.userInput;
1517
const userSplit = user.split("");
1618

@@ -32,6 +34,7 @@ function Text(props) {
3234
useEffect(() => {
3335
props.setCorrectchr(correct);
3436
props.setWrongchr(wrong);
37+
props.setSpacechr(space);
3538
// eslint-disable-next-line react-hooks/exhaustive-deps
3639
});
3740

@@ -56,6 +59,10 @@ function Text(props) {
5659
color = "#ff5c5c";
5760
colortxt = "white";
5861
wrong++;
62+
if (userSplit[i] === " " || userSplit[i] === "\n") {
63+
// 공백이나 개행으로 틀린 경우 속도 패널티 부여
64+
space++;
65+
}
5966
}
6067
}
6168

@@ -111,13 +118,15 @@ const mapStateToProps = (state) => {
111118
return {
112119
Correctchr: state.correct.Correctchr,
113120
Wrongchr: state.wrong.Wrongchr,
121+
Spacechr: state.space.Spacechr,
114122
};
115123
};
116124

117125
const mapDispatchToProps = (dispatch) => {
118126
return {
119127
setCorrectchr: (cor) => dispatch(setCorrectchr(cor)),
120128
setWrongchr: (wr) => dispatch(setWrongchr(wr)),
129+
setSpacechr: (sp) => dispatch(setSpacechr(sp)),
121130
};
122131
};
123132

client/src/redux/space/actions.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { SET_SPACECHR } from "./types";
2+
export const setSpacechr = (val) => {
3+
return {
4+
type: SET_SPACECHR,
5+
value: val,
6+
};
7+
};

client/src/redux/space/reducer.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { SET_SPACECHR } from "./types";
2+
3+
const setSpacechrState = {
4+
Spacechr: 10,
5+
};
6+
7+
const setSpacechrReducer = (state = setSpacechrState, action) => {
8+
switch (action.type) {
9+
case SET_SPACECHR:
10+
return {
11+
...state,
12+
Spacechr: action.value,
13+
};
14+
default:
15+
return state;
16+
}
17+
};
18+
19+
export default setSpacechrReducer;

client/src/redux/space/types.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SET_SPACECHR = "SET_SPACECHR";

client/src/redux/store.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { combineReducers } from "redux";
33
import { legacy_createStore as createStore } from "redux";
44
import setCorrectchrReducer from "./correct/reducer";
55
import setWrongchrReducer from "./wrong/reducer";
6+
import setSpacechrReducer from "./space/reducer";
67

78
const rootReducer = combineReducers({
89
correct: setCorrectchrReducer,
910
wrong: setWrongchrReducer,
11+
space: setSpacechrReducer,
1012
});
1113

1214
const store = createStore(rootReducer);

client/src/utils/speed.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
export default function speed(count, Correctchr) {
1+
export default function speed(count, Correctchr, Spacechr) {
22
// 타자속도 지정함수
3-
let sp = Math.floor((60 / count) * Correctchr);
3+
let sp = Math.floor((60 / count) * (Correctchr - Spacechr));
44
if (sp === Infinity || isNaN(sp)) {
55
return 0;
66
}
7+
8+
if (sp < 0) {
9+
sp = 0;
10+
}
711
return sp;
812
}

0 commit comments

Comments
 (0)