Re: Re: Re: Untitled

By Mungo Macaw, 11 Years ago, written in TCL/iTCL, viewed 461 times. This paste is a reply to Re: Re: Untitled by Diminutive Parakeet
URL http://pb.stoleyour.com/view/3098cf36 Embed
Download Paste or View RawExpand paste to full width of browser
  1. package require http
  2. package require json
  3.  
  4. bind pub - !gw2 pub:gw2Stats
  5.  
  6. namespace eval gw2Utils {
  7.  
  8.         set tsvPath "tsvgw2/"
  9.        
  10.         proc parsePairs { tsvName } {
  11.                 set f [open [file nativename $gw2Utils::tsvPath$tsvName] r]
  12.                 while { [eof $f] == 0 } {
  13.                         set splitLine [split [gets $f] "\t"]
  14.                         set pairMap([lindex $splitLine 0]) [lindex $splitLine 1]
  15.                 }
  16.                 return [array get pairMap]
  17.         }
  18.        
  19.         proc insertCommas { num {sep ,} } {
  20.                 while { [regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num] } {}
  21.                 return $num
  22.         }
  23.  
  24. proc pub:gw2Stats { nick uhost handle channel arg } {
  25.         set urlMatch "http://gw2stats.net/api/matches.json"
  26.         set urlRank "http://gw2stats.net/api/ratings.json"
  27.         set jsonMatch [http::data [http::geturl $urlMatch]]
  28.         set jsonRank [http::data [http::geturl $urlRank]]
  29.         set apiMatch [json::json2dict $jsonMatch]
  30.         set apiRank [json::json2dict $jsonRank]
  31.         set argList [regexp -inline -all -- {\S+} $arg]
  32.         set command [string trim [string tolower [lindex $argList 0]]]
  33.  
  34.         if { $command == "match" } {
  35.                 set region [string trim [string tolower [lindex $argList 1]]]
  36.                 set group [string trim [string tolower [lindex $argList 2]]]
  37.                
  38.                 if { ![dict exists [dict get $apiMatch "region"] $region] } {
  39.                         putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
  40.                         return
  41.                 }
  42.                
  43.                 if { [catch {set worlds [dict get [lindex [dict get [dict get $apiMatch "region"] $region] [expr { $group - 1 }]] "worlds"] } e] } {
  44.                         putserv "PRIVMSG $channel :$nick: Invalid group '$group'!"
  45.                         return
  46.                 }
  47.  
  48.                 set output ""
  49.                 foreach world $worlds {
  50.                         set name [dict get $world "name"]
  51.                         set score [dict get $world "score"]
  52.                         set output "$output$name: $score, "
  53.                 }
  54.                 set output [string range $output 0 end-2]
  55.                
  56.                 putserv "PRIVMSG $channel :$nick: $output"
  57.         }
  58.        
  59.         if { $command == "rank" } {
  60.                 set region [string trim [string tolower [lindex $argList 1]]]
  61.                 set server [string trim [string tolower [lindex $argList 2]]]
  62.                
  63.                 if { ![dict exists $apiRank $region] } {
  64.                         putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
  65.                         return
  66.                 }
  67.        
  68.        
  69.                 set stats [dict get [dict get [dict get [dict get $apiRank $region] "ratings" ] "1019" ] "data"]
  70.                 set rank [dict get $stats "current_rank"]
  71.                 set rating [dict get $stats "current_rating"]
  72.        
  73.                 putserv "PRIVMSG $channel :$nick: $rank $rating"
  74.         }
  75. }

Replies to Re: Re: Re: Untitled rss

Title Name When
Re: Re: Re: Re: Untitled Ungracious Prairie Dog 11 Years ago.

Reply to "Re: Re: Re: Untitled"

Here you can reply to the paste above