Henrybk

portals.txt JSON converter

Mar 4th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.38 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use utf8;
  4. use strict;
  5. use warnings;
  6. use JSON;
  7.  
  8. open IN, "<:utf8", "portals.txt" or die "Are you sure that portals.txt exists? Internal error: ".$!;
  9. open OUT, ">:utf8", "portalsJSON.txt" or die $!;
  10.  
  11.  
  12. my @portals;
  13. while (<IN>) {
  14.     next if $_ =~ /^#/;
  15.     $_ =~ s/\cM|\cJ//g;
  16.     $_ =~ s/\s+/ /g;
  17.     $_ =~ s/^\s+|\s+$//g;
  18.     $_ =~ s/(.*)[\s\t]+#.*$/$1/;
  19.    
  20.     if ($_ =~ /^([\w|@|-]+)\s(\d{1,3})\s(\d{1,3})\s([\w|@|-]+)\s(\d{1,3})\s(\d{1,3})\s?(.*)/) {
  21.         my %portal;
  22.         my ($source_map, $source_x, $source_y, $dest_map, $dest_x, $dest_y, $misc) = ($1, $2, $3, $4, $5, $6, $7);
  23.         $portal{'source'}{'map'} = $source_map;
  24.         $portal{'source'}{'x'} = $source_x;
  25.         $portal{'source'}{'y'} = $source_y;
  26.         $portal{'destination'}{'map'} = $dest_map;
  27.         $portal{'destination'}{'x'} = $dest_x;
  28.         $portal{'destination'}{'y'} = $dest_y;
  29.         if ($misc) {
  30.             if ($misc =~ /^(\d+)\s(\d)\s(.*)$/) { # [cost] [allow_ticket] [talk sequence]
  31.                 $portal{'cost'} = $1;
  32.                 $portal{'allow_ticket'} = $2;
  33.                 $portal{'steps'} = $3;
  34.             } elsif ($misc =~ /^(\d+)\s(.*)$/) { # [cost] [talk sequence]
  35.                 $portal{'cost'} = $1;
  36.                 $portal{'steps'} = $2;
  37.             } else { # [talk sequence]
  38.                 $portal{'steps'} = $misc;
  39.             }
  40.         }
  41.         push @portals, \%portal;
  42.     }
  43. }
  44.  
  45. print OUT to_json(\@portals, {utf8 => 1, pretty => 1, canonical => 1});
  46.  
  47.  
  48. close(IN);
  49. close(OUT);
  50.  
  51. print "Finished !\n";
  52.  
  53. system("pause");
Advertisement
Add Comment
Please, Sign In to add comment