Skip to content

Commit eb5bbca

Browse files
author
Vitaly
authored
Refactor play core modules (hexlet-codebattle#638)
1 parent fa0d59e commit eb5bbca

File tree

100 files changed

+1884
-2983
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1884
-2983
lines changed

.github/workflows/tests_run_on_push.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ jobs:
109109
docker pull codebattle/php:7.3.0
110110
docker pull codebattle/clojure:1.10.0
111111
docker pull codebattle/haskell:8.4.3
112-
docker pull codebattle/perl:5.26.2
113112
114113
- name: Run code checkers tests
115114
run: make test-code-checkers

ansible/pull_dockers.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
- "codebattle/php:7.3.0"
1616
- "codebattle/haskell:8.4.3"
1717
- "codebattle/clojure:1.10.0"
18-
- "codebattle/perl:5.26.2"
1918

2019
- name: prune docker images
2120
shell: docker rmi -f $(docker images | grep "<none>" | awk '{print $3}')

services/app/assets/js/widgets/actions/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export const userLeftChat = createAction('CHAT_USER_LEFT');
66
export const newMessageChat = createAction('CHAT_NEW_MESSAGE');
77

88
export const initGameList = createAction('INIT_GAME_LIST');
9-
export const newGameLobby = createAction('LOBBY_NEW_GAME');
10-
export const updateGameLobby = createAction('LOBBY_UPDATE_GAME');
11-
export const cancelGameLobby = createAction('LOBBY_CANCEL_GAME');
9+
export const upsertGameLobby = createAction('LOBBY_UPSERT_GAME');
10+
export const removeGameLobby = createAction('LOBBY_REMOVE_GAME');
11+
export const finishGame = createAction('LOBBY_FINISH_GAME');
1212

1313
export const finishStoreInit = createAction('FINISH_STORE_INIT');
1414

@@ -23,6 +23,7 @@ export const sendPlayerCode = createAction('SEND_PLAYER_CODE');
2323
export const updateEditorLang = createAction('UPDATE_EDITOR_LANG');
2424
export const updateEditorText = createAction('UPDATE_EDITOR_TEXT');
2525
export const updateEditorTextPlaybook = createAction('UPDATE_EDITOR_TEXT_PLAYBOOK');
26+
export const setLangs = createAction('SET_LANGS');
2627

2728
export const updateGameStatus = createAction('UPDATE_GAME_STATUS');
2829
export const setGameTask = createAction('SET_GAME_TASK');

services/app/assets/js/widgets/components/ExecutionOutput.jsx

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,48 @@ class ExecutionOutput extends PureComponent {
1313
return <span className={`badge badge-${stautsColors[status]}`}>{status}</span>;
1414
};
1515

16-
renderTestResults = (resultObj, asserts) => {
16+
renderTestResults = (resultObj, assertsCount, successCount) => {
1717
switch (resultObj.status) {
1818
case '':
1919
return i18n.t('Run your code!');
2020
case 'error':
21-
return i18n.t('You have some syntax errors: %{errors}', { errors: resultObj.result, interpolation: { escapeValue: false } });
21+
return i18n.t('You have some syntax errors: %{errors}', {
22+
errors: resultObj.result,
23+
interpolation: { escapeValue: false },
24+
});
2225
case 'failure':
2326
if (Array.isArray(resultObj.result)) {
24-
return i18n.t('Test failed with arguments (%{arguments})%{assertsInfo}', { arguments: resultObj.arguments.map(JSON.stringify).join(', '), assertsInfo: this.getInfoAboutFailuresAsserts(asserts), interpolation: { escapeValue: false } });
27+
return i18n.t('Test failed with arguments (%{arguments})%{assertsInfo}', {
28+
arguments: resultObj.arguments.map(JSON.stringify).join(', '),
29+
assertsInfo: this.getInfoAboutFailuresAsserts(assertsCount, successCount),
30+
interpolation: { escapeValue: false },
31+
});
2532
}
26-
return i18n.t('Test failed with arguments (%{arguments})%{assertsInfo}', { arguments: JSON.stringify(resultObj.arguments), assertsInfo: this.getInfoAboutFailuresAsserts(asserts), interpolation: { escapeValue: false } });
33+
return i18n.t('Test failed with arguments (%{arguments})%{assertsInfo}', {
34+
arguments: JSON.stringify(resultObj.arguments),
35+
assertsInfo: this.getInfoAboutFailuresAsserts(assertsCount, successCount),
36+
interpolation: { escapeValue: false },
37+
});
2738
case 'ok':
2839
return i18n.t('Yay! All tests passed!!111');
2940
default:
3041
return i18n.t('Oops');
3142
}
3243
};
3344

34-
getInfoAboutFailuresAsserts = ({ assertsCount, successCount }) => {
45+
getInfoAboutFailuresAsserts = (assertsCount, successCount) => {
3546
switch (assertsCount) {
36-
case -1:
47+
case 0:
3748
return '';
3849
default: {
3950
const percent = (100 * successCount) / assertsCount;
40-
return i18n.t(', and you passed %{successCount} from %{assertsCount} asserts. (%{percent}%)', { percent, successCount, assertsCount });
51+
return i18n.t(
52+
', and you passed %{successCount} from %{assertsCount} asserts. (%{percent}%)',
53+
{ percent, successCount, assertsCount },
54+
);
4155
}
4256
}
43-
}
57+
};
4458

4559
parseOutput = result => {
4660
try {
@@ -56,14 +70,13 @@ class ExecutionOutput extends PureComponent {
5670
}
5771

5872
return false;
59-
}
73+
};
6074

6175
render() {
6276
const {
6377
output: {
64-
output, result, asserts = { assertsCount: -1, successCount: -1 },
65-
} = {},
66-
id,
78+
output, result, assertsCount, successCount,
79+
} = {}, id,
6780
} = this.props;
6881
const resultObj = this.parseOutput(result);
6982

@@ -73,7 +86,9 @@ class ExecutionOutput extends PureComponent {
7386
<ul className="nav nav-tabs card-title">
7487
<li>
7588
<a
76-
className={`btn btn-sm rounded border btn-light ${this.isError(resultObj) ? '' : 'active'}`}
89+
className={`btn btn-sm rounded border btn-light ${
90+
this.isError(resultObj) ? '' : 'active'
91+
}`}
7792
data-toggle="tab"
7893
href={`#asserts_${id}`}
7994
>
@@ -82,7 +97,9 @@ class ExecutionOutput extends PureComponent {
8297
</li>
8398
<li>
8499
<a
85-
className={`btn btn-sm rounded border btn-light ${this.isError(resultObj) ? 'active' : ''}`}
100+
className={`btn btn-sm rounded border btn-light ${
101+
this.isError(resultObj) ? 'active' : ''
102+
}`}
86103
data-toggle="tab"
87104
href={`#output_${id}`}
88105
>
@@ -97,15 +114,19 @@ class ExecutionOutput extends PureComponent {
97114
</div>
98115
</div>
99116
<p className="card-text mb-0">
100-
<code>{this.renderTestResults(resultObj, asserts)}</code>
117+
<code>{this.renderTestResults(resultObj, assertsCount, successCount)}</code>
101118
</p>
102119
<div className="tab-content">
103-
<div id={`asserts_${id}`} className={`tab-pane ${this.isError(resultObj) ? '' : 'active'}`}>
104-
<pre className="card-text d-none d-md-block mt-3">
105-
{result}
106-
</pre>
120+
<div
121+
id={`asserts_${id}`}
122+
className={`tab-pane ${this.isError(resultObj) ? '' : 'active'}`}
123+
>
124+
<pre className="card-text d-none d-md-block mt-3">{result}</pre>
107125
</div>
108-
<div id={`output_${id}`} className={`tab-pane ${this.isError(resultObj) ? 'active' : ''}`}>
126+
<div
127+
id={`output_${id}`}
128+
className={`tab-pane ${this.isError(resultObj) ? 'active' : ''}`}
129+
>
109130
<pre className="card-text d-none d-md-block mt-3">{output}</pre>
110131
</div>
111132
</div>

services/app/assets/js/widgets/components/LanguageIcon.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const iconsToClass = {
1212
clojure: 'icon-clojure',
1313
python: 'icon-python',
1414
php: 'icon-php-alt',
15-
perl: 'icon-perl',
1615
};
1716

1817
const LanguageIcon = ({ lang }) => <span className={cn('d-flex', iconsToClass[lang])} />;

services/app/assets/js/widgets/components/LanguagePicker.jsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from 'lodash';
44
import Gon from 'gon';
55
import LanguageIcon from './LanguageIcon';
66

7-
const languages = Gon.getAsset('langs');
7+
const defaultLanguages = Gon.getAsset('langs');
88

99
const LangTitle = ({ slug, name, version }) => (
1010
<div className="d-inline-flex align-items-center">
@@ -14,19 +14,15 @@ const LangTitle = ({ slug, name, version }) => (
1414
</div>
1515
);
1616

17-
const LanguagePicker = ({ currentLangSlug, onChange, disabled }) => {
18-
const [[currentLang], otherLangs] = _.partition(
19-
languages,
20-
lang => lang.slug === currentLangSlug,
21-
);
17+
const LanguagePicker = ({
18+
languages, currentLangSlug, onChange, disabled,
19+
}) => {
20+
const langs = languages || defaultLanguages;
21+
const [[currentLang], otherLangs] = _.partition(langs, lang => lang.slug === currentLangSlug);
2222

2323
if (disabled) {
2424
return (
25-
<button
26-
className="btn btn-sm"
27-
type="button"
28-
disabled
29-
>
25+
<button className="btn btn-sm" type="button" disabled>
3026
<LangTitle {...currentLang} />
3127
</button>
3228
);
@@ -50,7 +46,9 @@ const LanguagePicker = ({ currentLangSlug, onChange, disabled }) => {
5046
type="button"
5147
className="dropdown-item btn rounded-0"
5248
key={slug}
53-
onClick={() => { onChange(slug); }}
49+
onClick={() => {
50+
onChange(slug);
51+
}}
5452
>
5553
<LangTitle slug={slug} name={name} version={version} />
5654
</button>

services/app/assets/js/widgets/config/languages.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export default {
99
php: 'php',
1010
python: 'python',
1111
clojure: 'clojure',
12-
perl: 'perl',
1312
};

services/app/assets/js/widgets/containers/CodebattlePlayer.jsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { connect } from 'react-redux';
55
import { Direction } from 'react-player-controls/dist/constants';
66
import * as selectors from '../selectors';
77
import * as actions from '../actions';
8-
import { getText, getFinalState } from '../lib/player';
8+
import { getText, getFinalState, parse } from '../lib/player';
99
import CodebattleSliderBar from '../components/CodebattleSliderBar';
1010

1111
const isEqual = (float1, float2) => {
@@ -68,7 +68,7 @@ class CodebattlePlayer extends Component {
6868
this.stop();
6969
}
7070

71-
async onSliderHandleChange(value) {
71+
onSliderHandleChange(value) {
7272
this.setState({ value });
7373

7474
const { isHold, delaySetGameState } = this.state;
@@ -86,7 +86,7 @@ class CodebattlePlayer extends Component {
8686
}
8787
}
8888

89-
async onSliderHandleChangeStart() {
89+
onSliderHandleChangeStart() {
9090
this.setState({ isHold: true });
9191

9292
const { isStop } = this.state;
@@ -96,7 +96,7 @@ class CodebattlePlayer extends Component {
9696
}
9797
}
9898

99-
async onSliderHandleChangeEnd() {
99+
onSliderHandleChangeEnd() {
100100
this.setState({ isHold: false });
101101

102102
const { isHoldPlay } = this.state;
@@ -108,11 +108,11 @@ class CodebattlePlayer extends Component {
108108
}
109109
}
110110

111-
async onSliderHandleChangeIntent(intent) {
111+
onSliderHandleChangeIntent(intent) {
112112
this.setState(() => ({ lastIntent: intent }));
113113
}
114114

115-
async onSliderHandleChangeIntentEnd() {
115+
onSliderHandleChangeIntentEnd() {
116116
this.setState(() => ({ lastIntent: 0 }));
117117
}
118118

@@ -141,7 +141,6 @@ class CodebattlePlayer extends Component {
141141
chat: chatState,
142142
nextRecordId,
143143
} = getFinalState({ recordId: resultId, records, gameInitialState });
144-
145144
this.setState({ nextRecordId });
146145

147146
editorsState.forEach(player => {
@@ -152,9 +151,8 @@ class CodebattlePlayer extends Component {
152151
});
153152

154153
updateExecutionOutput({
154+
...player.checkResult,
155155
userId: player.id,
156-
result: player.result,
157-
output: player.output,
158156
});
159157
});
160158

@@ -170,42 +168,39 @@ class CodebattlePlayer extends Component {
170168
getEditorTextPlaybook,
171169
} = this.props;
172170
const { nextRecordId } = this.state;
173-
const nextRecord = records[nextRecordId] || {};
171+
const nextRecord = parse(records[nextRecordId]) || {};
174172

173+
let editorText;
174+
let newEditorText;
175175
switch (nextRecord.type) {
176-
case 'editor_text': {
177-
const editorText = getEditorTextPlaybook(nextRecord);
178-
const newEditorText = getText(editorText, nextRecord.diff);
176+
case 'update_editor_data':
177+
editorText = getEditorTextPlaybook(nextRecord);
178+
newEditorText = getText(editorText, nextRecord.diff);
179179
updateEditorTextPlaybook({
180180
userId: nextRecord.userId,
181181
editorText: newEditorText,
182-
langSlug: nextRecord.editorLang,
182+
langSlug: nextRecord.diff.nextLang,
183183
});
184184
break;
185-
}
186-
case 'result_check': {
185+
case 'check_complete':
187186
updateExecutionOutput({
187+
...nextRecord.checkResult,
188188
userId: nextRecord.userId,
189-
result: nextRecord.result,
190-
output: nextRecord.output,
191189
});
192190
break;
193-
}
194191
case 'chat_message':
195192
case 'join_chat':
196-
case 'leave_chat': {
193+
case 'leave_chat':
197194
fetchChatData(nextRecord.chat);
198195
break;
199-
}
200-
default: {
196+
default:
201197
break;
202-
}
203198
}
204199

205200
this.setState({ nextRecordId: nextRecordId + 1 });
206201
}
207202

208-
async play() {
203+
play() {
209204
const { value, speed } = this.state;
210205

211206
const run = () => {

0 commit comments

Comments
 (0)