diff options
Diffstat (limited to 'libs/cocoslive/CLScoreServerRequest.m')
-rwxr-xr-x | libs/cocoslive/CLScoreServerRequest.m | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/libs/cocoslive/CLScoreServerRequest.m b/libs/cocoslive/CLScoreServerRequest.m new file mode 100755 index 0000000..e16b895 --- /dev/null +++ b/libs/cocoslive/CLScoreServerRequest.m | |||
@@ -0,0 +1,257 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2008-2010 Ricardo Quesada | ||
5 | * Copyright (c) 2011 Zynga Inc. | ||
6 | * | ||
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
8 | * of this software and associated documentation files (the "Software"), to deal | ||
9 | * in the Software without restriction, including without limitation the rights | ||
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
11 | * copies of the Software, and to permit persons to whom the Software is | ||
12 | * furnished to do so, subject to the following conditions: | ||
13 | * | ||
14 | * The above copyright notice and this permission notice shall be included in | ||
15 | * all copies or substantial portions of the Software. | ||
16 | * | ||
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
23 | * THE SOFTWARE. | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | |||
28 | // 3rd party imports | ||
29 | #import "CJSONDeserializer.h" | ||
30 | |||
31 | // local imports | ||
32 | #import "CLScoreServerPost.h" | ||
33 | #import "CLScoreServerRequest.h" | ||
34 | #import "ccMacros.h" | ||
35 | |||
36 | @implementation CLScoreServerRequest | ||
37 | |||
38 | @synthesize connection=connection_; | ||
39 | |||
40 | +(id) serverWithGameName:(NSString*) name delegate:(id)delegate | ||
41 | { | ||
42 | return [[[self alloc] initWithGameName:name delegate:delegate] autorelease]; | ||
43 | } | ||
44 | |||
45 | -(id) initWithGameName:(NSString*) name delegate:(id)aDelegate | ||
46 | { | ||
47 | self = [super init]; | ||
48 | if( self ) { | ||
49 | gameName = [[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] retain]; | ||
50 | delegate = [aDelegate retain]; | ||
51 | receivedData = [[NSMutableData data] retain]; | ||
52 | } | ||
53 | return self; | ||
54 | } | ||
55 | |||
56 | -(void) dealloc | ||
57 | { | ||
58 | CCLOGINFO(@"deallocing %@", self); | ||
59 | |||
60 | [delegate release]; | ||
61 | [gameName release]; | ||
62 | [receivedData release]; | ||
63 | [connection_ release]; | ||
64 | [super dealloc]; | ||
65 | } | ||
66 | |||
67 | -(BOOL) requestScores:(tQueryType)type | ||
68 | limit:(int)limit | ||
69 | offset:(int)offset | ||
70 | flags:(tQueryFlags)flags | ||
71 | category:(NSString*)category | ||
72 | { | ||
73 | // create the request | ||
74 | [receivedData setLength:0]; | ||
75 | |||
76 | // it's not a call for rank | ||
77 | reqRankOnly = NO; | ||
78 | |||
79 | NSString *device = @""; | ||
80 | if( flags & kQueryFlagByDevice ) | ||
81 | device = [[UIDevice currentDevice] uniqueIdentifier]; | ||
82 | |||
83 | // arguments: | ||
84 | // query: type of query | ||
85 | // limit: how many scores are being requested. Default is 25. Maximun is 100 | ||
86 | // offset: offset of the scores | ||
87 | // flags: bring only country scores, world scores, etc. | ||
88 | // category: string user defined string used to filter | ||
89 | NSString *url= [NSString stringWithFormat:@"%@?gamename=%@&querytype=%d&offset=%d&limit=%d&flags=%d&category=%@&device=%@", | ||
90 | SCORE_SERVER_REQUEST_URL, | ||
91 | gameName, | ||
92 | type, | ||
93 | offset, | ||
94 | limit, | ||
95 | flags, | ||
96 | [category stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding], | ||
97 | device | ||
98 | ]; | ||
99 | |||
100 | // NSLog(@"%@", url); | ||
101 | |||
102 | NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url] | ||
103 | cachePolicy:NSURLRequestUseProtocolCachePolicy | ||
104 | timeoutInterval:10.0]; | ||
105 | |||
106 | // create the connection with the request | ||
107 | // and start loading the data | ||
108 | self.connection=[NSURLConnection connectionWithRequest:request delegate:self]; | ||
109 | if (! connection_) | ||
110 | return NO; | ||
111 | |||
112 | return YES; | ||
113 | } | ||
114 | |||
115 | -(BOOL) requestScores:(tQueryType)type | ||
116 | limit:(int)limit | ||
117 | offset:(int)offset | ||
118 | flags:(tQueryFlags)flags | ||
119 | { | ||
120 | // create the request | ||
121 | [receivedData setLength:0]; | ||
122 | |||
123 | // arguments: | ||
124 | // query: type of query | ||
125 | // limit: how many scores are being requested. Maximun is 100 | ||
126 | // offset: offset of the scores | ||
127 | // flags: bring only country scores, world scores, etc. | ||
128 | return [self requestScores:type limit:limit offset:offset flags:flags category:@""]; | ||
129 | } | ||
130 | |||
131 | -(NSArray*) parseScores | ||
132 | { | ||
133 | NSArray *array = nil; | ||
134 | NSError *error = nil; | ||
135 | NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:receivedData error:&error]; | ||
136 | |||
137 | // NSLog(@"r: %@", dictionary); | ||
138 | if( ! error ) { | ||
139 | array = [dictionary objectForKey:@"scores"]; | ||
140 | } else { | ||
141 | CCLOG(@"Error parsing scores: %@", error); | ||
142 | } | ||
143 | return array; | ||
144 | } | ||
145 | |||
146 | #pragma mark Request rank for score | ||
147 | |||
148 | -(BOOL) requestRankForScore:(int)score andCategory:(NSString*)category { | ||
149 | // create the request | ||
150 | [receivedData setLength:0]; | ||
151 | |||
152 | reqRankOnly = YES; | ||
153 | |||
154 | // arguments: | ||
155 | // score: score for which you need rank | ||
156 | // category: user defined string used to filter | ||
157 | NSString *url= [NSString stringWithFormat:@"%@?gamename=%@&category=%@&score=%d", | ||
158 | SCORE_SERVER_GETRANK_URL, | ||
159 | gameName, | ||
160 | [category stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding], | ||
161 | score | ||
162 | ]; | ||
163 | |||
164 | NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url] | ||
165 | cachePolicy:NSURLRequestUseProtocolCachePolicy | ||
166 | timeoutInterval:10.0]; | ||
167 | |||
168 | // create the connection with the request | ||
169 | // and start loading the data | ||
170 | self.connection=[NSURLConnection connectionWithRequest:request delegate:self]; | ||
171 | if (! connection_) | ||
172 | return NO; | ||
173 | |||
174 | return YES; | ||
175 | } | ||
176 | |||
177 | -(int) parseRank { | ||
178 | // NSString *rankStr = [NSString stringWithCString:[receivedData bytes] length: [receivedData length]]; | ||
179 | NSString *rankStr = [NSString stringWithCString:[receivedData bytes] encoding: NSASCIIStringEncoding]; | ||
180 | |||
181 | // NSLog(@"XXXX: Ranking: %@", rankStr); | ||
182 | |||
183 | // creating trimmed string by trimming everything that's not numbers from the receivedData | ||
184 | NSString *trimmedStr = [rankStr stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]; | ||
185 | |||
186 | int scoreInt = [trimmedStr intValue]; | ||
187 | |||
188 | return scoreInt; | ||
189 | } | ||
190 | |||
191 | |||
192 | #pragma mark NSURLConnection Delegate | ||
193 | |||
194 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | ||
195 | { | ||
196 | // this method is called when the server has determined that it | ||
197 | // has enough information to create the NSURLResponse | ||
198 | |||
199 | // it can be called multiple times, for example in the case of a | ||
200 | // redirect, so each time we reset the data. | ||
201 | // receivedData is declared as a method instance elsewhere | ||
202 | |||
203 | [receivedData setLength:0]; | ||
204 | } | ||
205 | |||
206 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | ||
207 | { | ||
208 | // append the new data to the receivedData | ||
209 | // receivedData is declared as a method instance elsewhere | ||
210 | [receivedData appendData:data]; | ||
211 | } | ||
212 | |||
213 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | ||
214 | { | ||
215 | // release the connection, and the data object | ||
216 | self.connection = nil; | ||
217 | |||
218 | |||
219 | CCLOG(@"Error getting scores: %@", error); | ||
220 | |||
221 | if( [delegate respondsToSelector:@selector(scoreRequestFail:) ] ) | ||
222 | [delegate scoreRequestFail:self]; | ||
223 | |||
224 | } | ||
225 | |||
226 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection | ||
227 | { | ||
228 | // release the connection, and the data object | ||
229 | self.connection = nil; | ||
230 | |||
231 | |||
232 | if(reqRankOnly) { | ||
233 | // because it's request for rank, different delegate method is called scoreRequestRankOk: | ||
234 | // if connection failed the same delegate method is used as for standard scores - scoreRequestFail: | ||
235 | if( [delegate respondsToSelector:@selector(scoreRequestRankOk:) ] ) { | ||
236 | [delegate scoreRequestRankOk:self]; | ||
237 | } | ||
238 | } else { | ||
239 | if( [delegate respondsToSelector:@selector(scoreRequestOk:) ] ) { | ||
240 | [delegate scoreRequestOk:self]; | ||
241 | } | ||
242 | |||
243 | } | ||
244 | } | ||
245 | |||
246 | -(NSURLRequest *)connection:(NSURLConnection *)connection | ||
247 | willSendRequest:(NSURLRequest *)request | ||
248 | redirectResponse:(NSURLResponse *)redirectResponse | ||
249 | { | ||
250 | NSURLRequest *newRequest=request; | ||
251 | if (redirectResponse) { | ||
252 | newRequest=nil; | ||
253 | } | ||
254 | return newRequest; | ||
255 | } | ||
256 | |||
257 | @end | ||