Friends management

Friends get people visit your site again, of course, besides offering fun games. Friends are established on the platform between people who already know each other and new ones are discovered while interacting on the game community site.

Making new friends involves the following steps:

  1. Player A sends a friendship invitation to Player B.
  2. Player B receives a notification.
  3. Player B either accepts or rejects the friendship.
  4. If Player B accepts, a friendship relation is established.

The steps are fairly simple as you might have experienced in any standard instant messenger. The pending notifications are kept on the server until the recipient (Player B) responds. The pending notification may be purged in case the recipients don't respond in a certain time period. The friendship relation is kept in a different object or a database table, shown as follows:

Friends management

Note that other additional properties may also be tracked, such as the date when players became friends, a status field to keep track if the friendship is fully established or is still pending, etc.

Another important function for a game server to perform when an avatar logs into the game is to send notifications letting the friends know that the avatar is now online and also to let the entering avatar know of all the friends that are currently online. The game server also sends out similar notifications when an avatar logs out of the game.

Friends in Hello World

In Hello World, we leave all the default behavior related to friends up to the PulseUI framework. That is the reason why we don't see any code in the Hello World project.

The friends API

As you may have seen in any standard instant messenger, making friends starts with a player requesting to add another player as a friend. The recipient may accept or reject the offer. If accepted, the friendship is formed until one of them decides to terminate the relation.

One of the API methods to make new friends or break existing friends is as follows:

public function makeFriend(avatarName:String):void
public function respondToFriendInvite(accept:Boolean, avName:String):void
public function breakFriend(avatarName:String):void

To make a new friend, the game code is required to call makeFriend API with the name of the avatar. The recipient will receive the onMakeFriend notification.

function onMakeFriend(newFriend:GameAvatarClient):void;

The recipient may then respond by calling the respondToFriendInvite method. The avName parameter should be the same as the initiating avatar's name.

Customizing friends display

The following figure shows the classes that create the object instance that are responsible for displaying friends. You may observe the slight difference between the creations of player display objects and the friend display objects. Unlike player display objects where both are created by PulseGame class, the friends display is created by PulseGame, but the FriendDisplay is created by the FriendsDisplay class.

Customizing friends display

The following code overrides the method in MyGame to create our own subclass of FriendsDisplay (MyFriendsDisplay):

protected override function initFriendsDisplay():void {
m_friendsDisplay = new MyFriendsDisplay();
}

To create our own version of FriendDisplay (MyFriendDisplay), we override the method in MyFriendsDisplay as shown:

public override function
createNewFriendsDisplay(av:GameAvatarClient):FriendDisplay {
return new MyFriendDisplay(av);
}
Customizing friends display

If we explored the PlayerDisplay, we will find that the little circle sprite registers for a mouse-click event that handles the request to make friends with the corresponding avatar associated with the PlayerDisplay instance.

protected function makeFriend(event:MouseEvent):void {
var gc:GameClient;
gc = PulseGame.getInstance().getGameClient();
gc.makeFriend(m_av.getName());
}

Similarly, when a friend relation is broken by clicking on the little square sprite on the Friend Display object, a request is sent to the server in code as shown below:

protected function breakFriend(event:MouseEvent):void {
var gc:GameClient;
gc = PulseGame.getInstance().getGameClient();
gc.breakFriend(m_av.getName());
}

The following is the code that handles the notification sent by the server for a friend request. The code is handled in PulseGame. The following callback method is implemented, which simply accepts the friend request. In our subclass of PulseGame, we may want to inform that a request was received, and accept or reject the request based on the player's response.

public function onMakeFriend(newFriend:GameAvatarClient):void {
m_netClient.respondToFriendInvite(true, newFriend.getName());
}

PulseGame also handles all the updates regarding friends:

public function onFriendUpdate(status:int,
errorCode:int,
aFriend:GNetFriend):void {
switch(status) {
case GameConstants.F_ACCEPT:
// new friend, create a new FriendDisplay
var av:GameAvatarClient;
av = aFriend.getAvatar() as GameAvatarClient;
m_friendsDisplay.addFriend(aFriend);
m_playersDisplay.updatePlayer(av.getId());
break;
case GameConstants.F_REJECT:
// player rejected friendship :(
// inform the player..
break;
case GameConstants.F_BREAK:
// no longer friends, anymore! :( remove the FriendDisplay
m_friendsDisplay.removeFriend(aFriend.getId());
m_playersDisplay.updatePlayer(aFriend.getAvatar().getId());
break;
case GameConstants.F_ENTER:
case GameConstants.F_EXIT:
// Friend came online, update the FriendDisplay
// Friend went offline, update the FriendDisplay
m_friendsDisplay.updateFriend(aFriend);
break;
}
}

All updates regarding friends are received via the onFriendUpdate. We first examine the type of update received by switching on the status parameter.

A status value of F_ACCEPT indicates that another player accepted our friend request. At this point, we need to update the player display so as to remove the friend circle sprite. In addition, we need to add a new friend display.

For a status value of F_REJECT, there is no screen update required; however, a subclass may inform the player of the fact.

A status value of F_BREAK means an existing friendship was dissolved. In this case, we remove the corresponding friend display sprite and also update the player display sprite to again add the make friend circle button.

PulseUI incorporates a visual hint as to whether a friend is online or offline; the server automatically sends notification whenever the status value is either F_ENTER when a friend enters the game or F_EXIT when a friend leaves the game. We simply find the corresponding player display and redraw it. Update is called with status F_ENTER and also in the case when a player enters the game for the first time, and for each friend the player has, the client will receive an F_ENTER, even though the friend may be offline. The sure way of finding out if the player is online is by calling the method, isFriendOnline, on the GameClient API.

The following screenshot shows the visual hint. When the player is online, a shadow filter is added to the friend sprite. In the screenshot, we observe that Tony is friends with Wilson and Jessie, and Jessie is online.

Customizing friends display
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155