rules.component.ts 4.95 KB
Newer Older
Romain DELEAU's avatar
Romain DELEAU committed
1 2 3
import { Component, Input, OnInit } from '@angular/core';
import { Ressource } from 'src/app/class/ressource/ressource';
import { Scenario } from 'src/app/class/scenario/scenario';
4
import { TooltipService } from 'src/app/services/tooltip/tooltip.service';
5 6
import { MatDialog } from '@angular/material/dialog';
import { SuppressDialogComponent } from 'src/app/components/dialogs/suppress-dialog/suppress-dialog.component';
7 8 9
import { CleanDialogComponent } from 'src/app/components/dialogs/clean-dialog/clean-dialog.component';
import { PieceDetailsService } from 'src/app/services/piece-details/piece-details.service';
import { Task } from 'src/app/class/task/task';
10
import { Trace } from 'src/app/class/trace/trace';
11
import { MinimapService } from 'src/app/services/minimap/minimap.service';
12
import { TranslateService } from '@ngx-translate/core';
Romain DELEAU's avatar
Romain DELEAU committed
13 14 15 16 17 18 19 20

@Component({
  selector: 'app-rules',
  templateUrl: './rules.component.html',
  styleUrls: ['./rules.component.scss']
})
export class RulesComponent implements OnInit {

21
  displayMenu: string = 'hide';
Romain DELEAU's avatar
Romain DELEAU committed
22 23
  @Input() scenario: Scenario = new Scenario();

24
  constructor(protected tooltipService: TooltipService, public dialog: MatDialog, protected pieceDetailsService: PieceDetailsService, private minimapService: MinimapService, protected translate: TranslateService) { }
Romain DELEAU's avatar
Romain DELEAU committed
25 26

  ngOnInit(): void {
Romain DELEAU's avatar
Romain DELEAU committed
27 28
  }

29 30
  onClickPiece(): void {
    this.pieceDetailsService.piece = this.scenario;
Romain DELEAU's avatar
Romain DELEAU committed
31 32 33
    this.pieceDetailsService.missionIndex = undefined;
    this.pieceDetailsService.roleIndex = undefined;
    this.pieceDetailsService.pieceIndex = undefined;
34 35 36
  }

  onClickErase(): void {
37
    const dialogRef = this.dialog.open(CleanDialogComponent, { data: this.translate.instant('rules_clean') });
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.scenario.gameRules = '';
        this.scenario.ressources.forEach((ressource, i) => {
          this.scenario.missions.forEach(mission => {
            mission.roles.forEach(role => {
              role.tasks.forEach(inlineTasks => {
                inlineTasks.forEach(task => {
                  if (task instanceof Task) {
                    task.prerequireRessources.forEach((taskRessource, j) => {
                      if (ressource == taskRessource.ressource) {
                        task.prerequireRessources.splice(j, 1);
                      }
                    });
52 53 54 55
                    if (task.rewardType == 'object' && ressource == task.reward) {
                      task.resetReward();
                      task.rewardType = 'none';
                    }
56 57 58 59 60 61 62
                  }
                });
              });
            });
          });
        });
        this.scenario.ressources = [];
63
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',undefined,undefined,'all','Rules', '#C6C2BD'));
64
        this.minimapService.reset();
65 66
      } else {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',undefined,undefined,'all','Rules', '#C6C2BD'));
67 68 69 70
      }
    });
  }

Romain DELEAU's avatar
Romain DELEAU committed
71 72
  addRessource(): void {
    this.scenario.ressources.push(new Ressource());
73
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'new',undefined,undefined,'Ressource_['+(this.scenario.traces.length-1)+']','Rules', '#C6C2BD'));
74
    this.minimapService.reset();
Romain DELEAU's avatar
Romain DELEAU committed
75 76 77
  }

  removeRessource(index: number): void {
78
    const dialogRef = this.dialog.open(SuppressDialogComponent, { data: this.translate.instant('rules_delete') });
79 80 81 82 83 84 85 86 87 88 89
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.scenario.missions.forEach(mission => {
          mission.roles.forEach(role => {
            role.tasks.forEach(inlineTasks => {
              inlineTasks.forEach(task => {
                task?.prerequireRessources.forEach((prerequire, j) => {
                  if (this.scenario.ressources[index] == prerequire.ressource) {
                    task.prerequireRessources.splice(j, 1);
                  }
                });
90 91 92 93
                if (task?.rewardType == 'object' && this.scenario.ressources[index] == task.reward) {
                  task.resetReward();
                  task.rewardType = 'none';
                }
94
              });
Romain DELEAU's avatar
Romain DELEAU committed
95 96 97
            });
          });
        });
98
        this.scenario.ressources.splice(index, 1);
99
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete',undefined,undefined,'Ressource_['+index+']','Rules', '#C6C2BD'));
100
        this.minimapService.reset();
101 102
      } else {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_delete',undefined,undefined,'Ressource_['+index+']','Rules', '#C6C2BD'));
103
      }
Romain DELEAU's avatar
Romain DELEAU committed
104
    });
Romain DELEAU's avatar
Romain DELEAU committed
105
  }
106 107 108 109 110 111 112 113

  editTrace(event: any, source: string): void {
    if (event.target.value != '') {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'write',undefined,undefined,source,'Rules', '#C6C2BD'));
    } else {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',undefined,undefined,source,'Rules', '#C6C2BD'));
    }
  }
Romain DELEAU's avatar
Romain DELEAU committed
114
}