Warden
Join Date: Jul 2005
Location: The Far Side
Posts: 137
|
Re: mIRC code for Planetarion dumps
Here is some code to parse the dump and handle rankings. Probably not the greatest but it's pretty fast.
Call the parse routines from the sockclose portion of the code above
Code:
;
; parseplanetlisting
;
; Abstract: Called when successful SOCKCLOSE
; it creates a hash table of planet names for later searching
alias -l parseplanetlisting {
var %i = 1, %line, %planet, %ruler, %joint, %tick
var %name = planet_listing
var %dumpname = %name $+ .txt
var %bakname = %name $+ .bak
var %time
%time = $asctime(hh:nn:ss)
echo parseplanetlisting called %time
hfree %name
hmake %name 3000
%line = $read(%dumpname, n, 4)
%tick = $gettok(%line, 2, 32)
hadd %name PATICK.PATICK %tick
; filter dump so it only has planet lines
filter -cffrx 7-5000 %dumpname %bakname *:*
remove %dumpname
%line = $read(%bakname, n, %i)
while (%line != $null) {
; parse line
%planet = $gettok($gettok(%line, 4, 9), 1, 34)
%ruler = $gettok($gettok(%line, 5, 9), 1, 34)
%joint = $removespaces(%ruler of %planet)
%line = %line $+ $chr(9) $+ %joint
hadd %name %joint %line
write %dumpname %line
inc %i
%line = $read(%bakname, n, %i)
}
;also sort to files for rankings
filter -centuff 10 09 %dumpname %name $+ _xp.txt *
filter -centuff 9 09 %dumpname %name $+ _value.txt *
filter -centuff 8 09 %dumpname %name $+ _score.txt *
filter -centuff 7 09 %dumpname %name $+ _size.txt *
hsave -o %name %name $+ .dat
%time = $asctime(hh:nn:ss)
echo planetlisting sorted %time
}
;
; parsegalaxylisting
;
; Abstract: Called when successful SOCKCLOSE
;
alias -l parsegalaxylisting {
var %i = 1, %line
var %name = galaxy_listing
var %dumpname = %name $+ .txt
; filter dump so it only has planet lines
filter -cffrx 7-5000 %dumpname %dumpname *:*
;sort to files for rankings
filter -centuff 7 09 %dumpname %name $+ _xp.txt *
filter -centuff 6 09 %dumpname %name $+ _value.txt *
filter -centuff 5 09 %dumpname %name $+ _score.txt *
filter -centuff 4 09 %dumpname %name $+ _size.txt *
}
Then these sections can be used to lookup by coords (planet or galaxy) or search by name (planet only). The first 2 aliases are called by the user via on text and on open blocks. The replyto argument is either the channel or nick doing the requesting but you can change that easily enough. The 3rd alias is a binary search on the planet listing that's pretty fast.
Code:
;
; static lookup(replyto,
; coords)
;
; Abstract: Searches the planet and galaxy listings for <coords>
;
alias -l lookup {
var %i, %data, %x, %y, %z
var %gfile = galaxy_listing.txt, %replyto = $1
if ($3 != $null) {
%x = $2
%y = $3
%z = $4
}
else {
%x = $gettok($2, 1, 58)
%y = $gettok($2, 2, 58)
%z = $gettok($2, 3, 58)
}
if (%x == $null || %y == $null) {
msg $1 I need coords to look someone up
return
}
if (%z == $null) {
%i = $lines(%gfile)
while (%i > 0) {
%data = $read(%gfile, n, %i)
if (%x == $gettok(%data, 1, 9) && %y == $gettok(%data, 2, 9)) {
displaygalaxy %replyto %data
return
}
dec %i
}
}
else {
%data = $findplanet(%x, %y, %z)
if (%data) {
displayplanet %replyto %data
return
}
}
}
;
; static finder(replyto,
; start,
; search)
;
; Abstract: look for <search> in the planet listing hash table
; send entries <start> -> <start> + 4 to <replyto>
;
alias -l finder {
var %replyto = $1, %search, %n, %display = $3-
var %n2, %tick, %hash = planet_listing
var %reply, %i, %start = $int($2), %end, %more
var %urank, %vrank, %rrank
%tick = $hget(%hash, PATICK.PATICK)
if (%start == $null || %start == 0) {
%start = 1
}
else {
tokenize 32 %display
%display = [ $ $+ 1- [ $+ [ $calc( $0 -1) ] ] ]
}
%search = $removespaces(%display)
%n = $hfind(%hash, $+(*,%search,*), 0, w)
if (%start > %n) {
%start = 1
}
%end = %start + 4
if (%end > %n) %end = %n
if (%n > %end) %more = $true
if ($3 != $null || %n > %end) {
msg %replyto I found %n %display in tick %tick showing %start - %end
}
else {
msg %replyto I found %n %display in tick %tick
}
%i = %start
while (%i <= %end) {
%n2 = $hfind(%hash, $+(*,%search,*), %i, w)
if (%i >= %start) {
displayplanet %replyto $hget(%hash, %n2)
}
inc %i
dec %n
}
if (%more == $true) {
msg %replyto - more matches, use $me find %search $calc(%end + 1)
}
}
;
; static findplanet(x,
; y,
; z)
;
; Abstract: Binary search the planet listing for <x>:<y>:<z>
;
alias -l findplanet {
var %x = $1, %y = $2, %z = $3
var %pmin, %pmid, %pmax, %px, %py, %pz, %data
if ( %x == $null || %y == $null || %z == $null ) {
return $null
}
; binary search planets
%pmin = 1
%pmax = $calc($lines(planet_listing.txt) + 1)
%pmid = $int($calc((%pmin + %pmax) / 2))
%data = $read(planet_listing.txt, n, %pmid)
%px = $gettok(%data, 1, 09)
while (%pmid > %pmin) {
if (%px > %x) {
%pmax = %pmid
}
else if (%px < %x) {
%pmin = %pmid
}
else if (%px == %x) {
%py = $gettok(%data, 2, 09)
if (%py > %y) {
%pmax = %pmid
}
else if (%py < %y) {
%pmin = %pmid
}
else if (%py == %y) {
%pz = $gettok(%data, 3, 09)
if (%pz > %z) {
%pmax = %pmid
}
else if (%pz < %z) {
%pmin = %pmid
}
else if (%pz == %z) {
break
}
}
}
%pmid = $int($calc((%pmin + %pmax) / 2))
%data = $read(planet_listing.txt, n, %pmid)
%px = $gettok(%data, 1, 09)
if ( %pmid == %pmin ) {
%py = $gettok(%data, 2, 09)
%pz = $gettok(%data, 3, 09)
break
}
}
if (%x == %px && %y == %py && %z == %pz) {
return %data
}
}
They use some helping functionality to remove spaces and display the results
Code:
;
; removespaces
;
; Abstract: Remove spaces (character 32) from the arguments and return them concatenated
;
alias -l removespaces {
var %i, %vrb, %line
tokenize 32 $1-
%i = $0
while (%i > 0) {
%vrb = $+($,%i)
%line = $+([ [ %vrb ] ], %line)
dec %i
}
return %line
}
;
; static displayplanet(replyto,
; planet)
;
; Abstract: Tells <replyto> about <planet>
; where <planet> is a line from the planet_listing
;
alias -l displayplanet {
var %replyto = $1, %reply, %urank, %vrank, %rrank
tokenize 9 $2-
%srank = $read(planet_listing_score.txt, nw, $+(*,$11,*))
%srank = $readn
%vrank = $read(planet_listing_value.txt, nw, $+(*,$11,*))
%vrank = $readn
%rrank = $read(planet_listing_size.txt, nw, $+(*,$11,*))
%rrank = $readn
%xrank = $read(planet_listing_xp.txt, nw, $+(*,$11,*))
%xrank = $readn
%reply = $+($1,:,$2,:,$3) $&
$gettok($5, 1, 34) of $gettok($4, 1, 34) $+([,$6,]) $&
Score: $bytes($8, b) / %srank $&
Value: $bytes($9, b) / %vrank $&
Roids: $bytes($7, b) / %rrank $&
XP: $bytes($10, b) / %xrank $&
Ratio: $base($calc(10000 * $7 / $9), 10, 10, 0, 2) $&
v/s: $base($calc(%vrank / %rrank), 10, 10, 0, 2)
msg %replyto %reply
}
;
; static displaygalaxy(replyto,
; galaxy)
;
; Abstract: Tells <replyto> about <galaxy>
; where <galaxy> is a line from the galaxy_listing
;
alias -l displaygalaxy {
var %replyto = $1, %reply
tokenize 9 $2-
%srank = $read(galaxy_listing_score.txt, nw, $+(*,$3,*))
%srank = $readn
%vrank = $read(galaxy_listing_value.txt, nw, $+(*,$3,*))
%vrank = $readn
%rrank = $read(galaxy_listing_size.txt, nw, $+(*,$3,*))
%rrank = $readn
%xrank = $read(galaxy_listing_xp.txt, nw, $+(*,$3,*))
%xrank = $readn
%reply = $+($1,:,$2) $&
$gettok($3, 1, 34) $&
Score: $bytes($5, b) / %srank $&
Value: $bytes($6, b) / %vrank $&
Roids: $bytes($4, b) / %rrank $&
XP: $bytes($7, b) / %xrank $&
Ratio: $base($calc(10000 * $4 / $6), 10, 10, 0, 2) $&
v/s: $base($calc(%vrank / %rrank), 10, 10, 0, 2)
msg %replyto %reply
}
It's a bit messy because it was cobbled together over time as I learnt mirc scripting. Hopefully it's pretty easy to follow although a bit hacky in places. Any suggestions for improvement are welcome as this code is in use (slightly modified).
|