Finally fixed the slow animation performance of board games; all it took was changing the board's z index from 1 to 0, somehow. Games working well now.
5 // Created by Jens Alfke on 3/16/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "Game-Persistence.h"
12 static NSDictionary* parseURLFields( NSURL* url );
15 @implementation Game (Persistence)
18 static NSMutableDictionary *sPersistentGames;
21 - (id) initWithCoder: (NSCoder*)decoder
25 _players = [[decoder decodeObjectForKey: @"players"] mutableCopy];
26 _states = [[decoder decodeObjectForKey: @"states"] mutableCopy];
27 _moves = [[decoder decodeObjectForKey: @"moves"] mutableCopy];
28 self.currentTurn = self.maxTurn;
34 - (void) encodeWithCoder: (NSCoder*)coder
36 [coder encodeObject: _players forKey: @"players"];
37 [coder encodeObject: _states forKey: @"states"];
38 [coder encodeObject: _moves forKey: @"moves"];
44 return [NSURL URLWithString:
45 [NSString stringWithFormat: @"game:type=%@&id=%@&turn=%u&move=%@",
46 [[self class] identifier], _uniqueID, self.currentTurn,_moves.lastObject]];
50 + (Game*) gameWithURL: (NSURL*)url
52 if( 0 != [@"game" caseInsensitiveCompare: url.scheme] )
54 NSDictionary *fields = parseURLFields(url);
55 NSString *type = [fields objectForKey: @"type"];
56 NSString *uuid = [fields objectForKey: @"id"];
57 int turn = [[fields objectForKey: @"turn"] intValue];
58 if( !type || !uuid || turn<=0 )
61 Game *game = [sPersistentGames objectForKey: uuid];
63 if( ![type isEqualToString: [[game class] identifier]] )
65 } else if( turn == 1 ) {
66 Class gameClass = NSClassFromString( [type stringByAppendingString: @"Game"] );
67 if( ! gameClass || ! [gameClass isSubclassOfClass: [Game class]] )
69 game = [[gameClass alloc] initWithUniqueID: uuid];
70 [game setNumberOfPlayers: 2];
71 if( ! sPersistentGames )
72 sPersistentGames = [[NSMutableDictionary alloc] init];
73 [sPersistentGames setObject: game forKey: uuid];
80 - (BOOL) addMoveFromURL: (NSURL*)url
82 NSDictionary *fields = parseURLFields(url);
83 NSString *uuid = [fields objectForKey: @"id"];
84 NSString *move = [fields objectForKey: @"move"];
85 int turn = [[fields objectForKey: @"turn"] intValue];
86 return [uuid isEqualToString: self.uniqueID]
87 && turn==self.currentTurn
89 && [self applyMoveString: move];
98 static NSDictionary* parseURLFields( NSURL* url )
100 // Parse the URL into key-value pairs:
101 NSMutableDictionary *fields = [NSMutableDictionary dictionary];
102 for( NSString *field in [url.resourceSpecifier componentsSeparatedByString: @"&"] ) {
103 NSRange e = [field rangeOfString: @"="];
104 NSString *key, *value;
106 key = [field substringToIndex: e.location];
107 value = [field substringFromIndex: NSMaxRange(e)];
112 [fields setObject: value forKey: key];