Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: f786112236b057247a9e5b2bb0aab16e8890719e (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
import { Observable } from "./observable";
// import counters from '../../public/assets/images/counters.svg';
// console.log('counters', counters);

// import svg from '../../public/assets/images/counters.svg';

// const doc = new DOMParser().parseFromString(svg, 'image/svg+xml');
// const rifle = document.importNode(doc.querySelector('#rifle'), true);
// const smg = document.importNode(doc.querySelector('#smg'), true);

// console.log('svg', counters);
// console.log('doc', doc);
// console.log('imported svg', rifle);
// console.log('imported svg', smg);

const weapons = {
  rifle: {
    name: 'Rifle',
    damage: '4L',
    shortRange: '1-27',
    longRange: '28-75'
  },
  smg: {
    name: 'SMG',
    damage: '3L',
    shortRange: '1-15',
    longRange: '16-25'
  },
  blazer: {
    name: 'Blazer',
    damage: '4L',
    shortRange: '1-17',
    longRange: '18-105'
  }
}

const cacheBuster = Array(20).fill(null).map(() => getRandomIntInclusive(0, 9)).join('');

function createIcon(number) {
  const [icon, use, text] = ['svg', 'use', 'text'].map(t => document.createElementNS(svgns, t));

  icon.setAttributeNS(null, 'viewBox', '-6 -6 12 12');
  icon.setAttribute('xmlns', svgns);

  // use.setAttributeNS(null, 'href', `assets/images/counters.svg?v=${cacheBuster}#counter-base`);
  use.setAttributeNS(null, 'href', `assets/images/counters.svg#counter-base`);
  // use.setAttributeNS(null, 'href', `${counters}#counter-base`);

  text.textContent = number;

  icon.appendChild(use);
  icon.appendChild(text);

  return icon;
}

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
function getRandomIntInclusive(min, max) {
  const minCeiled = Math.ceil(min);
  const maxFloored = Math.floor(max);
  return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled); // The maximum is inclusive and the minimum is inclusive
}

function createWeaponIcon(type) {
  const [icon, use] = ['svg', 'use'].map(t => document.createElementNS(svgns, t));

  icon.setAttributeNS(null, 'viewBox', '-6 -6 12 12');
  icon.setAttribute('xmlns', svgns);
  icon.classList.add('weapon-icon');

  // use.setAttributeNS(null, 'href', `assets/images/counters.svg?v=${cacheBuster}#${type}`);
  // use.setAttributeNS(null, 'href', `${counters}#${type}`);
  use.setAttributeNS(null, 'href', `assets/images/counters.svg#${type}`);

  icon.appendChild(use);
  // icon.appendChild(document.importNode(doc.querySelector(`#${type}`), true));

  return icon;
}

function createRecord(unit) {
  const { dataset: { allegiance, number, squad }} = unit,
    primaryWeapon = unit.querySelector('.primary-weapon'),
    pw = primaryWeapon.getAttributeNS(null, 'href').split('#').pop() || 'rifle',
    div = document.createElement('soldier-record-block'),
    spans = Array(6).fill('span').map(t => document.createElement(t)),
    [tn, sn, pwt, pwd, pwrs, pwrl] = spans;

  div.classList.add('soldier-record');
  if (unit.classList.contains('selected')) div.classList.add('selected');
  div.dataset.number = number;
  div.dataset.allegiance = allegiance;

  tn.setAttribute('slot', 'troop-number');
  tn.appendChild(createIcon(number));

  sn.setAttribute('slot', 'squad-number');
  sn.appendChild(createIcon(squad || 1));

  pwt.setAttribute('slot', 'primary-weapon-type');
  pwt.textContent = ' ' + weapons[pw].name;
  pwt.prepend(createWeaponIcon(pw));

  pwd.setAttribute('slot', 'primary-weapon-damage');
  pwd.textContent = weapons[pw].damage;

  pwrs.setAttribute('slot', 'primary-weapon-range-short');
  pwrs.textContent = weapons[pw].shortRange;

  pwrl.setAttribute('slot', 'primary-weapon-range-long');
  pwrl.textContent = weapons[pw].longRange;

  spans.forEach(el => div.appendChild(el));

  return div;
}

function createRecords(units) {
  const grouped = Array.from(units).reduce((acc, unit) => {
    acc[unit.dataset.allegiance]?.push(unit) || (acc[unit.dataset.allegiance] = [unit]);
    return acc;
  }, {});

  for (const al in grouped) {
    grouped[al] = grouped[al].map(createRecord);
  }

  return grouped;
}

function getRecord({ dataset: { allegiance: al, number: n }}) {
  const selector = `.soldier-record[data-number="${n}"][data-allegiance="${al}"]`;

  return document.querySelector(selector);
}

function deselect() {
  const selected = getSelected();

  if (selected) {
    selected.classList.remove('selected');
  }
}

function clear() {
  document.querySelectorAll('#record-sheet .soldier-record').forEach(el => el.remove());
  document.querySelector('#attacker-record .name').textContent = 'attacker';
  document.querySelector('#defender-record .name').textContent = 'defender';
}

function select(data) {
  const record = data && getRecord(data);
  const isSelected = record?.classList.contains('selected');

  deselect();

  if (isSelected || !data) return;

  record.classList.add('selected');
}

function endMove() {
  const selected = getSelected();

  if (selected) {
    selected.classList.toggle('movement-ended');
  }

  deselect();
}

export function getSelected() {
  return document.querySelector('.soldier-record.selected');
}

export function start(startLoc, units) {
  clear();
  const forces = createRecords(units);

  for (const affiliation in forces) {
    const container = document.querySelector(`#${affiliation}-record`);
    const records = container.querySelector('.records');
    const name = startLoc?.dataset[`${affiliation}Name`];

    if (name) {
      container.querySelector('.name').textContent = name;
    }
    forces[affiliation].forEach(r => records.appendChild(r));
  }

  document.querySelectorAll('.soldier-record').forEach(el =>
    el.addEventListener('click', () => Observable.notify('select', el))
  );

  Observable.subscribe('select', select);
  Observable.subscribe('endmove', endMove);
}

export function stop() {
  Observable.unsubscribe('select', select);
  Observable.unsubscribe('endmove', endMove);
}