/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/App/ClientController.m

00001 //
00002 //  ClientController.m
00003 //  MacTrek
00004 //
00005 //  Created by Aqua on 5/19/06.
00006 //  Copyright 2006 __MyCompanyName__. All rights reserved.
00007 //
00008 
00009 #import "ClientController.h"
00010 
00011 
00012 @implementation ClientController
00013 
00014 // privates
00015 bool ghostStart = NO;
00016 
00017 - (id) initWithUniverse:(Universe*)newUniverse {
00018         self = [super init];
00019         if (self != nil) {
00020                 universe = newUniverse;
00021         communication = [[Communication alloc] initWithUniverse:universe baseUdpPort:0];
00022         
00023         // need the main run loop other wise the performSelector afterDelay will fail
00024         [notificationCenter addObserver:self selector:@selector(checkForDeath:) name:@"SP_PSTATUS" 
00025                                  object:nil useLocks:NO useMainRunLoop:YES];
00026     }
00027         return self;
00028 }
00029 
00030 - (void) checkForDeath:(Player *)player {
00031     // did i die?
00032     if ([player isMe]) {
00033         NSLog(@"ClientController.checkForDeath status = %@", [player statusString]);
00034         if (([player status] == PLAYER_EXPLODE) ) {
00035             NSLog(@"ClientController.checkForDeath: today was a good day to die");
00036             // || ([player status] == PLAYER_OUTFIT)){
00037             // wait for the drawing to end 
00038             // $$ then generate an event on the drawing of the explosion!
00039             [self performSelector: @selector(iDied:) withObject:player afterDelay: 1.0];
00040             // $$ doesn't happen..
00041             [self iDied:player];
00042         }
00043     }
00044 }
00045 
00046 - (void) iDied:(Player *)me {
00047     // go to outfitting
00048     NSLog(@"ClientController.iDied: today was a good day to die");
00049     [me setStatus:PLAYER_OUTFIT];
00050     [notificationCenter postNotificationName:@"CC_GO_OUTFIT" 
00051                                       object:self 
00052                                     userInfo:nil];    
00053 }
00054 
00055 - (bool) slotObtained {
00056     
00057     // if i got a slot, i have become a player
00058         Player *me = [universe playerThatIsMe];
00059         if (me == nil) {
00060         NSLog(@"ClientController.slotObtained checking... nope");
00061         return NO;
00062     } else {
00063         NSLog(@"ClientController.slotObtained checking... YES");
00064         return YES;
00065     }    
00066 }
00067 
00068 - (void) setupSlot {
00069     
00070     // should only be called if i got a slot
00071     if (![self slotObtained]) {
00072         // i'm not a player yet
00073                  NSLog(@"ClientController.setupSlot called, but not a player");
00074                 return; 
00075     }
00076     
00077     Player *me = [universe playerThatIsMe];
00078     
00079         // report our success
00080     NSLog(@"ClientController.setupSlot started");
00081         [notificationCenter postNotificationName:@"CC_SLOT_FOUND" 
00082                                                                           object:self 
00083                                                                         userInfo:nil];
00084         
00085         if (ghostStart) {
00086                 // find my team $$ should be done already by Player class
00087                 // see if i exploded
00088                 if ([me damage] > [[me ship] maxDamage]) {
00089                         [me setStatus:PLAYER_OUTFIT];
00090                         // go to outfitting
00091                         [notificationCenter postNotificationName:@"CC_GO_OUTFIT" 
00092                                                                                           object:self 
00093                                                                                         userInfo:nil];
00094                 } else {
00095                         [me setStatus:PLAYER_ALIVE];
00096             // continue game
00097                 }               
00098         } 
00099         
00100         return;
00101 }
00102 
00103 
00104 // use this when multithreading
00105 - (void) findSlot {
00107     
00108     // loop until i find a slot $$ very BAD, wait for the proper event (something with
00109     // a player update i suppose :
00110     // SP_S_YOU_SS or SP_S_YOU or SP_YOU (probably the last entry) then unsubscribe
00111     // to avoid updates, for now it works ok
00112     if (![self slotObtained]) {
00113         // i'm not a player yet, keep trying
00114                 [self performSelector: @selector(findSlot) withObject: self afterDelay: 0.1];
00115                 return; 
00116     }
00117     [self setupSlot];
00118 }
00119 
00120 - (bool)sendSlotSettingsToServer {
00121         
00122         //try UDP
00123         //[communication sendUdpReq:[NSNumber numberWithInt:COMM_UDP]];
00124     [notificationCenter postNotificationName:@"COMM_SEND_SHORT_REQ" object:self userInfo:[NSNumber numberWithInt:COMM_UDP]];        
00125         //try short packets
00126         //[communication sendShortReq:[NSNumber numberWithInt:SPK_VON]];
00127     [notificationCenter postNotificationName:@"COMM_SEND_SHORT_REQ" object:self userInfo:[NSNumber numberWithInt:SPK_VON]];
00128         //send options we have set
00129         //[communication sendOptionsPacket];
00130     [notificationCenter postNotificationName:@"COMM_SEND_OPTIONS_PACKET" object:self userInfo:nil];
00131         // $$ don't ping yet
00132     //[communication sendPingReq:[NSNumber numberWithBool:NO]];
00133     [notificationCenter postNotificationName:@"COMM_SEND_PING_REQ" object:self userInfo:[NSNumber numberWithBool:NO]];
00134         //send desired updates per second
00135         //[communication sendUpdatePacket:[NSNumber numberWithInt:COMM_UPDATES_PER_SECOND]];
00136     [notificationCenter postNotificationName:@"COMM_SEND_UPDATE_PACKET" object:self userInfo:[NSNumber numberWithInt:COMM_UPDATES_PER_SECOND]]; 
00137     return YES;
00138 }
00139 
00140 - (void)singleReadFromServer {
00141     [communication readFromServer];
00142 }
00143 
00144 - (bool)startClientAt:(NSString *)server port:(int)port seperate:(bool)multiThread {
00145         
00146         NSLog(@"ClientController.startClientAt: %@ port %d", server, port);
00147     
00148     // call the server
00149         if ([communication callServer:server port:port]) {      
00150         [notificationCenter postNotificationName:@"CC_SERVER_CALL_SUCCESS"];
00151         
00152         // start listening to it
00153         [communication setMultiThreaded:multiThread]; 
00154         if (multiThread) {
00155             // invoke a seperate thread to read from the server
00156             // and use findslot to check if it was successfull
00157             if ([communication startCommunicationThread]) {            
00158                 // try to find a slot
00159                 [self findSlot];
00160                 return YES;
00161             } else {
00162                 NSLog(@"ClientController.startClientAt: ERROR starting thread");
00163                 return NO;
00164             } 
00165         } else {
00166             // poll loop until we get a slot
00167             while (![self slotObtained]) {
00168                 [communication readFromServer];  // $$ let's hope it doesn't block
00169             }
00170             // yeah we are in
00171             [self setupSlot];
00172             return YES;
00173         }
00174 
00175     } else {
00176         NSLog(@"ClientController.startClientAt: %@ port %d NO CONNECTION", server, port);
00177         [notificationCenter postNotificationName:@"CC_SERVER_CALL_FAILED"];
00178         return NO;
00179     }
00180 }
00181 
00182 - (void)stop {
00183     NSLog(@"ClientController.stop stoping thread and cleaning self");
00184     [communication stopCommunicationThread];
00185         [communication cleanUp:self];   
00186     // remove me from the universe (or i become a ghost)
00187     [[universe playerThatIsMe] setIsMe:NO];
00188 }
00189 
00190 @end

Generated on Fri Jul 28 19:15:13 2006 for MacTrek by  doxygen 1.4.7