Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
FreeApp2014
committed
Apr 22, 2020
1 parent
152ac1e
commit aeae699
Showing
5 changed files
with
205 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Created by freeapp on 20.04.2020. | ||
// | ||
|
||
import Foundation | ||
import SwiftyJSON | ||
|
||
//Represents a game object on SCM, typically received from the game endpoint | ||
public struct Game { | ||
public let id: String, title: String, songCount: UInt; | ||
public var songs: [PartialSongField]; | ||
public init(_ id: String) throws { | ||
self.id = id; | ||
guard let e = performGetRequest(url: "https://smashcustommusic.net/json/game/\(self.id)") else { | ||
throw SCMError.httpRequestError | ||
}; | ||
let json = JSON(data: e.1); | ||
guard let state = json["ok"].bool else { | ||
throw SCMError.otherApiError | ||
} | ||
if (state != true){ | ||
if (json["error"].string! == "404") { | ||
throw SCMError.objectNotFoundError | ||
} else { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
self.title = json["game_name"].string!; | ||
self.songs = []; | ||
self.songCount = UInt(json["track_count"].int!); | ||
guard let songs = json["songs"].array else {return} | ||
for song in songs{ | ||
let songo = PartialSongField(id: String(song["song_id"].int!), | ||
title: song["song_name"].string!, | ||
secLength: UInt(song["song_length"].string!)!, | ||
uploader: song["song_uploader"].string!, | ||
canDownload: song["song_available"].int! == 1, | ||
downloadCount: UInt(song["song_downloads"].string!)!); | ||
self.songs.append(songo); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
//The struct for game information received from the song endpoint | ||
public struct PartialGameField{ | ||
public let id: String, title: String; | ||
public func resolveGame() throws -> Game{ //Get full Game object for this PartialGame | ||
do{ | ||
let e = try Game(self.id); | ||
return e; | ||
} catch SCMError.httpRequestError { | ||
throw SCMError.httpRequestError | ||
} catch SCMError.objectNotFoundError { | ||
throw SCMError.objectNotFoundError | ||
} catch SCMError.otherApiError { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
} | ||
|
||
//The struct for game information from gamelist | ||
public struct GameListGameField { | ||
public let id: String, title: String, songCount: UInt; | ||
public func resolveGame() throws -> Game{ //Get full Game object for this PartialGame | ||
do{ | ||
let e = try Game(self.id); | ||
return e; | ||
} catch SCMError.httpRequestError { | ||
throw SCMError.httpRequestError | ||
} catch SCMError.objectNotFoundError { | ||
throw SCMError.objectNotFoundError | ||
} catch SCMError.otherApiError { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Created by freeapp on 20.04.2020. | ||
// | ||
import Foundation | ||
import SwiftyJSON | ||
|
||
//Used for supplying Song object with information | ||
public struct SongLoopInformation{ | ||
public var doesLoop: Bool, loopStart: UInt, loopTypeDesc: String; | ||
} | ||
|
||
//Enum containing file types that can be downloaded from the API | ||
public enum SongFileType:String { | ||
case brstm = "brstm", wav = "wav", bwav = "bwav"; | ||
} | ||
|
||
//The struct for song information received from the game endpoint | ||
public struct PartialSongField{ | ||
public let id: String, title: String, secLength: UInt, uploader: String, canDownload: Bool, downloadCount: UInt; | ||
public func resolveSong() throws -> Song{ //Get full Song object for this PartialSong | ||
do { | ||
let e = try Song(self.id); | ||
return e; | ||
} catch SCMError.httpRequestError { | ||
throw SCMError.httpRequestError | ||
} catch SCMError.objectNotFoundError { | ||
throw SCMError.objectNotFoundError | ||
} catch SCMError.otherApiError { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
} | ||
//The struct for song information received from the game endpoint | ||
public struct SearchSongField{ | ||
public let id: String, title: String, secLength: UInt, uploader: String, canDownload: Bool, downloadCount: UInt, gameId: String; | ||
public func resolveSong() throws -> Song{ //Get full Song object for this PartialSong | ||
do { | ||
let e = try Song(self.id); | ||
return e; | ||
} catch SCMError.httpRequestError { | ||
throw SCMError.httpRequestError | ||
} catch SCMError.objectNotFoundError { | ||
throw SCMError.objectNotFoundError | ||
} catch SCMError.otherApiError { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
} | ||
|
||
//Represents a song object on SCM, typically received from the song endpoint | ||
public struct Song { | ||
public let id: String, title: String, uploader: String, secLength: UInt, sampleRate: UInt, loop: SongLoopInformation, canDownload: Bool, downloadCount: UInt, game: PartialGameField; | ||
public init (_ id: String) throws { | ||
self.id = id; | ||
guard let e = performGetRequest(url: "https://smashcustommusic.net/json/song/\(self.id)") else { | ||
throw SCMError.httpRequestError | ||
}; | ||
let json = JSON(data: e.1); | ||
guard let state = json["ok"].bool else { | ||
throw SCMError.otherApiError | ||
} | ||
if (state != true){ | ||
if (json["error"].string! == "404") { | ||
throw SCMError.objectNotFoundError | ||
} else { | ||
throw SCMError.otherApiError | ||
} | ||
} | ||
self.title = json["name"].string!; | ||
self.uploader = json["uploader"].string!; | ||
self.canDownload = json["available"].int! == 1; | ||
self.sampleRate = UInt(json["sample_rate"].string!)!; | ||
self.game = PartialGameField(id: String(json["game_id"].int!), title: json["game_name"].string!); | ||
self.loop = SongLoopInformation(doesLoop: json["loop_type"].string! != "None", loopStart: UInt(json["start_loop_point"].string!)!, loopTypeDesc: json["loop_type"].string!); | ||
self.secLength = UInt(json["length"].string!)!; | ||
self.downloadCount = UInt(json["downloads"].string!)!; | ||
} | ||
public func download(inFormat format: SongFileType) -> Data?{ //Download the file in specified file type | ||
guard let data = performGetRequest(url: "https://smashcustommusic.net/\(format)/\(self.id)") else { | ||
return nil | ||
} | ||
return data.1; | ||
} | ||
} |