1 /*---------------------------------------------------------------------------*
2 Project: RevolutionDWC Demos
3 File: ./ranking/src/main.c
4
5 Copyright 2005-2008 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 *---------------------------------------------------------------------------*/
14 /**
15 *
16 *
17 * Brief comment: This is a general-purpose ranking sample.
18 *
19 * This is a general-purpose ranking sample.
20 * This sample performs three processes in order: uploading of scores, retrieval of one's own ranking, and retrieval of the top ranking list.
21 * The program ends either when all three processes finish or when an error occurs while processing.
22 *
23 * Version: 1.4.10: Timeouts were implemented internally in the library.
24 */
25
26 #include "../../common/include/common.h"
27
28
29 // Constant definitions
30 //---------------------------------------------------------
31
32 #define RANKING_INITDATA "hxBfMOkOvlOHpUGfQlOb0001ed350000685d0040000037012e6bdwctest"
33
34
35 /**
36 * Main
37 */
DWCDemoMain()38 void DWCDemoMain()
39 {
40
41 DWCAccUserData userdata; // DWC user data
42 DWCRnkError res; // For ranking library error acquisition
43 DWCRnkGetParam param; // Parameters for acquiring ranking
44
45 u32 order;
46 u32 count;
47 int i;
48
49
50 //---------------------------------
51 // Login process
52 //---------------------------------
53 //
54 // Perform the user data acquisition process here.
55 // Because the ranking library is dependent on GameSpy, the initialization will fail unless the user data shows it has successfully logged in at least once and has a valid GameSpyProfileID.
56 //
57 // A temporary ID is not valid.
58 //
59 // If a user data obtained from a previous login is saved as a backup, this can be used.
60 // However, as set down in the guidelines, be aware that you will always need to log in each time you connect to the Internet.
61 //
62 //
63 //
64 // When an external authentication server is used, a valid GameSpyProfileID cannot be acquired, so the ranking library cannot be used.
65 // In such a case, the GameSpy server resources cannot be used.
66 //
67 //
68 // <Precaution>
69 //
70 // For the sake of simplicity in this sample, we are forcing the user data to be generated, but you should actually use the proper user data acquired by logging in.
71 //
72 // When any other data is passed, while the ranking module initialization may be successful, the operation will be undefined.
73 //
74 //
75 {
76 u32 dummyuserdata[] = { 0x00000040,0x12345678,0x87654321,0xabcdef12,
77 0x00000996,0xfedcba34,0x12345678,0x000186a0,
78 0x87654321,0xabcdef12,0xfedcba34,0x12345678,
79 0x87654321,0xabcdef12,0xfedcba34,0x7861237f };
80
81 DWCi_Np_CpuCopy8( dummyuserdata, &userdata, DWC_ACC_USERDATA_BUFSIZE );
82 }
83
84 //---------------------------------
85 // Ranking Module Process
86 //---------------------------------
87
88 // Ranking Module Initialization
89 //------------------------------
90 res = DWC_RnkInitialize( RANKING_INITDATA,
91 &userdata );
92
93 switch ( res )
94 {
95 case DWC_RNK_SUCCESS:
96 DWCDemoPrintf("DWC_RnkInitialize success\n");
97 break;
98 case DWC_RNK_ERROR_INIT_ALREADYINITIALIZED:
99 DWCDemoPrintf("[err]DWC_RnkInitialize already initialized\n");
100 goto exit;
101 case DWC_RNK_ERROR_INIT_INVALID_INITDATASIZE:
102 DWCDemoPrintf("[err]DWC_RnkInitialize invalid init data size\n");
103 goto exit;
104 case DWC_RNK_ERROR_INIT_INVALID_INITDATA:
105 DWCDemoPrintf("[err]DWC_RnkInitialize invalid init data\n");
106 goto exit;
107 case DWC_RNK_ERROR_INIT_INVALID_USERDATA:
108 DWCDemoPrintf("[err]DWC_RnkInitialize invaild user data\n");
109 goto exit;
110 }
111
112 // Upload
113 //------------------------------
114
115 // Start score upload request
116 res = DWC_RnkPutScoreAsync( 10, // category
117 DWC_RNK_REGION_JP, // region
118 1234, // score
119 (void*)"test data", // user defined data
120 strlen("test data") + 1 );
121
122 switch ( res )
123 {
124 case DWC_RNK_SUCCESS:
125 DWCDemoPrintf("DWC_RnkPutScoreAsync success\n");
126 break;
127 case DWC_RNK_IN_ERROR:
128 DWCDemoPrintf("[err]DWC_RnkPutScoreAsync in error\n");
129 goto exit;
130 case DWC_RNK_ERROR_PUT_NOTREADY:
131 DWCDemoPrintf("[err]DWC_RnkPutScoreAsync not initialized\n");
132 goto exit;
133 case DWC_RNK_ERROR_INVALID_PARAMETER:
134 DWCDemoPrintf("[err]DWC_RnkPutScoreAsync invalid parameter\n");
135 goto exit;
136 case DWC_RNK_ERROR_PUT_INVALID_KEY:
137 DWCDemoPrintf("[err]DWC_RnkPutScoreAsync invalid key\n");
138 goto exit;
139 case DWC_RNK_ERROR_PUT_NOMEMORY:
140 DWCDemoPrintf("[err]DWC_RnkPutScoreAsync nomemory\n");
141 goto exit;
142 }
143
144 // Asynchronous process
145
146 while ( (res = DWC_RnkProcess()) == DWC_RNK_SUCCESS )
147 {
148
149 DWCDemoUpdate();
150
151 }
152
153 switch ( res )
154 {
155 case DWC_RNK_PROCESS_TIMEOUT:
156 DWCDemoPrintf("Timed out!!\n");
157 goto exit;
158 case DWC_RNK_IN_ERROR:
159 DWCDemoPrintf("[err]DWC_RnkProcess in error [code:%d]\n", DWC_GetLastError( NULL ) );
160 goto exit;
161 case DWC_RNK_PROCESS_NOTASK:
162 DWCDemoPrintf("DWC_RnkProcess done\n");
163 break;
164 }
165
166 // Get network communications results
167 switch ( DWC_RnkGetState() )
168 {
169
170 case DWC_RNK_STATE_COMPLETED:
171 DWCDemoPrintf("DWC_RnkProcess Succeeded \n");
172 break;
173
174 case DWC_RNK_STATE_ERROR:
175 DWCDemoPrintf("[err]DWC_RnkGetState [code:%d]\n", DWC_RnkGetState() );
176 goto exit;
177
178 }
179
180
181
182 // Get own ranking
183 //------------------------------
184
185 // Start the request for ranking
186
187 param.size = sizeof( param.order );
188 param.order.since = 0;
189 param.order.sort = DWC_RNK_ORDER_ASC;
190
191 res = DWC_RnkGetScoreAsync( DWC_RNK_GET_MODE_ORDER, // mode
192 10, // category
193 DWC_RNK_REGION_JP, // region
194 ¶m ); // parameter
195
196 switch ( res )
197 {
198 case DWC_RNK_SUCCESS:
199 DWCDemoPrintf("DWC_RnkGetScoreAsync success\n");
200 break;
201 case DWC_RNK_IN_ERROR:
202 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync in error\n");
203 goto exit;
204 case DWC_RNK_ERROR_GET_NOTREADY:
205 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync not initialized\n");
206 goto exit;
207 case DWC_RNK_ERROR_INVALID_PARAMETER:
208 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync invalid parameter\n");
209 goto exit;
210 case DWC_RNK_ERROR_GET_INVALID_KEY:
211 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync invalid key\n");
212 goto exit;
213 case DWC_RNK_ERROR_GET_NOMEMORY:
214 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync nomemory\n");
215 goto exit;
216 }
217
218 // Asynchronous process
219
220 while ( (res = DWC_RnkProcess()) == DWC_RNK_SUCCESS )
221 {
222
223 DWCDemoUpdate();
224
225 }
226
227 switch ( res )
228 {
229 case DWC_RNK_PROCESS_TIMEOUT:
230 DWCDemoPrintf("Timed out!!\n");
231 goto exit;
232 case DWC_RNK_IN_ERROR:
233 DWCDemoPrintf("[err]DWC_RnkProcess in error [code:%d]\n", DWC_GetLastError( NULL ) );
234 goto exit;
235 case DWC_RNK_PROCESS_NOTASK:
236 DWCDemoPrintf("DWC_RnkProcess done\n");
237 break;
238 }
239
240 // Get network communications results
241 switch ( DWC_RnkGetState() )
242 {
243
244 case DWC_RNK_STATE_COMPLETED:
245 DWCDemoPrintf("DWC_RnkProcess success \n");
246 break;
247
248 case DWC_RNK_STATE_ERROR:
249 DWCDemoPrintf("[err]DWC_RnkGetState [code:%d]\n", DWC_GetLastError( NULL ) );
250 goto exit;
251
252 }
253
254 // Display
255 res = DWC_RnkResGetOrder( &order );
256 switch ( res )
257 {
258 case DWC_RNK_SUCCESS:
259 break;
260 case DWC_RNK_IN_ERROR:
261 DWCDemoPrintf("[err]DWC_RnkResGetOrder in error [code:%d]\n", DWC_GetLastError( NULL ) );
262 goto exit;
263 case DWC_RNK_ERROR_INVALID_MODE:
264 DWCDemoPrintf("[err]DWC_RnkResGetOrder invalid mode\n");
265 goto exit;
266 case DWC_RNK_ERROR_INVALID_PARAMETER:
267 DWCDemoPrintf("[err]DWC_RnkResGetOrder invalid parameter\n");
268 goto exit;
269 case DWC_RNK_ERROR_NOTCOMPLETED:
270 DWCDemoPrintf("[err]DWC_RnkResGetOrder not completed\n");
271 goto exit;
272 case DWC_RNK_ERROR_EMPTY_RESPONSE:
273 DWCDemoPrintf("[err]DWC_RnkResGetOrder empty response\n");
274 goto exit;
275 }
276
277 DWCDemoPrintf("order=%d\n", order);
278
279
280 // Get ranking
281 //------------------------------
282
283 // Start top ranking acquisition request
284 param.size = sizeof( param.toplist );
285 param.toplist.sort = DWC_RNK_ORDER_DES;
286 param.toplist.limit = 10;
287 param.toplist.since = 0;
288
289 res = DWC_RnkGetScoreAsync( DWC_RNK_GET_MODE_TOPLIST, // mode
290 10, // category
291 DWC_RNK_REGION_JP, // region
292 ¶m ); // parameter
293
294 switch ( res )
295 {
296 case DWC_RNK_SUCCESS:
297 DWCDemoPrintf("DWC_RnkGetScoreAsync success\n");
298 break;
299 case DWC_RNK_IN_ERROR:
300 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync in error\n");
301 goto exit;
302 case DWC_RNK_ERROR_GET_NOTREADY:
303 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync not initialized\n");
304 goto exit;
305 case DWC_RNK_ERROR_INVALID_PARAMETER:
306 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync invalid parameter\n");
307 goto exit;
308 case DWC_RNK_ERROR_GET_INVALID_KEY:
309 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync invalid key\n");
310 goto exit;
311 case DWC_RNK_ERROR_GET_NOMEMORY:
312 DWCDemoPrintf("[err]DWC_RnkGetScoreAsync nomemory\n");
313 goto exit;
314 }
315
316 // Asynchronous process
317
318 while ( (res = DWC_RnkProcess()) == DWC_RNK_SUCCESS )
319 {
320
321 DWCDemoUpdate();
322
323 }
324
325 switch ( res )
326 {
327 case DWC_RNK_PROCESS_TIMEOUT:
328 DWCDemoPrintf("Timed out!!\n");
329 goto exit;
330 case DWC_RNK_IN_ERROR:
331 DWCDemoPrintf("[err]DWC_RnkProcess in error [code:%d]\n", DWC_GetLastError( NULL ) );
332 goto exit;
333 case DWC_RNK_PROCESS_NOTASK:
334 DWCDemoPrintf("DWC_RnkProcess done\n");
335 break;
336 }
337
338 // Get network communications results
339 switch ( DWC_RnkGetState() )
340 {
341
342 case DWC_RNK_STATE_COMPLETED:
343 DWCDemoPrintf("DWC_RnkProcess success \n");
344 break;
345
346 case DWC_RNK_STATE_ERROR:
347 DWCDemoPrintf("[err]DWC_RnkGetState [code:%d]\n", DWC_GetLastError( NULL ) );
348 goto exit;
349
350 }
351
352
353 // Get list
354
355 res = DWC_RnkResGetRowCount( &count );
356
357 switch ( res )
358 {
359 case DWC_RNK_SUCCESS:
360 break;
361 case DWC_RNK_IN_ERROR:
362 DWCDemoPrintf("[err]DWC_RnkResGetRowCount in error [code:%d]\n", DWC_GetLastError( NULL ) );
363 goto exit;
364 case DWC_RNK_ERROR_INVALID_MODE:
365 DWCDemoPrintf("[err]DWC_RnkResGetRowCount invalid mode\n");
366 goto exit;
367 case DWC_RNK_ERROR_INVALID_PARAMETER:
368 DWCDemoPrintf("[err]DWC_RnkResGetRowCount invalid parameter\n");
369 goto exit;
370 case DWC_RNK_ERROR_NOTCOMPLETED:
371 DWCDemoPrintf("[err]DWC_RnkResGetRowCount not completed\n");
372 goto exit;
373 case DWC_RNK_ERROR_EMPTY_RESPONSE:
374 DWCDemoPrintf("[err]DWC_RnkResGetRowCount empty response\n");
375 goto exit;
376 }
377
378 for ( i=0; i<(int)count; i++ )
379 {
380
381 DWCRnkData data;
382
383 if ( DWC_RnkResGetRow( &data, (u32)i ) != DWC_RNK_SUCCESS )
384 {
385 DWCDemoPrintf("[err]DWC_RnkResGetRow()\n");
386 break;
387 }
388
389 //
390 // NOTE
391 // Ranking is detected by one's own self.
392 // Refer to the Programming Manual for conditions on placing a value in data.order.
393 //
394 DWCDemoPrintf("%dth score=%d pid=%d region=%d usrdatalen=%d\n",
395 i + 1, // * Not data.order
396 data.score,
397 data.pid,
398 data.region,
399 data.size );
400
401 }
402
403 DWCDemoUpdate();
404
405 exit:
406
407 // End ranking module
408 DWC_RnkShutdown();
409
410 // If an error has occurred, clear it.
411 {
412
413 int errorCode;
414 DWCErrorType errorType;
415
416 if ( DWC_GetLastErrorEx( &errorCode, &errorType ) != DWC_ERROR_NONE )
417 {
418
419 DWCDemoPrintf( "DWC_GetLastErrorEx ErrorCode:%d ErrorType:%d\n",
420 errorCode,
421 (int)errorType );
422
423 // Clear the errors.
424 DWC_ClearError();
425
426 }
427
428 }
429
430 }
431