final-task.component.ts 14.6 KB
Newer Older
Romain DELEAU's avatar
Romain DELEAU committed
1 2
import { Component, Input, OnInit } from '@angular/core';
import { Mission } from 'src/app/class/mission/mission';
Romain DELEAU's avatar
Romain DELEAU committed
3 4 5
import { PrerequireRessource } from 'src/app/class/prerequires/prerequire-ressource/prerequire-ressource';
import { PrerequireTask } from 'src/app/class/prerequires/prerequire-task/prerequire-task';
import { Ressource } from 'src/app/class/ressource/ressource';
Romain DELEAU's avatar
Romain DELEAU committed
6
import { Role } from 'src/app/class/role/role';
Romain DELEAU's avatar
Romain DELEAU committed
7
import { Scenario } from 'src/app/class/scenario/scenario';
Romain DELEAU's avatar
Romain DELEAU committed
8
import { Task } from 'src/app/class/task/task';
Romain DELEAU's avatar
Romain DELEAU committed
9
import { PieceDetailsService } from 'src/app/services/piece-details/piece-details.service';
10
import { TooltipService } from 'src/app/services/tooltip/tooltip.service';
11 12 13
import { SuppressDialogComponent } from 'src/app/components/dialogs/suppress-dialog/suppress-dialog.component';
import { MatDialog } from '@angular/material/dialog';
import { CleanDialogComponent } from 'src/app/components/dialogs/clean-dialog/clean-dialog.component';
14 15
import { MatSnackBar } from '@angular/material/snack-bar';
import { IdentifierSnackbarComponent } from 'src/app/components/snackbars/identifier-snackbar/identifier-snackbar.component';
16
import { Trace } from 'src/app/class/trace/trace';
17
import { MinimapService } from 'src/app/services/minimap/minimap.service';
18
import { TranslateService } from '@ngx-translate/core';
19
import { UnityService } from 'src/app/services/unity/unity.service';
Romain DELEAU's avatar
Romain DELEAU committed
20 21 22 23 24 25 26 27 28 29 30 31

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

  displayMenu: string = 'hide';
  displaySymbolChoice: string = 'hide';
  displayPrequires: string = 'hide';

Romain DELEAU's avatar
Romain DELEAU committed
32
  pieceWidth: number = 400;
Romain DELEAU's avatar
Romain DELEAU committed
33

Romain DELEAU's avatar
Romain DELEAU committed
34
  @Input() task: Task = new Task('normal');
Romain DELEAU's avatar
Romain DELEAU committed
35
  @Input() scenario: Scenario = new Scenario();
Romain DELEAU's avatar
Romain DELEAU committed
36
  @Input() mission!: Mission;
37
  @Input() missionIndex: number = 0;
Romain DELEAU's avatar
Romain DELEAU committed
38
  @Input() role!: Role;
39
  @Input() roleIndex: number = 0;
Romain DELEAU's avatar
Romain DELEAU committed
40 41
  @Input() i!: number;
  @Input() j!: number;
Romain DELEAU's avatar
Romain DELEAU committed
42

43
  urlIcon: string = 'url("./assets/background-images/final.png")';
44
  antecedent: boolean = false;
45

46
  constructor(protected pieceDetailsService: PieceDetailsService, protected tooltipService: TooltipService, public dialog: MatDialog,
47
    private _snackBar: MatSnackBar, private minimapService: MinimapService, protected translate: TranslateService, protected unityService: UnityService) { }
Romain DELEAU's avatar
Romain DELEAU committed
48 49

  ngOnInit(): void {
Romain DELEAU's avatar
Romain DELEAU committed
50 51
    this.setPieceWidth();
    this.mission.equalizeLengths();
52
    this.minimapService.reset();
Romain DELEAU's avatar
Romain DELEAU committed
53
  }
Romain DELEAU's avatar
Romain DELEAU committed
54
  
Romain DELEAU's avatar
Romain DELEAU committed
55
  durationChange(): void {
Romain DELEAU's avatar
Romain DELEAU committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    let beforeWidth: number = this.pieceWidth;
    this.setPieceWidth();
    let afterWidth: number = this.pieceWidth;
    let difference: number;
    // Increase
    if (beforeWidth < afterWidth) {
      difference = (afterWidth/beforeWidth)-1;
      for(let k = 0; k < difference; k++) {
        if (!(this.role.tasks[this.i][this.j+k+1] instanceof Task)) {
          this.role.tasks[this.i].splice(this.j+k+1, 1);
        }
      }
    }
    // Decrease
    if (afterWidth < beforeWidth) {
      difference = (beforeWidth/afterWidth)-1
      for (let k = 0; k < difference; k++) {
        this.role.tasks[this.i].splice(this.j+k+1, 0, null);
      }
    }
    this.mission.equalizeLengths();
77
    this.minimapService.reset();
Romain DELEAU's avatar
Romain DELEAU committed
78 79 80
  }

  setPieceWidth(): void {
Romain DELEAU's avatar
Romain DELEAU committed
81 82
    if(this.task.durationUnit === 'UT') {
      if(this.task.duration >= 1 && this.task.duration <= 10) {
Romain DELEAU's avatar
Romain DELEAU committed
83
        this.pieceWidth = (this.task.duration*400);
Romain DELEAU's avatar
Romain DELEAU committed
84
      } else if(this.task.duration > 10) {
Romain DELEAU's avatar
Romain DELEAU committed
85
        this.pieceWidth = 4000;
Romain DELEAU's avatar
Romain DELEAU committed
86
      } else {
Romain DELEAU's avatar
Romain DELEAU committed
87
        this.pieceWidth = 400;
Romain DELEAU's avatar
Romain DELEAU committed
88 89
      }
    } else {
Romain DELEAU's avatar
Romain DELEAU committed
90
      this.pieceWidth = 400;
Romain DELEAU's avatar
Romain DELEAU committed
91 92 93
    }
  }

94
  onClickErase(): void {
95
    const dialogRef = this.dialog.open(CleanDialogComponent, { data: this.translate.instant('finalTask_title') });
96 97 98 99 100 101 102 103
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.task.duration = 1;
        this.task.durationUnit = 'UT';
        this.task.identifier = '';
        this.task.objective = '';
        this.task.symbol.color = '';
        this.task.symbol.symbol = '';
104 105 106 107 108 109 110 111 112 113 114
        this.task.prerequireRessources = [];
        this.task.prerequireTasks = [];   
        this.role.tasks.forEach(inlineTasks => {
          inlineTasks.forEach(task => {
            task?.prerequireTasks.forEach((prerequire, index) => {
              if (prerequire.identifier == this.task.identifier) {
                task.prerequireTasks.splice(index, 1);
              }
            });
          });
        });
115 116 117
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,this.roleIndex,'all','Final_task_['+this.i+';'+this.j+']', '#B28386'));
      } else {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_erase',this.missionIndex,this.roleIndex,'all','Final_task_['+this.i+';'+this.j+']', '#B28386'));
118 119
      }
    });
120
  } 
Romain DELEAU's avatar
Romain DELEAU committed
121

122
  onClickPiece(): void {
Romain DELEAU's avatar
Romain DELEAU committed
123
    this.pieceDetailsService.piece = this.task;
124
    this.pieceDetailsService.parent = this.role;
Romain DELEAU's avatar
Romain DELEAU committed
125 126 127
    this.pieceDetailsService.missionIndex = this.missionIndex;
    this.pieceDetailsService.roleIndex = this.roleIndex;
    this.pieceDetailsService.pieceIndex = [this.i,this.j];
Romain DELEAU's avatar
Romain DELEAU committed
128 129
  }

Romain DELEAU's avatar
Romain DELEAU committed
130 131 132 133 134 135 136
  onClickChange(type: string): void {
    if (type == 'annexe') {
      this.task.symbol.color = '';
      this.task.symbol.symbol = '';
    }
    this.task.changeType(type);
    this.mission.equalizeLengths();
137
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'transform_into_['+type+']',this.missionIndex,this.roleIndex,'all','Final_task_['+this.i+';'+this.j+']', '#B28386'));
138 139 140
  }

  onClickDelete(): void {
141
    const dialogRef = this.dialog.open(SuppressDialogComponent, { data: this.translate.instant('task_prefix')+' '+this.translate.instant('finalTask_title') });
142 143 144 145 146 147 148 149 150 151
    dialogRef.afterClosed().subscribe(result => {
      if (result == true) {
        this.role.tasks.forEach(inlineTasks => {
          inlineTasks.forEach(task => {
            task?.prerequireTasks.forEach((prerequire, index) => {
              if (prerequire.identifier == this.task.identifier) {
                task.prerequireTasks.splice(index, 1);
              }
            });
          });
Romain DELEAU's avatar
Romain DELEAU committed
152
        });
153 154
        this.role.removeTask(this.i, this.j);
        this.mission.equalizeLengths();
155 156 157
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete',this.missionIndex,this.roleIndex,'all','Final_task_['+this.i+';'+this.j+']', '#B28386'));
      } else {
        this.scenario.traces.push(new Trace(this.scenario.traces.length,'cancel_delete',this.missionIndex,this.roleIndex,'all','Final_task_['+this.i+';'+this.j+']', '#B28386'));
158
      }
Romain DELEAU's avatar
Romain DELEAU committed
159
    });
Romain DELEAU's avatar
Romain DELEAU committed
160 161 162 163 164 165 166 167 168 169 170
  }

  changeDisplaySymbolChoice(): void {
    if(this.displaySymbolChoice == 'show') {
      this.displaySymbolChoice = 'hide';
    } else {
      this.displaySymbolChoice = 'show';
    }
  }

  setSymbol(symbol: string, symbolColor: string): void {
Romain DELEAU's avatar
Romain DELEAU committed
171 172
    this.task.symbol.symbol = symbol;
    this.task.symbol.color = symbolColor;
Romain DELEAU's avatar
Romain DELEAU committed
173
    this.displaySymbolChoice = 'hide';
174 175 176 177 178
    if (symbol != '' && symbolColor != '') {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'select_common',this.missionIndex,this.roleIndex,'symbol','Final_task_['+this.i+';'+this.j+']', '#B28386'));
    } else {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete_common',this.missionIndex,this.roleIndex,'symbol','Final_task_['+this.i+';'+this.j+']', '#B28386'));
    }
Romain DELEAU's avatar
Romain DELEAU committed
179
  }
180 181 182 183 184 185 186 187 188 189
  
  canUseSymbol(symbol: string, symbolColor: string): string {
    let res: string = 'enable';
      this.role.tasks.forEach(inlineTasks => {
        if (inlineTasks.some(task => task?.symbol.symbol == symbol && task?.symbol.color == symbolColor)) {
          res = 'disable';
        }
      });
    return res;
  }
Romain DELEAU's avatar
Romain DELEAU committed
190 191 192 193

  changeDisplayPrerequires(): void {
    if(this.displayPrequires == 'show') {
      this.displayPrequires = 'hide';
194
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'hide',this.missionIndex,this.roleIndex,'prerequires','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
195 196
    } else {
      this.displayPrequires = 'show';
197
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'show',this.missionIndex,this.roleIndex,'prerequires','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
198 199 200
    }
  }

Romain DELEAU's avatar
Romain DELEAU committed
201
  moveTask(direction: string): void {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    if (direction == 'left' && this.canMoveTo('left')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    } else if (direction == 'top' && this.canMoveTo('top')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    } else if (direction == 'right' && this.canMoveTo('right')) {
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
Romain DELEAU's avatar
Romain DELEAU committed
220
    } else if (direction == 'bottom') {
221 222 223 224 225 226
      this.role.moveTask(this.i, this.j, direction);
      this.displayMenu = 'hide';
      this.displayPrequires = 'hide';
      this.displaySymbolChoice = 'hide';
      this.mission.equalizeLengths();
    }
Romain DELEAU's avatar
Romain DELEAU committed
227 228 229 230 231 232 233 234
  }

  canMoveTo(direction: string): boolean {
    let res: boolean = true;
    if (direction == 'left') {
      if (this.role.tasks[this.i][this.j-1] instanceof Task || this.j == 0) {
        res = false;
      }
Romain DELEAU's avatar
Romain DELEAU committed
235 236 237 238
    } else if (direction == 'top') {
      if (this.i == 0) {
        res = false;
      }
Romain DELEAU's avatar
Romain DELEAU committed
239 240 241 242
    }
    return res;
  }

243 244 245 246 247 248 249 250 251 252
  findFirstIndexOfTaskType(type: string): number[] {
    for(let i = 0; i < this.role.tasks.length; i++) {
      for(let j = 0; j < this.role.tasks[i].length; j++) {
        if (this.role.tasks[i][j] instanceof Task && this.role.tasks[i][j]?.type == type) {
          return [i, j];
        }
      }
    }
    return [0, 0];
  } 
Romain DELEAU's avatar
Romain DELEAU committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

  changeIdentifier(event: any): void {
    let value: string = event.target.value;
    if (value != '') {
      this.role.tasks.forEach(inlineTasks => {
        inlineTasks.forEach(task => {
          task?.prerequireTasks.forEach(prerequire => {
            if (prerequire.identifier == this.task.identifier) {
              prerequire.identifier = value;
            }
          });
        });
      });
    } else {
      this.role.tasks.forEach(inlineTasks => {
        inlineTasks.forEach(task => {
          task?.prerequireTasks.forEach((prerequire, index) => {
            if (prerequire.identifier == this.task.identifier) {
              task.prerequireTasks.splice(index, 1);
            }
          });
        });
      }); 
    }
277
    if (this.role.isAlreadyUsedIdentifier(this.task.identifier)) {
278 279
      this._snackBar.openFromComponent(IdentifierSnackbarComponent, { duration: 5000 });
      this.task.identifier = '';  
280
    }
281
    this.editTrace(event,'Task_identifier');
Romain DELEAU's avatar
Romain DELEAU committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  }

  checkboxChangedTask(event: any, task:(Task|null)): void {
    if (task instanceof Task) {
      if (event.target.checked) {
        this.onCheckTask(task);
      } else {
        this.onUncheckTask(task);
      }      
    }
  }

  isCheckedTask(task: (Task|null)): boolean {
    if (task instanceof Task) {
      return this.task.prerequireTasks.some(element => element.identifier == task.identifier);
    }
    return false;
  }

  onCheckTask(task: Task): void {
    this.task.prerequireTasks.push(new PrerequireTask(task.identifier));
303
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'new',this.missionIndex,this.roleIndex,'prerequire_task','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
304 305 306 307 308
  }

  onUncheckTask(task: Task): void {
    let i: number = this.task.prerequireTasks.findIndex(element => element.identifier == task.identifier);
    this.task.prerequireTasks.splice(i,1);
309
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete',this.missionIndex,this.roleIndex,'prerequire_task','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  }

  checkboxChangedRessource(event: any, ressource: Ressource): void {
    if (event.target.checked) {
      this.onCheckRessource(ressource);
    } else {
      this.onUncheckRessource(ressource);
    }
  }

  isCheckedRessource(ressource: Ressource): boolean {
    return this.task.prerequireRessources.some(element => element.ressource == ressource);
  }

  onCheckRessource(ressource: Ressource): void {
    this.task.prerequireRessources.push(new PrerequireRessource(ressource));
326
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'new',this.missionIndex,this.roleIndex,'prerequire_ressource','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
327 328 329 330 331
  }

  onUncheckRessource(ressource: Ressource): void {
    let i: number = this.task.prerequireRessources.findIndex(element => ressource == element.ressource);
    this.task.prerequireRessources.splice(i, 1);
332
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'delete',this.missionIndex,this.roleIndex,'prerequire_ressource','Final_task_['+this.i+';'+this.j+']', '#B28386'));
Romain DELEAU's avatar
Romain DELEAU committed
333 334 335 336 337 338
  }

  getAssociatePrerequireRessource(ressource: Ressource): PrerequireRessource {
    let i: number = this.task.prerequireRessources.findIndex(element => ressource == element.ressource);
    return this.task.prerequireRessources[i];
  }
339 340 341 342
  
  hasPossibleAntecedents(): boolean {
    let res = false;
    this.role.tasks.forEach(inlineTask => {
343
      for(let i = 0; i < inlineTask.length; i++) {
344 345 346 347 348 349 350
        if (inlineTask[i]?.identifier && (this.task.identifier != inlineTask[i]?.identifier)) {
          res = true;
        }
      }
    });
    return res;
  }
351 352 353 354 355 356 357 358 359 360 361 362
  
  editTrace(event: any, source: string): void {
    if (event.target.value != '') {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'write',this.missionIndex,this.roleIndex,source,'Final_task_['+this.i+';'+this.j+']', '#B28386'));
    } else {
      this.scenario.traces.push(new Trace(this.scenario.traces.length,'erase',this.missionIndex,this.roleIndex,source,'Final_task_['+this.i+';'+this.j+']', '#B28386'));
    }
  }

  editMoveTrace(event: any, source: string): void {
    this.scenario.traces.push(new Trace(this.scenario.traces.length,'move',this.missionIndex,this.roleIndex,source,'Final_task_['+this.i+';'+this.j+']', '#B28386'));
  }
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  
  addCombineObject(): void {
    this.task.combineObjects.push([null,1]);
  }

  removeCombineObject(index: number): void {
    this.task.combineObjects.splice(index,1);
  }

  addExchangeGiveObject(): void {
    this.task.giveObjects.push([null,1]);
  }

  removeExchangeGiveObject(index: number): void {
    this.task.giveObjects.splice(index,1);
  }

  addExchangeReceiveObject(): void {
    this.task.receiveObjects.push([null,1]);
  }

  removeExchangeReceiveObject(index: number): void {
    this.task.receiveObjects.splice(index,1);
  }
Romain DELEAU's avatar
Romain DELEAU committed
387
}