summaryrefslogtreecommitdiff
path: root/play.js
blob: 7ecdbd2b2009b256e240200334c2c514df505039 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

const seed = 'none'
const scenario = 'standard'
const options = 'none'
//const rules = require("./rules")

const last_card = 110
const last_power_card = 52

const toolbar = document.getElementById('toolbar')
const vpMarker = document.getElementById('vp')
const mapContainer = document.querySelector('.map')

const countries= [
	null,
	"East Germany",
	"Poland",
	"Czechoslovakia",
	"Hungary",
	"Romania",
	"Bulgaria"
]


const overlay = document.getElementById('overlay');
const spaceNameElement = document.getElementById('space-name');
const spaceCharacteristicsElement = document.getElementById('space-characteristics');


// Event listener to track mouse movement over the map
        document.querySelector('.map').addEventListener('mousemove', function(event) {
            const x = event.offsetX; // X-coordinate of mouse relative to container
            const y = event.offsetY; // Y-coordinate of mouse relative to container
	spaceCharacteristicsElement.innerText = `X: ${x}, Y: ${y}`;
	})


 // Create map areas dynamically based on coordinates

 function create_ui() {

// CREATE MAP

	spaces.forEach((space) => {
		if (space && space.box) {
                const { x, y, h, w } = space.box;
                const spaceArea = document.createElement('div');
                spaceArea.classList.add('space-area', space.country)
				spaceArea.id=`space_${space.space_id}`;
                spaceArea.style.left = x + 'px';
                spaceArea.style.top = y + 'px';
                spaceArea.style.width = w + 'px';
                spaceArea.style.height = h + 'px';
				spaceArea.style.zIndex = 2;
				spaceArea.my_space = space.name_unique;  
        		spaceArea.addEventListener('mousedown', on_click_space);
				//spaceArea.addEventListener('click', finishSupportCheck)

			/*if (space.demInfl > 0 && space.demInfl - space.comInfl >= space.stability) {
				const img = document.createElement('img')
				img.classList.add('demInfl', space.country)
				img.id=space.name
				img.src = `images/US_${space.demInfl}.gif`
				spaceArea.appendChild(img)
			} else if (space.demInfl > 0) {*/
				const dem_img = document.createElement('div')
				dem_img.classList.add('demInfl', space.country)
				dem_img.style.display = 'none'
				dem_img.id=`${space.name_unique}_demInfl`
				dem_img.style.zIndex = 1
				dem_img.my_space = space.name_unique;  
        		dem_img.addEventListener('mousedown', on_click_space);
				spaceArea.appendChild(dem_img)

				const demInflValue = document.createElement('p')
				demInflValue.className='demInflValue'
				demInflValue.style.display = 'none'
				demInflValue.id=`${space.name_unique}_demInflValue`
				demInflValue.innerText=space.demInfl
				demInflValue.style.zIndex = 1
				demInflValue.my_space = space.name_unique;  
        		demInflValue.addEventListener('mousedown', on_click_space);
				spaceArea.appendChild(demInflValue)
			//}

			/*} else if (space.comInfl > 0 && space.comInfl - space.demInfl >= space.stability) {
				const img = document.createElement('img')
				img.className='comInfl'
				img.id=space.name
				img.src = `images/SV_${space.comInfl}.gif`
				spaceArea.appendChild(img)		
			} else if (space.comInfl > 0) {*/
				const com_img = document.createElement('div')
				com_img.className='comInfl'
				com_img.style.display='none'
				com_img.id=`${space.name_unique}_comInfl`
				com_img.style.zIndex = 1
				com_img.my_space = space.name_unique;  
        		com_img.addEventListener('mousedown', on_click_space);
				spaceArea.appendChild(com_img)

				const comInflValue = document.createElement('p')
				comInflValue.className='comInflValue'
				comInflValue.style.display='none'
				comInflValue.id=`${space.name_unique}_comInflValue`
				comInflValue.innerText=space.comInfl
				comInflValue.style.zIndex = 1
				comInflValue.my_space = space.name_unique;  
        		comInflValue.addEventListener('mousedown', on_click_space);
				spaceArea.appendChild(comInflValue)
			//} 
		
		mapContainer.appendChild(spaceArea);
		}
	});

// CREATE CARDS

	for (let c = 1; c <= last_card; ++c) {

		const hand_card = document.createElement('img');
        hand_card.classList.add('hand_card')
		hand_card.id=`card_${c}`;
		hand_card.src = `cards/e${c}.gif`
		hand_card.my_card = c;  
        hand_card.addEventListener('mousedown', on_click_card);
		ui.cards.push(hand_card);
	}
	//for (let card of power_cards)
	for (let card of power_cards) {
		if (!card) continue;
    	const power_card = document.createElement('img');
    	power_card.classList.add('power_card');
    	power_card.id = `power_card_${card.number}`;
    	power_card.src = `cards_2/${card.url}.gif`;
    	power_card.my_card = card.number;
    	power_card.addEventListener('mousedown', on_click_card);
    	ui.power_cards.push(power_card);
	}
}


function on_click_space(evt) {
    if (evt.button === 0) {
        const space = evt.target.my_space;
        //console.log('on_click_space_called with space:', space);
        if (send_action('infl', space)) {
			evt.stopPropagation();
        } else if (send_action('sc', space)) {
			//console.log('send_action with sc:', space);
			evt.stopPropagation();
		} else {
           // console.log('send_action failed for space:', space);
        }
    }
    //hide_popup_menu();
}

function on_click_card(evt) {
	//console.log('on_click_card')
	if (evt.button === 0) {
        const card = evt.target.my_card;
      //  console.log('on_click_card_called with card:', card);
        if (send_action('card', card)) {
            evt.stopPropagation();
        } else {
        //    console.log('send_action failed for card:', card);
        }
    }
}

function is_action(action) {
	//console.log('is_action called with: ', action)
	if (view.actions && view.actions[action])
		return true
	return false
}


function on_log(text) { // eslint-disable-line no-unused-vars
	let p = document.createElement("div")

	if (text.match(/^>/)) {
		text = text.substring(1)
		p.className = 'i'
	}

	if (text.match(/^\.h1/)) {
		text = text.substring(4)
		p.className = 'h1'
	}
	else if (text.match(/^\.h2d/)) {
		text = text.substring(5)
		p.className = 'h2 dem'
	}
	else if (text.match(/^\.h2c/)) {
		text = text.substring(5)
		p.className = 'h2 com'
	}
	else if (text.match(/^\.h2/)) {
		text = text.substring(4)
		p.className = 'h2'
	}
	else if (text.match(/^\.h3/)) {
		text = text.substring(4)
		p.className = 'h3'
	}

	p.innerHTML = text
	return p
}

let ui = {
	favicon: document.getElementById('favicon'),
	player: [
		document.getElementById("role_Democrat"),
		document.getElementById("role_Communist"),
	],
	cards: [ null ],
	power_cards: [],
	dem_hand_count: document.getElementById("role_stat_dem"),
	com_hand_count: document.getElementById("role_stat_com"),
	played_card: 0,
	events_panel: document.getElementById("events_panel"),
	hand_panel: document.getElementById("hand_panel"),
	turn: document.getElementById("turn-tracker"),
	round: document.getElementById("action-round-tracker"),
	stability: document.getElementById("stability-track"),
	dem_TST: document.getElementById("dem-TST"),
	com_TST: document.getElementById("com-TST"),
	vp: document.getElementById("vp"),
	spaces: document.getElementsByClassName("space-area")

}

function on_update() {
	//console.log('on_update called')
	//console.log('view.valid_spaces: ', view.valid_spaces)
//	console.log('view.actions: ', view.actions)
	//console.log('view.power_cards:', view.power_cards)
	document.querySelectorAll('[id^="space_"].selected').forEach(spaceElement => {spaceElement.classList.remove('selected');});
    document.getElementById("power_hand")?.querySelectorAll('.selected').forEach(cardElement => {cardElement.classList.remove('selected');});
	view.valid_spaces.forEach(space_id => {
    	const spaceElementId = `space_${space_id}`;
    	const spaceElement = document.getElementById(spaceElementId);

    	if (spaceElement) {
        	spaceElement.classList.add('selected');
    	}
	});

	//Check influence values
	const pieces = view.pieces
	pieces.forEach(piece => {
		const dem_marker = document.getElementById(`${piece.name_unique}_demInfl`);
		const dem_number = document.getElementById(`${piece.name_unique}_demInflValue`);
		const com_marker = document.getElementById(`${piece.name_unique}_comInfl`);
		const com_number = document.getElementById(`${piece.name_unique}_comInflValue`);
		
		dem_number.innerText=piece.demInfl
		if (piece.demInfl > 0) {
			dem_marker.style.display = 'block';
			dem_number.style.display = 'block';

			if(piece.demInfl - piece.comInfl >= piece.stability){
				dem_marker.classList.add('controlled')
				dem_marker.classList.remove('uncontrolled')
			} else {
				dem_marker.classList.add('uncontrolled')
				dem_marker.classList.remove('controlled')
			}
		} else {
			dem_marker.style.display = 'none';
			dem_number.style.display = 'none';
		}
		com_number.innerText=piece.comInfl
		if (piece.comInfl > 0) {
			com_marker.style.display = 'block';
			com_number.style.display = 'block';

			if(piece.comInfl - piece.demInfl >= piece.stability){
				com_marker.classList.add('controlled')
				com_marker.classList.remove('uncontrolled')
			} else {
				com_marker.classList.add('uncontrolled')
				com_marker.classList.remove('controlled')
			}
		} else {
			com_marker.style.display = 'none';
			com_number.style.display = 'none';
		}
		
	});

// UPDATE COUNTRY MARKERS
	for (let i = 1; i < countries.length; i++) {
		const country = countries[i];
		const marker = document.getElementById(country);

		if (view.revolutions[country]) {
			marker.classList.add('revolution')
			marker.style.display = 'block'
		} else if (view.times_held[country] > 0 ) {
			marker.classList.add('held')
			marker.style.display = 'block'
		}
		else {marker.style.display = 'none'}
	} 

// UPDATE ASIDE

	ui.dem_hand_count.innerText = `${view.democrat_hand} cards in hand`
	ui.com_hand_count.innerText = `${view.communist_hand} cards in hand`

// UPDATE HAND

	document.getElementById("hand").replaceChildren()
	document.getElementById("events").replaceChildren()
		
	if (view.hand.length && view.is_pwr_struggle === false) {
		document.getElementById("hand_panel").classList.remove("hide")
		for (let c of view.hand) {
			//const cardElement = ui.cards[c];
		//if (cardElement) {
			document.getElementById("hand").appendChild(ui.cards[c]);
		/*} else {
			console.error(`Card with key ${c} not found in ui.cards`);
		}*/
	}	
	} else {
		document.getElementById("hand_panel").classList.add("hide")
	}

// EVENT PANEL
	if (view.played_card > 0) {
		document.getElementById("events_panel").classList.remove("hide")
		document.getElementById("events").appendChild(ui.cards[view.played_card]);
	} else {
		document.getElementById("events_panel").classList.add("hide")
	}


// POWER STRUGGLE HAND
	document.getElementById("power_hand").replaceChildren()

	if (view.is_pwr_struggle) {
		document.getElementById("power_panel").classList.remove("hide")
		//console.log('ui.power_cards: ', ui.power_cards)
		//console.log('ui.cards: ', ui.cards)


const power_hand = document.getElementById("power_hand");

if (!power_hand) {
    console.error("Element with id 'power_hand' not found.");
} else {
	//console.log('view.power_hand: ', view.power_hand)
	//console.log('view.hand:', view.hand)
	//console.log('view.valid_cards: ', view.valid_cards)
	for (let card_number of view.power_hand) {
        const power_card = ui.power_cards.find(card => card.my_card === card_number);
        if (power_card instanceof Node) {
			//console.log(`Checking card number: ${card_number}`)
			//console.log(`Is card number ${card_number} valid? ${view.valid_cards.includes(card_number)}`);
			const cardInfo = power_cards.find(card => card.number === card_number);

            if (cardInfo) {
                //console.log(`Card info for number ${card_number}:`, cardInfo);
               // console.log(`Socio value for card ${card_number}: ${cardInfo.socio}`);
			}
            if (view.valid_cards.includes(card_number)) {
                power_card.classList.add('selected');
            }			
            power_hand.appendChild(power_card);
        } else {
           // console.error(`ui.power_cards[${index-1}] is not a valid DOM node.`);
        }
    }
}
	} else {
		//console.log('document.getElementById("power_panel"): ', document.getElementById("power_panel"))
		document.getElementById("power_panel").classList.add("hide")
	}

// UPDATE BOARD MARKERS
	ui.turn.className = `t${view.turn}`
	//console.log('view.round_player: ', view.round_player)
	if (view.round_player === 'Democrat') {
		//console.log('in democrat')
		ui.round.className = `dem-action-round-tracker r${view.round}`
	} else {
		//console.log('in communist')
		ui.round.className = `com-action-round-tracker r${view.round}`
	}
	ui.stability.className = `s${view.stability}`
	ui.dem_TST.className = `tst${view.dem_tst}`
	//console.log('view.com_tst: ', view.com_tst)
	ui.com_TST.className = `tst${view.com_tst}`
	ui.vp.className = `vp${view.vp}`

console.log('strategy discard: ', view.strategy_discard)
console.log('valid spaces: ', view.valid_spaces)

//console.log('view.pieces: ', view.pieces)


	action_button("strike", "Strike")
	action_button("march", "March")
	action_button("rally", "Rally in the Square")
	action_button("petition", "Petition")	
	action_button("scoring", "Score country")
	action_button("surrender", "Surrender Power")
	action_button("retain", "Retain Power")
	action_button("take", "Take Power")
	action_button("concede", "Concede")
	action_button("struggle", "Begin power struggle")
	action_button("raise", "Raise the stakes")	
	action_button("draw", "Draw cards")	
	action_button("scoring", "Scoring")	
	action_button("event", "Event")
	action_button("influence", "Influence")
	action_button("tst", "Tiananmen Square Track")
	action_button("support_check", "Support Check")
	action_button("roll", "Roll a die")
	action_button("done", "Done")
	action_button("undo", "Undo")
}


create_ui()