summaryrefslogtreecommitdiffhomepage
path: root/contrib/very-important-source-code/make-csv.pl
blob: 552edea71b33133f5c8d6dcf638b6d4bf56c3131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env perl

use strict;
use List::Util qw'reduce';

use POSIX qw(locale_h);
setlocale(LC_ALL, "C");

sub get_games_1
{
    my @games;

    open my $fd, "<", $ARGV[1] or die "open: $!";
    <$fd>;

    while (defined(my $line = <$fd>))
    {
        chomp $line;
        if ($line !~ /^(\d+)\s+"([^"]+)"(?:\s+\(([0-9A-F]{16})\))?$/)
        {
            warn "Broken line";
            next;
        }
        push @games, +{ id => $1, name => $2, key => defined $3 ? (sprintf "%04X", $1) . $3 . '00' : undef};
    }

    [@games];
}

sub get_games_2
{
    open my $fd, "<", $ARGV[0] or die "open: $!";
    <$fd>;
    my @games;
    while (defined(my $line = <$fd>))
    {
        chomp $line;
        my @line = split/;/, $line;
        if (@line != 8)
        {
            warn "Broken line";
            next;
        }
        my @cols = qw'no name proto since verified by id key';
        push @games, +{ map { $cols[$_] => $line[$_] } 0..$#cols };
    }
    [@games];
}

sub merge
{
    my ($new_games, $old_games) = @_;
    my $no = (reduce { $a->{no} > $b->{no} ? $a : $b } +{id=>0}, @$old_games)->{no} + 1;
    my %game_hash = map { $_->{name} => $_ } @$old_games;
    my %ids = map { $_->{id} => 1 } @$old_games;
    for my $g (@$new_games)
    {
        if (!exists $ids{$g->{id}} || defined $g->{key})
        {
            $game_hash{$g->{name}} =
            +{
                no => $no++,
                name => $g->{name},
                proto => 'FreeTrack20',
                since => (defined $g->{key} ? 'V170' : 'V160'),
                verified => '',
                by => '',
                id => $g->{id},
                key => $g->{key}
            };
        }
    }
    print "No;Game Name;Game protocol;Supported since;Verified;By;INTERNATIONAL_ID;FTN_ID\n";
    for (sort { $a->{name} cmp $b->{name} } values %game_hash)
    {
        my $g = {%$_};
        if (!defined $g->{key})
        {
            $g->{key} = (sprintf "%04X", $g->{no}) . (join"", map { sprintf "%02X", int rand 256 } 0 .. 7) . '00';
        }
        my @cols = qw'no name proto since verified by id key';
        print join";", map { $g->{$_} } @cols;
        print "\n";
    }
}

if (@ARGV != 2)
{
    warn "usage: $0 orig.csv dump.txt\n";
    exit 1;
}
else
{
    merge(get_games_1(), get_games_2());
    exit 0;
}