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