1 module pubg.player;
2 
3 import pubg.request;
4 import std.json;
5 import std.file: write;
6 import std.stdio: writeln;
7 
8 class PlayerAttributes
9 {
10 public:
11     this(JSONValue json)
12     {
13         this.json = json;
14     }
15     string getCreationDate()
16     {
17         return this.json["createdAt"].str;
18     }
19     string getName()
20     {
21         return this.json["name"].str;
22     }
23     string getPatchVersion()
24     {
25         return this.json["patchVersion"].str;
26     }
27     string getShardId()
28     {
29         return this.json["shardId"].str;
30     }
31     string getTitleId()
32     {
33         return this.json["titleId"].str;
34     }
35     string getUpdateDate()
36     {
37         return this.json["updatedAt"].str;
38     }
39 private:
40     JSONValue json;
41 }
42 
43 class PlayerRelationships
44 {
45 public:
46     this(JSONValue json)
47     {
48         this.json = json;
49     }
50     string[] getMatchIds()
51     {
52         string[] matches;
53         auto data = this.json["matches"]["data"].array;
54         foreach (j; data)
55         {
56             matches ~= j["id"].str;
57         }
58         return matches;
59     }
60 private:
61     JSONValue json;
62 }
63 
64 class Player
65 {
66 public:
67     this(char[] content)
68     {
69         this.json = parseJSON(cast(string)content);
70     }
71     PlayerAttributes getAttributes()
72     {
73         return new PlayerAttributes(this.json["data"].array[0]["attributes"]);
74     }
75     PlayerRelationships getRelationships()
76     {
77         return new PlayerRelationships(this.json["data"].array[0]["relationships"]);
78     }
79     string getType()
80     {
81         return this.json["data"].array[0]["type"].str;
82     }
83     string getId()
84     {
85         return this.json["data"].array[0]["id"].str;
86     }
87     GameModeStats getGameModeStats(string gamemode)
88     {
89         return new GameModeStats(this.json["data"]["attributes"]["gameModeStats"][gamemode]);
90     }
91 private:
92     JSONValue json;
93 }
94 
95 class GameModeStats
96 {
97 public:
98     this(JSONValue json)
99     {
100         this.json = json;
101     }
102     int getDBNOs()
103     {
104         return cast(int)this.json["DBNOs"].integer;
105     }
106     int getAssists()
107     {
108         return cast(int)this.json["assists"].integer;
109     }
110     int getBoosts()
111     {
112         return cast(int)this.json["boosts"].integer;
113     }
114     int getDamageDealt()
115     {
116         return cast(int)this.json["damageDealt"].integer;
117     }
118     string getDeathType()
119     {
120         return this.json["deathType"].str;
121     }
122     int getHeadshotKills()
123     {
124         return cast(int)this.json["headshotKills"].integer;
125     }
126     int getHeals()
127     {
128         return cast(int)this.json["heals"].integer;
129     }
130     int getKillPlace()
131     {
132         return cast(int)this.json["killPlace"].integer;
133     }
134     int getKillPoints()
135     {
136         return cast(int)this.json["killPoints"].integer;
137     }
138     float getKillPointsDelta()
139     {
140         return cast(float)this.json["killPointsDelta"].floating;
141     }
142     int getKillStreaks()
143     {
144         return cast(int)this.json["killPoints"].integer;
145     }
146     int getKills()
147     {
148         return cast(int)this.json["kills"].integer;
149     }
150     int getLastKillPoints()
151     {
152         return cast(int)this.json["lastKillPoints"].integer;
153     }
154     int getLastWinPoints()
155     {
156         return cast(int)this.json["lastWinPoints"].integer;
157     }
158     int getLongestKill()
159     {
160         return cast(int)this.json["longestKill"].integer;
161     }
162     int getMostDamage()
163     {
164         return cast(int)this.json["longestKill"].integer;
165     }
166     string getName()
167     {
168         return this.json["name"].str;
169     }
170     string getPlayerId()
171     {
172         return this.json["playerId"].str;
173     }
174     int getRevives()
175     {
176         return cast(int)this.json["revives"].integer;
177     }
178     int getRideDistance()
179     {
180         return cast(int)this.json["rideDistance"].integer;
181     }
182     int getRoadKills()
183     {
184         return cast(int)this.json["roadKills"].integer;
185     }
186     int getTeamKills()
187     {
188         return cast(int)this.json["teamKills"].integer;
189     }
190     int getTimeSurvived()
191     {
192         return cast(int)this.json["timeSurvived"].integer;
193     }
194     int getVehicleDestroys()
195     {
196         return cast(int)this.json["vehicleDestroys"].integer;
197     }
198     float getWalkDistance()
199     {
200         return cast(float)this.json["walkDistance"].floating;
201     }
202     int getWeaponsAcquired()
203     {
204         return cast(int)this.json["weaponsAcquired"].integer;
205     }
206     int getWinPlace()
207     {
208         return cast(int)this.json["winPlace"].integer;
209     }
210     int getWinPoints()
211     {
212         return cast(int)this.json["winPoints"].integer;
213     }
214     float getWinPointsDelta()
215     {
216         return cast(float)this.json["winPointsDelta"].floating;
217     }
218     int getWins()
219     {
220         return cast(int)this.json["wins"].integer;
221     }
222     int getLosses()
223     {
224         return cast(int)this.json["losses"].integer;
225     }
226 private:
227     JSONValue json;
228 }
229 
230 class PlayerRequest : ObjectRequest 
231 {
232 public:
233     this(string region)
234     {
235         super("https://api.playbattlegrounds.com/shards/" ~ region ~ "/");
236     }
237     Player getPlayerFromName(string name)
238     {
239         return new Player(this.request("players?filter[playerNames]=" ~ name));
240     }
241     Player getPlayerFromId(string id)
242     {
243         return new Player(this.request("players?filter[playerIds]=" ~ id));
244     }
245     Player getExtendedPlayer(string id, string season)
246     {
247         auto content = this.request("players/" ~ id ~ "/seasons/" ~ season);
248         //auto seasons = this.request("seasons");
249         //writeln(cast(string)seasons);
250         //write("ext.json", parseJSON(cast(string)content).toPrettyString());
251         return new Player(content);
252     }
253 }