This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New UI] Unity Build in React (#616)
* Add react-unity-webgl package as a dependency * Introduce new unity module to the component * Update the README * Move GameView to be a container * Add some extra functionality local to the file for now * Add utilities and a API with JSON settings. Resolves #612 * More functionality to connect two systems * More Redux changes for success & connection * Refactor method name of the view to not include 'view' * Add initial state & send information across to Unity WebGL * Add socketIO dependency to generated HTML * Call Unity functions as redux actions now * Remove irrelevant comments now * Serialise boolean SSL flag for Unity * More changes to how communication is conducted * Handle errors * Add lunch json for VS code to git ignore * Emit success event in the epic * Remove middlewares * New feature introduced for Game * Remove old game reducer and move to its own. Resolves #619 Same as below * Separate epics for each event refactoring * Remove older epic * Update .gitignore launch json path Ignore all .vscode files Update .gitignore * Remove a TODO comment * Fix existing tests to suit new code * Merge branch 'UI_new_design' into add_unity_to_react * Regenerate yarn.lock * Revert the refactoring, can be done in a different PR * Add epic tests for the happy path * Remove unwanted comment and add EOF lines * Change reducer name, forgot to change beforehand * Add reducer test for all current cases * More function tests * Some testing changes as requested in PR by @mrniket * Change imports to one liner using aliases * Remove redundant alias for default import * Refactoring of setGameURL successful, rest not done * Refactoring of setGameURL fixed * Finish refactoring * Merge remote-tracking branch 'origin/UI_new_design' into add_unity_to_react * GameView minor esLint changes * Merge remote-tracking branch 'origin/UI_new_design' into add_unity_to_react * Fix indentation and other eslint issues in unity * Certain eslint fixes * More eslint * Fix minor SSL bug * Add extra developer settings to the parcel bundler * Initial connectionParams naming changes More naming changes More naming changes again connectionParams naming change done * Name change in the view to parameters * utilities called game_helpers now * Add another shallow theme test to the layout * Test passing, still unhappy with marble completion * Use action creators instead of explicit objects * Add css to Game Page testing * Change the djangoBundler check environment before setting vars * Update tests without theme to use shallow instead * Export unity as a object containing all the calls instead * Bring id of current avatar view to be called game_id again * Add naming change to game_helpers as requested * Fix tests to be correct now * Make aimmo compatible with Django 1.9 (#642) * Make aimmo compatible with Django 1.9 * Using patch version of Django 1.9 (#643) * Using patch version of Django 1.9 * Merge remote-tracking branch 'origin/master' into add_unity_to_react * Simply the API for api.js, put error in payload of fail actions * Add test for emit unity failure
- Loading branch information
1 parent
61e080a
commit dd2df80
Showing
36 changed files
with
985 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,3 +89,6 @@ node_modules | |
|
||
# Vagrant | ||
.vagrant/** | ||
|
||
# VSCode | ||
game_frontend/\.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
/* eslint-env jest */ | ||
import React from 'react' | ||
import GamePage, { GamePageLayout } from 'components/GamePage' | ||
import createShallowWithTheme from 'testHelpers/createShallow' | ||
import { shallow } from 'enzyme' | ||
import GamePage from 'components/GamePage' | ||
|
||
describe('<GamePage />', () => { | ||
it('matches snapshot', () => { | ||
const component = shallow(<GamePage />) | ||
const tree = shallow(<GamePage />) | ||
|
||
expect(component).toMatchSnapshot() | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe('<GamePageLayout />', () => { | ||
it('matches snapshot', () => { | ||
const tree = createShallowWithTheme(<GamePageLayout />) | ||
|
||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
12 changes: 0 additions & 12 deletions
12
game_frontend/src/components/GameView/__snapshots__/index.test.js.snap
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
game_frontend/src/containers/GameView/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<GameView /> matches snapshot 1`] = ` | ||
<styled.div> | ||
<Unity | ||
loader="/static/unity/Build/UnityLoader.js" | ||
src="/static/unity/Build/unity.json" | ||
/> | ||
</styled.div> | ||
`; | ||
|
||
exports[`<GameViewLayout /> matches snapshot 1`] = ` | ||
.c0 { | ||
grid-area: game-view; | ||
} | ||
<div | ||
className="c0" | ||
/> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import styled from 'styled-components' | ||
import React, { Component } from 'react' | ||
import Unity, { RegisterExternalListener } from 'react-unity-webgl' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
import { actions } from 'features/Game' | ||
|
||
export const GameViewLayout = styled.div` | ||
grid-area: game-view | ||
` | ||
|
||
export class GameView extends Component { | ||
constructor (props) { | ||
super(props) | ||
|
||
this.props.getConnectionParameters() | ||
|
||
RegisterExternalListener('SendAllConnect', this.sendAllConnect.bind(this)) | ||
} | ||
|
||
sendAllConnect () { | ||
this.props.setGameURL(this.props.gameURL) | ||
this.props.setGamePath(this.props.gamePath) | ||
this.props.setGamePort(this.props.gamePort) | ||
this.props.setGameSSL(this.serialisedSSLFlag()) | ||
this.props.establishGameConnection() | ||
} | ||
|
||
serialisedSSLFlag () { | ||
let boolString = this.props.gameSSL.toString() | ||
|
||
return boolString.charAt(0).toUpperCase() + boolString.slice(1) | ||
} | ||
|
||
render () { | ||
return ( | ||
<GameViewLayout> | ||
<Unity | ||
src='/static/unity/Build/unity.json' | ||
loader='/static/unity/Build/UnityLoader.js' | ||
/> | ||
</GameViewLayout> | ||
) | ||
} | ||
} | ||
|
||
GameView.propTypes = { | ||
gameURL: PropTypes.string, | ||
gamePath: PropTypes.string, | ||
gamePort: PropTypes.number, | ||
gameSSL: PropTypes.bool, | ||
getConnectionParameters: PropTypes.func, | ||
setGameURL: PropTypes.func, | ||
setGamePath: PropTypes.func, | ||
setGamePort: PropTypes.func, | ||
setGameSSL: PropTypes.func, | ||
establishGameConnection: PropTypes.func | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
gameURL: state.game.connectionParameters.game_url_base, | ||
gamePath: state.game.connectionParameters.game_url_path, | ||
gamePort: state.game.connectionParameters.game_url_port, | ||
gameSSL: state.game.connectionParameters.game_ssl_flag | ||
}) | ||
|
||
const mapDispatchToProps = { | ||
getConnectionParameters: actions.getConnectionParametersRequest, | ||
setGameURL: actions.setGameURL, | ||
setGamePath: actions.setGamePath, | ||
setGamePort: actions.setGamePort, | ||
setGameSSL: actions.setGameSSL, | ||
establishGameConnection: actions.establishGameConnection | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(GameView) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-env jest */ | ||
import React from 'react' | ||
import { GameView, GameViewLayout } from 'containers/GameView' | ||
import { shallow } from 'enzyme/build/index' | ||
|
||
describe('<GameView />', () => { | ||
it('matches snapshot', () => { | ||
const props = { | ||
gameURL: 'test', | ||
gamePath: '/test', | ||
gamePort: 8000, | ||
gameSSL: false, | ||
getConnectionParameters: jest.fn(), | ||
setGameURL: jest.fn(), | ||
setGamePath: jest.fn(), | ||
setGamePort: jest.fn(), | ||
setGameSSL: jest.fn(), | ||
establishGameConnection: jest.fn() | ||
} | ||
|
||
const component = shallow(<GameView {...props} />) | ||
expect(component).toMatchSnapshot() | ||
}) | ||
|
||
it('serialisedSSLFlag function returns correctly', () => { | ||
const props = { | ||
getConnectionParameters: jest.fn(), | ||
gameSSL: false | ||
} | ||
|
||
const flagReturned = shallow(<GameView {...props} />).instance().serialisedSSLFlag() | ||
expect(flagReturned).toBe('False') | ||
}) | ||
|
||
it('sendAllConnect function calls all action dispatchers', () => { | ||
const setGameURL = jest.fn() | ||
const setGamePath = jest.fn() | ||
const setGamePort = jest.fn() | ||
const setGameSSL = jest.fn() | ||
const establishGameConnection = jest.fn() | ||
|
||
const props = { | ||
gameURL: 'test', | ||
gamePath: '/test', | ||
gamePort: 8000, | ||
gameSSL: false, | ||
getConnectionParameters: jest.fn(), | ||
setGameURL, | ||
setGamePath, | ||
setGamePort, | ||
setGameSSL, | ||
establishGameConnection | ||
} | ||
|
||
const wrapper = shallow(<GameView {...props} />) | ||
|
||
wrapper.instance().sendAllConnect() | ||
|
||
expect(setGameURL.mock.calls.length).toBe(1) | ||
expect(setGamePath.mock.calls.length).toBe(1) | ||
expect(setGamePort.mock.calls.length).toBe(1) | ||
expect(setGameSSL.mock.calls.length).toBe(1) | ||
expect(establishGameConnection.mock.calls.length).toBe(1) | ||
}) | ||
}) | ||
|
||
describe('<GameViewLayout />', () => { | ||
it('matches snapshot', () => { | ||
const tree = shallow(<GameViewLayout />) | ||
expect(tree).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import post from './post' | ||
import get from './get' | ||
import unity from './unity' | ||
|
||
export default { get, post } | ||
export default { | ||
get, | ||
post, | ||
unity | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { UnityEvent } from 'react-unity-webgl' | ||
import { Observable } from 'rxjs' | ||
import { map, catchError } from 'rxjs/operators' | ||
|
||
const sendExternalEvent = communicator => action$ => | ||
action$.mergeMap(action => | ||
Observable.of(action).pipe( | ||
communicator, | ||
map(event => action.payload.successAction), | ||
catchError(error => Observable.of(action.payload.failAction(error))) | ||
) | ||
) | ||
|
||
const emitToUnity = action$ => | ||
action$.map( | ||
action => { | ||
let unityEvent = new UnityEvent('World Controller', action.payload.unityEvent) | ||
|
||
if (unityEvent.canEmit()) { | ||
unityEvent.emit(action.payload.unityData) | ||
} else { | ||
throw new Error('Cannot emit the function!') | ||
} | ||
} | ||
) | ||
|
||
export default { | ||
sendExternalEvent, | ||
emitToUnity | ||
} |
Oops, something went wrong.