Step toward a one-page application
[vchess.git] / public / javascripts / utils / printDiagram.js
1 // Turn (human) marks into coordinates
2 function getMarkArray(marks)
3 {
4 if (!marks || marks == "-")
5 return [];
6 let markArray = doubleArray(V.size.x, V.size.y, false);
7 const squares = marks.split(",");
8 for (let i=0; i<squares.length; i++)
9 {
10 const coords = V.SquareToCoords(squares[i]);
11 markArray[coords.x][coords.y] = true;
12 }
13 return markArray;
14 }
15
16 // Turn (human) shadow indications into coordinates
17 function getShadowArray(shadow)
18 {
19 if (!shadow || shadow == "-")
20 return [];
21 let shadowArray = doubleArray(V.size.x, V.size.y, false);
22 const squares = shadow.split(",");
23 for (let i=0; i<squares.length; i++)
24 {
25 const rownum = V.size.x - parseInt(squares[i]);
26 if (!isNaN(rownum))
27 {
28 // Shadow a full row
29 for (let i=0; i<V.size.y; i++)
30 shadowArray[rownum][i] = true;
31 continue;
32 }
33 if (squares[i].length == 1)
34 {
35 // Shadow a full column
36 const colnum = V.ColumnToCoord(squares[i]);
37 for (let i=0; i<V.size.x; i++)
38 shadowArray[i][colnum] = true;
39 continue;
40 }
41 if (squares[i].indexOf("-") >= 0)
42 {
43 // Shadow a range of squares, horizontally or vertically
44 const firstLastSq = squares[i].split("-");
45 const range =
46 [
47 V.SquareToCoords(firstLastSq[0]),
48 V.SquareToCoords(firstLastSq[1])
49 ];
50 const step =
51 [
52 range[1].x == range[0].x
53 ? 0
54 : (range[1].x - range[0].x) / Math.abs(range[1].x - range[0].x),
55 range[1].y == range[0].y
56 ? 0
57 : (range[1].y - range[0].y) / Math.abs(range[1].y - range[0].y)
58 ];
59 // Convention: range always from smaller to larger number
60 for (let x=range[0].x, y=range[0].y; x <= range[1].x && y <= range[1].y;
61 x += step[0], y += step[1])
62 {
63 shadowArray[x][y] = true;
64 }
65 continue;
66 }
67 // Shadow just one square:
68 const coords = V.SquareToCoords(squares[i]);
69 shadowArray[coords.x][coords.y] = true;
70 }
71 return shadowArray;
72 }
73
74 // args: object with position (mandatory), and
75 // orientation, marks, shadow (optional)
76 function getDiagram(args)
77 {
78 // Obtain the array of pieces images names:
79 const board = V.GetBoard(args.position);
80 const orientation = args.orientation || "w";
81 const markArray = getMarkArray(args.marks);
82 const shadowArray = getShadowArray(args.shadow);
83 let boardDiv = "";
84 const [startX,startY,inc] = orientation == 'w'
85 ? [0, 0, 1]
86 : [V.size.x-1, V.size.y-1, -1];
87 for (let i=startX; i>=0 && i<V.size.x; i+=inc)
88 {
89 boardDiv += "<div class='row'>";
90 for (let j=startY; j>=0 && j<V.size.y; j+=inc)
91 {
92 boardDiv += "<div class='board board" + V.size.y + " " +
93 ((i+j)%2==0 ? "light-square-diag" : "dark-square-diag") +
94 (shadowArray.length > 0 && shadowArray[i][j] ? " in-shadow" : "") +
95 "'>";
96 if (board[i][j] != V.EMPTY)
97 {
98 boardDiv += "<img src='/images/pieces/" +
99 V.getPpath(board[i][j]) + ".svg' class='piece'/>";
100 }
101 if (markArray.length > 0 && markArray[i][j])
102 boardDiv += "<img src='/images/mark.svg' class='mark-square'/>";
103 boardDiv += "</div>";
104 }
105 boardDiv += "</div>";
106 }
107 return boardDiv;
108 }