Today I am going to talk about the last part of my
course work. After nearly finishing my coursework, I got an idea to make the
game more interesting. I added a feature that checks if a player beats the
current high score, and the current high score was achieved by another player,
the game sends an email to the other player to tell him or her that her score
was beaten.
Modifying
the database
Even though this was not very difficult to do, it
took me a whole week to manage to sort it out. First, I added a field in the
database of the users to add the email address of the player. This requires the
email to be added manually to the database as a new user cannot add an email
address in the login form. This was done without because the scope of the login
page was not to create a fully featured login form with a registration form. So, I added an email address to each record in
the database. For testing purposes, I inserted my email address in all the
fields so that I would not bug other people with my emails.
Checking if
the highest score was beaten by another player
Then, I started to modify the function which
compares the high scores. After opening the connection with the database, I
made a query to obtain the username, the high score and the email of the person
who is at the top of the hall of fame.
The query which I used is shown below:
select username, highscore, email
from
cannon_game.login
order by highscore desc limit 1;
The record obtained is inserted into the variable
$row and from it I obtained a value for the $topuser, $topscore and $topemail,
which are the details of the player with the highest score in the hall of fame.
Then an if statement checks if the current score is larger than the $topscore
value from the database. If the score is higher than the topscore, the
difference is calculated. Then, another if statement checks if the current
player is the topuser. If it is not the same user, then it means that the
person who was first, now moves to second place. Then, a function called email
is called and it is passed the values for topuser, topemail and the score
difference. Then the function continues to update the scores accordingly like
it used to do before.
Testing this
function
Knowing that emailing would not work immediately, I
tested this function a bit primitively, using echo. I tested by using different
users to play the game and trying to achieve a better score every time. I was
not playing the game to obtain the highest possible score. I tried to keep
scores low so I would not have to reset the scores from the database because I
would not be able to beat them. By echoing the variables values I knew that the
function to check if the high score was beaten by another player was working
great.
Email
function
I created the email function and I passed to it the
variables $topuser, $topemail and $scoredifference. To send emails you need a
to address to send emails to, a from address, a subject and a body. The to
email was obtained from the database and passed to the function. I used my personal
email as the from email address. Then I crafted a subject and a body message
using the user name and the score difference. The code below shows the first
part of the email function:
$to = $topemail;
$from = "danielborgmt@gmail.com";
$subject = ucwords($topuser).", be
careful!!";
$body = ucwords($topuser).", your
score was beaten by " .$scoredifference." points!! Make sure
to beat his score to stay in the
first place!!!";
The function ucwords() is used to capitalize the
first letter of the string passed to it. Then you need an SMTP server to be
able to send emails. This gave me a lot of problems and took me nearly a week
to solve. I talked about this in my previous blog. After trying a lot of
solutions, I managed to find a working script which worked nearly immediately.
The script was a PEAR extension and I used the following code to be able to use
it:
$er = error_reporting(0);
require_once "Mail.php";
SMTP Server
Settings
The error_reporting(0) is used to hide errors for
using methods which were not called using a STRICT method. This was done at the
top of the php file. After enabling SSL on my web server, I added the details
of my Gmail account to use Gmail's SMTP server. Then I called the function to
send the email to the details of the player who was first using my Gmail SMTP
server. The code is shown below:
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "danielborgmt";
$password = "mypassword";
$headers = array ('From' => $from, 'To'
=> $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host'
=> $host,'port' => $port, 'auth' =>
true, 'username' => $username, 'password' =>
$password));
$mail = $smtp->send($to, $headers,
$body);
if (PEAR::isError($mail)) {
echo("<p>"
. $mail->getMessage() . "</p>");
}
This code allowed me to send emails properly to the
player who lost the first place.
Figure 1: Sample Email from PHP Script
Figure 2: Email details
This concludes my second coursework.