Skip to content

Commit

Permalink
Fix replays after Kotlinization of GameView
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Mar 23, 2019
1 parent db3d9f8 commit 0395211
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 53 deletions.
31 changes: 15 additions & 16 deletions src/views/games/gameTools.js
Expand Up @@ -18,6 +18,17 @@ function createPlayer(map, playerIndex) {
};
}

function createGameFromMap(map) {
return {
gameId: -1,
playerData: [createPlayer(map, 0), createPlayer(map, 1)],
clickable: true,
eliminations: [],
yourResult: null,
map: map
};
}

function createGame(gameInfo) {
let map = new core.Game(16, 16);
map = map.plugin(events => {
Expand All @@ -29,14 +40,8 @@ function createGame(gameInfo) {
map.addPlayer(new core.PlayerController("Nothing", () => null));
map.placeMines(51, Kotlin.kotlin.random.Random.Default);
map.recount();
let game = {
gameId: gameInfo.gameId,
playerData: [createPlayer(map, 0), createPlayer(map, 1)],
clickable: true,
eliminations: [],
yourResult: null,
map: map
};
let game = createGameFromMap(map);
game.gameId = gameInfo.gameId;
let index = parseInt(gameInfo.yourIndex, 10);
if (index >= 0) {
game.playerData[index].controllable = true;
Expand All @@ -48,20 +53,14 @@ function createLocalGame() {
let map = mapFactory.classic(16);
map.placeMines(51, Kotlin.kotlin.random.Random.Default);
map.recount();
let game = {
gameId: 0,
playerData: [createPlayer(map, 0), createPlayer(map, 1)],
clickable: true,
eliminations: [],
yourResult: null,
map: map
};
let game = createGameFromMap(map);
game.playerData.forEach(pl => (pl.controllable = true));
return game;
}

export default {
createGame: createGame,
createGameFromMap: createGameFromMap,
createLocalGame: createLocalGame,
setPlayerNames: setPlayerNames
};
47 changes: 10 additions & 37 deletions src/views/replay/Replay.vue
Expand Up @@ -17,20 +17,13 @@
import GameView from "../games/GameView";
import gameTools from "../games/gameTools";
function createGame(gameId, playerNames) {
let game = gameTools.createGame({ gameId: gameId, yourIndex: -1 });
gameTools.addPlayers(game, playerNames);
return game;
}
export default {
name: "Replay",
props: ["gameId", "gameInfo"],
components: { GameView },
data() {
let coreLib = require("../../kotlin/minesweeper-core");
let core = coreLib.default.net.zomis.minesweeper.core;
console.log(core);
let mapFactory = new core.MapFactory();
let map = mapFactory.classic(16);
let replayFactory = new core.ReplayFactory();
Expand All @@ -39,46 +32,26 @@ export default {
this.gameInfo.minePositions,
this.gameInfo.clicksString
);
replay.setPosition(0);
let game = gameTools.createGameFromMap(map);
game.gameId = this.gameId;
gameTools.setPlayerNames(
game,
this.gameInfo.players.map(pl => pl.playerName)
);
return {
position: replay.replayPosition,
max: replay.length(),
game: createGame(
this.gameId,
this.gameInfo.players.map(pl => pl.playerName)
),
game: game,
replay: replay
};
},
mounted() {
this.updateGame(this.replay.viewing);
},
methods: {
updateGame(map) {
map.players.toArray().forEach(pl => {
let player = this.game.players[pl.index];
player.score = pl.score;
});
this.game.fields.forEach((row, y) => {
row.forEach((field, x) => {
let mapField = this.replay.viewing.fieldAt(x, y);
field.blocked = mapField.blocked;
field.clicked = mapField.isVisible();
field.mine = mapField.isFoundMine();
field.clickedBy = mapField.whoClicked
? mapField.whoClicked.index
: null;
field.value =
field.clicked && !field.mine ? mapField.getKnownValue() : 0;
});
});
}
},
mounted() {},
methods: {},
watch: {
position(value) {
this.replay.setPosition(value);
this.updateGame(this.replay.viewing);
}
}
};
Expand Down

0 comments on commit 0395211

Please sign in to comment.