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