Classes/GameLobbyController.m

/*
 File: GameLobbyController.m
 Abstract: Lists available peers and handles the user interface related to connecting to
 a peer.
 Version: 1.0
 
 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
 Inc. ("Apple") in consideration of your agreement to the following
 terms, and your use, installation, modification or redistribution of
 this Apple software constitutes acceptance of these terms.  If you do
 not agree with these terms, please do not use, install, modify or
 redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and
 subject to these terms, Apple grants you a personal, non-exclusive
 license, under Apple's copyrights in this original Apple software (the
 "Apple Software"), to use, reproduce, modify and redistribute the Apple
 Software, with or without modifications, in source and/or binary forms;
 provided that if you redistribute the Apple Software in its entirety and
 without modifications, you must retain this notice and the following
 text and disclaimers in all such redistributions of the Apple Software.
 Neither the name, trademarks, service marks or logos of Apple Inc. may
 be used to endorse or promote products derived from the Apple Software
 without specific prior written permission from Apple.  Except as
 expressly stated in this notice, no other rights or licenses, express or
 implied, are granted by Apple herein, including but not limited to any
 patent rights that may be infringed by your derivative works or by other
 works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 Copyright (C) 2010 Apple Inc. All Rights Reserved.
 
 */
 
#import "GameLobbyController.h"
#import "RocketController.h"
#import <GameKit/GameKit.h> 
 
@implementation GameLobbyController
@synthesize manager;
 
#pragma mark View Controller Methods
 
- (void)viewDidLoad 
{
    [super viewDidLoad];
    manager = [[SessionManager alloc] init]; 
    manager.lobbyDelegate = self;
    [manager setupSession];
    
    [self peerListDidChange:nil];
}
 
- (void)dealloc 
{
    manager.lobbyDelegate = nil;
    [manager release];
    [peerList release];
    [alertView release];
    [super dealloc];
}
 
#pragma mark -
#pragma mark Opening Method
// Called when user selects a peer from the list or accepts a call invitation.
- (void) openGameScreenWithPeerID:(NSString *)peerID
{
    RocketController *gameScreen = [[RocketController alloc]
                                        initWithNibName:@"RocketController"
                                        bundle:[NSBundle mainBundle]
                                        manager: manager];
    [self.navigationController pushViewController:gameScreen animated:YES];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    [gameScreen release];
}
 
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [peerList count];
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    static NSString *TopLevelCellIdentifier = @"TopLevelCellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TopLevelCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                       reuseIdentifier:TopLevelCellIdentifier] autorelease];
    }
 
    NSUInteger row = [indexPath row];
    
    cell.textLabel.text = [manager displayNameForPeer:[peerList objectAtIndex:row]]; 
    return cell;
}
 
#pragma mark Table View Delegate Methods
 
// The user selected a peer from the list to connect to.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [manager connect:[peerList objectAtIndex:[indexPath row]]]; 
    [self openGameScreenWithPeerID:[peerList objectAtIndex:[indexPath row]]]; 
}
 
#pragma mark -
#pragma mark GameSessionLobbyDelegate Methods
 
- (void) peerListDidChange:(SessionManager *)session;
{
    NSArray *tempList = peerList;
    peerList = [session.peerList copy];
    [tempList release];
    [self.tableView reloadData]; 
}
 
// Invitation dialog due to peer attempting to connect.
- (void) didReceiveInvitation:(SessionManager *)session fromPeer:(NSString *)participantID;
{
    NSString *str = [NSString stringWithFormat:@"Incoming Invite from %@", participantID];
    if (alertView.visible) {
        [alertView dismissWithClickedButtonIndex:0 animated:NO];
        [alertView release];
    }
    alertView = [[UIAlertView alloc] 
                 initWithTitle:str
                 message:@"Do you wish to accept?" 
                 delegate:self 
                 cancelButtonTitle:@"Decline" 
                 otherButtonTitles:nil];
    [alertView addButtonWithTitle:@"Accept"]; 
    [alertView show];
}
 
// Display an alert sheet indicating a failure to connect to the peer.
- (void) invitationDidFail:(SessionManager *)session fromPeer:(NSString *)participantID
{
    NSString *str;
    if (alertView.visible) {
        // Peer cancelled invitation before it could be accepted/rejected
        // Close the invitation dialog before opening an error dialog
        [alertView dismissWithClickedButtonIndex:0 animated:NO];
        [alertView release];
        str = [NSString stringWithFormat:@"%@ cancelled call", participantID]; 
    } else {
        // Peer rejected invitation or exited app.
        str = [NSString stringWithFormat:@"%@ declined your call", participantID]; 
    }
    
    alertView = [[UIAlertView alloc] initWithTitle:str message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}
 
#pragma mark -
#pragma mark UIAlertViewDelegate Methods
 
// User has reacted to the dialog box and chosen accept or reject.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // User accepted.  Open the game screen and accept the connection.
        if ([manager didAcceptInvitation])
            [self openGameScreenWithPeerID:manager.currentConfPeerID]; 
    } else {
        [manager didDeclineInvitation];
    }
}
 
@end