moar code cleanup and initial saving of stats
authorkoda
Thu, 30 Dec 2010 02:03:24 +0100
changeset 4754 a0fd8211c00f
parent 4752 d65705a67c4e
child 4757 7ca9ebb6895d
moar code cleanup and initial saving of stats
project_files/HedgewarsMobile/Classes/GameConfigViewController.m
project_files/HedgewarsMobile/Classes/GameSetup.h
project_files/HedgewarsMobile/Classes/GameSetup.m
project_files/HedgewarsMobile/Classes/HelpPageViewController.m
project_files/HedgewarsMobile/Classes/MainMenuViewController.m
project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m
--- a/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Thu Dec 30 02:03:24 2010 +0100
@@ -226,8 +226,10 @@
                                     [NSNumber numberWithInt:self.interfaceOrientation],@"orientation",
                                     nil];
 
-    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:gameDictionary,@"game_dictionary", @"",@"savefile",
-                                      [NSNumber numberWithBool:NO],@"netgame", nil];
+    NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:gameDictionary,@"game_dictionary",
+                                      @"",@"savefile",
+                                      [NSNumber numberWithBool:NO],@"netgame",
+                                      nil];
     if (IS_IPAD())
         [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
     else {
--- a/project_files/HedgewarsMobile/Classes/GameSetup.h	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.h	Thu Dec 30 02:03:24 2010 +0100
@@ -25,11 +25,12 @@
 @interface GameSetup : NSObject {
     NSDictionary *systemSettings;
     NSDictionary *gameConfig;
+    NSMutableDictionary *statsDictionary;
 
     NSInteger ipcPort;  // Port on which engine will listen
     TCPsocket csd;      // Client socket descriptor
     TCPsocket esd;      // External socket descriptor
-    
+
     NSString *savePath;
     BOOL isNetGame;
     BOOL menuStyle;
@@ -37,6 +38,7 @@
 
 @property (nonatomic, retain) NSDictionary *systemSettings;
 @property (nonatomic, retain) NSDictionary *gameConfig;
+@property (nonatomic, retain) NSMutableDictionary *statsDictionary;
 @property (nonatomic, retain) NSString *savePath;
 @property (assign) BOOL menuStyle;
 
@@ -48,6 +50,6 @@
 -(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams;
 -(NSInteger) provideScheme:(NSString *)schemeName;
 
--(const char **)getSettings:(NSString *)recordFile;
+-(const char **)getGameSettings:(NSString *)recordFile;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.m	Thu Dec 30 02:03:24 2010 +0100
@@ -28,7 +28,7 @@
 #define BUFFER_SIZE 255     // like in original frontend
 
 @implementation GameSetup
-@synthesize systemSettings, gameConfig, savePath, menuStyle;
+@synthesize systemSettings, gameConfig, statsDictionary, savePath, menuStyle;
 
 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
     if (self = [super init]) {
@@ -57,11 +57,14 @@
             [outputFormatter release];
         } else
             self.savePath = path;
+
+        self.statsDictionary = nil;
     }
     return self;
 }
 
 -(void) dealloc {
+    [statsDictionary release];
     [gameConfig release];
     [systemSettings release];
     [savePath release];
@@ -204,12 +207,12 @@
 
 #pragma mark -
 #pragma mark Network relevant code
--(void) dumpRawData:(const uint8_t*)buffer ofSize:(uint8_t) length {
+-(void) dumpRawData:(const char *)buffer ofSize:(uint8_t) length {
     // is it performant to reopen the stream every time?
     NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES];
     [os open];
     [os write:&length maxLength:1];
-    [os write:buffer maxLength:length];
+    [os write:(const uint8_t *)buffer maxLength:length];
     [os close];
     [os release];
 }
@@ -218,7 +221,7 @@
 -(int) sendToEngine:(NSString *)string {
     uint8_t length = [string length];
 
-    [self dumpRawData:(const uint8_t *)[string UTF8String] ofSize:length];
+    [self dumpRawData:[string UTF8String] ofSize:length];
     SDLNet_TCP_Send(csd, &length, 1);
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
@@ -238,7 +241,7 @@
     IPaddress ip;
     int eProto;
     BOOL clientQuit;
-    uint8_t buffer[BUFFER_SIZE];
+    char const buffer[BUFFER_SIZE];
     uint8_t msgSize;
 
     clientQuit = NO;
@@ -268,10 +271,10 @@
 
     while (!clientQuit) {
         msgSize = 0;
-        memset(buffer, '\0', BUFFER_SIZE);
+        memset((void *)buffer, '\0', BUFFER_SIZE);
         if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
             break;
-        if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
+        if (SDLNet_TCP_Recv(csd, (void *)buffer, msgSize) <= 0)
             break;
 
         switch (buffer[0]) {
@@ -283,7 +286,7 @@
                 else
                     [self sendToEngineNoSave:@"TL"];
                 NSString *saveHeader = @"TS";
-                [self dumpRawData:(const uint8_t *)[saveHeader UTF8String] ofSize:[saveHeader length]];
+                [self dumpRawData:[saveHeader UTF8String] ofSize:[saveHeader length]];
 
                 // seed info
                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
@@ -345,18 +348,25 @@
                 }
                 break;
             case 'i':
+                // initialized with maximum
+                if (self.statsDictionary == nil)
+                    self.statsDictionary = [[NSMutableDictionary alloc] initWithCapacity:10];
                 switch (buffer[1]) {
                     case 'r':
                         DLog(@"Winning team: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"winning_team"];
                         break;
                     case 'D':
                         DLog(@"Best Shot: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"best_shot"];
                         break;
                     case 'k':
                         DLog(@"Best Hedgehog: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"best_hog"];
                         break;
                     case 'K':
                         DLog(@"Hogs Killed: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"kills"];
                         break;
                     case 'H':
                         //something about team health
@@ -369,12 +379,15 @@
                         break;
                     case 's':
                         DLog(@"Most self damage: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"self_dmg"];
                         break;
                     case 'S':
                         DLog(@"Most friendly fire: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"friendly_fire"];
                         break;
                     case 'B':
                         DLog(@"Most turn skipped by: %s", &buffer[2]);
+                        [self.statsDictionary setObject:[NSString stringWithUTF8String:&buffer[2]] forKey:@"turn_skips"];
                         break;
                     default:
                         DLog(@"Unhandled stat message, see statsPage.cpp");
@@ -384,7 +397,6 @@
             case 'q':
                 // game ended, can remove the savefile
                 [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
-                //[[NSNotificationCenter defaultCenter] postNotificationName:@"removedSave" object:nil];
                 // and remove + disable the overlay
                 [[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil];
                 break;
@@ -408,7 +420,7 @@
 #pragma mark -
 #pragma mark Setting methods
 // returns an array of c-strings that are read by engine at startup
--(const char **)getSettings: (NSString *)recordFile {
+-(const char **)getGameSettings:(NSString *)recordFile {
     NSInteger width, height;
     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort];
     NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
@@ -431,11 +443,11 @@
         
     NSString *horizontalSize = [[NSString alloc] initWithFormat:@"%d", width];
     NSString *verticalSize = [[NSString alloc] initWithFormat:@"%d", height];
-    const char **gameArgs = (const char**) malloc(sizeof(char *) * 10);
-    NSInteger tmpQuality;
+    const char **gameArgs = (const char **)malloc(sizeof(char *) * 10);
     BOOL enhanced = [[self.systemSettings objectForKey:@"enhanced"] boolValue];
 
     NSString *modelId = modelType();
+    NSInteger tmpQuality;
     if ([modelId hasPrefix:@"iPhone1"] || [modelId hasPrefix:@"iPod1,1"] || [modelId hasPrefix:@"iPod2,1"])     // = iPhone and iPhone 3G or iPod Touch or iPod Touch 2G
         tmpQuality = 0x00000001 | 0x00000002 | 0x00000008 | 0x00000040;                 // rqLowRes | rqBlurryLand | rqSimpleRope | rqKillFlakes
     else if ([modelId hasPrefix:@"iPhone2"] || [modelId hasPrefix:@"iPod3"])                                    // = iPhone 3GS or iPod Touch 3G
--- a/project_files/HedgewarsMobile/Classes/HelpPageViewController.m	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/HelpPageViewController.m	Thu Dec 30 02:03:24 2010 +0100
@@ -36,10 +36,10 @@
 
 // on iPhone the XIBs contain UIScrollView
 -(void) viewDidLoad {
-    if (scrollView.tag == 0)
+    if (scrollView.tag == 0)    // ipad
         scrollView.contentSize = CGSizeMake(480,650);
-    else
-        scrollView.contentSize = CGSizeMake(480,460);
+    else                        // iphone
+        scrollView.contentSize = CGSizeMake(480,470);
     scrollView.maximumZoomScale = 4.0;
     scrollView.minimumZoomScale = 0.75;
     scrollView.clipsToBounds = YES;
@@ -57,7 +57,6 @@
     [super dealloc];
 }
 
-// on iPad the XIBs contain UIControl
 -(IBAction) dismiss {
     [UIView beginAnimations:@"helpingame" context:NULL];
     self.view.alpha = 0;
--- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Thu Dec 30 02:03:24 2010 +0100
@@ -99,8 +99,6 @@
     createWeaponNamed(@"Minefield", 5);
     createWeaponNamed(@"Thinking with Portals", 6);
     // merge not needed because weapons not present in the set are 0ed by GameSetup
-
-    DLog(@"Success");
 }
 
 #pragma mark -
--- a/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m	Wed Dec 29 00:28:39 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/SDL_uikitappdelegate.m	Thu Dec 30 02:03:24 2010 +0100
@@ -118,6 +118,7 @@
     }
     [blackView release];
 
+
     // pull out useful configuration info from various files
     GameSetup *setup = [[GameSetup alloc] initWithDictionary:gameDictionary];
     NSNumber *isNetGameNum = [gameDictionary objectForKey:@"netgame"];
@@ -126,23 +127,26 @@
                              toTarget:setup
                            withObject:nil];
 
-    const char **gameArgs = [setup getSettings:[gameDictionary objectForKey:@"savefile"]];
     NSNumber *menuStyle = [NSNumber numberWithBool:setup.menuStyle];
-    [setup release];
-
-    // since the sdlwindow is not yet created, we add the overlayController with a delay
+    NSNumber *orientation = [[gameDictionary objectForKey:@"game_dictionary"] objectForKey:@"orientation"];
     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                           isNetGameNum,@"net",
                           menuStyle,@"menu",
-                          [[gameDictionary objectForKey:@"game_dictionary"] objectForKey:@"orientation"],@"orientation",
+                          orientation,@"orientation",
                           nil];
     [self performSelector:@selector(displayOverlayLater:) withObject:dict afterDelay:1];
 
-    // this is the pascal fuction that starts the game (wrapped around isInGame)
+    // need to set again [gameDictionary objectForKey:@"savefile"] because if it's empty it means it's a normal game
+    const char **gameArgs = [setup getGameSettings:[gameDictionary objectForKey:@"savefile"]];
     self.isInGame = YES;
+    // this is the pascal fuction that starts the game
     Game(gameArgs);
     self.isInGame = NO;
     free(gameArgs);
+    
+    NSDictionary *stat = setup.statsDictionary;
+    [setup release];
+
 
     [self.uiwindow makeKeyAndVisible];
     [self.uiwindow bringSubviewToFront:self.mainViewController.view];
@@ -156,9 +160,11 @@
     [UIView commitAnimations];
     [refBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1];
     [refSecondBlackView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
+
+    DLog(@"%@",stat);
 }
 
-// overlay with controls, become visible later, with a transparency effect
+// overlay with controls, become visible later, with a transparency effect since the sdlwindow is not yet created
 -(void) displayOverlayLater:(id) object {
     NSDictionary *dict = (NSDictionary *)object;
     self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];