summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/lobby.js
diff options
context:
space:
mode:
authorVictor Chabbert <chabbertvi@eisti.eu>2017-01-20 23:18:57 +0100
committerVictor Chabbert <chabbertvi@eisti.eu>2017-01-20 23:25:26 +0100
commitcc3db2982dc22899a9ebea21e8a8dfbfd710f842 (patch)
treed8e138b300a8d8fded0651ac6be204d8c3342e68 /frontend/src/sagas/lobby.js
parentRemove shitty code and move generator stars to the right (diff)
downloadseven-wonders-cc3db2982dc22899a9ebea21e8a8dfbfd710f842.tar.gz
seven-wonders-cc3db2982dc22899a9ebea21e8a8dfbfd710f842.tar.bz2
seven-wonders-cc3db2982dc22899a9ebea21e8a8dfbfd710f842.zip
DUCKS! Refactor front architecture
Diffstat (limited to 'frontend/src/sagas/lobby.js')
-rw-r--r--frontend/src/sagas/lobby.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/frontend/src/sagas/lobby.js b/frontend/src/sagas/lobby.js
new file mode 100644
index 00000000..144d523a
--- /dev/null
+++ b/frontend/src/sagas/lobby.js
@@ -0,0 +1,74 @@
+import { call, put, take } from 'redux-saga/effects'
+import { eventChannel } from 'redux-saga'
+import { fromJS } from 'immutable'
+import { push } from 'react-router-redux'
+
+import { actions, types } from '../../redux/game'
+
+function createSocketChannel(socket) {
+ return eventChannel(emit => {
+ const makeHandler = (type) => (event) => {
+ const response = fromJS(JSON.parse(event.body))
+
+ emit({
+ type,
+ response
+ })
+ }
+
+ const newGameHandler = makeHandler(types.NEW_GAME)
+ const joinGameHandler = makeHandler(types.JOIN_GAME)
+
+ const newGame = socket.subscribe('/topic/games', newGameHandler)
+ const joinGame = socket.subscribe('/user/queue/join-game', joinGameHandler)
+
+ const unsubscribe = () => {
+ newGame.unsubscribe()
+ joinGame.unsubscribe()
+ }
+
+ return unsubscribe
+ })
+}
+
+export function* watchGames(socketConnection) {
+
+ const { socket } = socketConnection
+ const socketChannel = createSocketChannel(socket)
+
+ while (true) {
+ const { type, response } = yield take(socketChannel)
+
+ switch (type) {
+ case types.NEW_GAME:
+ yield put(actions.newGame(response))
+ break;
+ case types.JOIN_GAME:
+ yield put(actions.joinGame(response))
+ break;
+ default:
+ console.error('Unknown type')
+ }
+ }
+}
+
+export function* createGame(socketConnection) {
+ const { name } = yield take(types.CREATE_GAME)
+ const { socket } = socketConnection
+
+ socket.send("/app/lobby/create-game", JSON.stringify({
+ 'gameName': name,
+ 'playerName': 'Cesar92'
+ }), {})
+}
+
+export function* gameBrowserSaga(socketConnection) {
+ yield put(push('/lobby'))
+
+ yield [
+ call(watchGames, socketConnection),
+ call(createGame, socketConnection)
+ ]
+}
+
+export default gameBrowserSaga
bgstack15