aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/me/brysonsteck/wiimmfiwatcher/wiimmfi/RoomData.java
blob: 64afedcf637d1b2586e1ee4375815945470065a2 (plain)
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
package me.brysonsteck.wiimmfiwatcher.wiimmfi;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class RoomData {
    String roomHeader;
    String playerLink;
    String friendCode;
    ArrayList<Player> players = new ArrayList<>();

    public RoomData (ArrayList<Player> players, String friendCode) {
        this.friendCode = friendCode;
        getPlayerLink();
        Document doc = null;

        if (this.playerLink == null) {
            System.out.println("The player link is null for some reason");
        } else {

            try {
                doc = Jsoup.connect("https://wiimmfi.de/" + this.playerLink)
                        .userAgent("Wiimmfi Watcher for Android (https://github.com/brysonsteck/wiimmfi-watcher) (UNDER DEVELOPMENT)")
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Element table = doc.select("table").get(0);
            Elements rows = table.select("tr");

            for (int i = 0; i < rows.size(); i++) {
                Element row = rows.get(i);
                Elements colPlayers = row.select("td");
                Elements colHeader = row.select("th");

                if (colHeader.size() > 0) {
                    if (!colHeader.get(0).text().equals("friend code")) {
                        roomHeader = colHeader.get(0).text();
                    }
                }
                if (colPlayers.size() > 0) {

                    Player currentPlayer = new Player();
                    for (int j = 0; j < colPlayers.size(); j++) {
                        System.out.println("Player Data: " + colPlayers.get(j).text());
                        switch (j) {
                            case 0:
                                currentPlayer.friendCode = colPlayers.get(0).text();
                                break;
                            case 1:
                                currentPlayer.role = colPlayers.get(1).text().split(" ")[1];
                                currentPlayer.rosterNumber = colPlayers.get(1).text().split(" ")[0].replaceAll("\\s", "");
                                break;
                            case 2:
                                currentPlayer.loginRegion = colPlayers.get(2).text();
                                break;
                            case 3:
                                currentPlayer.roomMatch = colPlayers.get(3).text();
                                break;
                            case 4:
                                currentPlayer.world = colPlayers.get(4).text();
                                break;
                            case 5:
                                currentPlayer.connFail = colPlayers.get(5).text();
                                break;
                            case 6:
                                currentPlayer.vr = colPlayers.get(6).text();
                                break;
                            case 7:
                                currentPlayer.br = colPlayers.get(7).text();
                                break;
                            case 8:
                                currentPlayer.miiName = colPlayers.get(8).text();
                                break;
                        }
                    }
                    if (currentPlayer.friendCode.equals(friendCode)) {
                        currentPlayer.watching = true;
                    }
                    players.add(currentPlayer);
                }

            }
        }
    }

    public void getPlayerLink() {
        try {
            Document doc = Jsoup.connect("https://wiimmfi.de/stats/mkw")
                    .userAgent("Wiimmfi Watcher for Android (https://github.com/brysonsteck/wiimmfi-watcher)")
                    .get();
            Element table = doc.select("table").get(0);
            Elements rows = table.select("tr");

            for (int i = 0; i < rows.size(); i++) {
                Element row = rows.get(i);
                Elements colPlayers = row.select("td");


                if (colPlayers.size() > 0) {

                    String data = colPlayers.get(0).select("a").toString();
                    if (data.contains(friendCode)) {
                        System.out.println("Found friend code");
                        playerLink = data.split("\"")[3];
                        System.out.println("Player link: " + playerLink);
                        break;
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public ArrayList<Player> getPlayers() { return players; }

    public String getRoomHeader() { return this.roomHeader; }

    public RoomData refresh() {
        players.clear();
        roomHeader = "";
        return new RoomData(players, friendCode);
    }
}