/
GameJavascript.tsx
152 lines (127 loc) · 4.48 KB
/
GameJavascript.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
import React, { Component, createContext } from 'react';
import { updateSessionScore, updateGameNo, createSessionId, makeNewGameSession } from '../helpers/database';
import { navigateTo } from '../helpers/utilities'
import { Constants } from '@game3js/common';
export const GameJavascriptContext = createContext({});
export default class GameJavascript extends Component<any, any> {
constructor(props) {
super(props);
this.state = {
isPlayerDead: false,
isGameRunning: true,
sessionId: '0',
playerAddress: '',
tournamentId: '',
gameName: Constants.TOSIOS
}
this.playerIsDead = this.playerIsDead.bind(this);
this.gameIsRunning = this.gameIsRunning.bind(this);
this.setSessionId = this.setSessionId.bind(this);
this.initiateGame = this.initiateGame.bind(this);
this.updateSessionHighScore = this.updateSessionHighScore.bind(this);
this.endGame = this.endGame.bind(this);
}
async updateSessionHighScore() {
const { isPlayerDead, sessionId, playerAddress, tournamentId } = this.state;
const timeFinished = Date.now();
let gamePayload = {}
switch (this.state.gameName) {
case Constants.TOSIOS:
gamePayload = {
timeFinished,
didWin: !isPlayerDead
}
break;
default:
break;
}
let updatedData = await updateSessionScore(sessionId, playerAddress, tournamentId, gamePayload);
console.log("Data updated with", updatedData);
return updatedData;
}
async updateGameNumber(sessionId: any, playerAddress: any, tournamentId: any) {
console.log("GAME JAVASCRIPT: UpdateGameNumber")
await updateGameNo(sessionId, playerAddress, tournamentId);
}
playerIsDead(isDead) {
console.log("GAME JAVASCRIPT: PlayerIsDead")
this.setState({
isPlayerDead: isDead
})
}
gameIsRunning(isRunning) {
console.log("GAME JAVASCRIPT: PlayerIsDead")
this.setState({
isGameRunning: isRunning
})
}
async replaySaveToServer() {
}
async setSessionId(playerAddress, tournamentId) {
console.log("GAME JAVASCRIPT: setSessionId")
console.log("GAME JAVASCRIPT-setSessionId: Player_add", playerAddress)
console.log("GAME JAVASCRIPT-setSessionId: tournamentId", tournamentId)
let sessionId = await createSessionId(playerAddress, tournamentId);
this.setState({
sessionId
})
}
async endGame(died: boolean) {
const { stopRecording } = this.props;
const { tournamentId } = this.state;
this.gameIsRunning(false);
this.playerIsDead(died);
const data = await this.updateSessionHighScore();
console.log("GAME JAVASCRIPT-endGame: Tourney ID", tournamentId)
if (tournamentId || tournamentId === 0) {
console.log("GAME JAVASCRIPT-endGame: New Highscore?", data.newHighScore);
await stopRecording(data.newHighScore);
console.log("GAME JAVASCRIPT-endGame: Recording stopped")
}
const gameEnded = new Event('gameend');
dispatchEvent(gameEnded);
}
async initiateGame(params: any) {
console.log("GAME JAVASCRIPT: Initiate Game")
const { playerAddress, tournamentId, isDead, isGameRunning, players, endsAt } = params;
const { startRecording } = this.props;
this.setState({
playerAddress,
tournamentId
})
console.log("GAME JAVASCRIPT: Playeraddress", playerAddress)
console.log("GAME JAVASCRIPT: Tourney ID", tournamentId)
await this.setSessionId(playerAddress, tournamentId);
console.log("GAME JAVASCRIPT: Session ID", this.state.sessionId)
// gameName, sessionId, tournamentId, gamePayload
let gamePayload = {
timeLeft: Date.now(),
players
}
await makeNewGameSession(Constants.TOSIOS, this.state.sessionId, tournamentId, gamePayload)
await this.updateGameNumber(this.state.sessionId, playerAddress, tournamentId);
this.gameIsRunning(isGameRunning);
this.playerIsDead(isDead);
if (tournamentId || tournamentId === 0) {
startRecording.call()
console.log("GAME JAVASCRIPT: Recording started...")
}
}
render() {
return (
<GameJavascriptContext.Provider value={
{
...this.state,
updateSessionHighScore: this.updateSessionHighScore,
updateGameNumber: this.updateGameNumber,
playerIsDead: this.playerIsDead,
gameIsRunning: this.gameIsRunning,
initiateGame: this.initiateGame,
endGame: this.endGame
}
}>
{this.props.children}
</GameJavascriptContext.Provider>
)
}
}