-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
2042 lines (1747 loc) · 84.8 KB
/
script.js
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//ClASSES//
class RailRoad {
constructor(name, minimumPrice, imageLink) {
this.name = name;
this.minimumPrice = minimumPrice;
this.imageLink = imageLink;
this.remainingInDeck = 4;
}
removeOneFromDeck() {
this.remainingInDeck -= 1;
}
}
class Town {
constructor(specificPrice, specificType, anyPrice, imageLink,commodityImageLink) {
this.specificPrice = specificPrice;
this.specificType = specificType;
this.anyPrice = anyPrice;
this.imageLink = imageLink;
this.inDeck = true;
this.commodityImageLink = commodityImageLink;
}
removeFromDeck() {
this.inDeck = false;
}
}
class Building {
constructor(
name,
price,
numberInDeck,
upgradable,
pointChanger,
turnChanger,
playerChanger,
imageLink,
upgradedImageLink
) {
this.name = name;
this.price = price;
this.numberInDeck = numberInDeck;
this.upgradable = upgradable;
this.pointChanger = pointChanger;
this.turnChanger = turnChanger;
this.playerChanger = playerChanger;
this.imageLink = imageLink;
this.upgradedImageLink = upgradedImageLink;
}
action() {
//methods for the action eahc will if they have one
}
}
class Stock{
constructor(name, element, positionBottom, positionLeft,stockElement,imageLink){
this.name = name;
this.element = element;
this.positionBottom = positionBottom;
this.positionLeft = positionLeft;
this.stockElement = stockElement;
this.imageLink = imageLink;
this.value = 0;
}
}
class Player{
constructor(element,num,inAuction){
this.element = element;
this.num = num;
this.inAuction = inAuction;
this.name = '';
this.railRoads = [];
this.towns = [];
this.buildings = [];
this.commodies = {
wheat : 0,
wood : 0,
iron : 0,
coal : 0,
goods : 0,
luxury : 0,
};
this.productionCards = [];
this.money = 10;
this.commodityMax = 10;
this.handSize = 3;
this.productionMax = 3;
}
}
//GLOBAL VARIABLES//
const railRoadStack = document.getElementById("cards-railroads-stack");
const railRoadAvaiableOne = document.getElementById('cards-railroads-one');
const railRoadAvaiableTwo = document.getElementById('cards-railroads-two');
const townStack = document.getElementById("cards-towns-stack");
const townAvaiable = document.getElementById('cards-towns-inplay')
const buildingStack = document.getElementById("cards-buildings-stack");
const buildingAvaiableOne = document.getElementById('cards-buildings-one');
const buildingAvaiableTwo = document.getElementById('cards-buildings-two');
const buildingAvaiableThree = document.getElementById('cards-buildings-three');
const buildingAvaiableFour = document.getElementById('cards-buildings-four');
const priceAndProduciton = document.getElementById('pp-card');
const playerOneElement = document.getElementById('player-one');
const playerTwoElement = document.getElementById('player-two');
const playerThreeElement = document.getElementById('player-three');
const playerFourElement = document.getElementById('player-four');
const woodPNG = document.getElementById('woodpng');
const wheatPNG = document.getElementById('wheatpng');
const ironPNG = document.getElementById('ironpng');
const coalPNG = document.getElementById('coalpng');
const goodsPNG = document.getElementById('goodspng');
const luxuryPNG = document.getElementById('luxurypng');
const wheatStock = document.getElementById('stocks-wheat');
const woodStock = document.getElementById('stocks-wood');
const ironStock = document.getElementById('stocks-iron');
const coalStock = document.getElementById('stocks-coal');
const goodsStock = document.getElementById('stocks-goods');
const luxuryStock = document.getElementById('stocks-luxury');
let wheatPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--wheat-positionBottom');
let wheatPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--wheat-positionLeft');
let woodPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--wood-positionBottom');
let woodPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--wood-positionLeft');
let ironPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--iron-positionBottom');
let ironPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--iron-positionLeft');
let coalPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--coal-positionBottom');
let coalPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--coal-positionLeft');
let goodsPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--goods-positionBottom');
let goodsPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--goods-positionLeft');
let luxuryPosBottom = getComputedStyle(document.documentElement)
.getPropertyValue('--luxury-positionBottom');
let luxuryPosLeft = getComputedStyle(document.documentElement)
.getPropertyValue('--luxury-positionLeft');
let confirmCancel = document.getElementById('confirm-cancel');
let root = document.querySelector(':root');
let startBtn = document.getElementById("start-button");
let whosTurn = document.getElementById('whos-turn');
let shownText = document.getElementById('showText');
let confirmButton = document.getElementById('confirm-choice');
let cancelButton = document.getElementById('cancel-choice');
let numPlayers = 0;
let railRoadRandomStack = [];
let townRandomStack = [];
let buildingRandomStackStart = []
let buildingRandomStackEnd = [];
let buildingRandomStack = [];
let confirmed = 0;
let productionArray = [];
let priceArray = [];
let howMany = document.createElement('input');
howMany.id = 'how-many';
let currTownAny = [];
let counter = 0;
let produceCounter = 0;
let constructionCompanyTurnOne = false;
let turnInput = document.createElement('input');
turnInput.id = 'turnInput';
let submitButton = document.createElement('div');
submitButton.id = 'submit';
submitButton.innerText = 'Enter';
playerOneNameInput = document.getElementById('player-one-name-input');
playerTwoNameInput = document.getElementById('player-two-name-input');
playerThreeNameInput = document.getElementById('player-three-name-input');
playerFourNameInput = document.getElementById('player-four-name-input');
playerOneNameInput.addEventListener('click', () => {removeInnerText(playerOneNameInput)},{once:true})
playerTwoNameInput.addEventListener('click',() => {removeInnerText(playerTwoNameInput)},{once:true})
playerThreeNameInput.addEventListener('click',() => {removeInnerText(playerThreeNameInput)},{once:true})
playerFourNameInput.addEventListener('click',() => {removeInnerText(playerFourNameInput)},{once:true})
//OBJECT CONSTRUCTION//
//STOCKS//
let wheat = new Stock('wheat', wheatPNG, wheatPosBottom, wheatPosLeft,wheatStock, 'images/commodies/wheat.png');
wheat.value = 1;
let wood = new Stock('wood', woodPNG, woodPosBottom, woodPosLeft,woodStock,'images/commodies/wood.png');
wood.value = 1;
let iron = new Stock('iron', ironPNG, ironPosBottom, ironPosLeft,ironStock,'images/commodies/iron.png');
iron.value = 2;
let coal = new Stock('coal', coalPNG, coalPosBottom, coalPosLeft,coalStock,'images/commodies/coal.png');
coal.value = 2;
let goods = new Stock('goods', goodsPNG, goodsPosBottom, goodsPosLeft,goodsStock,'images/commodies/goods.png');
goods.value = 3;
let luxury = new Stock('luxury', luxuryPNG, luxuryPosBottom, luxuryPosLeft,luxuryStock,'images/commodies/luxury.png');
luxury.value = 3;
//RAIL ROADS//
let rr1 = new RailRoad("Skunk Works", 2, "images/railroads/skunk.jpg");
let rr2 = new RailRoad("Sly Fox", 3, "images/railroads/fox.jpg");
let rr3 = new RailRoad("Fat Cat", 4, "images/railroads/cat.jpg");
let rr4 = new RailRoad("Big Bear", 5, "images/railroads/bear.png");
let rr5 = new RailRoad("Top Dog", 6, "images/railroads/dog.png");
let rr6 = new RailRoad("Tycoon", 7, "images/railroads/tycoon.png");
//TOWNS//
let t1 = new Town(2, "wheat", 4, "images/towns/2-wheat.jpg", "images/commodies/wheat.png");
let t2 = new Town(2, "wood", 4, "images/towns/2-wood.jpg", "images/commodies/wood.png");
let t3 = new Town(2, "iron", 4, "images/towns/2-iron.jpg", "images/commodies/iron.png");
let t4 = new Town(2, "coal", 4, "images/towns/2-coal.jpg", "images/commodies/coal.png");
let t5 = new Town(3, "wheat", 4, "images/towns/3-wheat.jpg", "images/commodies/wheat.png");
let t6 = new Town(3, "wood", 5, "images/towns/3-wood.jpg", "images/commodies/wood.png");
let t7 = new Town(3, "goods", 5, "images/towns/3-goods.jpg", "images/commodies/goods.png");
let t8 = new Town(3, "luxury", 5, "images/towns/3-luxury.jpg", "images/commodies/luxury.png");
let t9 = new Town(4, "wheat", 6, "images/towns/4-wheat.jpg", "images/commodies/wheat.png");
let t10 = new Town(4, "wood", 6, "images/towns/4-wood.jpg", "images/commodies/wood.png");
let t11 = new Town(4, "iron", 6, "images/towns/4-iron.jpg", "images/commodies/iron.png");
let t12 = new Town(4, "coal", 6, "images/towns/4-coal.jpg", "images/commodies/coal.png");
let t13 = new Town(5, "wheat", 8, "images/towns/5-wheat.jpg", "images/commodies/wheat.png");
let t14 = new Town(5, "wood", 8, "images/towns/5-wood.jpg", "images/commodies/wood.png");
let t15 = new Town(5, "goods", 8, "images/towns/5-goods.jpg", "images/commodies/goods.png");
let t16 = new Town(5, "luxury", 8, "images/towns/5-luxury.jpg", "images/commodies/luxury.png");
//randomizes the town deck
townRandomStack = [t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16];
//PLAYERS
let player1 = new Player(playerOneElement,1,true);
let player2 = new Player(playerTwoElement,2,true);
let player3 = new Player(playerThreeElement,3,true);
let player4 = new Player(playerFourElement,4,true);
let playerArray = [player1,player2,player3,player4];
let currPlayer = player1;
let inputText = '';
let outCounter = 0;
let currBid = 0;
let originalCurrPlayer = player1;
//BUILDINGS//
let b1 = new Building("iron1",5,1,12,false,true,false,"images/buildings/iron.jpg","images/buildings/double iron.jpg");
let b2 = new Building("luxury1",6,1,15,false,true,false,"images/buildings/luxury.jpg","images/buildings/doubleLuxury.jpg");
let b3 = new Building("wood1",4,1,9,false,true,false,"images/buildings/wood.jpg","images/buildings/doubleWood.jpg");
let b4 = new Building("wheat1",4,1,9,false,true,false,"images/buildings/wheat.jpg","images/buildings/doubleWheat.jpg");
let b5 = new Building("coal1",5,1,12,false,true,false,"images/buildings/coal.jpg","images/buildings/doubleCoal.jpg");
let b6 = new Building("goods1",6,1,15,false,true,false,"images/buildings/goods.jpg","images/buildings/doubleGoods.jpg");
let b7 = new Building("coal&iron",10,1,false,false,true,false,"images/buildings/coal&iron.jpg",false);
let b8 = new Building("goods&luxury",10,1,false,false,true,false,"images/buildings/good&luxury.jpg",false);
let b9 = new Building("wheat&wood",10,1,false,false,true,false,"images/buildings/wood&wheat.jpg",false);
let b10 = new Building("Cottage Industry",30,1,false,false,false,true,"images/buildings/cottageIndustry.jpg",false);
let b11 = new Building("Warehouse",10,2,false,false,false,true,"images/buildings/warehouse.jpg",false);
let b12 = new Building("Factory",40,2,false,false,false,true,"images/buildings/factory.jpg",false);
let b13 = new Building("Smuggler",20,1,false,false,false,true,"images/buildings/smuggler.jpg",false);
let b14 = new Building("Black Market",30,1,false,false,false,true,"images/buildings/blackMarket.jpg",false);
let b15 = new Building("Mayors Office",30,1,false,true,false,false,"images/buildings/mayorsOffice.jpg",false);
let b16 = new Building("Bank",30,1,false,true,false,false,"images/buildings/bank.jpg",false);
let b17 = new Building("Rail Baron",30,1,false,true,false,false,"images/buildings/railBaron.jpg",false);
let b18 = new Building("Govenors Mansion",30,1,false,true,false,false,"images/buildings/govenorsMansion.jpg",false);
let b19 = new Building("Export Company",30,1,false,false,true,false,"images/buildings/exportCompany.jpg",false);
let b20 = new Building("Brick Works",25,1,false,false,true,false,"images/buildings/brickWorks.jpg",false);
let b21 = new Building("Construction Company",20,1,false,false,true,false,"images/buildings/constructionCompany.jpg",false);
let b23 = new Building("Auction House",15,1,false,false,true,false,"images/buildings/auctionHouse.jpg",false);
let b24 = new Building("Freight Company",25,1,false,false,true,false,"images/buildings/freightCompany.jpg",false);
let b25 = new Building("Factory",40,2,false,false,false,true,"images/buildings/factory.jpg",false);
let b26 = new Building("Warehouse",10,2,false,false,false,true,"images/buildings/warehouse.jpg",false);
//FRONT END FUNCTIONS
const removeInnerText = (element) => {
element.value = '';
}
const refillSlot = (containerWithImage, endContainer, newCard) => {
containerWithImage.addEventListener('animationend',function(){
moveCardFunc(containerWithImage, endContainer, newCard)
});
//function to recieve an element with solely an image in it, and another image that will be on the reverse side
//creates a new element that is an image
let newImage = document.createElement("img");
// assigns that new element an src of what ever the next railroad is
newImage.src = newCard.imageLink;
//gives the same styling to the second image as the first one has
newImage.classList.add("flip-card-styling");
//initiated the old image to the front side of the card
containerWithImage.children[0].classList.add("front");
//initiated the new image to the back side of the flip by rotating it 180 degrees;
newImage.classList.add("back");
//appends the new image to the container
containerWithImage.appendChild(newImage);
//flip both of the inside images at the same time
containerWithImage.classList.add("flipCard");
}
const moveCardFunc = (startContainer, endContainer, newCard) => {
startContainer.addEventListener('animationend',function(){
setMovedCard(startContainer,endContainer,newCard);
});
//here is where you would add some code to make the flipped cards stay flipped/
startContainer.children[1].classList.remove('back');
//obtain the differnce between where the image is now, and where it needs to be moved to
startRect = startContainer.getBoundingClientRect();
let startPosX = startRect.x;
let startPosY = startRect.y;
endRect = endContainer.getBoundingClientRect();
let endPosX = endRect.x;
let endPosY = endRect.y;
let moveX = endPosX - startPosX;
let moveY = endPosY - startPosY;
root.style.setProperty('--cardEndX', Number(moveX));
root.style.setProperty('--cardEndY', Number(moveY));
startContainer.classList.add('move-card');
}
const setMovedCard = (startContainer,endContainer,newCard) => {
startContainer.classList.remove('flipCard', 'move-card');
//removes the hidden attribute of back
startContainer.children[1].classList.remove('back');
//adds the created imaged to the end contaier post flip and move
endContainer.appendChild(startContainer.children[1]);
// startContainer.replaceWith(startContainer.cloneNode(true));
// console.log('it runs');
}
const moveCommodity = (pieceObject,amount,movingUp) => {
// INPUT: a stock element piece as a variable, and a boonlean to wether its going up or down
for(let i = 0; i < amount; i++){
let currBottom = Number(getComputedStyle(document.documentElement).getPropertyValue(`--${pieceObject.name}-positionBottom`).slice(0,-2));
let currLeft = getComputedStyle(document.documentElement).getPropertyValue(`--${pieceObject.name}-positionLeft`);
if(currBottom === 0 && !movingUp){break};
if(currBottom > 222 && movingUp){break};
//get the current positon of that commidty and store it in variables as the start position for the keyframe
// Number(pieceObject.positionBottom.slice(0,-2));
// pieceObject.positionLeft;
//create new corresponding values based on the starting ones for the end value of the keyframe
//if the left value === 28, make it 55.
//if the left value === 55, make it 28.
let newLeft = '';
if(currLeft === '28px'){
newLeft = '62px';
}else{
newLeft = '28px';
}
// if its going up, increase the bottom value by 15px(about)
// if its going down , decrease the bottom value by 15px(about)
let newBottom = '';
if(movingUp){
currBottom += 21;
newBottom = `${currBottom}px`;
}else{
currBottom -= 21;
newBottom = `${currBottom}px`;
}
// apply these new variables to the variables inside css
// root.style.setProperty('--commodity-StartBottom',pieceObject.positionBottom);
// root.style.setProperty('--commodity-StartLeft',pieceObject.positionLeft);
// root.style.setProperty('--commodity-EndBottom',newBottom);
// root.style.setProperty('--commodity-EndLeft',newLeft);
//apply the moveStock class with the updated variables to the correspoidning png
// pieceObject.element.classList.add('moveStock');
//save the png's position as the end value variables
root.style.setProperty(`--${pieceObject.name}-positionBottom`,newBottom);
root.style.setProperty(`--${pieceObject.name}-positionLeft`,newLeft);
}
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
const afterMoveCommodity = (pieceObject) => {
pieceObject.element.classList.remove('moveStock');
}
//BACKEND FUNCTIONs
const changeCurrPlayer = (currPlayer) =>{
//runs when evr a full player change happens(mainly used to change player in auctions without actually changing the players turn)
if(counter === 0){
removeEventListeners();
constructionCompanyTurnOne = false;
currTownAny = [] ;
commodityMaxCheck()
produceCounter = 0;
currPlayer.element.children[1].innerText = currPlayer.money;
addEventListeners();
revertConfirmCancel();
revertUI();
organizeCommodies();
whosTurn.innerText = `${currPlayer.name} plays`;
}
//changes the current player to the next accoring to how many players there are
currPlayer.element.children[1].innerText = currPlayer.money;
if(numPlayers === 2){
if(currPlayer === player1){
currPlayer = player2;
player1.element.classList.replace('currPlayer','player');
player2.element.classList.replace('player','currPlayer');
}else{
currPlayer = player1;
player2.element.classList.replace('currPlayer','player');
player1.element.classList.replace('player','currPlayer');
}
}else if(numPlayers === 3){
if(currPlayer === player1){
currPlayer = player2;
player1.element.classList.replace('currPlayer','player');
player2.element.classList.replace('player','currPlayer');
}else if(currPlayer === player2){
currPlayer = player3;
player2.element.classList.replace('currPlayer','player');
player3.element.classList.replace('player','currPlayer');
}else{
currPlayer = player1;
player3.element.classList.replace('currPlayer','player');
player1.element.classList.replace('player','currPlayer');
}
}else{
if(currPlayer === player1){
currPlayer = player2;
player1.element.classList.replace('currPlayer','player');
player2.element.classList.replace('player','currPlayer');
}else if(currPlayer === player2){
currPlayer = player3;
player2.element.classList.replace('currPlayer','player');
player3.element.classList.replace('player','currPlayer');
}else if(currPlayer === player3){
currPlayer = player4;
player3.element.classList.replace('currPlayer','player');
player4.element.classList.replace('player','currPlayer');
}else{
currPlayer = player1;
player4.element.classList.replace('currPlayer','player');
player1.element.classList.replace('player','currPlayer');
}
}
return currPlayer;
}
const submit = () =>{
//used to delete the inside of the text boxes bnefore you typoe in it
inputText = turnInput.value;
}
const gameOverCheck = () => {
//checks for if the game ends
if(townRandomStack.length === 0 || railRoadRandomStack.length === 0){
return true;
}else{return false};
}
const countPoints = (player) => {
//adds up the poiunts of the current player
let total = 0;
let skunkWorks = player.railRoads.filter(rr => rr.name === 'Skunk Works');
console.log(skunkWorks);
if(skunkWorks.lengh === 1){total += 2}
if(skunkWorks.lengh === 2){total += 5}
if(skunkWorks.lengh === 3){total += 9}
if(skunkWorks.lengh === 4){total += 15}
let slyFox = player.railRoads.filter(rr => rr.name === 'Sly Fox');
console.log(slyFox);
if(slyFox.lengh === 1){total += 2}
if(slyFox.lengh === 2){total += 5}
if(slyFox.lengh === 3){total += 10}
if(slyFox.lengh === 4){total += 17}
let fatCat = player.railRoads.filter(rr => rr.name === 'Fat Cat');
console.log(fatCat);
if(fatCat.lengh === 1){total += 3}
if(fatCat.lengh === 2){total += 7}
if(fatCat.lengh === 3){total += 12}
if(fatCat.lengh === 4){total += 19}
let bigBear = player.railRoads.filter(rr => rr.name === 'Big Bear');
console.log(bigBear);
if(bigBear.lengh === 1){total += 3}
if(bigBear.lengh === 2){total += 7}
if(bigBear.lengh === 3){total += 13}
if(bigBear.lengh === 4){total += 21}
let topDog = player.railRoads.filter(rr => rr.name === 'Top Dog');
console.log(topDog);
if(topDog.lengh === 1){total += 4}
if(topDog.lengh === 2){total += 9}
if(topDog.lengh === 3){total += 15}
if(topDog.lengh === 4){total += 23}
let tycoon = player.railRoads.filter(rr => rr.name === 'Tycoon');
console.log(tycoon);
if(tycoon.lengh === 1){total += 4}
if(tycoon.lengh === 2){total += 9}
if(tycoon.lengh === 3){total += 16}
if(tycoon.lengh === 4){total += 25}
for(let i = 0; i < player.towns.length; i++){
total += player.towns[i].specificPrice;
}
if(player.towns.length <= player.railRoads.lengh){total += (player.towns.length * 2)}
if(player.towns.length > player.railRoads.lengh){total += (player.railRoads.length * 2)}
total += player.buildings.length;
if(searchForBuilding('Mayors Office')){total += player.buildings.length}
if(searchForBuilding('Bank')){total += Math.floor(player.money / 20)}
if(searchForBuilding('Rail Baron')){total += player.railRoads.length}
if(searchForBuilding('Govenors Mansion')){total += player.towns.length}
player.total = total;
}
const skipExtraTurn = () => {
// skps the extra turn given to some of the buildings
currPlayer = changeCurrPlayer(currPlayer);
}
const displayResults = () => {
//activeates when the game is over and shows the results accdoringly
confirmCancel.style.opacity = 0;
revertUI();
document.getElementById('UI').innerHTML = '';
removeEventListeners();
let winningPoints = 0;
let winner = '';
console.log(winningPoints);
//finds the winning players
for(let i = 0; i < numPlayers; i++){
currPlayer = playerArray[i];
countPoints(currPlayer);
if(playerArray[i].total > winningPoints){
winner = playerArray[i]
winningPoints = playerArray[i].total;
};
}
//displays the winnning players name
shownText.innerText = `${winner.name} Wins!`;
shownText.style.fontSize = '50px';
document.getElementById('UI').appendChild(shownText);
//shows the score board depending on how many people are in the game
if(numPlayers === 2){
removeEventListeners();
let playerOnePoints = document.createElement('div');
playerOnePoints.innerText = `${player1.name} scored ${player1.total} points`
playerOnePoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerOnePoints)
let playerTwoPoints = document.createElement('div');
playerTwoPoints.innerText = `${player2.name} scored ${player2.total} points`
playerTwoPoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerTwoPoints)
}
if(numPlayers === 3){
removeEventListeners();
let playerOnePoints = document.createElement('div');
playerOnePoints.innerText = `${player1.name} scored ${player1.total} points`
playerOnePoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerOnePoints)
let playerTwoPoints = document.createElement('div');
playerTwoPoints.innerText = `${player2.name} scored ${player2.total} points`
playerTwoPoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerTwoPoints)
let playerThreePoints = document.createElement('div');
playerThreePoints.innerText = `${player3.name} scored ${player3.total} points`
playerThreePoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerThreePoints)
}
if(numPlayers === 4){
removeEventListeners();
let playerOnePoints = document.createElement('div');
playerOnePoints.innerText = `${player1.name} scored ${player1.total} points`
playerOnePoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerOnePoints)
let playerTwoPoints = document.createElement('div');
playerTwoPoints.innerText = `${player2.name} scored ${player2.total} points`
playerTwoPoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerTwoPoints)
let playerThreePoints = document.createElement('div');
playerThreePoints.innerText = `${player3.name} scored ${player3.total} points`
playerThreePoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerThreePoints)
let playerFourPoints = document.createElement('div');
playerFourPoints.innerText = `${player4.name} scored ${player4.total} points`
playerFourPoints.classList.add('playerWin')
document.getElementById('UI').appendChild(playerFourPoints)
}
}
const confirmBuyBuildingOne = () =>{
revertConfirmCancel();
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', () => {buyBuildings(buildingAvaiableOne, 0)},{once:true});
cancelButton.addEventListener('click',() => {
if(searchForBuilding('Construction Company') && constructionCompanyTurnOne){
confirmCancel.innerHTML = '';
let skipButton = document.createElement('div');
skipButton.id = 'skip-button';
confirmCancel.appendChild(skipButton);
skipButton.innerText = 'Skip';
skipButton.addEventListener('click', skipExtraTurn);
}else{addEventListeners}
});
}
const confirmBuyBuildingTwo = () =>{
revertConfirmCancel();
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', () => {buyBuildings(buildingAvaiableTwo, 1)},{once:true});
cancelButton.addEventListener('click',() => {
if(searchForBuilding('Construction Company') && constructionCompanyTurnOne){
confirmCancel.innerHTML = '';
let skipButton = document.createElement('div');
skipButton.id = 'skip-button';
confirmCancel.appendChild(skipButton);
skipButton.innerText = 'Skip';
skipButton.addEventListener('click', skipExtraTurn);
}else{addEventListeners}
});
}
const confirmBuyBuildingThree = () =>{
revertConfirmCancel();
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', () => {buyBuildings(buildingAvaiableThree, 2)},{once:true});
cancelButton.addEventListener('click',() => {
if(searchForBuilding('Construction Company') && constructionCompanyTurnOne){
confirmCancel.innerHTML = '';
let skipButton = document.createElement('div');
skipButton.id = 'skip-button';
confirmCancel.appendChild(skipButton);
skipButton.innerText = 'Skip';
skipButton.addEventListener('click', skipExtraTurn);
}else{addEventListeners}
});
}
const confirmBuyBuildingFour = () =>{
revertConfirmCancel();
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', () => {buyBuildings(buildingAvaiableFour, 3)},{once:true});
cancelButton.addEventListener('click',() => {
if(searchForBuilding('Construction Company') && constructionCompanyTurnOne){
confirmCancel.innerHTML = '';
let skipButton = document.createElement('div');
skipButton.id = 'skip-button';
confirmCancel.appendChild(skipButton);
skipButton.innerText = 'Skip';
skipButton.addEventListener('click', skipExtraTurn);
}else{addEventListeners}
});
}
const buyBuildings = (building, num) => {
//ran once the confirm button is clicked on any given building\
//checks if the player has enough money
if(currPlayer.money >= buildingRandomStack[num].price){
//check for special buildings and adjusts the players object accordingly
if(buildingRandomStack[num].name === "Warehouse"){currPlayer.commodityMax += 3};
if(buildingRandomStack[num].name === "Black Market"){
if (currPlayer.handSize === 3){
currPlayer.handSize = 5;
createProductionCard();
createProductionCard();
}else if(currPlayer.handSize === 4){
currPlayer.handSize = 5;
createProductionCard();
}
};
if(buildingRandomStack[num].name === "Smuggler"){
if(currPlayer.handSize === 3){
currPlayer.handSize = 4;
createProductionCard();
}
}
if(buildingRandomStack[num].name === "Factory"){currPlayer.productionMax = 5}
if(buildingRandomStack[num].name === "Cottage Industry"){currPlayer.productionMax = 4};
//increases players commodity size when evr they buy a building
currPlayer.commodityMax += 1;
//gives the purchasing player that building
currPlayer.buildings.push(buildingRandomStack[num]);
//pays foir the building
currPlayer.money -= buildingRandomStack[num].price;
//replaces the one that weas purchased with the next in the array and then removes that onbe from the array
buildingRandomStack.splice(num, 1, buildingRandomStack[4]);
buildingRandomStack.splice(4,1);
//updates that players money
currPlayer.element.children[1].innerText = currPlayer.money;
//gives it new styling and gives the player the card on the front end
building.children[0].classList.replace('flip-card-styling','playerBuilding');
currPlayer.element.children[5].appendChild(building.children[0]);
// adds the ability to upgrade specific buildings
if(currPlayer.buildings[currPlayer.buildings.length-1].upgradable){
currPlayer.element.children[5].lastElementChild.addEventListener('click', () => {upgrade(currPlayer.buildings[0],currPlayer.buildings.length-1)});
}
//checks if there are any left in the bulding deck and if so, replenishes it, if not it stops
if(buildingRandomStack.length>4){
let newImage = document.createElement('img');
newImage.src = buildingRandomStack[num].imageLink;
newImage.classList.add('flip-card-styling')
building.appendChild(newImage);
};
//check for a building that would let that player buy another one and if it does, it redoes the buying building process
if(searchForBuilding('Construction Company') && !constructionCompanyTurnOne){
shownText.innerText = 'You may purchase a second building or click skip';
confirmCancel.innerHTML = '';
let skipButton = document.createElement('div');
skipButton.id = 'skip-button';
confirmCancel.appendChild(skipButton);
skipButton.innerText = 'Skip';
skipButton.addEventListener('click', skipExtraTurn);
constructionCompanyTurnOne = true;
buildingAvaiableOne.addEventListener('click',confirmBuyBuildingOne);
buildingAvaiableTwo.addEventListener('click',confirmBuyBuildingTwo);
buildingAvaiableThree.addEventListener('click',confirmBuyBuildingThree);
buildingAvaiableFour.addEventListener('click',confirmBuyBuildingFour);
}else{currPlayer = changeCurrPlayer(currPlayer)};
}else{
//case for when the player does not have enough money
shownText.innerText = 'You Are Broke. Do something else.'
addEventListeners();
revertConfirmCancel();
revertUI();
}
}
const upgrade = (building,num,event) => {
//function used to upgrade a building if allowed
if(currPlayer.money >= building.upgradable){
building.name = building.name.replace('1','2');
currPlayer.element.children[5].children[0].src = building.upgradedImageLink;
currPlayer.money -= building.upgradable;
currPlayer = changeCurrPlayer(currPlayer);
}else{
shownText.innerText = 'You Are Broke. Do Something Else';
}
}
const searchForBuilding = (buildingName) => {
//intakes a building and retures a boolean depeing on if the current player has that building in their hand
for(let i = 0; i < currPlayer.buildings.length; i++){
console.log(currPlayer.buildings[i].name, buildingName)
if(currPlayer.buildings[i].name === buildingName){
return true;
}
}
return false;
}
//railroads
const confirmRailRoadOne = (event) =>{
//saves whos started the auction
originalCurrPlayer = currPlayer;
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', buyRailRoadOne,{once:true});
cancelButton.addEventListener('click',addEventListeners);
}
const buyRailRoadOne = (event) =>{
//auctionHouse
let originalCurrPlayer = currPlayer;
for(let i = 0; i < playerArray.length; i++){
currPlayer = playerArray[i];
if(searchForBuilding('Auction House')){
playerArray[i].money += 5;
playerArray[i].element.children[1].innerText = playerArray[i].money;
}
}
currPlayer = originalCurrPlayer;
//once that comfirm buytton has been clicked, it is hidden until it is needed again
confirmCancel.style.opacity = 0;
//chanegs the UI to show what the bid is at and whos turn it is to bid
shownText.innerText = `Current Bid: $${currBid}
${currPlayer.name} Enter Your Bid Amount or Enter Out to be Out: `;
document.getElementById('UI').appendChild(turnInput);
document.getElementById('UI').appendChild(submitButton);
//removes event listeners to avoid breaking
removeEventListeners();
console.log('end of buy railroad two')
oldElement = document.getElementById('submit');
newElement = oldElement.cloneNode(true);
oldElement.parentNode.replaceChild(newElement, oldElement);
// adds so that when the enter button under the input div is clicked, it runs an auction round with what evr information is inside the input div
document.getElementById('submit').addEventListener('click', function(){auctionRound(0, railRoadAvaiableOne)});
}
const confirmRailRoadTwo = (event) =>{
//saves whos started the auction
originalCurrPlayer = currPlayer;
//shows the option to either confirm that choice or cancel it
confirmCancel.style.opacity = 1 ;
//reoves lisenetrs from the board to avoid breaking
removeEventListeners();
//adds a listener to the confirm button that will run the auction function for the first avalable railroad
confirmButton.addEventListener('click', buyRailRoadTwo,{once:true});
cancelButton.addEventListener('click',addEventListeners);
}
const buyRailRoadTwo = (event) =>{
//auctionHouse
let originalCurrPlayer = currPlayer;
for(let i = 0; i < playerArray.length; i++){
currPlayer = playerArray[i];
if(searchForBuilding('Auction House')){
playerArray[i].money += 5;
playerArray[i].element.children[1].innerText = playerArray[i].money;
}
}
currPlayer = originalCurrPlayer;
//once that comfirm buytton has been clicked, it is hidden until it is needed again
confirmCancel.style.opacity = 0;
//chanegs the UI to show what the bid is at and whos turn it is to bid
shownText.innerText = `Current Bid: $${currBid}
${currPlayer.name} Enter Your Bid Amount or Enter Out to be Out: `;
document.getElementById('UI').appendChild(turnInput);
document.getElementById('UI').appendChild(submitButton);
//removes event listeners to avoid breaking
removeEventListeners();
console.log('end of buy railroad one')
oldElement = document.getElementById('submit');
newElement = oldElement.cloneNode(true);
oldElement.parentNode.replaceChild(newElement, oldElement);
// adds so that when the enter button under the input div is clicked, it runs an auction round with what evr information is inside the input div
document.getElementById('submit').addEventListener('click', function(){auctionRound(1, railRoadAvaiableTwo)});
}
const auctionRound = (whichRailRoad,whichRailRoadElement) => {
console.log(railRoadRandomStack)
console.log('start of auctionOne')
//takes the value inside the input div and attaches it to a variables
inputText = turnInput.value;
console.log(inputText);
/// makes the input div empty so the next user doesnt have to delete it
turnInput.value = '';
//main if statment that runs each time the thew submut button is clicked and it evaluates the value inside of the input div
if(inputText === 'out' || inputText === 'Out' || inputText === 'OUT'){ // case for when that player wants out of the bid
console.log('first line inside of out if statment');
// increase a counter keeping track of how many people are left in the bif
outCounter++;
//flags that player as out of the bid
currPlayer.inAuction = false;
counter = 0;
do{
counter++;
console.log('inside out do whiile loop');
currPlayer = changeCurrPlayer(currPlayer);
}while(currPlayer.inAuction === false && counter < 5);
counter = 0;
console.log(outCounter);
if(outCounter === numPlayers - 1){ // whenever someone drops out, this line checks if there is only one player reaming
console.log('inside outCounter check but before for loop')
for(let i = 0; i < numPlayers; i++){
console.log('first line inside for loop' , playerArray[i].inAuction)
if(playerArray[i].inAuction === true){//when there is only one remaing, it searches for the player that hasnt been flagged out, and uses them for winning actions
//resets event listener for submit buitton so it doesnt run multiple times
console.log('first line inside when it found a winning player')
//makes the player pay
playerArray[i].money -= currBid;
//restes the bid for the next auction
currBid = 0;
outCounter = 0;
//pushes the railtroad that winner won onto that players railroad array
playerArray[i].railRoads.push(railRoadRandomStack[whichRailRoad]);
railRoadRandomStack.splice(whichRailRoad, 1,railRoadRandomStack[2]);
railRoadRandomStack.splice(2,1);
//refils the available slot with a new railroad from the rest of the deck