Re: Re: Re: Re: Untitled

By Ungracious Prairie Dog, 11 Years ago, written in TCL/iTCL, viewed 509 times. This paste is a reply to Re: Re: Re: Untitled by Mungo Macaw
URL http://pb.stoleyour.com/view/8410cc79 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.         array set serverID [parsePairs "gw2servers.tsv"]
  24. }
  25.  
  26. proc pub:gw2Stats { nick uhost handle channel arg } {
  27.         set urlMatch "http://gw2stats.net/api/matches.json"
  28.         set urlRank "http://gw2stats.net/api/ratings.json"
  29.         set jsonMatch [http::data [http::geturl $urlMatch]]
  30.         set jsonRank [http::data [http::geturl $urlRank]]
  31.         set apiMatch [json::json2dict $jsonMatch]
  32.         set apiRank [json::json2dict $jsonRank]
  33.         set argList [regexp -inline -all -- {\S+} $arg]
  34.         set command [string trim [string tolower [lindex $argList 0]]]
  35.  
  36.         if { $command == "match" } {
  37.                 set region [string trim [string tolower [lindex $argList 1]]]
  38.                 set group [string trim [string tolower [lindex $argList 2]]]
  39.                
  40.                 if { ![dict exists [dict get $apiMatch "region"] $region] } {
  41.                         putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
  42.                         return
  43.                 }
  44.                
  45.                 if { [catch {set worlds [dict get [lindex [dict get [dict get $apiMatch "region"] $region] [expr { $group - 1 }]] "worlds"] } e] } {
  46.                         putserv "PRIVMSG $channel :$nick: Invalid group '$group'!"
  47.                         return
  48.                 }
  49.  
  50.                 set output ""
  51.                 foreach world $worlds {
  52.                         set name [dict get $world "name"]
  53.                         set score [dict get $world "score"]
  54.                         set output "$output$name: $score, "
  55.                 }
  56.                 set output [string range $output 0 end-2]
  57.                
  58.                 putserv "PRIVMSG $channel :$nick: $output"
  59.         }
  60.        
  61.         if { $command == "rank" } {
  62.                 set region [string trim [string tolower [lindex $argList 1]]]
  63.                 set server [string trim [string tolower [lindex $argList 2]]]
  64.                
  65.                 if { ![dict exists $apiRank $region] } {
  66.                         putserv "PRIVMSG $channel :$nick: Invalid region '$region'!"
  67.                         return
  68.                 }
  69.        
  70.        
  71.                 set stats [dict get [dict get [dict get [dict get $apiRank $region] "ratings" ] "1019" ] "data"]
  72.                 set rank [dict get $stats "current_rank"]
  73.                 set rating [dict get $stats "current_rating"]
  74.        
  75.                 putserv "PRIVMSG $channel :$nick: $rank $rating"
  76.         }
  77. }

Replies to Re: Re: Re: Re: Untitled rss

Title Name When
Re: Re: Re: Re: Re: Untitled Tacky Crow 11 Years ago.

Reply to "Re: Re: Re: Re: Untitled"

Here you can reply to the paste above