-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.tsx
174 lines (159 loc) · 7.8 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {BrowserRouter as Router, Route, Switch} from "react-router-dom";
import {applyMiddleware, createStore, Store} from 'redux';
import {Action, IApplyConfig, IClickOnCiv, ISetName, ISetRole, IUpdateDrafts} from "./actions";
import Footer from "./components/menu/Footer";
import NavBar from "./components/menu/NavBar";
import Menu from "./components/menu/Menu";
import {ClientActions, ServerActions} from "./constants";
import Draft from './containers/Draft';
import './i18n';
import './sass/bulma.scss';
import '@creativebulma/bulma-tooltip/dist/bulma-tooltip.min.css';
import {IDraftConfig} from "./types/IDraftConfig";
import {SocketUtil} from "./util/SocketUtil";
import {default as updateState} from './reducers';
import {ApplicationState, IRecentDraft} from './types';
import {initialDraftState} from "./reducers/draft";
import {initialDraftCountdownState} from "./reducers/draftCountdown";
import {initialDraftOwnPropertiesState} from "./reducers/draftOwnProperties";
import {initialLanguageState} from "./reducers/language";
import {initialModalState} from "./reducers/modal";
import {initialPresetEditorState} from "./reducers/presetEditor";
import SpectateDraft from "./containers/SpectateDraft";
import {initialColorSchemeState} from "./reducers/colorScheme";
import {initialReplayState} from "./reducers/replay";
import {initialIconStyleState} from "./reducers/iconStyle";
import {initialRecentDraftsState} from './reducers/recentDrafts';
import {initialAdminState} from "./reducers/admin";
const createMySocketMiddleware = () => {
return (storeAPI: { dispatch: (action: Action) => void; getState: () => ApplicationState }) => {
let socket: SocketIOClient.Socket | null = null;
let lobbySocket: SocketIOClient.Socket | null = null; // These two sockets should be merged together
return (next: (arg0: any) => void) => (action: Action) => {
if (action.type === ClientActions.SPECTATE_DRAFTS) {
const { subscribeCount } = storeAPI.getState().recentDrafts;
console.log("SPECTATE_DRAFTS", lobbySocket, subscribeCount);
if (subscribeCount === 0) {
lobbySocket = SocketUtil.initLobbySocketIfFirstUse(lobbySocket, storeAPI) as SocketIOClient.Socket;
lobbySocket.emit('spectate_drafts', {}, (data: IRecentDraft[]) => {
console.log('spectate_drafts callback', data);
storeAPI.dispatch({type: ServerActions.UPDATE_DRAFTS, value: data} as IUpdateDrafts);
});
}
}
if (action.type === ClientActions.UNSPECTATE_DRAFTS) {
const { subscribeCount } = storeAPI.getState().recentDrafts;
console.log('UNSPECTATE_DRAFTS', lobbySocket, subscribeCount);
if (subscribeCount === 1) {
if (lobbySocket !== null && lobbySocket.connected) {
SocketUtil.disconnect(lobbySocket);
}
lobbySocket = null;
}
}
if (action.type === ClientActions.CONNECT_TO_SERVER) {
console.log("CONNECT", SocketUtil.initSocketIfFirstUse, socket, storeAPI);
if (socket !== null && socket.connected) {
SocketUtil.disconnect(socket);
}
socket = null;
socket = SocketUtil.initSocketIfFirstUse(socket, storeAPI) as SocketIOClient.Socket;
}
if (action.type === ClientActions.SEND_SET_ROLE) {
console.log("SET_ROLE", SocketUtil.initSocketIfFirstUse, socket, storeAPI);
socket = SocketUtil.initSocketIfFirstUse(socket, storeAPI) as SocketIOClient.Socket;
if (socket.disconnected) {
return;
}
const setRole = action as ISetRole;
socket.emit('set_role', {name: setRole.name, role: setRole.role}, (data: IDraftConfig) => {
console.log('setRole callback', data);
storeAPI.dispatch({type: ServerActions.APPLY_CONFIG, value: data} as IApplyConfig);
});
return;
}
if (action.type === ClientActions.SEND_SET_NAME) {
console.log("SEND_SET_NAME", SocketUtil.initSocketIfFirstUse, socket, storeAPI);
socket = SocketUtil.initSocketIfFirstUse(socket, storeAPI) as SocketIOClient.Socket;
if (socket.disconnected) {
return;
}
const setName = action as ISetName;
socket.emit('set_name', {name: setName.name}, () => {
});
return;
}
if (action.type === ClientActions.SEND_READY) {
socket = SocketUtil.initSocketIfFirstUse(socket, storeAPI) as SocketIOClient.Socket;
if (socket.disconnected) {
return;
}
socket.emit('ready', {}, (data: IDraftConfig) => {
console.log('ready callback', data);
storeAPI.dispatch({type: ServerActions.APPLY_CONFIG, value: data} as IApplyConfig);
});
return;
}
if (action.type === ClientActions.SEND_CLICK_PANEL) {
socket = SocketUtil.initSocketIfFirstUse(socket, storeAPI) as SocketIOClient.Socket;
if (socket.disconnected) {
return;
}
const clickCivilisation = action as IClickOnCiv;
socket.emit('act', clickCivilisation.playerEvent, clickCivilisation.callback);
}
if (action.type === ClientActions.DISCONNECT_FROM_SERVER) {
console.log('DISCONNECT');
if (socket !== null && socket.connected) {
SocketUtil.disconnect(socket);
}
socket = null;
}
return next(action);
}
}
};
const store: Store = createStore<ApplicationState, Action, any, Store>(updateState,
{
iconStyle: initialIconStyleState,
replay: initialReplayState,
draft: initialDraftState,
countdown: initialDraftCountdownState,
ownProperties: initialDraftOwnPropertiesState,
language: initialLanguageState,
colorScheme: initialColorSchemeState,
modal: initialModalState,
presetEditor: initialPresetEditorState,
recentDrafts: initialRecentDraftsState,
admin: initialAdminState,
},
applyMiddleware(createMySocketMiddleware()));
console.log(store.getState());
ReactDOM.render(
<Provider store={store}>
<Router>
<NavBar/>
<Switch>
<Route exact path="/" component={Menu}/>
<Route path="/draft/:id" component={Draft}/>
<Route path="/presets" component={Menu}/>
<Route path="/preset/create" component={Menu}/>
<Route path="/preset/:id" component={Menu}/>
<Route path="/preset/:id/new" component={Menu}/>
<Route path="/spectate/:id" component={SpectateDraft}/>
<Route path="/spectate" component={Menu}/>
<Route path="/practice" component={Menu}/>
<Route path="/api" component={Menu}/>
<Route path="/admin/login" component={Menu}/>
<Route path="/admin/draft/:draftName" component={Menu}/>
<Route path="/admin" component={Menu}/>
<Route component={Menu}/>
</Switch>
<Footer/>
</Router>
</Provider>,
document.getElementById('root') as HTMLElement
);
// registerServiceWorker();