Browse Source

Add Side to match, add radiant & dire methods to match

Add get_JSON_any method to client
Move player and side definitions to own files
Change folder structure for better organization
Update README accordingly
Update .travis.yml to only check master branch

Basic definitions & sub-definitions pertaining to
classes are now in their own folders. Much cleaner
this way, and looks better in dota.cr requirements
calls.
master
Andrew Zah 2 years ago
parent
commit
0e21876703
Signed by: Andrew Zah <zah@andrewzah.com> GPG Key ID: 0AE942445EB70FAA

+ 3
- 0
.travis.yml View File

@@ -1,4 +1,7 @@
1 1
 language: crystal
2
+branches:
3
+  only:
4
+  - master
2 5
 script:
3 6
 - crystal deps
4 7
 - crystal spec

+ 28
- 7
README.md View File

@@ -1,5 +1,4 @@
1
-
2
-![Logo](https://sli.mg/IeIxRC.png)
1
+![Logo](http://sli.mg/24VTkn.png)
3 2
 
4 3
 [![Build Status](https://travis-ci.org/azah/dotacr.svg?branch=master)](https://travis-ci.org/azah/dotacr)   [![docs](https://img.shields.io/badge/docs-latest-green.svg)](https://azah.github.io/dotacr/doc/master/)
5 4
 
@@ -39,7 +38,10 @@ Then use the client:
39 38
 
40 39
 ```crystal
41 40
 api = Dota.api
41
+```
42 42
 
43
+## Supported API Methods
44
+```crystal
43 45
 api.hero(13)                            # => (Cached) A single hero - "Puck"
44 46
 api.heroes                              # => (Cached) All heroes
45 47
 
@@ -86,7 +88,7 @@ api.cosmetic_rarities                   # => All cosmetic rarities
86 88
 api.friends(76561198052976237)           # => All friends of user
87 89
 ```
88 90
 
89
-#### Custom Requests
91
+## Custom Requests
90 92
 
91 93
 For the unsupported endpoints, you can use `api.get`. For example, the following code is similar to `api.matches(789645621)` except it will return the raw JSON payload instead of an array of `Dota::Match`es.
92 94
 
@@ -101,6 +103,12 @@ api.get("GetMatchDetails", API::Match, "IDOTA2Match_570", {"match_id" => 7896456
101 103
 * League => LeaguesList
102 104
 * etc.
103 105
 
106
+You can also get the response as `JSON::Any`, with `#get_JSON_any`:
107
+
108
+```crystal
109
+api.get_JSON_any("GetMatchDetails", "IDOTA2Match_570", {"match_id" => 789645621, "api_version" => "v1"})
110
+```
111
+
104 112
 ## API Objects
105 113
 
106 114
 ### Hero
@@ -207,6 +215,8 @@ hero_id       # Int32
207 215
 `Dota::API::MatchesList` has one member: `matches: Array(Dota::API::Match)`.
208 216
 
209 217
 ```crystal
218
+match.radiant                 # Dota::API::Match::Side
219
+match.dire                    # Dota::API::Match::Side
210 220
 match.id                      # Int64
211 221
 match.radiant_win             # Bool
212 222
 match.duration                # Int32, Length of the match, in seconds since the match began
@@ -265,14 +275,25 @@ player.item2_id      # Int16
265 275
 player.item3_id      # Int16
266 276
 player.item4_id      # Int16
267 277
 player.item5_id      # Int16
278
+
279
+# Dota::API::Match::Side
280
+side.score           # Int32
281
+side.barracks_status # Dota::API::MatchStatus::Barracks
282
+side.tower_status    # Dota::API::MatchStatus::Towers
283
+side.team_id         # Int32 | Nil
284
+side.team_name       # String | Nil
285
+side.team_logo       # String | Nil
286
+side.team_complete   # Int32 | Nil
287
+side.picks_bans      # Array(Dota::API::Match::Draft) | Nil
288
+side.players         # Array(Dota::API::Match::Player) | Nil
268 289
 ```
269 290
 
270 291
 #### Live Matches
271 292
 `Dota::API::LiveMatchesList` has one member: `liveMatches: Array(Dota::API::Match)`.
272 293
 
273 294
 ```crystal
274
-live_match.radiant          # Dota::API::LiveMatch::Side, Info about the team on the Radiant side
275
-live_match.dire             # Dota::API::LiveMatch::Side, Info about the team on the Dire side
295
+live_match.radiant          # Dota::API::LiveMatch::Side
296
+live_match.dire             # Dota::API::LiveMatch::Side
276 297
 
277 298
 live_match.id               # Int64
278 299
 live_match.lobby_id         # Int64
@@ -306,7 +327,7 @@ sb.radiant                  # Dota::API::LiveMatch::Side
306 327
 sb.dire                     # Dota::API::LiveMatch::Side
307 328
 ```
308 329
 
309
-### Side (Currently only used by LiveMatch)
330
+### Side
310 331
 ```crystal
311 332
 side.score           # Int32
312 333
 side.tower_state     # Enum, Dota::API::MatchStatus::Towers
@@ -352,7 +373,7 @@ lp.item5_id           # Int16
352 373
 `Dota::API::FriendsList` has one member: `friends: Array(Dota::API::Friend)`.
353 374
 
354 375
 ```crystal
355
-friend.steamid  # String
376
+friend.steamid         # String
356 377
 friend.relationship    # String
357 378
 friend.friend_since    # Int32
358 379
 ```

+ 1
- 1
shard.yml View File

@@ -1,5 +1,5 @@
1 1
 name: dota
2
-version: 0.0.1
2
+version: 0.1.1
3 3
 
4 4
 dependencies:
5 5
   cossack:

+ 3
- 0
src/dota.cr View File

@@ -3,7 +3,10 @@ require "json"
3 3
 
4 4
 require "./dota/utils/mapped"
5 5
 require "./dota/api/status/*"
6
+require "./dota/api/basic/*"
6 7
 require "./dota/api/cosmetic/rarity"
8
+require "./dota/api/match/*"
9
+require "./dota/api/live_match/*"
7 10
 require "./dota/*"
8 11
 require "./dota/api/*"
9 12
 

src/dota/api/basic_match.cr → src/dota/api/basic/basic_match.cr View File


src/dota/api/basic_player.cr → src/dota/api/basic/basic_player.cr View File

@@ -3,7 +3,7 @@ module Dota
3 3
     class BasicPlayer
4 4
       JSON.mapping(
5 5
         account_id: {type: Int64, nilable: true},
6
-        player_slot: Int8,
6
+        player_slot: Int16,
7 7
         hero_id: Int8
8 8
       )
9 9
     end

+ 26
- 0
src/dota/api/client.cr View File

@@ -138,6 +138,32 @@ module Dota
138 138
         raise ResponseException.new(result.error) if result.is_a?(ErrorResponse)
139 139
         result
140 140
       end
141
+
142
+      # For those who want a JSON::Any object to do their bidding with.
143
+      def get_JSON_any(method, interface, params)
144
+        do_request_JSON(method, interface, params)
145
+      end
146
+
147
+      private def do_request_JSON(method, interface = "IDOTA2Match_570", params = {} of String => String)
148
+        method_version = params.delete(:api_version) || configuration.api_version
149
+        url = "https://api.steampowered.com/#{interface}/#{method}/#{method_version}"
150
+        stringParams = {"key" => configuration.api_key.as(String)}
151
+        params.each { |k, v| stringParams[k] = "#{v}" }
152
+
153
+        @cossack = Cossack::Client.new(url)
154
+        response = @cossack.not_nil!.get("", stringParams)
155
+
156
+        if response.status != 200
157
+          case response.status
158
+          when 400...499
159
+            raise ClientErrorException.new("#{response.status}: #{response.body}")
160
+          when 500...599
161
+            raise ServerErrorException.new("#{response.status}: #{response.body}")
162
+          end
163
+        end
164
+
165
+        JSON.parse(response.body)
166
+      end
141 167
     end
142 168
   end
143 169
 end

+ 15
- 68
src/dota/api/live_match.cr View File

@@ -23,8 +23,8 @@ module Dota
23 23
         league_game_id: Int32,
24 24
         stage_name: String,
25 25
         league_tier: League::Tiers,
26
-        dire_team: {type: Team, nilable: true},
27
-        radiant_team: {type: Team, nilable: true},
26
+        dire_team: {type: SimpleTeam, nilable: true},
27
+        radiant_team: {type: SimpleTeam, nilable: true},
28 28
         scoreboard: {type: Scoreboard, nilable: true}
29 29
       )
30 30
 
@@ -36,6 +36,18 @@ module Dota
36 36
         @scoreboard["roshan_respawn_timer"]
37 37
       end
38 38
 
39
+      def radiant
40
+        if scoreboard = @scoreboard
41
+          return scoreboard.radiant
42
+        end
43
+      end
44
+
45
+      def dire
46
+        if scoreboard = @scoreboard
47
+          return scoreboard.dire
48
+        end
49
+      end
50
+
39 51
       class SimplePlayer
40 52
         include Dota::API::PlayerStatus
41 53
         JSON.mapping(
@@ -46,7 +58,7 @@ module Dota
46 58
         )
47 59
       end
48 60
 
49
-      class Team
61
+      class SimpleTeam
50 62
         include Dota::API::Converters
51 63
 
52 64
         JSON.mapping(
@@ -65,71 +77,6 @@ module Dota
65 77
           dire: Side
66 78
         )
67 79
       end
68
-
69
-      class Side
70
-        include Dota::API::MatchStatus
71
-
72
-        class Pick
73
-          JSON.mapping(hero_id: Int32)
74
-        end
75
-
76
-        class Ban
77
-          JSON.mapping(hero_id: Int32)
78
-        end
79
-
80
-        class Ability
81
-          JSON.mapping(
82
-            ability_id: Int32,
83
-            ability_level: Int8
84
-          )
85
-        end
86
-
87
-        JSON.mapping(
88
-          score: Int32,
89
-          tower_state: Towers,
90
-          barracks_state: Barracks,
91
-          picks: {type: Array(Pick), nilable: true},
92
-          bans: {type: Array(Ban), nilable: true},
93
-          players: Array(LivePlayer),
94
-          abilities: {type: Array(Ability), nilable: true}
95
-        )
96
-      end
97
-
98
-      class LivePlayer
99
-        JSON.mapping(
100
-          account_id: Int64,
101
-          player_slot: Int32,
102
-          hero_id: Int32,
103
-          level: Int8,
104
-          kills: Int16,
105
-          deaths: {key: "death", type: Int16},
106
-          assists: Int16,
107
-          last_hits: Int16,
108
-          denies: Int16,
109
-          gold: Int32,
110
-          gold_per_min: Int16,
111
-          xp_per_min: Int32,
112
-          ultimate_state: Int32,
113
-          ultimate_cooldown: Int32,
114
-          respawn_timer: Int32,
115
-          position_x: Float32,
116
-          position_y: Float32,
117
-          net_worth: Int32,
118
-          item0_id: {type: Int16, key: "item0"},
119
-          item1_id: {type: Int16, key: "item1"},
120
-          item2_id: {type: Int16, key: "item2"},
121
-          item3_id: {type: Int16, key: "item3"},
122
-          item4_id: {type: Int16, key: "item4"},
123
-          item5_id: {type: Int16, key: "item5"}
124
-        )
125
-
126
-        def items
127
-          [
128
-            item0_id, item1_id, item2_id,
129
-            item3_id, item4_id, item5_id,
130
-          ].map { |id| Item.new(id.to_i32) }
131
-        end
132
-      end
133 80
     end
134 81
   end
135 82
 end

+ 42
- 0
src/dota/api/live_match/live_player.cr View File

@@ -0,0 +1,42 @@
1
+module Dota
2
+  module API
3
+    class LiveMatch
4
+      class LivePlayer
5
+        JSON.mapping(
6
+          account_id: Int64,
7
+          player_slot: Int32,
8
+          hero_id: Int32,
9
+          level: Int8,
10
+          kills: Int16,
11
+          deaths: {key: "death", type: Int16},
12
+          assists: Int16,
13
+          last_hits: Int16,
14
+          denies: Int16,
15
+          gold: Int32,
16
+          gold_per_min: Int16,
17
+          xp_per_min: Int32,
18
+          ultimate_state: Int32,
19
+          ultimate_cooldown: Int32,
20
+          respawn_timer: Int32,
21
+          position_x: Float32,
22
+          position_y: Float32,
23
+          net_worth: Int32,
24
+          item0_id: {type: Int16, key: "item0"},
25
+          item1_id: {type: Int16, key: "item1"},
26
+          item2_id: {type: Int16, key: "item2"},
27
+          item3_id: {type: Int16, key: "item3"},
28
+          item4_id: {type: Int16, key: "item4"},
29
+          item5_id: {type: Int16, key: "item5"},
30
+          name: {type: String, nilable: true}
31
+        )
32
+
33
+        def items
34
+          [
35
+            item0_id, item1_id, item2_id,
36
+            item3_id, item4_id, item5_id,
37
+          ].map { |id| Item.new(id.to_i32) }
38
+        end
39
+      end
40
+    end
41
+  end
42
+end

+ 34
- 0
src/dota/api/live_match/side.cr View File

@@ -0,0 +1,34 @@
1
+module Dota
2
+  module API
3
+    class LiveMatch
4
+      class Side
5
+        include Dota::API::MatchStatus
6
+
7
+        class Pick
8
+          JSON.mapping(hero_id: Int32)
9
+        end
10
+
11
+        class Ban
12
+          JSON.mapping(hero_id: Int32)
13
+        end
14
+
15
+        class Ability
16
+          JSON.mapping(
17
+            ability_id: Int32,
18
+            ability_level: Int8
19
+          )
20
+        end
21
+
22
+        JSON.mapping(
23
+          score: Int32,
24
+          tower_state: Towers,
25
+          barracks_state: Barracks,
26
+          picks: {type: Array(Pick), nilable: true},
27
+          bans: {type: Array(Ban), nilable: true},
28
+          players: Array(LivePlayer),
29
+          abilities: {type: Array(Ability), nilable: true}
30
+        )
31
+      end
32
+    end
33
+  end
34
+end

+ 14
- 24
src/dota/api/match.cr View File

@@ -46,6 +46,20 @@ module Dota
46 46
         @match_id
47 47
       end
48 48
 
49
+      def radiant
50
+        Side.new(:radiant, @radiant_score, @barracks_status_radiant,
51
+          @tower_status_radiant, @radiant_team_id, @radiant_name,
52
+          @radiant_logo, @radiant_team_complete, @radiant_captain,
53
+          @picks_bans, @players)
54
+      end
55
+
56
+      def dire
57
+        Side.new(:dire, @dire_score, @barracks_status_dire,
58
+          @tower_status_dire, @dire_team_id, @dire_name,
59
+          @dire_logo, @dire_team_complete, @dire_captain,
60
+          @picks_bans, @players)
61
+      end
62
+
49 63
       class Draft
50 64
         JSON.mapping(
51 65
           is_pick: Bool,
@@ -54,30 +68,6 @@ module Dota
54 68
           order: Int8
55 69
         )
56 70
       end
57
-
58
-      class Player < BasicPlayer
59
-        include Dota::API::PlayerStatus
60
-
61
-        JSON.mapping(
62
-          account_id: {type: Int64, nilable: true},
63
-          player_slot: Int8,
64
-          hero_id: Int8,
65
-          kills: Int16,
66
-          deaths: Int16,
67
-          assists: Int16,
68
-          leaver_status: Status,
69
-          last_hits: Int16,
70
-          denies: Int16,
71
-          gold_per_min: Int16,
72
-          xp_per_min: Int16,
73
-          item0_id: {type: Int16, key: "item_0"},
74
-          item1_id: {type: Int16, key: "item_1"},
75
-          item2_id: {type: Int16, key: "item_2"},
76
-          item3_id: {type: Int16, key: "item_3"},
77
-          item4_id: {type: Int16, key: "item_4"},
78
-          item5_id: {type: Int16, key: "item_5"}
79
-        )
80
-      end
81 71
     end
82 72
   end
83 73
 end

+ 29
- 0
src/dota/api/match/player.cr View File

@@ -0,0 +1,29 @@
1
+module Dota
2
+  module API
3
+    class Match
4
+      class Player < BasicPlayer
5
+        include Dota::API::PlayerStatus
6
+
7
+        JSON.mapping(
8
+          account_id: {type: Int64, nilable: true},
9
+          player_slot: Int16,
10
+          hero_id: Int8,
11
+          kills: Int16,
12
+          deaths: Int16,
13
+          assists: Int16,
14
+          leaver_status: Status,
15
+          last_hits: Int16,
16
+          denies: Int16,
17
+          gold_per_min: Int16,
18
+          xp_per_min: Int16,
19
+          item0_id: {type: Int16, key: "item_0"},
20
+          item1_id: {type: Int16, key: "item_1"},
21
+          item2_id: {type: Int16, key: "item_2"},
22
+          item3_id: {type: Int16, key: "item_3"},
23
+          item4_id: {type: Int16, key: "item_4"},
24
+          item5_id: {type: Int16, key: "item_5"}
25
+        )
26
+      end
27
+    end
28
+  end
29
+end

+ 50
- 0
src/dota/api/match/side.cr View File

@@ -0,0 +1,50 @@
1
+module Dota
2
+  module API
3
+    class Match
4
+      class Side
5
+        getter score, barracks_status, tower_status, id, name
6
+        getter logo, complete, captain, picks_bans, players
7
+
8
+        def initialize(
9
+                       side : Symbol,
10
+                       @score : Int32,
11
+                       @barracks_status : Barracks,
12
+                       @tower_status : Towers,
13
+                       @team_id : Int32 | Nil = nil,
14
+                       @team_name : String | Nil = nil,
15
+                       @team_logo : String | Nil = nil,
16
+                       @team_complete : Int32 | Nil = nil,
17
+                       @team_captain : Int32 | Nil = nil,
18
+                       @picks_bans : Array(Draft) | Nil = nil,
19
+                       @players : Array(Player) | Nil = nil)
20
+          @players = sort_players(side)
21
+          @picks_bans = sort_picks_bans(side)
22
+        end
23
+
24
+        def sort_players(side : Symbol)
25
+          if players = @players
26
+            case side
27
+            when :radiant
28
+              players = players.reject { |player| (128..132).covers? player.player_slot }
29
+            when :dire
30
+              players = players.reject { |player| (0..4).covers? player.player_slot }
31
+            end
32
+            players
33
+          end
34
+        end
35
+
36
+        def sort_picks_bans(side : Symbol)
37
+          if picks_bans = @picks_bans
38
+            case side
39
+            when :radiant
40
+              picks_bans = picks_bans.reject { |pb| pb.team != Teams::Radiant }
41
+            when :dire
42
+              picks_bans = picks_bans.reject { |pb| pb.team != Teams::Dire }
43
+            end
44
+            picks_bans
45
+          end
46
+        end
47
+      end
48
+    end
49
+  end
50
+end

+ 1
- 1
src/dota/version.cr View File

@@ -1,3 +1,3 @@
1 1
 module Dota
2
-  VERSION = "0.0.1"
2
+  VERSION = "0.1.1"
3 3
 end

Loading…
Cancel
Save