Skip to content

Instantly share code, notes, and snippets.

@anthonycvella
Created June 3, 2012 20:40
Show Gist options
  • Save anthonycvella/2864951 to your computer and use it in GitHub Desktop.
Save anthonycvella/2864951 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;
NSDictionary *_ip;
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];
}
- (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];
}];
}
#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"];
}
_ip = [_dataJSON objectAtIndex:indexPath.row];
NSDictionary *details = [_ip objectForKey:@"details"];
_serverIP = [_ip objectForKey:@"ip"];
_gameState = [details objectForKey:@"gamestate"];
_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(@"State: %@", _gameState);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"ShowSelectedServers" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ShowSelectedServers"])
{
DetailsViewController *detailViewController = [segue destinationViewController];
NSIndexPath * indexPath = (NSIndexPath*)sender;
detailViewController.ip = _ip;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment