#!/usr/bin/perl use strict; use warnings; use Asterisk::AGI; my $agi = new Asterisk::AGI; my %input = $agi->ReadParse(); my %op = ( "minus" => sub { return $_[0] - $_[1]; }, "add" => sub { return $_[0] + $_[1]; }, # "multiply" => sub { # return $_[0] * $_[1]; # }, # "divide" => sub { # return $_[0] / $_[1]; # }, ); my $score = 0; my $count = 0; my $limit = 10; my $starttime = time(); my $time = time() - $starttime; $agi->exec('Festival','"Welcome to subtraction addition game"'); while ( $time < 30 ) { for (keys %op) { my $big = int(rand($limit+1)); $big += 10; my $subt = int(rand($big)); my $ans = $op{$_}->($big, $subt); $count += 1; $agi->exec('Festival',"number: \"$big\" : \"$_\" : number : \"$subt\" : equals."); my $finished = 0; my $res = ''; while (!$finished) { my $input = chr($agi->wait_for_digit('5000')); if ($input =~ /^[0-9\*\#]$/) { if ($input =~ /^[\*\#]$/) { $finished = 1; } $res .= $input; } else { $finished = 1; } } $res =~ s/[\#\*]$//; if (int($res) == $ans){ $score+=1; $agi->exec('Festival',"answer: \"$res\" is right. "); } else { $agi->exec('Festival',"answer is wrong. your answer is: \"$res\". the right answer is: \"$ans\""); } } $time = time() - $starttime; } my $pct = $score/$count*100; $agi->exec('Festival',"time is up: you got \"$score\": out of : \"$count\" : right");