11import * as React from 'react'
22
3- import story from './game.js'
3+ import { setUser } from '../../../actions'
4+ import { store } from '../../../createStore'
5+ import { Story } from '../../../reducers/states'
6+ import { getUser } from '../../../sagas/backend'
47
58type GameProps = DispatchProps & StateProps
69
@@ -10,45 +13,67 @@ export type DispatchProps = {
1013
1114export type StateProps = {
1215 canvas ?: HTMLCanvasElement
13- storyAct : string
16+ name : string
17+ story ?: Story
1418}
1519
1620export class Game extends React . Component < GameProps , { } > {
1721 private canvas : HTMLCanvasElement
1822 private div : HTMLDivElement
1923
20- public componentDidMount ( ) {
21- /**
22- * Basically, if the function story is called twice (on different canvas
23- * elements), the second time the component is mounted, the pixi.js canvas
24- * will show nothing but a black screen. This means that if the user
25- * navigate aways from the game tab, and then back again, the game would not
26- * work.
27- *
28- * So, we save a reference to the first canvas that is loaded. Thereafter,
29- * when this component is mounted, use that canvas instead of the new canvas
30- * mounted with this div. This is a bit hacky, and refs aren't favoured in
31- * react, but it also prevents excessive loading of the game
32- */
24+ /**
25+ * Basically, if the function story is called twice (on different canvas
26+ * elements), the second time the component is mounted, the pixi.js canvas
27+ * will show nothing but a black screen. This means that if the user
28+ * navigate aways from the game tab, and then back again, the game would not
29+ * work.
30+ *
31+ * So, we save a reference to the first canvas that is loaded. Thereafter,
32+ * when this component is mounted, use that canvas instead of the new canvas
33+ * mounted with this div. This is a bit hacky, and refs aren't favoured in
34+ * react, but it also prevents excessive loading of the game
35+ *
36+ * Note that the story/4's 4th param is named 'attemptedAll'. It is true if a
37+ * storyline should not be loaded, and false if it should. In contrast,
38+ * backend sends us 'playStory', which is the negation (!) of `attemptedAll`.
39+ */
40+ public async componentDidMount ( ) {
41+ const story : any = ( await import ( './game.js' ) ) . default
42+ let storyOpts : Array < string | boolean >
3343 if ( this . props . canvas === undefined ) {
34- story ( this . div , this . canvas , this . props . storyAct )
44+ // First time rendering the Game component
45+ if ( this . props . story ) {
46+ storyOpts = [ this . props . story . story , ! this . props . story . playStory ]
47+ } else {
48+ // session.story is undefined if creating store from localStorage
49+ const state = store . getState ( )
50+ const tokens = {
51+ accessToken : state . session . accessToken ! ,
52+ refreshToken : state . session . refreshToken !
53+ }
54+ const user : any = await getUser ( tokens )
55+ if ( user ) {
56+ storyOpts = [ user . story . story , ! user . story . playStory ]
57+ store . dispatch ( setUser ( user ) )
58+ } else {
59+ // if user is null, actions.logOut is called anyways; nonetheless we
60+ // set storyOpts, otherwise typescript complains about using storyOpts
61+ // before assignment in story/4 below
62+ storyOpts = [ 'mission-1' , true ]
63+ }
64+ }
65+ story ( this . div , this . canvas , this . props . name , ...storyOpts )
3566 this . props . handleSaveCanvas ( this . canvas )
3667 } else {
68+ // This browser window has loaded the Game component & canvas before
3769 this . div . innerHTML = ''
3870 this . div . appendChild ( this . props . canvas )
3971 }
4072 }
4173
4274 public render ( ) {
4375 return (
44- < div
45- id = "game-display"
46- className = "sa-game"
47- data-story = "spaceship"
48- data-attempted-all = "true"
49- data-username = "mockUsername"
50- ref = { e => ( this . div = e ! ) }
51- >
76+ < div id = "game-display" className = "sa-game" ref = { e => ( this . div = e ! ) } >
5277 < canvas ref = { e => ( this . canvas = e ! ) } />
5378 </ div >
5479 )
0 commit comments