project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
changeset 6870 f72cac290325
parent 6832 fae8fd118da9
child 6908 896ed2afcfb8
equal deleted inserted replaced
6869:a187c280dd3d 6870:f72cac290325
    21 
    21 
    22 
    22 
    23 #define BUFFER_SIZE 255     // like in original frontend
    23 #define BUFFER_SIZE 255     // like in original frontend
    24 
    24 
    25 @implementation EngineProtocolNetwork
    25 @implementation EngineProtocolNetwork
    26 @synthesize statsArray, stream, csd, enginePort;
    26 @synthesize delegate, stream, csd, enginePort;
    27 
    27 
    28 -(id) init {
    28 -(id) initWithPort:(NSInteger) port {
    29     if (self = [super init]) {
    29     if (self = [super init]) {
    30         self.statsArray = nil;
    30         self.delegate = nil;
    31         self.csd = NULL;
    31         self.csd = NULL;
    32         self.stream = nil;
    32         self.stream = nil;
    33         self.enginePort = [HWUtils randomPort];
    33         self.enginePort = port;
    34     }
    34     }
    35     return self;
    35     return self;
    36 }
    36 }
    37 
    37 
       
    38 -(id) init {
       
    39     return [self initWithPort:[HWUtils randomPort]];
       
    40 }
       
    41 
    38 -(void) dealloc {
    42 -(void) dealloc {
    39     releaseAndNil(statsArray);
    43     self.delegate = nil;
    40     releaseAndNil(stream);
    44     releaseAndNil(stream);
    41     [super dealloc];
    45     [super dealloc];
    42 }
    46 }
    43 
    47 
    44 #pragma mark -
    48 #pragma mark -
   210 
   214 
   211 // this is launched as thread and handles all IPC with engine
   215 // this is launched as thread and handles all IPC with engine
   212 -(void) engineProtocol:(id) object {
   216 -(void) engineProtocol:(id) object {
   213     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   217     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   214     NSDictionary *gameConfig = (NSDictionary *)object;
   218     NSDictionary *gameConfig = (NSDictionary *)object;
       
   219     NSMutableArray *statsArray = nil;
   215     TCPsocket sd;
   220     TCPsocket sd;
   216     IPaddress ip;
   221     IPaddress ip;
   217     int eProto;
   222     int eProto;
   218     BOOL clientQuit;
   223     BOOL clientQuit;
   219     char const buffer[BUFFER_SIZE];
   224     char const buffer[BUFFER_SIZE];
   324                     DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
   329                     DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
   325                     clientQuit = YES;
   330                     clientQuit = YES;
   326                 }
   331                 }
   327                 break;
   332                 break;
   328             case 'i':
   333             case 'i':
   329                 if (self.statsArray == nil) {
   334                 if (statsArray == nil) {
   330                     self.statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
   335                     statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
   331                     NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
   336                     NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
   332                     [self.statsArray insertObject:ranking atIndex:0];
   337                     [statsArray insertObject:ranking atIndex:0];
   333                     [ranking release];
   338                     [ranking release];
   334                 }
   339                 }
   335                 NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
   340                 NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
   336                 NSArray *info = [tempStr componentsSeparatedByString:@" "];
   341                 NSArray *info = [tempStr componentsSeparatedByString:@" "];
   337                 NSString *arg = [info objectAtIndex:0];
   342                 NSString *arg = [info objectAtIndex:0];
   338                 int index = [arg length] + 3;
   343                 int index = [arg length] + 3;
   339                 switch (buffer[1]) {
   344                 switch (buffer[1]) {
   340                     case 'r':           // winning team
   345                     case 'r':           // winning team
   341                         [self.statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
   346                         [statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
   342                         break;
   347                         break;
   343                     case 'D':           // best shot
   348                     case 'D':           // best shot
   344                         [self.statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
   349                         [statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
   345                         break;
   350                         break;
   346                     case 'k':           // best hedgehog
   351                     case 'k':           // best hedgehog
   347                         [self.statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kill(s) in a turn", &buffer[index], arg]];
   352                         [statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kill(s) in a turn", &buffer[index], arg]];
   348                         break;
   353                         break;
   349                     case 'K':           // number of hogs killed
   354                     case 'K':           // number of hogs killed
   350                         [self.statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
   355                         [statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
   351                         break;
   356                         break;
   352                     case 'H':           // team health/graph
   357                     case 'H':           // team health/graph
   353                         break;
   358                         break;
   354                     case 'T':           // local team stats
   359                     case 'T':           // local team stats
   355                         // still WIP in statsPage.cpp
   360                         // still WIP in statsPage.cpp
   356                         break;
   361                         break;
   357                     case 'P':           // teams ranking
   362                     case 'P':           // teams ranking
   358                         [[self.statsArray objectAtIndex:0] addObject:tempStr];
   363                         [[statsArray objectAtIndex:0] addObject:tempStr];
   359                         break;
   364                         break;
   360                     case 's':           // self damage
   365                     case 's':           // self damage
   361                         [self.statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
   366                         [statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
   362                         break;
   367                         break;
   363                     case 'S':           // friendly fire
   368                     case 'S':           // friendly fire
   364                         [self.statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
   369                         [statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
   365                         break;
   370                         break;
   366                     case 'B':           // turn skipped
   371                     case 'B':           // turn skipped
   367                         [self.statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
   372                         [statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
   368                         break;
   373                         break;
   369                     default:
   374                     default:
   370                         DLog(@"Unhandled stat message, see statsPage.cpp");
   375                         DLog(@"Unhandled stat message, see statsPage.cpp");
   371                         break;
   376                         break;
   372                 }
   377                 }
   373                 break;
   378                 break;
   374             case 'q':
   379             case 'q':
   375                 // game ended and match finished, statsArray is full of delicious statistics
   380                 // game ended and match finished, statsArray is full of delicious statistics
       
   381                 if (self.delegate != nil && [self.delegate respondsToSelector:@selector(gameEndedWithStatistics:)])
       
   382                     [self.delegate gameEndedWithStatistics:statsArray];
       
   383                 [statsArray release];
   376                 [HWUtils setGameStatus:gsEnded];
   384                 [HWUtils setGameStatus:gsEnded];
   377                 // closing connection here would trigger a "IPC connection lost" error, so we have to wait until recv fails
   385                 // closing connection here would trigger a "IPC connection lost" error, so we have to wait until recv fails
   378                 break;
   386                 break;
   379             case 'Q':
   387             case 'Q':
   380                 // game exited but not completed, skip this message in the savefile
   388                 // game exited but not completed, skip this message in the savefile