role.ts 4.83 KB
Newer Older
Romain DELEAU's avatar
Romain DELEAU committed
1 2 3 4 5 6
import { Step } from "../step/step";
import { Task } from "../task/task";
import { Ressource } from "../ressource/ressource";
import { SupplementaryRole } from "../supplementary-role/supplementary-role";
import { Reward } from "../rewards/reward";
import { RoleOccurrence } from "../role-occurrence/role-occurrence";
Romain DELEAU's avatar
Romain DELEAU committed
7
import { RoleEducationnalObjective } from "../role-educationnal-objective/role-educationnal-objective";
Romain DELEAU's avatar
Romain DELEAU committed
8
import { Comment } from "../comment/comment";
Romain DELEAU's avatar
Romain DELEAU committed
9 10 11 12 13 14

export class Role {

    intitule: string = '';
    questName: string = '';
    description: string = '';
Romain DELEAU's avatar
Romain DELEAU committed
15
    educationnalObjectives: RoleEducationnalObjective[] = [new RoleEducationnalObjective()];
Romain DELEAU's avatar
Romain DELEAU committed
16 17 18 19 20
    rewards: Reward[] = [];
    stuff: string = '';
    ressources: Ressource[] = [];
    supplementaryRoles: SupplementaryRole[] = [];
    comments: Comment[] = [];
Romain DELEAU's avatar
Romain DELEAU committed
21 22 23
    occurences: RoleOccurrence[] = [new RoleOccurrence()];
    tasks: (Task | null)[][] = [[new Task('normal')], []];
    chronologie: (Step | null)[] = [];
Romain DELEAU's avatar
Romain DELEAU committed
24

Romain DELEAU's avatar
Romain DELEAU committed
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
    public addChronologieStep(index: number) {
        this.chronologie[index] = new Step();
    }

    public removeChronologieStep(index: number) {
        if (this.chronologie.length-1 == index) {
            this.chronologie.splice(index, 1);
        } else {
            this.chronologie[index] = null;
        }
    }

    public addTask(i: number, j: number, type: string) {
        this.tasks[i][j] = new Task(type);
        if (this.tasks[i+1] == null) {
            this.tasks[i+1] = [];
        }
    }

    public removeTask(i: number, j: number) {
        if (this.tasks[i].length-1 == j) {
            this.tasks[i].splice(j, 1);
        } else {
            this.tasks[i][j] = null;
        }
        if (!this.tasks[i].some(element => element instanceof Task)) {
            this.tasks.splice(i,1);
        }
    }

    public moveTask(i: number, j: number, direction: string): void {
        let tmp: Task|null = this.tasks[i][j];
        
        if (direction == 'left') {
            this.tasks[i][j] = this.tasks[i][j-1];
            this.tasks[i][j-1] = tmp;
Romain DELEAU's avatar
Romain DELEAU committed
61
        } else if (direction == 'right') {
Romain DELEAU's avatar
Romain DELEAU committed
62 63
            this.tasks[i][j] = this.tasks[i][j+1];
            this.tasks[i][j+1] = tmp;
Romain DELEAU's avatar
Romain DELEAU committed
64
        } else if (direction == 'top') {
Romain DELEAU's avatar
Romain DELEAU committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
            if (this.tasks[i-1].some(element => element?.type == 'final' || element?.type == 'repeat')) {
                if (this.tasks[i][j]?.type == 'final' || this.tasks[i][j]?.type == 'repeat') {
                    this.tasks[i][j] = this.tasks[i-1][this.getLastTaskIndex(i-1)];
                    this.tasks[i-1][this.getLastTaskIndex(i-1)] = tmp;
                } else {
                    let deplace = this.tasks[i-1][this.getLastTaskIndex(i-1)];
                    this.tasks[i-1][this.getLastTaskIndex(i-1)] = tmp;
                    this.tasks[i-1][this.getLastTaskIndex(i-1)+1] = deplace;
                    this.tasks[i].splice(j, 1);
                }
            } else {
                this.tasks[i-1][this.getLastTaskIndex(i-1)+1] = tmp;
                this.tasks[i].splice(j, 1);
            }
Romain DELEAU's avatar
Romain DELEAU committed
79 80 81 82 83 84 85
            if (!this.tasks[i].some(element => element instanceof Task)) {
                this.tasks.splice(i,1);
            }
        } else if (direction == 'bottom') {
            if (this.tasks[i+2] == null) {
                this.tasks[i+2] = [];
            }
Romain DELEAU's avatar
Romain DELEAU committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
            if (this.tasks[i+1].some(element => element?.type == 'final' || element?.type == 'repeat')) {
                if (this.tasks[i][j]?.type == 'final' || this.tasks[i][j]?.type == 'repeat') {
                    this.tasks[i][j] = this.tasks[i+1][this.getLastTaskIndex(i+1)];
                    this.tasks[i+1][this.getLastTaskIndex(i+1)] = tmp;
                } else {
                    let deplace = this.tasks[i+1][this.getLastTaskIndex(i+1)];
                    this.tasks[i+1][this.getLastTaskIndex(i+1)] = tmp;
                    this.tasks[i+1][this.getLastTaskIndex(i+1)+1] = deplace;
                    this.tasks[i].splice(j, 1);
                }
            } else {
                this.tasks[i+1][this.getLastTaskIndex(i+1)+1] = tmp;
                this.tasks[i].splice(j, 1);
            }
Romain DELEAU's avatar
Romain DELEAU committed
100
        }
Romain DELEAU's avatar
Romain DELEAU committed
101
    }
102 103 104 105 106 107 108 109 110 111 112

    public moveStep(i: number, direction: string): void {
        let tmp: Step|null = this.chronologie[i];
        if (direction == 'left') {
            this.chronologie[i] = this.chronologie[i-1];
            this.chronologie[i-1] = tmp;
        } else if (direction == 'right') {
            this.chronologie[i] = this.chronologie[i+1];
            this.chronologie[i+1] = tmp;
        }
    }
Romain DELEAU's avatar
Romain DELEAU committed
113 114 115 116 117 118 119 120 121 122 123 124 125

    public getLastTaskIndex(i: number): number {
        let index: number;
        if (this.tasks[i].some(element => element instanceof Task)) {
            index = this.tasks[i].length-1 ;
            while (!(this.tasks[i][index] instanceof Task)) {
                index--;
            }
        } else {
            index = 0;
        }
        return index;
    }
Romain DELEAU's avatar
Romain DELEAU committed
126
}