Skip to content

Instantly share code, notes, and snippets.

@anthonycvella
Created June 2, 2012 20:28
Show Gist options
  • Save anthonycvella/2859834 to your computer and use it in GitHub Desktop.
Save anthonycvella/2859834 to your computer and use it in GitHub Desktop.
//
// ServerViewController.m
// HungerCraft
//
// Created by Anthony Vella on 5/31/12.
// Copyright (c) 2012 Aurora High School. All rights reserved.
//
#import "ServerViewController.h"
#import "DetailsViewController.h"
@interface ServerViewController () <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *_tableView;
NSMutableArray *_dataJSON;
NSString *serverIP;
NSString *gameState;
NSString *map;
UIImage *statImage;
}
- (void)refreshServers;
@end
@implementation ServerViewController
@synthesize _dataJSON;
- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshServers];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[self refreshServers];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)JSONLoader:(id)sender
{
[self refreshServers];
}
- (void)refreshServers
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://old.hungercraft.net/serverlist.php"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
_dataJSON = [json copy];
[_tableView reloadData];
//NSLog(@"IP: %@", _data);
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataJSON count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServerCell"];
if (cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ServerCell"];
}
NSDictionary *ip = [_dataJSON objectAtIndex:indexPath.row];
NSDictionary *details = [ip objectForKey:@"details"];
NSString *serverIP = [ip objectForKey:@"ip"];
NSString *gameState = [details objectForKey:@"gamestate"];
NSString *map = [details objectForKey:@"map"];
NSString *status = [details objectForKey:@"stat"];
NSLog(@"%@", [ip allKeys]);
if([status isEqualToString:@"Online"])
{
statImage = [UIImage imageNamed:@"status_on.png"];
} else {
statImage = [UIImage imageNamed:@"status_off.png"];
}
cell.textLabel.text = serverIP;
cell.imageView.image = statImage;
//NSLog(@"Status: %@", [ip objectForKey:@"stat"]);
return cell;
}
#pragma mark - Table view delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowSelectedServers"])
{
DetailsViewController *detailViewController = [segue destinationViewController];
detailViewController.serverIP = serverIP;
detailViewController.gameState = gameState;
detailViewController.map = map;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment