/
GameContainer.tsx
230 lines (189 loc) · 7.11 KB
/
GameContainer.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import React, { Component, Fragment } from 'react';
import { Router } from '@reach/router';
import qs from 'querystringify';
import MediaQuery from 'react-responsive'
import { RouteComponentProps } from '@reach/router';
import { Database } from '@game3js/common';
import { localSaveReplay, clientSaveTournamentReplay, putGameReplay, getGameSessionId } from "../helpers/database";
import GameScene from '../components/GameScene';
import Game from './Game';
import GameUnity from './GameUnity';
import TournamentResultsCard from '../components/TournamentResultsCard';
import OutplayGameNavigation from '../components/OutplayGameNavigation';
import CSS from 'csstype';
import GameJavascript, { GameJavascriptContext } from './GameJavascript';
declare global {
interface Window { MediaRecorder: any; }
}
interface IProps extends RouteComponentProps {
playerProfile: Database.PlayerProfile;
address: any;
drizzle: any;
drizzleState: any;
contractMethodSendWrapper: any;
setRoute: any;
}
interface IState {
stateVar: any,
tournamentId?: any
}
const INITIAL_STATE: IState = {
stateVar: { value: "someValue" },
tournamentId: 'demo'
};
// external functions
function functionExample(someVar) {
}
export default class GameContainer extends Component<IProps, any> {
private mediaRecorder: any;
private recordedBlobs: any;
private canvas: any;
private stream: any;
constructor(props: any) {
super(props);
let params = qs.parse(window.location.search);
const { tournamentId } = params;
this.state = {
...INITIAL_STATE,
tournamentId: tournamentId
};
this.startRecording = this.startRecording.bind(this);
this.stopRecording = this.stopRecording.bind(this);
}
// HANDLERS: Gameplay Recorder
startRecording = async (params) => {
this.canvas = document.querySelector('canvas');
if (this.canvas) {
this.stream = this.canvas.captureStream(); // frames per second
console.log('Started stream capture from canvas element: ', this.stream);
}
this.recordedBlobs = [];
let options: any = { mimeType: 'video/webm' };
try {
this.mediaRecorder = new window.MediaRecorder(this.stream, options);
} catch (e0) {
console.log('Unable to create MediaRecorder with options Object: ', e0);
try {
options = { mimeType: 'video/webm,codecs=vp9' };
this.mediaRecorder = new window.MediaRecorder(this.stream, options);
} catch (e1) {
console.log('Unable to create MediaRecorder with options Object: ', e1);
try {
options = 'video/vp8'; // Chrome 47
this.mediaRecorder = new window.MediaRecorder(this.stream, options);
} catch (e2) {
alert('MediaRecorder is not supported by this browser.\n\n' +
'Try Firefox 29 or later, or Chrome 47 or later, ' +
'with Enable experimental Web Platform features enabled from chrome://flags.');
console.error('Exception while creating MediaRecorder:', e2);
return;
}
}
}
console.log('Created MediaRecorder', this.mediaRecorder, 'with options', options);
this.mediaRecorder.onstop = (event) => {
// console.log('Recorder stopped: ', event);
// const superBuffer = new Blob(this.recordedBlobs, { type: 'video/webm' });
// this.video.src = window.URL.createObjectURL(superBuffer);
}
this.mediaRecorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
this.recordedBlobs.push(event.data);
}
}
this.mediaRecorder.start(100); // collect 100ms of data
// console.log('MediaRecorder started', this.mediaRecorder);
}
stopRecording = async (shouldUpdateDB?: boolean) => {
this.mediaRecorder.stop();
// TODO: playerId = roomId? change to something more meaningful
const playerId = this.state.playerId || 'player';
const address = this.props.address;
const tournamentId = this.state.tournamentId || 'demo';
const time = Date.now(); // this.gameManager.gameEndsAt - Date.now();
console.log('Recorded Blobs: ', this.recordedBlobs);
console.log('Player ID: ', playerId);
const replayDate = new Date();
const filename = "replay_" + playerId + "_" + replayDate.valueOf() + ".webm";
const options = { type: 'video/webm' };
const file = new File(this.recordedBlobs, filename, options);
// this.video.controls = true;
// TODO: move to web worker so it doesn't pause main thread
if (tournamentId === 'demo') {
localSaveReplay(playerId, tournamentId, time, file);
} else {
const recordFileHash = await clientSaveTournamentReplay(file);
this.setState({
recordFileHash
})
if (shouldUpdateDB !== undefined && shouldUpdateDB) {
console.log("SHOULD UPDATE DB TO SERVER")
const sessionId = await getGameSessionId(address, tournamentId);
await putGameReplay(sessionId, address, recordFileHash);
}
}
}
// METHODS
// updateSomething = () => {
// }
// RENDER
render() {
const { drizzle, drizzleState, contractMethodSendWrapper, address, accountValidated, connectAndValidateAccount, setRoute } = this.props
const { tournamentId } = this.state;
return (
<>
<MediaQuery minDeviceWidth={728}>
{<OutplayGameNavigation />}
</MediaQuery>
<GameJavascript
startRecording={this.startRecording}
stopRecording={this.stopRecording}
>
<GameJavascriptContext.Consumer>{context => {
return (
<GameScene
drizzle={drizzle}
tournamentId={tournamentId}
playerAddress={address}
accountValidated={accountValidated}
connectAndValidateAccount={connectAndValidateAccount}
setRoute={setRoute}
>
<Router>
<Game
path=":roomId"
drizzle={drizzle}
drizzleState={drizzleState}
contractMethodSendWrapper={contractMethodSendWrapper}
gameJavascriptContext={context}
/>
<GameUnity
path="wom"
startRecording={this.startRecording}
stopRecording={this.stopRecording}
drizzle={drizzle}
drizzleState={drizzleState}
contractMethodSendWrapper={contractMethodSendWrapper}
tournamentId={tournamentId}
address={address}
/>
<GameUnity
path="flappybird"
startRecording={this.startRecording}
stopRecording={this.stopRecording}
drizzle={drizzle}
drizzleState={drizzleState}
contractMethodSendWrapper={contractMethodSendWrapper}
tournamentId={tournamentId}
address={address}
/>
</Router>
</GameScene>
)
}}
</GameJavascriptContext.Consumer>
</GameJavascript>
</>
);
}
}