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.score = (value != "*" ? value : "?");
73 // Always generate random ID for imported games, because they could be
74 // downloaded at different states (prefix 'i' for 'Import').
75 game.id = 'i' + getRandString()
77 // 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 spaceIdx = lines[idx].indexOf(' ');
89 const skipMoveNum = lines[idx].substr(spaceIdx + 1);
90 const lineParts = skipMoveNum.split(",");
92 lineParts.forEach(lpart => {
93 const smParts = lpart.split(' ');
94 const startEnd = smParts[0].split('.');
98 ? V.SquareToCoords(startEnd[0])
102 ? V.SquareToCoords(startEnd[1])
104 const appearVanish = smParts[1].split('/').map(av => {
105 if (av == "-") return [];
106 return av.split('.').map(psq => {
107 const xy = V.SquareToCoords(psq.substr(2));
116 sm.appear = appearVanish[0];
117 sm.vanish = appearVanish[1];
120 if (move.length == 1) move = move[0];
121 game.moves.push(move);
123 this.$emit("game-uploaded", game);
130 <style lang="sass" scoped>
140 @media screen and (max-width: 767px)