Skip to content

Commit 78f1381

Browse files
committed
Add classic streams
1 parent ac7965b commit 78f1381

File tree

21 files changed

+399
-57
lines changed

21 files changed

+399
-57
lines changed

services/app/apps/codebattle/assets/js/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
renderEventPage,
4747
renderTournamentPlayerPage,
4848
renderUserPage,
49+
renderStreamPage,
4950
renderUsersRating,
5051
} from './widgets';
5152

@@ -94,6 +95,7 @@ const gameWidgetRoot = document.getElementById('game-widget-root');
9495
const heatmapRoot = document.getElementById('heatmap-root');
9596
const onlineRoot = document.getElementById('online-root');
9697
const invitesRoot = document.getElementById('invites-root');
98+
const streamRoot = document.getElementById('stream-classic-root');
9799
const lobbyRoot = document.getElementById('lobby-root');
98100
const ratingList = document.getElementById('rating-list');
99101
const registrationRoot = document.getElementById('registration');
@@ -164,3 +166,8 @@ if (onlineRoot) {
164166
if (invitesRoot) {
165167
renderInvitesWidget(invitesRoot);
166168
}
169+
170+
if (streamRoot) {
171+
renderStreamPage(streamRoot);
172+
}
173+

services/app/apps/codebattle/assets/js/socket.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export const channelTopics = {
6666
waitingRoomPlayerMatchmakingStoppedTopic: 'waiting_room:player:matchmaking_stopped',
6767
waitingRoomPlayerMatchmakingPausedTopic: 'waiting_room:player:matchmaking_paused',
6868
waitingRoomPlayerMatchCreatedTopic: 'waiting_room:player:match_created',
69+
70+
streamActiveGameSelectedTopic: 'stream:active_game_selected',
6971
};
7072

7173
export const channelMethods = {
@@ -104,6 +106,7 @@ export const channelMethods = {
104106
matchmakingPause: 'matchmaking:pause',
105107
matchmakingResume: 'matchmaking:resume',
106108
matchmakingRestart: 'matchmaking:restart',
109+
107110
};
108111

109112
socket.connect();

services/app/apps/codebattle/assets/js/widgets/App.jsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const UserProfile = React.lazy(() => import('./pages/profile'));
7171
const Registration = React.lazy(() => import('./pages/registration'));
7272
const Tournament = React.lazy(() => import('./pages/tournament'));
7373
const TournamentAdmin = React.lazy(() => import('./pages/tournament/TournamentAdminWidget'));
74-
const TournamentStream = React.lazy(() => import('./pages/tournament/TournamentAdminWidget'));
74+
const Stream = React.lazy(() => import('./pages/stream/StreamWidget'));
7575
const EventWidget = React.lazy(() => import('./pages/event'));
7676
const TournamentPlayer = React.lazy(() => import('./pages/tournamentPlayer'));
7777

@@ -232,3 +232,13 @@ export const TournamentPlayerPage = () => (
232232
</PersistGate>
233233
</Provider>
234234
);
235+
236+
export const StreamPage = () => (
237+
<Provider store={store}>
238+
<PersistGate loading={null} persistor={persistor}>
239+
<Suspense>
240+
<Stream />
241+
</Suspense>
242+
</PersistGate>
243+
</Provider>
244+
);

services/app/apps/codebattle/assets/js/widgets/index.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
TournamentPlayerPage,
1818
UserPage,
1919
UsersRating,
20+
StreamPage,
2021
} from './App';
2122

2223
const Heatmap = React.lazy(() => import('./pages/profile/Heatmap'));
@@ -36,3 +37,5 @@ export const renderEventPage = domElement => createRoot(domElement).render(<Even
3637
export const renderTournamentPlayerPage = domElement => createRoot(domElement).render(<TournamentPlayerPage />);
3738
export const renderUserPage = domElement => createRoot(domElement).render(<UserPage />);
3839
export const renderUsersRating = domElement => createRoot(domElement).render(<UsersRating />);
40+
export const renderStreamPage = domElement => createRoot(domElement).render(<StreamPage />);
41+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Gon from 'gon';
2+
3+
import { channelTopics } from '../../socket';
4+
import { actions } from '../slices';
5+
6+
import Channel from './Channel';
7+
8+
const tournamentId = Gon.getAsset('tournament_id');
9+
10+
const channel = new Channel();
11+
12+
const establishStream = dispatch => {
13+
const getDispatchActionHandler = actionCreator => data => dispatch(actionCreator(data));
14+
15+
channel.join().receive('ok', () => {});
16+
17+
const handleActiveGame = getDispatchActionHandler(actions.setGameId);
18+
19+
return channel
20+
.addListener(channelTopics.streamActiveGameSelectedTopic, handleActiveGame)
21+
};
22+
23+
export const connectToStream = () => dispatch => {
24+
const page = `stream:${tournamentId}`;
25+
channel.setupChannel(page);
26+
const currentChannel = establishStream(dispatch);
27+
28+
return currentChannel;
29+
};

services/app/apps/codebattle/assets/js/widgets/middlewares/TournamentAdmin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,9 @@ export const getTask = (taskId, onSuccess) => () => {
329329
onSuccess(data.descriptionRu);
330330
});
331331
};
332+
333+
export const pushActiveMatchToStream = (gameId) => () => {
334+
channel
335+
.push('tournament:stream:active_game', { gameId })
336+
.receive('error', error => console.error(error));
337+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { useEffect } from 'react';
2+
3+
import { useDispatch, useSelector } from 'react-redux';
4+
import { connectToStream} from '../../middlewares/Stream';
5+
6+
function StreamWidget() {
7+
const dispatch = useDispatch();
8+
const gameId = useSelector(state => state.game.id);
9+
10+
console.log(gameId)
11+
useEffect(() => {
12+
dispatch(connectToStream());
13+
}, []);
14+
15+
return <div>Stream Widget</div>;
16+
}
17+
18+
export default StreamWidget;
19+

services/app/apps/codebattle/assets/js/widgets/pages/tournament/TournamentAdminWidget.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Gon from 'gon';
55

66
import { connectToTournament, requestMatchesForRound} from '../../middlewares/TournamentAdmin';
77
import * as selectors from '../../selectors';
8+
import { pushActiveMatchToStream } from '../../middlewares/TournamentAdmin';
89

910
function TournamentAdminWidget() {
1011
const tournamentId = Gon.getAsset('tournament_id');
@@ -106,6 +107,7 @@ function TournamentAdminWidget() {
106107
return (
107108
<button
108109
key={match.id}
110+
onClick={() => dispatch(pushActiveMatchToStream(match.gameId))}
109111
className={`btn ${buttonClass} btn-sm me-1 mb-1`}
110112
title={`Match ID: ${match.id}, State: ${match.state}, Started: ${new Date(match.startedAt).toLocaleTimeString()}`}
111113
>

services/app/apps/codebattle/assets/js/widgets/slices/game.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const game = createSlice({
1111
name: 'game',
1212
initialState,
1313
reducers: {
14+
setGameId: (state, { payload: { id } }) => {
15+
state.id = id;
16+
},
1417
clearGameStatus: state => {
1518
state.gameStatus = defaultGameStatusState;
1619
},

services/app/apps/codebattle/assets/js/widgets/slices/initial.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ const defaultTournamentPlayerParams = {
499499
*/
500500
export default {
501501
game: {
502+
id: null,
502503
gameStatus: initialGameStatus,
503504
award: initialGameAward,
504505
awardStatus: 'idle',

0 commit comments

Comments
 (0)