Skip to content

Commit eb5ff63

Browse files
committed
2 parents 100e217 + 569ceee commit eb5ff63

File tree

1 file changed

+60
-45
lines changed

1 file changed

+60
-45
lines changed

client/visualizer/src/runner.ts

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ export default class Runner {
162162

163163
loadMatch(files: FileList) {
164164
const file = files[0]
165-
console.log(file)
166165
if (file.name.endsWith('.json')) {
167166
this.onTournamentLoaded(file)
168167
} else {
@@ -203,6 +202,10 @@ export default class Runner {
203202
newGame.loadFullGameRaw(data)
204203
this.games.push(newGame)
205204
this.startGame()
205+
if (this.conf.tournamentMode) {
206+
this.tournamentState = TournamentState.START_SPLASH
207+
this.processTournamentState()
208+
}
206209
// } catch {
207210
// throw new Error("game load failed.");
208211
// }
@@ -297,7 +300,6 @@ export default class Runner {
297300
};
298301

299302
seekTournament(num: number) {
300-
console.log('seek tournament')
301303
this.tournament?.seek(num, 0)
302304
this.processTournamentState()
303305
};
@@ -316,46 +318,32 @@ export default class Runner {
316318
* to end splash (only if last match in game), to next game.
317319
*/
318320
private nextTournamentState() {
319-
console.log('actually next tournament thing!')
320-
if (this.tournament) {
321+
if (this.conf.tournamentMode) {
322+
321323
if (this.tournamentState === TournamentState.START_SPLASH) {
322324
// transition to mid game
323325
this.tournamentState = TournamentState.MID_GAME
324326
} else if (this.tournamentState === TournamentState.MID_GAME) {
325327
// go to the next game
326-
if (this.tournament.hasNext() && !this.tournament.isLastMatchInGame()) {
327-
this.tournament.next()
328+
if (this.currentMatch != null && this.currentGame != null && !this.matchesCompleted()) {
329+
this.goNextMatch()
328330
} else {
329331
// go to end splash
330332
this.tournamentState = TournamentState.END_SPLASH
331333
}
332334
} else if (this.tournamentState === TournamentState.END_SPLASH) {
333-
// go to start splash
334-
// if not ended
335-
if (this.tournament.hasNext()) {
336-
this.tournamentState = TournamentState.START_SPLASH
337-
this.tournament.next()
338-
} else {
339-
console.log("No more tournament games!")
340-
}
341335
}
342336
}
343337
}
344338

345339
private previousTournamentState() {
346-
if (this.tournament) {
340+
if (this.conf.tournamentMode) {
347341
if (this.tournamentState === TournamentState.START_SPLASH) {
348342
// transition to mid game
349-
if (this.tournament.hasPrev()) {
350-
this.tournamentState = TournamentState.END_SPLASH
351-
this.tournament.prev()
352-
} else {
353-
console.log("No previous tournament games!")
354-
}
355343
} else if (this.tournamentState === TournamentState.MID_GAME) {
356344
// go to the previous game
357-
if (this.tournament.hasPrev() && !this.tournament.isFirstMatchInGame()) {
358-
this.tournament.prev()
345+
if (this.currentMatch != null && this.currentGame != null && this.currentMatch > 0) {
346+
this.goPreviousMatch()
359347
} else {
360348
// go to start splash
361349
this.tournamentState = TournamentState.START_SPLASH
@@ -368,39 +356,66 @@ export default class Runner {
368356
}
369357
}
370358

359+
private getCurrentWins(team1: string, team2: string, numMatches: number) {
360+
let wins = {}
361+
wins[team1] = 0
362+
wins[team2] = 0
363+
if (this.currentGame != null) {
364+
for (let matchIdx = 0; matchIdx <= numMatches; matchIdx++) {
365+
const winnerID = this.games[this.currentGame].getMatch(matchIdx).winner
366+
const winner = this.games[this.currentGame].meta.teams[winnerID].name
367+
wins[winner]++
368+
}
369+
}
370+
return wins
371+
}
372+
373+
private matchesCompleted() {
374+
if (this.currentGame != null && this.currentMatch != null) {
375+
const team1 = this.games[this.currentGame].meta.teams[1].name
376+
const team2 = this.games[this.currentGame].meta.teams[2].name
377+
const numMatches = this.games[this.currentGame].matchCount
378+
const goal = Math.floor(numMatches / 2) + 1
379+
380+
const wins = this.getCurrentWins(team1, team2, this.currentMatch)
381+
382+
return wins[team1] >= goal || wins[team2] >= goal
383+
}
384+
return false
385+
386+
}
387+
371388
private processTournamentState() {
372-
console.log('update tour state!')
373-
if (this.tournament) {
374-
console.log('real update tour state!')
389+
if (this.conf.tournamentMode) {
375390
// clear things
376391
Splash.removeScreen()
377392
// simply updates according to the current tournament state
378-
this.resetAllGames()
379393
this.showBlankCanvas()
380-
if (this.tournamentState === TournamentState.START_SPLASH) {
381-
console.log('go from splash real update tour state!')
382-
Splash.addScreen(this.conf, this.root, this.tournament.current().team1, this.tournament.current().team2)
383-
} else if (this.tournamentState === TournamentState.END_SPLASH) {
384-
const wins = this.tournament.wins()
385-
const totalWins = this.tournament.totalWins()
386-
let result: string = ""
387-
const team1 = this.tournament.current().team1
388-
const team2 = this.tournament.current().team2
389-
if (wins[team1] > wins[team2]) result = `${team1} wins ${wins[team1]}-${wins[team2]}!`
390-
else result = `${team2} wins ${wins[team2]}-${wins[team1]}!`
391-
if (totalWins[team1] != wins[team1] || totalWins[team2] != wins[team2]) {
392-
if (totalWins[team1] > totalWins[team2]) result += ` (Final score ${totalWins[team1]}-${totalWins[team2]})`
393-
else result += ` (Final score ${totalWins[team2]}-${totalWins[team1]})`
394+
if (this.currentGame != null) {
395+
const team1 = this.games[this.currentGame].meta.teams[1].name
396+
const team2 = this.games[this.currentGame].meta.teams[2].name
397+
if (this.tournamentState === TournamentState.START_SPLASH) {
398+
Splash.addScreen(this.conf, this.root, team1, team2)
399+
} else if (this.tournamentState === TournamentState.END_SPLASH && this.currentGame != null && this.currentMatch != null) {
400+
// const wins = this.tournament.wins()
401+
// const totalWins = this.tournament.totalWins()
402+
const numMatches = this.games[this.currentGame].matchCount
403+
const wins = this.getCurrentWins(team1, team2, this.currentMatch)
404+
405+
let result: string = ""
406+
if (wins[team1] > wins[team2]) result = `${team1} wins ${wins[team1]}-${wins[team2]}!`
407+
else result = `${team2} wins ${wins[team2]}-${wins[team1]}!`
408+
409+
const overallWins = this.getCurrentWins(team1, team2, numMatches - 1)
410+
if (overallWins[team1] > overallWins[team2]) result += ` (Final score ${overallWins[team1]}-${overallWins[team2]})`
411+
else result += ` (Final score ${overallWins[team2]}-${overallWins[team1]})`
412+
Splash.addWinnerScreen(this.conf, this.root, result)
394413
}
395-
Splash.addWinnerScreen(this.conf, this.root, result)
396-
} else if (this.tournamentState === TournamentState.MID_GAME) {
397-
this.loadGameFromURL(this.tournament.current().url)
398414
}
399415
}
400416
}
401417

402418
private runMatch() {
403-
console.log('Running match.')
404419

405420
// THIS IS A QUICKFIX FOR CHECKING THAT CURRENTGAME IS NOT NULL
406421
// TODO: IDEALLY, WE NEED TO FIGURE THIS OUT: CAN CURRENTGAME EVER BE NULL???

0 commit comments

Comments
 (0)