aboutsummaryrefslogtreecommitdiff
path: root/player.mjs
blob: 5d8f16a74735ef02de201fa63d7537589c835b1c (plain) (blame)
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
import { Manager } from "./utils.mjs";

class Player {
    constructor(id, name, level, status, totalPlayed) {
        this.id = id;
        this.name = name;
        this.level = typeof level !== 'undefined' ? level : 1;
        this.status = typeof status !== 'undefined' ? status : false;
        this.totalPlayed = typeof totalPlayed !== 'undefined' ? status : 0;
    }
    toggle() {
        this.status = !this.status;
        this.print();
    }
    print() {
        let status = this.status ? "active" : "inactive";
        console.log(this.name + " has a level of " + this.level + " and is currently " + status);
    }
    render() {
        let type = this.status ? `class="button is-success"` : 'class="button is-danger"';
        return `<button id=${"regular_" + this.id} ${type}>${this.name}</button>`;
    }
}

function randomLevel() {
    const max_level = 10;
    return Math.floor(Math.random() * max_level + 1);
}

function randomStatus() {
    return Math.random() < 0.5;
}

function parseLineToPlayer(data) {
    var info = data.split(",");
    var id = Number(info[0].trim());
    var name = info[1].trim() + " " + info[2].trim();
    var level = Number(info[3].trim());
    return new Player(id, name, level, true, 0);
}

export class PlayerManager extends Manager {
    constructor() {
        super("regular", []);
        console.log("Loading player data.");
        let data = JSON.parse(localStorage.getItem(this.description));
        if (data === null || data.length === 0) {
            console.log("Player data unavailable: " + data);
            this.generate();
            return;
        }
        for (let i = 0; i < data.length; i++) {
            var id = Number(data[i].id);
            var name = data[i].name;
            var level = data[i].level;
            var status = data[i].status;
            var totalPlayed = data[i].totalPlayed;
            this.listData.push(new Player(id, name, level, status, totalPlayed));
        }
        console.log("Loaded " + this.listData.length + " players.");
        this.render();
    }
    generate() {
        console.log("Generating player data.");
        let maxPlayerCount = 71;
        for (let i = 0; i < maxPlayerCount; i++) {
            let p = new Player(i, "Player " + i, randomLevel(), randomStatus(), 0);
            this.listData.push(p);
        }
        console.log("Generated data for " + this.listData.length + " players");
        this.store();
        this.render();
    }
    import(data) {
        var lines = data.split("\n");
        var players = [];
        for (var i = 1; i < lines.length; i++) {
            if(lines[i] == undefined || lines[i].trim() == "") {continue;}
            players.push(parseLineToPlayer(lines[i]));
        }
        console.log("Loaded " + players.length + " Players");
        for (var i=0; i<players.length; i++) {players[i].print();}
        this.listData = players;
        this.store();
        this.render();
    }
}

// Try to add this functionality inside the class to call after constructor
export function addLoadBehaviour(p) {
    const f = document.querySelector("#player-load input[type=file]");
    f.addEventListener("change", () => {
        const file = f.files[0];
        let importData;
        console.log("Loading data from "+ file.name);
        const reader = new FileReader();
        reader.onload = function(e) {
            importData = reader.result;
            p.import(importData);
        };
        reader.readAsText(file);
    })
    
}