/[VRac]/Session.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /Session.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 150 - (hide annotations)
Sun Aug 5 15:16:10 2007 UTC (11 years, 8 months ago) by dpavlin
File size: 1720 byte(s)
session record/playback now works
1 dpavlin 145 package Session;
2    
3     # Dobrica Pavlinusic, <dpavlin@rot13.org> 08/05/07 14:27:15 CEST
4    
5     use strict;
6     use warnings;
7    
8     use Carp qw/confess/;
9     use Data::Dump qw/dump/;
10     use File::Slurp;
11    
12     use base qw/Class::Accessor/;
13     #__PACKAGE__->mk_accessors(qw());
14    
15    
16     =head1 NAME
17    
18     Session - save or load emulator interactive session
19    
20     =cut
21    
22     =head1 FUNCTIONS
23    
24     =head2 record_session
25    
26 dpavlin 150 $self->record_session( 'name', $value );
27 dpavlin 145
28     =cut
29    
30     sub record_session {
31     my $self = shift;
32 dpavlin 150 my $name = shift || confess "need name";
33 dpavlin 145
34     my $t = $self->app->ticks;
35    
36 dpavlin 150 $self->append_to_file('session.pl', "\$s->{$t}->{'$name'} = ", dump( @_ ),";\n");
37 dpavlin 145
38     }
39    
40     =head2 load_session
41    
42     $self->load_session( '/path/to/session.pl' );
43    
44     =cut
45    
46 dpavlin 150 my $s;
47     my @timeline;
48    
49 dpavlin 145 sub load_session {
50     my $self = shift;
51     my $path = shift || confess "no path?";
52    
53 dpavlin 150 if ( ! -r $path ) {
54     warn "WARNING: can't open session $path: $!\n";
55     return;
56     }
57    
58 dpavlin 145 eval read_file( $path );
59 dpavlin 150 warn "session = ",dump( $s );
60 dpavlin 145
61 dpavlin 150 @timeline = sort { $a <=> $b } keys %$s;
62 dpavlin 145
63     my ( $from, $to ) = @timeline[0,-1];
64     printf "loaded session %.2f-%.2fs with %d events\n", $from / 1000, $to / 1000, $#timeline + 1;
65     }
66    
67 dpavlin 150 =head2 session_event
68    
69     my $v = $self->session_event('key_pressed');
70    
71     =cut
72    
73     sub session_event {
74     my $self = shift;
75    
76     return unless @timeline;
77    
78     my $name = shift || confess "no name?";
79    
80     my $t = $self->app->ticks;
81    
82     # do we have events to trigger?
83     return if ( $t < $timeline[0] );
84    
85     my $e_t = $timeline[0];
86    
87     # do we have next event and should we take it?
88     if ( $#timeline > 0 && $t > $timeline[1] ) {
89     shift @timeline;
90     $e_t = $timeline[0];
91     warn "session_event $e_t $name ",dump( $s->{$e_t}->{$name} )," left ", $#timeline+1, " events\n";
92     }
93    
94     return $s->{$e_t}->{$name};
95     }
96    
97 dpavlin 145 =head1 SEE ALSO
98    
99     L<VRac>
100    
101     =cut
102    
103     1;

  ViewVC Help
Powered by ViewVC 1.1.26