package require http
package require json
bind pub - !gw2 pub:gw2Stats
namespace eval gw2Utils {
set tsvPath "tsvgw2/"
proc parsePairs { tsvName } {
set f [open [file nativename $gw2Utils::tsvPath$tsvName] r]
while { [eof $f] == 0 } {
set splitLine [split [gets $f] "\t"]
set pairMap([lindex $splitLine 0]) [lindex $splitLine 1]
}
return [array get pairMap]
}
proc insertCommas { num {sep ,} } {
while { [regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num] } {}
return $num
}
proc pub:gw2Stats { nick uhost handle channel arg } {
set urlMatch "http://gw2stats.net/api/matches.json"
set urlRank "http://gw2stats.net/api/ratings.json"
set jsonMatch [http::data [http::geturl $urlMatch]]
set jsonRank [http::data [http::geturl $urlRank]]
set apiMatch [json::json2dict $jsonMatch]
set apiRank [json::json2dict $jsonRank]
set argList [regexp -inline -all -- {\S+} $arg]
set command [string trim [string tolower [lindex $argList 0]]]
if { $command == "match" } {
set region [string trim [string tolower [lindex $argList 1]]]
set group [string trim [string tolower [lindex $argList 2]]]
if { ![dict exists [dict get $apiMatch "region"] $region] } {
putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
return
}
if { [catch {set worlds [dict get [lindex [dict get [dict get $apiMatch "region"] $region] [expr { $group - 1 }]] "worlds"] } e] } {
putserv "PRIVMSG $channel :$nick: Invalid group '$group'!"
return
}
set output ""
foreach world $worlds {
set name [dict get $world "name"]
set score [dict get $world "score"]
set output "$output$name: $score, "
}
set output [string range $output 0 end-2]
putserv "PRIVMSG $channel :$nick: $output"
}
if { $command == "rank" } {
set region [string trim [string tolower [lindex $argList 1]]]
set server [string trim [string tolower [lindex $argList 2]]]
if { ![dict exists $apiRank $region] } {
putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
return
}
set stats [dict get [dict get [dict get [dict get $apiRank $region] "ratings" ] "1019" ] "data"]
set rank [dict get $stats "current_rank"]
set rating [dict get $stats "current_rating"]
putserv "PRIVMSG $channel :$nick: $rank $rating"
}
}