]> www.fi.muni.cz Git - slotcarman.git/blob - SCX/RaceParser.pm
Make the main app use SCX::Parser
[slotcarman.git] / SCX / RaceParser.pm
1 #!/usr/bin/perl -w
2
3 package SCX::RaceParser;
4
5 use strict;
6
7 use IO::File;
8 use FileHandle;
9
10 use base qw(SCX::Parser);
11
12 our $LOG_FILE_LIMIT = 10_000_000; # bytes
13
14 sub new {
15         my ($class, $args) = @_;
16
17         my $self = SCX::Parser->new();
18
19         $self->{track} = $args->{track}
20                 or die "Track argument not given";
21         $self->{logfile} = $args->{logfile};
22
23         if ($self->{logfile}) {
24                 open my $fh, '>>', $self->{logfile}
25                         or die "Can't open $self->{logfile}: $!";
26                 $self->{logfh} = $fh;
27         }
28
29         bless $self, $class;
30
31         return $self;
32 }
33
34 sub log_print {
35         my ($self, @data) = @_;
36
37         my $size = $self->{logfh}->tell;
38
39         if ($size >= $LOG_FILE_LIMIT) {
40                 close $self->{logfh};
41                 my $gen = 8;
42                 while ($gen) {
43                         rename $self->{logfile} . '.' . $gen . '.bz2',
44                                 $self->{logfile} . '.' . $gen+1 . '.bz2';
45                         $gen--;
46                 }
47
48                 rename $self->{logfile}, $self->{logfile} . '.1';
49                 system 'bzip2 -9 ' . $self->{logfile} . '.1 &';
50
51                 open my $fh, '>', $self->{logfile}
52                         or die "Can't open $self->{logfile}: $!";
53                 $self->{logfh} = $fh;
54         }
55
56         $self->{logfh}->print(sprintf('% 10.3f ', $self->now),
57                 join(' ', map { sprintf('%02X', $_) } @data), "\n");
58         $self->{logfh}->flush;
59 }
60
61 sub log_packet {
62         my ($self, @data) = @_;
63
64         $self->track->packet_received($self->now);
65
66         $self->log_print(@data)
67                 if $self->{logfile};
68 }
69
70 sub bad_bytes {
71         my ($self, @data) = @_;
72
73         $self->log_print(@data)
74                 if $self->{logfile};
75 }
76
77 sub track { return shift->{track} }
78
79 sub race_setup {
80         my ($self, $rounds) = @_;
81
82         $self->track->race_setup($rounds, $self->now);
83 }
84
85 sub fuel_level {
86         my ($self, @fuel) = @_;
87
88         for my $car (0..5) {
89                 $self->track->car($car)->set_fuel($fuel[$car]);
90         }
91 }
92
93 sub qualification {
94         my ($self, $rounds, $cars) = @_;
95
96         $self->track->qualification_setup($rounds, $cars, $self->now);
97 }
98
99 sub end_of_race {
100         my ($self) = @_;
101
102         $self->track->race_end;
103 }
104
105 sub race_start {
106         my ($self) = @_;
107
108         $self->track->race_start($self->now);
109 }
110
111 sub finish_line {
112         my ($self, $regular, @cars_finished) = @_;
113
114         $self->track->finish_line(
115                 $self->now,
116                 $regular,
117                 @cars_finished
118         );
119 }
120
121 sub controller_status {
122         my ($self, @controllers) = @_;
123
124         for my $car (0..5) {
125                 my $c = $controllers[$car];
126                 $self->track->car($car)->set_throttle(
127                         $c->{throttle},
128                         $c->{button},
129                         $self->now
130                 );
131                 $self->track->car($car)->set_light($c->{light});
132         }
133 }
134
135 1;
136