Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Neopets Times

by sulfericacid (Deacon)
on Jun 05, 2003 at 07:41 UTC ( [id://263248]=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info /msg sulfericacid
Description: I am an avid player of www.neopets.com and I know a lot of people keep asking what events happen at what times so I designed a script to count down to key events that happen throughout the course of the day.

The script tracks three events (snowager, jobs and the new day). I doubt many of you would have much interest in this script but who knows, maybe some of you play on that site too.

From this script I learned: Calculate time between times and operations.

Problems: I know there is a lot of repetitive code but it's the best I could do.

#!/usr/bin/perl

use warnings;
use strict;
use CGI qw(:standard);

print header, start_html;
print "<center><H2>Important Neopets Times</H2></center>";
print "<br>";
my ($sec, $min, $hour) = localtime(time);
print "The current server time is $hour:$min:$sec";

print "<table>";
print "<tr>";

#
# Job time
#

print qq(<td width="15" bgcolor="gray">);
print "Jobs";
print "</td>";
print "<td>";

if ($min <= 10) {
my $newjobtime;
$newjobtime = 10 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(    <a href="http://www.neopets.com/faerieland/employ/employm
+ent.phtml" target="new">get a job!);
}
elsif ($min <= 20) {
my $newjobtime;
$newjobtime = 20 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(    <a href="http://www.neopets.com/faerieland/employ/employm
+ent.phtml" target="new">get a job!</a>);
}
elsif ($min <= 30) {
my $newjobtime;
$newjobtime = 30 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www
+.neopets.com/faerieland/employ/employment.phtml" target="new">get a j
+ob!</a>);
}
elsif ($min <= 40) {
my $newjobtime;
$newjobtime = 40 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(    <a href="http://www.neopets.com/faerieland/employ/employm
+ent.phtml" target="new">get a job!</a>);
}
elsif ($min <= 50) {
my $newjobtime;
$newjobtime = 50 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(    <a href="http://www.neopets.com/faerieland/employ/employm
+ent.phtml" target="new">get a job!</a>);
}
elsif ($min <= 60) {
my $newjobtime;
$newjobtime = 60 - $min;
print "There are <font color=blue>$newjobtime</font> minute(s) until j
+obs are replenished.";
print qq(    <a href="http://www.neopets.com/faerieland/employ/employm
+ent.phtml" target="new">get a job!</a>);
}

print "</td>";
print "</tr>";
print "<tr>";

#
# Snowager Time
#

print qq(<td width="15" bgcolor="gray">);
print "Snowager";
print "</td>";
print "<td>";

my ($sec1, $min1, $hour1) = localtime(time);

if ($hour1 = 1) {
print qq(<font color=blue>The snowager is sleeping!</font>&nbsp;&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
+sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.neopet
+s.com/winter/snowager2.phtml" location="new">steal your keep!</a>);
}
elsif ($hour1 >= 1 && $hour1 > 9) {
my $snowagertime;
my $snowagertime2;
$snowagertime = 9 - $hour1;
$snowagertime2 = 60 - $min1;
print "There are $snowagertime hours(s) and $snowagertime2 minute(s) l
+eft until he awakens!";
}
elsif ($hour1 = 9) {
print qq(<font color=blue>The snowager is sleeping!</font>&nbsp;&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.neopets.com/winter/snowa
+ger2.phtml" location="new">Steal your keep!</a>);
}
elsif ($hour1 > 9 && $hour1 < 17) {
my $snowagertime;
my $snowagertime2;
$snowagertime = 9 - $hour1;
$snowagertime2 = 60 - $min1;
print "There are $snowagertime hours(s) and $snowagertime2 minute(s) l
+eft until he awakens!";
}
elsif ($hour1 = 17) {
print qq(<font color=blue>The snowager is sleeping!</font>&nbsp;&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.neopets.com/winter/snowa
+ger2.phtml" location="new">Steal your keep!</a>);
}
elsif ($hour1 > 17 && $hour1 < 24) { 
my $snowagertime;
my $snowagertime2;
$snowagertime = 24 - $hour1;
$snowagertime2 = 60 - $min1; # calculate minutes to make hours match u
+p
print "There are $snowagertime hours(s) and $snowagertime2 minute(s) l
+eft until he awakens!";
}
elsif ($hour1 >= 24 && $hour > 1) {
print qq(<font color=blue>The snowager is sleeping!</font>&nbsp;&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.neopets.com/winter/snowa
+ger2.phtml" location="new">Steal your keep!</a>);}

print "</td>";
print "</tr>";
print "<tr>";
print qq(<td witdh="20" bgcolor="gray">);
print "New Day";
print "</td><td>";


my ($sec2, $min2, $hour2) = localtime(time);
if ($hour2 < 2) {
my $newday;
my $newday2;
$newday = 2 - $hour2;
$newday2 = 60 - $min2;
print "There are $newday hour(s) and $newday2 minute(s) until the new 
+day.";
print "</td>";
print "</tr>";
print "</table>";
print end_html;
}
elsif ($hour2 >= 2) {
my $newday;
my $newday2;
$newday = 24 - $hour2;
$newday2 = 60 - $min2;
print "There are $newday hour(s) and $newday2 minute(s) until the new 
+day.";
print "</td>";
print "</tr>";
print "</table>";
print end_html;
}
Replies are listed 'Best First'.
Re: Neopets Times
by TVSET (Chaplain) on Jun 05, 2003 at 09:49 UTC
Re: Neopets Times (program repair shop)
by Aristotle (Chancellor) on Jun 06, 2003 at 23:01 UTC

    A style note to begin with - try indenting your code. It's survivable for a simple script like this, but you'll easily trip up on more complex ones. Any decent editor will support you with that. There's also Perl::Tidy.

    Why are you looking up the time separately for each part of the script? You can just reuse the variables from the start.

    I would also suggest you accumulate your data first, then output all the results in one fell swoop. Littling prints all over the place makes CGI scripts very hard to maintain.

    Most of your repetitions are easily avoided if you just use some math.

    Math:

    my $newjobtime = $min % 10;
    Logic:
    if ($hour = 1 or $hour = 9 or $hour = 17 or $hour >= 24) {
    Now we see the "hot" periods are spaced 8 hours apart, so we can again go by the remainder. By subtracting one hour first, we can account for the fact that they lie on 1/9/17 rather than 0/8/16. But that will sometimes give us "-1 hours left", so we add 8 hours first, which doesn't change the result of the modulo but does make sure we always have positive values. Then, whether it is a now a hot period or not is only a matter of checking whether there are any hours left till the next one.
    my $snowagerleft = ($hour + 8 - 1) % 8; if($snowagerleft) { # "$snowagerleft until awakens" } else { # "steal your keep" }
    The following math in your code makes no sense:
    if ($hour < 2) { my $newday; $newday = 2 - $hour; # ... } elsif ($hour >= 2) { my $newday; $newday = 24 - $hour; # ... }
    My guess is that it's attempting to compensate for a timezone offset of two hours. In that case you can just subtract from 24, then massage the result. Math again:
    my $newday = 24 - $hour; $newday = ($newday + 2) % 24;

    Also note how you are repeatedly using the same bit of HTML to display each section's results. You should really use a templating system, but at least rather than copypasting those bits all over the place, you should just pull them into a subroutine.

    Finally, your HTML could be a bit cleaner. :) If you use CGI's HTML generation routines, it will do a lot of the job for you on its own.

    Everything put together:

    #!/usr/bin/perl use warnings; use strict; use CGI qw(:standard *table); sub format_section { my ($width, $title, $message) = @_; return Tr( td({width => $width, bgcolor => "gray" }, $title), td($message) ); } my ($sec, $min, $hour) = localtime(time); my $newdayleft = 24 - $hour; $newdayleft = ($newdayleft + 2) % 24; my $hourminsleft = 60 - $min; my $newjobleft = $min % 10; my $snowagerleft = ($hour + 8 - 1) % 8; my $snowagermsg = $snowagerleft ? ( "There are $snowagerleft hours(s) " . "and $hourminsleft minute(s) left until he awakens!" ) : ( font({ color => "blue" }, q(The snowager is sleeping!)) . q(&nbsp;) x 34 . a({ href => "http://www.neopets.com/winter/snowager2.phtml", target => "_blank" }, q(Steal your keep!)) ); print header . start_html . h2({align => "center"}, "Important Neopets Times") . br() . "The current server time is $hour:$min:$sec" . start_table . format_section(15, Jobs => ( "There are " . font({ color => "blue" }, $newjobleft) . " minute(s) until jobs are replenished." )) . format_section(15, Snowager => $snowagermsg) . format_section( 20, "New Day", "There are $newdayleft hour(s) " . "and $hourminsleft minute(s) until the new day." ) . end_table . end_html;

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://263248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-26 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found