SnailLife messaging system

posted in SnailLife
Published July 26, 2015
Advertisement
"What's SnailLife", you say? Well I'm glad you asked! My snail simulation has gone through a couple of names...and even though I was never 100% happy with Gastropoda it was the best I could come up with - a name that was unique and didn't allow the project to sound too "gamey" (because it's not a game). All of the cooler names I could come up with weren't suitable for various reasons (like domain name availability and such).

But recently I found out about the .life TLD! And I decided that nowadays we have such a varied domain name landscape that .com isn't as important as it used to be, and definitely not for an obscure hobby project. So Gastropoda is now SnailLife!
On to the messaging:

Messaging/Notifications



I noticed when working on the simulation that it was tough to figure out who died and why when snails disappeared from a jar.

(Sidenote...I just realized...in real life a snail wouldn't just disappear from a jar if it died. It would sit there and start decomposing until someone put it away. Maybe this should be the case with SnailLife, too).

Anyway, I wanted something to notify me immediately when something important happens, like a snail is born or dies. So I made a rudimentary messaging system to receive notifications from the simulation, which should be able to be pretty easily expanded into a user-to-user messaging system.

First I made a [font='courier new']user_messages[/font] table with the following columns:

  • messageID
  • recipientID
  • senderID
  • subject
  • content
  • isRead
  • created_at
  • updated_at

Then I made a [font='courier new']UserMessage[/font] model that looks like this for now:


'integer', 'senderID' => 'integer', 'subject' => 'alpha_num_spaces', 'content' => 'alpha_num_spaces', 'isRead' => 'boolean' ); protected $primaryKey = 'messageID'; protected $fillable = ['recipientID', 'senderID', 'subject', 'content', 'isRead', 'created_at', 'updated_at']; public function recipient() { $this->hasOne('App\User', 'userID', 'recipientID'); } public function sender() { $this->hasOne('App\User', 'userID', 'senderID'); } public function getSenderUserNameAttribute() { $username = 'SnailLife'; if (isset($this->sender)) { $username = $this->sender->username; } return $username; } public function updateMessage($propertiesToUpdate) { $this->update($propertiesToUpdate); return true; }}



When a snail is killed or born we create a new message for the user. For example:


if ($this->isEgg) { $notification = [ 'recipientID' => $this->ownerID, 'subject' => 'An egg has died!', 'content' => 'Egg ID ' . $this->snailID . ' has died. Cause of death: ' . $cod ];}else { $notification = [ 'recipientID' => $this->ownerID, 'subject' => 'A snail (' . $this->snailID . ') has died!', 'content' => 'SnailID ID ' . $this->snailID . ' has died. Cause of death: ' . $cod ];}$notification = new UserMessage($notification);$notification->save();


(An egg is really just an instance of a snail, just one without a birthDate, so when an egg or snail dies it's handled in the same model).

Then there's the view. When logged in the user gets a notification of unread messages in the header:


@if (count(Auth::user()->unreadMessages) > 0) You have unread messages - You've got mail! @endif




Oh, we get unread messages in the User model using an Eloquent [font='courier new']hasMany[/font] relationship:

public function unreadMessages() { return $this->hasMany('App\UserMessage', 'recipientID', 'userID')->where('isRead', '=', 0);}

Once they click through they get taken to their message page (ignore the double-death messages. That's being fixed right now...):

messages.png

Pretty simple and gets the job done for now.
1 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement