Skip to content

Commit cfe18a1

Browse files
author
Maxime Chéramy
committed
fix: fix code to use new version of cg-player
1 parent 95b0b27 commit cfe18a1

File tree

1 file changed

+55
-29
lines changed
  • runner/src/main/resources/view

1 file changed

+55
-29
lines changed

runner/src/main/resources/view/app.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
import * as config from '../config.js'
2-
import {Drawer} from '../core/Drawer.js'
32
import {ErrorLog} from '../core/ErrorLog.js'
43
import {demo as defaultDemo} from '../demo.js'
54
import Parser from './lib/Parser.js'
5+
import './player.js'
6+
7+
const createCGPlayer = (opts) => {
8+
return window['cg-player'].default({
9+
...opts,
10+
localStorageKey: 'ngStorage-gameParams',
11+
src: './player.html',
12+
libraries: {
13+
PIXI4: './lib/pixi4.js'
14+
}
15+
})
16+
}
617

718
/* global fetch, angular, $, XMLHttpRequest */
819

9-
function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameManagerFactory, $localStorage) {
20+
function PlayerCtrl ($scope, $timeout, $interval, $filter, $element) {
1021
'ngInject'
1122
const ctrl = this
23+
let cgPlayer = null
1224
let player = null
1325
let lastWidth
1426
let currentFrame = null
1527

16-
let playerLoadedPromise = new Promise((resolve) => {
17-
$scope.playerLoaded = function (playerApi) {
18-
ctrl.playerApi = playerApi
19-
resolve(playerApi)
20-
}
21-
})
22-
23-
$scope.gameParams = $localStorage.$default({
24-
gameParams: {}
25-
})
2628
$scope.loadGame = loadGame
2729
$scope.selectReplay = selectReplay
2830
$scope.viewReplay = viewReplay
@@ -57,14 +59,32 @@ function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameMa
5759

5860
/// //////////////
5961

60-
function init () {
61-
drawerFactory.createDrawer(Drawer).then(drawer => {
62-
$scope.drawer = drawer
63-
fetchGame().then(data => {
64-
ctrl.data = data
65-
loadGame()
66-
})
62+
async function init () {
63+
cgPlayer = createCGPlayer({
64+
viewerUrl: '/core/Drawer.js'
6765
})
66+
cgPlayer.on('parsedGameInfo', onParsedGameInfo)
67+
cgPlayer.createIframe($element.find('.cg-player-sandbox')[0])
68+
cgPlayer.setOptions({
69+
showConsole: false,
70+
showRankings: false,
71+
showSmallRankings: false,
72+
asyncRendering: true,
73+
shareable: false,
74+
showReplayPrompt: false
75+
})
76+
fetchGame().then(data => {
77+
ctrl.data = data
78+
loadGame()
79+
})
80+
}
81+
82+
function onParsedGameInfo (gameInfo) {
83+
$scope.playerColors = {}
84+
gameInfo.agents.forEach(function (agent) {
85+
$scope.playerColors[agent.index] = agent.color
86+
})
87+
cgPlayer.off('parsedGameInfo', onParsedGameInfo)
6888
}
6989

7090
function loadGame () {
@@ -83,12 +103,9 @@ function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameMa
83103
ctrl.gameInfo = convertFrameFormat(ctrl.data)
84104
$scope.agents = {...ctrl.data.agents}
85105

86-
ctrl.gameManager = gameManagerFactory.createGameManagerFromGameInfo($scope.drawer, ctrl.gameInfo, true)
87-
ctrl.gameManager.subscribe(onUpdate)
88-
89-
return playerLoadedPromise.then(playerApi => {
90-
playerApi.initReplay(ctrl.gameManager)
91-
})
106+
cgPlayer.sendFrames(ctrl.gameInfo)
107+
// ctrl.gameManager = gameManagerFactory.createGameManagerFromGameInfo($scope.drawer, ctrl.gameInfo, true)
108+
cgPlayer.subscribe(onUpdate)
92109
}
93110

94111
function onUpdate (frame, progress, playing, isSubFrame, isTurnBased, atEnd) {
@@ -170,7 +187,6 @@ function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameMa
170187
let result = null
171188
try {
172189
const json = JSON.parse(this.responseText)
173-
json.agents.forEach(agent => { agent.color = Drawer.playerColors[agent.index] })
174190
result = json
175191
} catch (e) {
176192
console.error(e)
@@ -211,8 +227,18 @@ function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameMa
211227
}
212228

213229
function viewReplay () {
214-
drawerFactory.createDrawer(Drawer, ctrl.introReplayData).then(drawer => {
215-
$scope.replayDrawer = drawer
230+
const cgPlayerReplay = createCGPlayer({
231+
viewerUrl: '/core/Drawer.js',
232+
customDemo: ctrl.introReplayData
233+
})
234+
cgPlayerReplay.createIframe($element.find('.cg-player-sandbox-replay')[0])
235+
cgPlayerReplay.setOptions({
236+
showConsole: false,
237+
showRankings: false,
238+
showSmallRankings: false,
239+
asyncRendering: true,
240+
shareable: false,
241+
showReplayPrompt: false
216242
})
217243
$scope.showViewReplayPopup = true
218244
}
@@ -326,7 +352,7 @@ function PlayerCtrl ($scope, $timeout, $interval, $filter, drawerFactory, gameMa
326352
}
327353
}
328354

329-
angular.module('player')
355+
angular.module('player', ['ngStorage'])
330356
.controller('PlayerCtrl', PlayerCtrl)
331357
.directive('resizeHandle', function ($localStorage) {
332358
'ngInject'

0 commit comments

Comments
 (0)