Skip to content

Commit

Permalink
Changed mappings to use PageIterator. Also fixed some misc. bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
badBlackShark committed Apr 28, 2021
1 parent f14f750 commit a575af5
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 62 deletions.
7 changes: 2 additions & 5 deletions src/srcr/mappings/category/category.cr
Expand Up @@ -22,10 +22,7 @@ struct Srcom::Category
end

# Gets all `Run`s completed in this `Category`.
#
# Defaults to getting all of them since there shouldn't be an absurd amount of `Run´s in
# a single `Category`.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
return Srcom::Api::Runs.find_by(category: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
return Srcom::Api::Runs.find_by(category: @id, page_size: page_size)
end
end
4 changes: 2 additions & 2 deletions src/srcr/mappings/developer/developer.cr
Expand Up @@ -9,7 +9,7 @@ struct Srcom::Developer
# Gets all the `Game`s developed by this `Developer`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(developer: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(developer: @id, page_size: page_size)
end
end
4 changes: 2 additions & 2 deletions src/srcr/mappings/engine/engine.cr
Expand Up @@ -9,7 +9,7 @@ struct Srcom::Engine
# Gets all the `Game`s running on this `Engine`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(engine: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(engine: @id, page_size: page_size)
end
end
13 changes: 6 additions & 7 deletions src/srcr/mappings/game/game.cr
Expand Up @@ -52,8 +52,8 @@ struct Srcom::Game
#
# Defaults to getting all of them since there shouldn't be an absurd amount of `Run´s in
# a single `Game`.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
Srcom::Api::Runs.find_by(game: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
Srcom::Api::Runs.find_by(game: @id, page_size: page_size)
end

# Gets every `Leaderboard` with the *top* N runs for this `Game`, skipping over empty
Expand All @@ -71,9 +71,8 @@ struct Srcom::Game
scope : String = "all",
miscellaneous : Bool = true,
skip_empty : Bool = false,
all_pages : Bool = true,
max_results_per_page : Int32 = 200) : Array(Leaderboard)
Srcom::Api::Games.get_records(@id, top, scope, miscellaneous, skip_empty, all_pages, max_results_per_page)
page_size : Int32 = 200) : Srcom::Api::PageIterator(Leaderboard)
Srcom::Api::Games.get_records(@id, top, scope, miscellaneous, skip_empty, page_size)
end

# Gets all the `Series` this `Game` belongs to, if it belongs to any.
Expand Down Expand Up @@ -101,8 +100,8 @@ struct Srcom::Game
end

# Returns all `Game`s derived from this `Game`.
def derived_games(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Game)
return Srcom::Api::Games.get_derived_games(@id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def derived_games(page_size : Int32 = 200) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.get_derived_games(@id, page_size: page_size)
end

# Returns the `Leaderboard` for the `Category` that is first shown when this `Game` is visited
Expand Down
4 changes: 2 additions & 2 deletions src/srcr/mappings/game/gametype.cr
Expand Up @@ -12,7 +12,7 @@ struct Srcom::Game::Gametype
#
# NOTE: Depending on the gametype this request might take quite a while. It also defaults to only 20
# results per page as otherwise it might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(gametype: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(gametype: @id, page_size: page_size)
end
end
20 changes: 8 additions & 12 deletions src/srcr/mappings/game/simple_game.cr
Expand Up @@ -39,8 +39,8 @@ struct Srcom::SimpleGame
end

# Gets the full `Gametype`s applicable to this game.
def full_gametypes : Array(Gametype)
gametypes = Array(Gametype).new
def full_gametypes : Array(Game::Gametype)
gametypes = Array(Game::Gametype).new
@gametypes.each do |id|
gametypes << Srcom::Api::Gametypes.find_by_id(id)
end
Expand Down Expand Up @@ -122,8 +122,8 @@ struct Srcom::SimpleGame
#
# Defaults to getting all of them since there shouldn't be an absurd amount of `Run´s in
# a single `Game`.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
Srcom::Api::Runs.find_by(game: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
Srcom::Api::Runs.find_by(game: @id, page_size: page_size)
end

# Gets all the `Level`s belonging to this game.
Expand All @@ -148,17 +148,13 @@ struct Srcom::SimpleGame
# be returned. If it is set to `true` `Leaderboard`s for both miscellaneous and non-miscellaneous
# categories will be returned.
#
# This method defaults to getting everything, as it is doubtful a single `Game` will have
# truly that many `Leaderboard`s.
#
# NOTE: This can result in more than N runs per `Leaderboard`, as ties can occur.
def records(top : Int32 = 3,
scope : String = "all",
miscellaneous : Bool = true,
skip_empty : Bool = false,
all_pages : Bool = true,
max_results_per_page : Int32 = 200) : Array(Leaderboard)
Srcom::Api::Games.get_records(@id, top, scope, miscellaneous, skip_empty, all_pages, max_results_per_page)
page_size : Int32 = 200) : Srcom::Api::PageIterator(Leaderboard)
Srcom::Api::Games.get_records(@id, top, scope, miscellaneous, skip_empty, page_size)
end

# Gets all the `Series` this `Game` belongs to, if it belongs to any.
Expand Down Expand Up @@ -186,8 +182,8 @@ struct Srcom::SimpleGame
end

# Returns all `Game`s derived from this `Game`.
def derived_games(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Game)
return Srcom::Api::Games.get_derived_games(@id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def derived_games(page_size : Int32 = 200) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.get_derived_games(@id, page_size: page_size)
end

# Returns the `Leaderboard` for the `Category` that is first shown when this `Game` is visited
Expand Down
4 changes: 2 additions & 2 deletions src/srcr/mappings/genre/genre.cr
Expand Up @@ -9,7 +9,7 @@ struct Srcom::Genre
# Gets all the `Game`s belonging to this `Genre`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(genre: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(genre: @id, page_size: page_size)
end
end
7 changes: 2 additions & 5 deletions src/srcr/mappings/level/level.cr
Expand Up @@ -21,10 +21,7 @@ struct Srcom::Level
end

# Gets all `Run`s completed for this `Level`.
#
# Defaults to getting all of them since there shouldn't be an absurd amount of `Run´s for
# a single `Level`.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
return Srcom::Api::Runs.find_by(level: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
return Srcom::Api::Runs.find_by(level: @id, page_size: page_size)
end
end
10 changes: 5 additions & 5 deletions src/srcr/mappings/platform/platform.cr
Expand Up @@ -10,14 +10,14 @@ struct Srcom::Platform
# Gets all the `Game`s playable on this `Platform`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(platform: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(platform: @id, page_size: page_size)
end

# Gets all `Run`s completed while playing on this `Platform`.
#
# NOTE: Depending on the `Platform` this request almost definitely crashes at some point.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
return Srcom::Api::Runs.find_by(platform: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
# NOTE: Depending on the `Platform` trying to get all `Run`s almost definitely crashes at some point.
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
return Srcom::Api::Runs.find_by(platform: @id, page_size: page_size)
end
end
4 changes: 2 additions & 2 deletions src/srcr/mappings/publisher/publisher.cr
Expand Up @@ -9,7 +9,7 @@ struct Srcom::Publisher
# Gets all the `Game`s published by this `Publisher`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(platform: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(platform: @id, page_size: page_size)
end
end
10 changes: 5 additions & 5 deletions src/srcr/mappings/region/region.cr
Expand Up @@ -9,14 +9,14 @@ struct Srcom::Region
# Gets all the `Game`s playable in this `Region`.
#
# NOTE: Defaults to 20 results per page as otherwise the request might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Games.find_by(region: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(region: @id, page_size: page_size)
end

# Gets all `Run`s completed while playing on this `Platform`.
#
# NOTE: Depending on the `Region` this request almost definitely crashes at some point.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
return Srcom::Api::Runs.find_by(region: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
# NOTE: Depending on the `Region` trying to get all `Run`s almost definitely crashes at some point.
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
return Srcom::Api::Runs.find_by(region: @id, page_size: page_size)
end
end
8 changes: 5 additions & 3 deletions src/srcr/mappings/run/simple_run.cr
Expand Up @@ -36,7 +36,9 @@ struct Srcom::SimpleRun
players << Srcom::Api::Users.find_by_id(player.id.not_nil!)
else
# Guests without a name is one of the weird things that can happen, unfortunately.
players << Srcom::Api::Guests.find_by_name(player.name) if player.name
if (name = player.name)
players << Srcom::Api::Guests.find_by_name(name)
end
end
end

Expand All @@ -52,12 +54,12 @@ struct Srcom::SimpleRun
# Gets the full `Region` this run was played in, if provided.
def region : Region?
id = @system.region
return Srcom::Api::Region.find_by_id(id) if id
return Srcom::Api::Regions.find_by_id(id) if id
end

# Gets the full `Game` this `Run` was played in.
def full_game : Game
return Srcom::Api::Games.find_by_id(@game.id)
return Srcom::Api::Games.find_by_id(@game)
end

# Gets the full `Level` this `Run` was played in.
Expand Down
Expand Up @@ -23,7 +23,7 @@ struct Srcom::Series
#
# NOTE: Depending on the series this request might take quite a while. It also defaults to only 20
# results per page as otherwise it might very well 503.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 20) : Array(Game)
return Srcom::Api::Series.get_games(@id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Series.get_games(@id, page_size: page_size)
end
end
4 changes: 2 additions & 2 deletions src/srcr/mappings/user/guest.cr
Expand Up @@ -19,8 +19,8 @@ struct Srcom::Guest
# Gets all `Run`s completed by this `Guest`.
#
# NOTE: As `Guest`s likely don't play too many runs before signing up, this request shouldn't crash.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
name = @name || ""
return Srcom::Api::Runs.find_by(guest: name, all_pages: all_pages, max_results_per_page: max_results_per_page)
return Srcom::Api::Runs.find_by(guest: name, page_size: page_size)
end
end
8 changes: 4 additions & 4 deletions src/srcr/mappings/user/user.cr
Expand Up @@ -29,13 +29,13 @@ struct Srcom::User
# Gets all `Run`s completed by this `User`.
#
# NOTE: Depending on the `User` this request might take a long time or crash.
def runs(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Run)
return Srcom::Api::Runs.find_by(platform: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def runs(page_size : Int32 = 200) : Srcom::Api::PageIterator(Run)
return Srcom::Api::Runs.find_by(platform: @id, page_size: page_size)
end

# Gets all the `Game`s this `User` moderates.
def games(all_pages : Bool = true, max_results_per_page : Int32 = 200) : Array(Game)
return Srcom::Api::Games.find_by(moderator: @id, all_pages: all_pages, max_results_per_page: max_results_per_page)
def games(page_size : Int32 = 20) : Srcom::Api::PageIterator(Game)
return Srcom::Api::Games.find_by(moderator: @id, page_size: page_size)
end

# Gets the personal bests of this `User`.
Expand Down
4 changes: 2 additions & 2 deletions src/srcr/mappings/variable/values/values.cr
@@ -1,9 +1,9 @@
struct Srcom::Variable::Values
include JSON::Serializable

@[Deprecated]
# DEPRECATED: This field is no longer actively used by speedrun.com and only present for legacy purposes
property _note : String?
@[Deprecated]
# DEPRECATED: This field is no longer actively used by speedrun.com and only present for legacy purposes
property choices : Hash(String, String)?
# Value ID => Value
property values : Hash(String, Variable::Values::Value)
Expand Down

0 comments on commit a575af5

Please sign in to comment.