3 input#upload(type="file" @change="upload")
5 @click="uploadTrigger()"
6 aria-label="store.state.tr['Upload a game']"
8 img.inline(src="/images/icons/upload.svg")
12 import { getRandString } from "@/utils/alea";
14 name: "my-upload-game",
16 uploadTrigger: function() {
17 document.getElementById("upload").click();
20 const file = (e.target.files || e.dataTransfer.files)[0];
21 var reader = new FileReader();
22 reader.onloadend = ev => {
23 this.parseAndEmit(ev.currentTarget.result);
25 reader.readAsText(file);
27 parseAndEmit: async function(pgn) {
29 // Players potential ID and socket IDs are not searched
35 const lines = pgn.split('\n');
38 while (lines[idx].length > 0) {
39 // NOTE: not using "split(' ')" because the FEN has spaces
40 const spaceIdx = lines[idx].indexOf(' ');
41 const prop = lines[idx].substr(0, spaceIdx).match(/^\[(.*)$/)[1];
42 const value = lines[idx].substr(spaceIdx + 1).match(/^"(.*)"\]$/)[1];
48 game.created = new Date(value).getTime();
51 game.players[0].name = value;
54 game.players[1].name = value;
57 game.fenStart = value;
60 // Allow importing unfinished games, but mark them as
61 // "unknown result" to avoid running the clocks...
62 game.result = (value != "*" ? value : "?");
65 // Prefix "I_" to say "this is an import"
66 game.id = "i" + value.match(/\/game\/([a-zA-Z0-9]+)$/)[1];
75 game.id = "i" + getRandString();
76 // Provide a random cadence, just to be sure nothing breaks:
79 game.chats = []; //not stored in PGN :)
80 // Skip "human moves" section:
81 while (lines[++idx].length > 0) {}
84 await import("@/variants/" + game.vname + ".js")
86 window.V = vModule[game.vname + "Rules"];
87 while (++idx < lines.length && lines[idx].length > 0) {
88 const lineParts = lines[idx].split(" ");
89 const startEnd = lineParts[1].split('.');
91 if (startEnd[0] != "-") move.start = V.SquareToCoords(startEnd[0]);
92 if (startEnd[1] != "-") move.end = V.SquareToCoords(startEnd[1]);
93 const appearVanish = lineParts[2].split('/').map(lpart => {
94 if (lpart == "-") return [];
95 return lpart.split('.').map(psq => {
96 const xy = V.SquareToCoords(psq.substr(2));
105 move.appear = appearVanish[0];
106 move.vanish = appearVanish[1];
107 game.moves.push(move);
109 this.$emit("game-uploaded", game);
116 <style lang="sass" scoped>
126 @media screen and (max-width: 767px)