aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/web/src/gui/map/dom_order_builder.js
blob: 14ba743b2a755327bad2620b4ab58ddee0e72eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ==============================================================================
// Copyright (C) 2019 - Philip Paquette, Steven Bocco
//
//  This program is free software: you can redistribute it and/or modify it under
//  the terms of the GNU Affero General Public License as published by the Free
//  Software Foundation, either version 3 of the License, or (at your option) any
//  later version.
//
//  This program is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
//  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
//  details.
//
//  You should have received a copy of the GNU Affero General Public License along
//  with this program.  If not, see <https://www.gnu.org/licenses/>.
// ==============================================================================
import {UTILS} from "../../diplomacy/utils/utils";
import $ from "jquery";
import {extendOrderBuilding} from "../utils/order_building";
import {Diplog} from "../../diplomacy/utils/diplog";

function parseLocation(txt) {
    if (txt.length > 2 && txt[1] === ' ' && ['A', 'F'].includes(txt[0]))
        return txt.substr(2);
    return txt;
}

export class DOMOrderBuilder {

    constructor(svgElement, onOrderBuilding, onOrderBuilt, onSelectLocation, onSelectVia, onError) {
        this.svg = svgElement;
        this.cbOrderBuilding = onOrderBuilding;
        this.cbOrderBuilt = onOrderBuilt;
        this.cbSelectLocation = onSelectLocation;
        this.cbSelectVia = onSelectVia;
        this.cbError = onError;

        this.game = null;
        this.mapData = null;
        this.orderBuilding = null;

        this.provinceColors = {};
        this.clickedID = null;
        this.clickedNeighbors = [];

        this.onProvinceClick = this.onProvinceClick.bind(this);
        this.onLabelClick = this.onLabelClick.bind(this);
        this.onUnitClick = this.onUnitClick.bind(this);
    }

    saveProvinceColors() {
        // Get province colors.
        const elements = this.svg.getElementsByTagName('path');
        for (let element of elements) {
            this.provinceColors[element.id] = element.getAttribute('class');
        }
    }

    provinceNameToMapID(name) {
        return `_${name.toLowerCase()}___${this.svg.parentNode.id}`;
    }

    mapID(id) {
        return `${id}___${this.svg.parentNode.id}`;
    }

    onOrderBuilding(svgPath, powerName, orderPath) {
        this.cbOrderBuilding(powerName, orderPath);
    }

    onOrderBuilt(svgPath, powerName, orderString) {
        this.cbOrderBuilt(powerName, orderString);
    }

    onError(svgPath, error) {
        this.cbError(error.toString());
    }

    handleSvgPath(svgPath) {
        const orderBuilding = this.orderBuilding;
        if (!orderBuilding.builder)
            return this.onError(svgPath, 'No orderable locations.');

        const province = this.mapData.getProvince(svgPath.id);
        if (!province)
            return;

        const stepLength = orderBuilding.builder.steps.length;
        if (orderBuilding.path.length >= stepLength)
            throw new Error(`Order building: current steps count (${orderBuilding.path.length}) should be less than` +
                ` expected steps count (${stepLength}) (${orderBuilding.path.join(', ')}).`);

        const lengthAfterClick = orderBuilding.path.length + 1;
        let validLocations = [];
        const testedPath = [orderBuilding.type].concat(orderBuilding.path);
        const value = UTILS.javascript.getTreeValue(this.game.ordersTree, testedPath);
        if (value !== null) {
            const checker = orderBuilding.builder.steps[lengthAfterClick - 1];
            try {
                const possibleLocations = checker(province, orderBuilding.power);
                for (let possibleLocation of possibleLocations) {
                    possibleLocation = possibleLocation.toUpperCase();
                    if (value.includes(possibleLocation))
                        validLocations.push(possibleLocation);
                }
            } catch (error) {
                return this.onError(svgPath, error);
            }
        }
        if (!validLocations.length)
            return this.onError(svgPath, 'Disallowed.');

        if (validLocations.length > 1 && orderBuilding.type === 'S' && orderBuilding.path.length >= 2) {
            // We are building a support order and we have a multiple choice for a location.
            // Let's check if next location to choose is a coast. To have a coast:
            // - all possible locations must start with same 3 characters.
            // - we expect at least province name in possible locations (e.g. 'SPA' for 'SPA/NC').
            // If we have a coast, we will remove province name from possible locations.
            let isACoast = true;
            let validLocationsNoProvinceName = [];
            for (let i = 0; i < validLocations.length; ++i) {
                let location = validLocations[i];
                if (i > 0) {
                    // Compare 3 first letters with previous location.
                    if (validLocations[i - 1].substring(0, 3).toUpperCase() !== validLocations[i].substring(0, 3).toUpperCase()) {
                        // No same prefix with previous location. We does not have a coast.
                        isACoast = false;
                        break;
                    }
                }
                if (location.length !== 3)
                    validLocationsNoProvinceName.push(location);
            }
            if (validLocations.length === validLocationsNoProvinceName.length) {
                // We have not found province name.
                isACoast = false;
            }
            if (isACoast) {
                // We want to choose location in a coastal province. Let's remove province name.
                validLocations = validLocationsNoProvinceName;
            }
        }

        if (validLocations.length > 1) {
            if (this.cbSelectLocation) {
                return this.cbSelectLocation(validLocations, orderBuilding.power, orderBuilding.type, orderBuilding.path);
            } else {
                Diplog.warn(`Forced to select first valid location.`);
                validLocations = [validLocations[0]];
            }
        }
        let orderBuildingType = orderBuilding.type;
        if (lengthAfterClick === stepLength && orderBuildingType === 'M') {
            const moveOrderPath = ['M'].concat(orderBuilding.path, validLocations[0]);
            const moveTypes = UTILS.javascript.getTreeValue(this.game.ordersTree, moveOrderPath);
            if (moveTypes !== null) {
                if (moveTypes.length === 2) {
                    // This move can be done either regularly or VIA a fleet. Let user choose.
                    return this.cbSelectVia(validLocations[0], orderBuilding.power, orderBuilding.path);
                } else {
                    orderBuildingType = moveTypes[0];
                }
            }
        }
        this.clickedID = svgPath.id;

        this.cleanBuildingView();
        if (lengthAfterClick < stepLength)
            this.renderBuildingView(validLocations[0]);
        extendOrderBuilding(
            orderBuilding.power, orderBuildingType, orderBuilding.path, validLocations[0],
            this.cbOrderBuilding, this.cbOrderBuilt, this.cbError
        );

    }

    getPathFromProvince(province) {
        let path = this.svg.getElementById(this.provinceNameToMapID(province.name));
        if (!path) {
            for (let alias of province.aliases) {
                path = this.svg.getElementById(this.provinceNameToMapID(alias));
                if (path)
                    break;
            }
        }
        return path;
    }

    onProvinceClick(event) {
        this.handleSvgPath(event.target);
    }

    onLabelClick(event) {
        const province = this.mapData.getProvince(event.target.textContent);
        if (province) {
            const path = this.getPathFromProvince(province);
            if (path)
                this.handleSvgPath(path);
        }
    }

    onUnitClick(event) {
        const province = this.mapData.getProvince(event.target.getAttribute('diplomacyUnit'));
        if (province) {
            let path = this.getPathFromProvince(province);
            if (!path && province.isCoast())
                path = this.svg.getElementById(this.provinceNameToMapID(province.parent.name));
            if (path) {
                this.handleSvgPath(path);
            }
        }
    }

    cleanBuildingView() {
        if (this.clickedID) {
            const path = this.svg.getElementById(this.clickedID);
            if (path)
                path.setAttribute('class', this.provinceColors[this.clickedID]);
        }
        for (let neighborName of this.clickedNeighbors) {
            const province = this.mapData.getProvince(neighborName);
            if (!province)
                continue;
            const path = this.getPathFromProvince(province);
            if (path)
                path.setAttribute('class', this.provinceColors[path.id]);
        }
        this.clickedNeighbors = [];
    }

    renderBuildingView(extraLocation) {
        if (this.clickedID) {
            const path = this.svg.getElementById(this.clickedID);
            if (path)
                path.setAttribute('class', 'provinceRed');
        }
        const selectedPath = [this.orderBuilding.type].concat(this.orderBuilding.path);
        if (extraLocation)
            selectedPath.push(extraLocation);
        const possibleNeighbors = UTILS.javascript.getTreeValue(this.game.ordersTree, selectedPath);
        if (!possibleNeighbors)
            return;
        this.clickedNeighbors = possibleNeighbors.map(neighbor => parseLocation(neighbor));
        if (this.clickedNeighbors.length) {
            for (let neighbor of this.clickedNeighbors) {
                let neighborProvince = this.mapData.getProvince(neighbor);
                if (!neighborProvince)
                    throw new Error('Unknown neighbor province ' + neighbor);
                let path = this.getPathFromProvince(neighborProvince);
                if (!path && neighborProvince.isCoast())
                    path = this.getPathFromProvince(neighborProvince.parent);
                if (!path)
                    throw new Error(`Unable to find SVG path related to province ${neighborProvince.name}.`);
                path.setAttribute('class', neighborProvince.isWater() ? 'provinceBlue' : 'provinceGreen');
            }
        }
    }

    update(game, mapData, orderBuilding) {
        this.game = game;
        this.mapData = mapData;
        this.orderBuilding = orderBuilding;
        this.saveProvinceColors();
        // If there is a building path, then we are building, so we don't clean anything.
        this.cleanBuildingView();
        if (this.orderBuilding.path.length)
            this.renderBuildingView();
        // I don't yet know why I should place this here. Maybe because unit are re-rendered manually at every reloading ?
        $(`#${this.svg.parentNode.id} svg use[diplomacyUnit]`).click(this.onUnitClick);
    }

    init(game, mapData, orderBuilding) {
        $(`#${this.svg.parentNode.id} svg path`).click(this.onProvinceClick);
        $(`#${this.mapID('BriefLabelLayer')} text`).click(this.onLabelClick);
        this.update(game, mapData, orderBuilding);
    }

}