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 : "?");
70 // Always generate random ID for imported games, because they could be
71 // downloaded at different states (prefix 'i' for 'Import').
72 game.id = 'i' + getRandString()
74 // Provide a random cadence, just to be sure nothing breaks:
76 game.chats = []; //not stored in PGN :)
77 // Skip "human moves" section:
78 while (lines[++idx].length > 0) {}
81 await import("@/variants/" + game.vname + ".js")
83 window.V = vModule[game.vname + "Rules"];
84 while (++idx < lines.length && lines[idx].length > 0) {
85 const spaceIdx = lines[idx].indexOf(' ');
86 const skipMoveNum = lines[idx].substr(spaceIdx + 1);
87 const lineParts = skipMoveNum.split(",");
89 lineParts.forEach(lpart => {
90 const smParts = lpart.split(' ');
91 const startEnd = smParts[0].split('.');
95 ? V.SquareToCoords(startEnd[0])
99 ? V.SquareToCoords(startEnd[1])
101 const appearVanish = smParts[1].split('/').map(av => {
102 if (av == "-") return [];
103 return av.split('.').map(psq => {
104 const xy = V.SquareToCoords(psq.substr(2));
113 sm.appear = appearVanish[0];
114 sm.vanish = appearVanish[1];
117 if (move.length == 1) move = move[0];
118 game.moves.push(move);
120 this.$emit("game-uploaded", game);
127 <style lang="sass" scoped>
137 @media screen and (max-width: 767px)