简体   繁体   English

存储用户“排名”的最佳方式 - iPhone App

[英]Best way to store a user's “rank” - iPhone App

I'm creating a simple game (iPhone app) and I want users to have a ranking based on points.我正在创建一个简单的游戏(iPhone 应用程序),我希望用户根据积分进行排名。 What would be the best way to store all users points and then assign a rank to a user when the app first launches?存储所有用户积分然后在应用首次启动时为用户分配排名的最佳方式是什么? I have a server (a website), so I can use SQL if needed.我有一个服务器(一个网站),所以如果需要我可以使用 SQL。 Any ideas?有任何想法吗?

I'll suggest you take a look at Apple Game Center.我建议你看看 Apple Game Center。 It contains (almost) pre-built leaderboards.它包含(几乎)预建的排行榜。

You could use an NSMutableURLRequest to access a php page that reads the ranking(s) from a mySQL database.您可以使用NSMutableURLRequest访问 php 页面,该页面从 mySQL 数据库中读取排名。 Have the php output xml and parse the result.有 php output xml 并解析结果。 The following code sends a request to a php page, then parses the xml that is returned by that page.以下代码向 php 页面发送请求,然后解析该页面返回的 xml。 (You could also post data to a different php page to update DB entries, etc.). (您还可以将数据发布到不同的 php 页面以更新数据库条目等)。

//IN THE .h class (of a viewController)
... : UIViewController {

  //I use a label to display the data
  IBOutlet UILabel *label1;
  //Create global variable
  NSString *tempString;
  //Dataset for response from HTTP Request
  NSMutableData *receivedData;
  NSXMLParser *xmlParser;

}

-(void) initiateAPIConnection;

@property (nonatomic, retain) NSString *tempString;

@property (nonatomic, retain) UILabel *label1;

@end
//IN THE .h class


//IN THE .m class
//...
@synthesize label1, tempString;
//...

-(void)initiateAPIConnection{

  NSString *post = [NSString stringWithFormat:@"user=Chris"];
  NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

  NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
  [request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php"]];
  [request setHTTPMethod:@"POST"];
  [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  [request setTimeoutInterval:10.0]; //fail after 10 seconds with no response
  [request setHTTPBody:postData];

  NSURLConnection *conn=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
  if (conn){
    NSLog(@"In if conn");
    receivedData = [[NSMutableData data] retain];
    NSLog(@"End of if conn");
  }
  else{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Conn error" message:@"No Server" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
  }

}//initiateAPIConnection

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Connection error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
  [alert show];
  [alert release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
  NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
  NSLog(@"%i",[urlResponse statusCode]);
  [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  [receivedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  xmlParser = [[NSXMLParser alloc]initWithData:receivedData];
  [xmlParser setDelegate:self];

  //you may want to enable these features of NSXMLParser.
  [xmlParser setShouldProcessNamespaces:NO];
  [xmlParser setShouldReportNamespacePrefixes:NO];
  [xmlParser setShouldResolveExternalEntities:NO];
  [xmlParser parse];
}


//XMLdeligate methods
-(void)parserDidStartDocument:(NSXMLParser *)parser{
  NSLog(@"Started Parsing");
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
  NSLog(@"Started Element name: %@", elementName);
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
  NSLog(@"Found characters: %@", string);
  tempString = string;
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
  NSLog(@"Finished Element name: %@", elementName);

  //my outputted xml would have <ranking>...ValueFromDb...</ranking> in it's response
  if([elementName isEqualToString:@"ranking"]){
    //display result in a label (you could save it to a local variable instead)
    label1.text = tempString;
  }

}

-(void)parserDidEndDocument:(NSXMLParser *)parser{
  NSLog(@"Finished Parsing");
}


//...


//Don't forget to dealloc
-(void)dealloc {
  //...
  [label1 release];
  [tempString release];
  [xmlParser release];
  [receivedData release];
  //...
  [super dealloc];
}
//IN THE .m class

You'll have to work out the logic required to search the database for the user in questions ranking yourself.您必须制定出在数据库中搜索用户对自己进行排名的问题所需的逻辑。 You can pass login information by appending (eg)?user=USERNAME&pass=PASSWORD to the end of the.php file... ie ...您可以通过将 (eg)?user=USERNAME&pass=PASSWORD 附加到 .php 文件的末尾来传递登录信息...即...

[request setURL:[NSURL URLWithString:@"http://www.yourDomain.com/yourPhpPage.php?user=USERNAME&pass=PASSWORD"]];

USERNAME and PASSWORD would be values read from the sandbox etc. ... you'll need to format the URLWithString (like you do with stringWithFormat) USERNAME 和 PASSWORD 将是从沙箱等中读取的值。...您需要格式化 URLWithString(就像使用 stringWithFormat 一样)

-Chris Allinson ——克里斯·阿林森

yeah this is the best option in my opinion that to use rank function in that screen where you are fetching or viewing users information..是的,我认为这是在您获取或查看用户信息的屏幕中使用排名 function 的最佳选择。

you can use different function for this screen where you have to select user sorted by there score decremently, by this kind of programming you don't have to do ay extra work您可以在此屏幕上使用不同的 function ,您必须对 select 用户按分数递减排序,通过这种编程,您不必做任何额外的工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM