project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
changeset 6365 c992df555afb
parent 6353 d8f62c805619
child 6700 e04da46ee43c
equal deleted inserted replaced
6364:c6adb60239f4 6365:c992df555afb
   220 
   220 
   221 // this is launched as thread and handles all IPC with engine
   221 // this is launched as thread and handles all IPC with engine
   222 -(void) engineProtocol:(id) object {
   222 -(void) engineProtocol:(id) object {
   223     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   223     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   224     NSDictionary *gameConfig = (NSDictionary *)object;
   224     NSDictionary *gameConfig = (NSDictionary *)object;
   225     NSMutableArray *tempStats = nil;
       
   226     TCPsocket sd;
   225     TCPsocket sd;
   227     IPaddress ip;
   226     IPaddress ip;
   228     int eProto;
   227     int eProto;
   229     BOOL clientQuit;
   228     BOOL clientQuit;
   230     char const buffer[BUFFER_SIZE];
   229     char const buffer[BUFFER_SIZE];
   335                     DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
   334                     DLog(@"ERROR - wrong protocol number: %d (expecting %d)", netProto, eProto);
   336                     clientQuit = YES;
   335                     clientQuit = YES;
   337                 }
   336                 }
   338                 break;
   337                 break;
   339             case 'i':
   338             case 'i':
   340                 if (tempStats == nil) {
   339                 if (self.statsArray == nil) {
   341                     tempStats = [[NSMutableArray alloc] initWithCapacity:10 - 2];
   340                     self.statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
   342                     NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
   341                     NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
   343                     [tempStats insertObject:ranking atIndex:0];
   342                     [self.statsArray insertObject:ranking atIndex:0];
   344                     [ranking release];
   343                     [ranking release];
   345                 }
   344                 }
   346                 NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
   345                 NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
   347                 NSArray *info = [tempStr componentsSeparatedByString:@" "];
   346                 NSArray *info = [tempStr componentsSeparatedByString:@" "];
   348                 NSString *arg = [info objectAtIndex:0];
   347                 NSString *arg = [info objectAtIndex:0];
   349                 int index = [arg length] + 3;
   348                 int index = [arg length] + 3;
   350                 switch (buffer[1]) {
   349                 switch (buffer[1]) {
   351                     case 'r':           // winning team
   350                     case 'r':           // winning team
   352                         [tempStats insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
   351                         [self.statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
   353                         break;
   352                         break;
   354                     case 'D':           // best shot
   353                     case 'D':           // best shot
   355                         [tempStats addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
   354                         [self.statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
   356                         break;
   355                         break;
   357                     case 'k':           // best hedgehog
   356                     case 'k':           // best hedgehog
   358                         [tempStats addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]];
   357                         [self.statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kill(s) in a turn", &buffer[index], arg]];
   359                         break;
   358                         break;
   360                     case 'K':           // number of hogs killed
   359                     case 'K':           // number of hogs killed
   361                         [tempStats addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
   360                         [self.statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
   362                         break;
   361                         break;
   363                     case 'H':           // team health/graph
   362                     case 'H':           // team health/graph
   364                         break;
   363                         break;
   365                     case 'T':           // local team stats
   364                     case 'T':           // local team stats
   366                         // still WIP in statsPage.cpp
   365                         // still WIP in statsPage.cpp
   367                         break;
   366                         break;
   368                     case 'P':           // teams ranking
   367                     case 'P':           // teams ranking
   369                         [[tempStats objectAtIndex:0] addObject:tempStr];
   368                         [[self.statsArray objectAtIndex:0] addObject:tempStr];
   370                         break;
   369                         break;
   371                     case 's':           // self damage
   370                     case 's':           // self damage
   372                         [tempStats addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
   371                         [self.statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
   373                         break;
   372                         break;
   374                     case 'S':           // friendly fire
   373                     case 'S':           // friendly fire
   375                         [tempStats addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
   374                         [self.statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
   376                         break;
   375                         break;
   377                     case 'B':           // turn skipped
   376                     case 'B':           // turn skipped
   378                         [tempStats addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
   377                         [self.statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
   379                         break;
   378                         break;
   380                     default:
   379                     default:
   381                         DLog(@"Unhandled stat message, see statsPage.cpp");
   380                         DLog(@"Unhandled stat message, see statsPage.cpp");
   382                         break;
   381                         break;
   383                 }
   382                 }
   384                 break;
   383                 break;
   385             case 'q':
   384             case 'q':
   386                 // game ended and match finished, statsArray is full of delicious statistics
   385                 // game ended and match finished, statsArray is full of delicious statistics
   387                 [HWUtils setGameStatus:gsEnded];
   386                 [HWUtils setGameStatus:gsEnded];
   388                 self.statsArray = [[NSArray arrayWithArray:tempStats] retain];
       
   389                 // closing connection here would trigger a "IPC connection lost" error, so we have to wait until recv fails
   387                 // closing connection here would trigger a "IPC connection lost" error, so we have to wait until recv fails
   390                 break;
   388                 break;
   391             case 'Q':
   389             case 'Q':
   392                 // game exited but not completed, skip this message in the savefile
   390                 // game exited but not completed, skip this message in the savefile
   393                 [HWUtils setGameStatus:gsInterrupted];
   391                 [HWUtils setGameStatus:gsInterrupted];
   400     }
   398     }
   401     DLog(@"Engine exited, ending thread");
   399     DLog(@"Engine exited, ending thread");
   402 
   400 
   403     [self.stream close];
   401     [self.stream close];
   404     [self.stream release];
   402     [self.stream release];
   405     [tempStats release];
       
   406 
   403 
   407     // Close the client socket
   404     // Close the client socket
   408     SDLNet_TCP_Close(csd);
   405     SDLNet_TCP_Close(csd);
   409     SDLNet_Quit();
   406     SDLNet_Quit();
   410 
   407