Skip to content

Commit

Permalink
Adds background to main game list. Query nearby games after first loo…
Browse files Browse the repository at this point in the history
…king up the user's location.
  • Loading branch information
aaronpk committed Sep 8, 2011
1 parent 926fdc9 commit 5823c9a
Show file tree
Hide file tree
Showing 21 changed files with 425 additions and 82 deletions.
10 changes: 8 additions & 2 deletions Classes/GameListViewController.h
Expand Up @@ -8,10 +8,16 @@


#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "GameCell.h" #import "GameCell.h"
#import <CoreLocation/CoreLocation.h>


@interface GameListViewController : UIViewController <UITableViewDelegate> { @interface GameListViewController : UIViewController <UITableViewDelegate, CLLocationManagerDelegate> {
IBOutlet GameCell *gameCell; IBOutlet GameCell *gameCell;
NSMutableArray *games; NSMutableArray *games;
#ifdef FAKE_CORE_LOCATION
FTLocationSimulator *locationManager;
#else
CLLocationManager *locationManager;
#endif
} }


@property (nonatomic, retain) IBOutlet UIButton *reloadBtn; @property (nonatomic, retain) IBOutlet UIButton *reloadBtn;
Expand All @@ -22,6 +28,6 @@


- (IBAction)reloadBtnPressed; - (IBAction)reloadBtnPressed;
- (IBAction)logoutBtnPressed; - (IBAction)logoutBtnPressed;
- (void)getNearbyLayers; - (void)refreshNearbyLayers;


@end @end
30 changes: 26 additions & 4 deletions Classes/GameListViewController.m
Expand Up @@ -21,6 +21,7 @@ - (void)dealloc {
[selectedIndex release]; [selectedIndex release];
[tableView release]; [tableView release];
[reloadBtn release]; [reloadBtn release];
[locationManager release];
[super dealloc]; [super dealloc];
} }


Expand All @@ -46,22 +47,43 @@ - (void)loadView {
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
games = [[NSMutableArray alloc] init]; games = [[NSMutableArray alloc] init];
[self getNearbyLayers]; [self refreshNearbyLayers];
} }


- (IBAction)reloadBtnPressed { - (IBAction)reloadBtnPressed {
[self getNearbyLayers]; [self refreshNearbyLayers];
} }


- (IBAction)logoutBtnPressed { - (IBAction)logoutBtnPressed {
[[LQClient single] logout]; [[LQClient single] logout];
} }


- (void)getNearbyLayers { - (void)refreshNearbyLayers {
[[LQClient single] getNearbyLayers:^(NSError *error, NSDictionary *response){ if (!locationManager) {
#ifdef FAKE_CORE_LOCATION
locationManager = [[FTLocationSimulator alloc] init];
#else
locationManager = [[CLLocationManager alloc] init];
#endif
locationManager.distanceFilter = 1.0;
locationManager.delegate = self;
}

[locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {

[[LQClient single] getNearbyLayers:newLocation withCallback:^(NSError *error, NSDictionary *response){
self.games = [response objectForKey:@"nearby"]; self.games = [response objectForKey:@"nearby"];
NSLog(@"Found games: %@", self.games);
[self.tableView reloadData]; [self.tableView reloadData];
}]; }];

[locationManager stopUpdatingLocation];
} }


- (NSString *)urlForGameAtIndex:(NSInteger)index { - (NSString *)urlForGameAtIndex:(NSInteger)index {
Expand Down
17 changes: 15 additions & 2 deletions Classes/GeoloqiReadClient.m
Expand Up @@ -17,6 +17,7 @@
#define TAG_DEVICE_ID_SENT 1 #define TAG_DEVICE_ID_SENT 1
#define TAG_MESSAGE_RECEIVED 2 #define TAG_MESSAGE_RECEIVED 2


#define VERBOSE 0


@implementation GeoloqiReadClient @implementation GeoloqiReadClient


Expand Down Expand Up @@ -66,17 +67,29 @@ - (void)reconnect
// After the client finishes writing the UUID, start listening for new data // After the client finishes writing the UUID, start listening for new data
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{ {
DLog(@"[Read] Did write data with tag %d", tag); if(VERBOSE)
DLog(@"[Read] Did write data with tag %d", tag);
[asyncSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:TAG_MESSAGE_RECEIVED]; [asyncSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:TAG_MESSAGE_RECEIVED];
} }




- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{ {
DLog(@"[Read] Did read data with tag %d: %@", tag, data); if(VERBOSE)
DLog(@"[Read] Did read data with tag %d: %@", tag, data);

if([data isEqualToData:[@"ok\r\n" dataUsingEncoding:NSUTF8StringEncoding]]) {
if(VERBOSE)
DLog(@"[Read] Got 'ok' response");
[asyncSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:TAG_MESSAGE_RECEIVED];
return;
}

NSError **err; NSError **err;
NSDictionary *dict; NSDictionary *dict;


// DLog(@"[Read] String: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

dict = [[CJSONDeserializer deserializer] deserialize:data error:err]; dict = [[CJSONDeserializer deserializer] deserialize:data error:err];
DLog(@"[Read] Message: %@", dict); DLog(@"[Read] Message: %@", dict);


Expand Down
4 changes: 1 addition & 3 deletions Classes/GeoloqiSocketClient.h
Expand Up @@ -32,8 +32,6 @@
- (NSData *)dataFromLocation:(CLLocation *)location; - (NSData *)dataFromLocation:(CLLocation *)location;
- (void)startMonitoringLocation; - (void)startMonitoringLocation;
- (void)stopMonitoringLocation; - (void)stopMonitoringLocation;

- (BOOL)locationUpdateState;
// TODO: Make a delegate protocol.
// TODO: Send CLLocation to server.


@end @end
37 changes: 24 additions & 13 deletions Classes/GeoloqiSocketClient.m
Expand Up @@ -14,6 +14,8 @@
#define TIMEOUT_SEC 6.0 #define TIMEOUT_SEC 6.0
#define TAG_DEVICE_ID_SENT 1 #define TAG_DEVICE_ID_SENT 1


#define VERBOSE 0

#if LITTLE_ENDIAN #if LITTLE_ENDIAN


#pragma pack(push) /* push current alignment to stack */ #pragma pack(push) /* push current alignment to stack */
Expand Down Expand Up @@ -47,7 +49,7 @@ - (id) init
{ {
// Change to use UDP // Change to use UDP
asyncSocket = [[AsyncUdpSocket alloc] initWithDelegate:self]; asyncSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
distanceFilterDistance = 1.0; distanceFilterDistance = 0.5;
trackingFrequency = 1; trackingFrequency = 1;
sendingFrequency = 1; sendingFrequency = 1;
uuid = [[MapAttackAppDelegate UUID] retain]; uuid = [[MapAttackAppDelegate UUID] retain];
Expand All @@ -66,16 +68,15 @@ - (void) dealloc
[super dealloc]; [super dealloc];
} }



- (void) normalConnect - (void) normalConnect
{ {
NSError *error = nil; NSError *error = nil;


NSString *host = LQ_WRITE_SOCKET_HOST; NSString *host = LQ_WRITE_SOCKET_HOST;
UInt16 port = LQ_WRITE_SOCKET_PORT; UInt16 port = LQ_WRITE_SOCKET_PORT;

DLog(@"[Write] Connecting to %@:%i", host, port);


DLog(@"[Write] Connecting to %@:%i", host, port);

// Change to use UDP // Change to use UDP
if (![asyncSocket connectToHost:LQ_WRITE_SOCKET_HOST onPort:LQ_WRITE_SOCKET_PORT error:&error]) if (![asyncSocket connectToHost:LQ_WRITE_SOCKET_HOST onPort:LQ_WRITE_SOCKET_PORT error:&error])
{ {
Expand All @@ -85,7 +86,9 @@ - (void) normalConnect


#pragma mark - #pragma mark -



- (BOOL)locationUpdateState {
return locationUpdatesOn;
}


- (void)startMonitoringLocation { - (void)startMonitoringLocation {
if (!locationManager) { if (!locationManager) {
Expand Down Expand Up @@ -153,39 +156,46 @@ - (void)locationManager:(CLLocationManager *)manager
// object:self]; // object:self];


NSData *data = [self dataFromLocation:newLocation]; NSData *data = [self dataFromLocation:newLocation];
DLog(@"[Write] Sending location data: %@", data); if(VERBOSE)
DLog(@"[Write] Sending location data: %@", data);
[asyncSocket sendData:data toHost:LQ_WRITE_SOCKET_HOST port:LQ_WRITE_SOCKET_PORT withTimeout:10.0 tag:TAG_DEVICE_ID_SENT]; [asyncSocket sendData:data toHost:LQ_WRITE_SOCKET_HOST port:LQ_WRITE_SOCKET_PORT withTimeout:10.0 tag:TAG_DEVICE_ID_SENT];
//Look for ack back //Look for ack back
[asyncSocket receiveWithTimeout:30.0 tag:TAG_DEVICE_ID_SENT]; [asyncSocket receiveWithTimeout:30.0 tag:TAG_DEVICE_ID_SENT];


} else { } else {
#if LQ_LOCMAN_DEBUG #if LQ_LOCMAN_DEBUG
DLog(@"[Write] Location update (to %@; from %@) rejected", newLocation, oldLocation); if(VERBOSE)
DLog(@"[Write] Location update (to %@; from %@) rejected", newLocation, oldLocation);
#endif #endif
} }
} }


- (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag; - (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag;
{ {
DLog(@"[Write] did send"); if(VERBOSE)
DLog(@"[Write] did send");
} }


- (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error; - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error;
{ {
DLog(@"[Write] did not get ack back"); if(VERBOSE)
DLog(@"[Write] did not get ack back");
} }


- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port; - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port;
{ {
//TODO: determine if this is a valid packet //TODO: determine if this is a valid packet
DLog(@"[Write] Recieved packet back from server: %@", data); if(VERBOSE)
DLog(@"[Write] Recieved packet back from server: %@", data);


if (data.length == sizeof(uint32_t)) { if (data.length == sizeof(uint32_t)) {
uint32_t time = OSSwapBigToHostInt32(*(uint32_t *)data.bytes); uint32_t time = OSSwapBigToHostInt32(*(uint32_t *)data.bytes);
DLog(@"[Write] Accepted packet with timestamp: %u", time); if(VERBOSE)
DLog(@"[Write] Accepted packet with timestamp: %u", time);
return YES; return YES;
} else { } else {
DLog(@"[Write] packet invalid size: %d", data.length); if(VERBOSE)
DLog(@"[Write] packet invalid size: %d", data.length);
return NO; return NO;
} }
} }
Expand Down Expand Up @@ -224,7 +234,8 @@ - (NSData *)dataFromLocation:(CLLocation *)location {
// DLog(@"Offset of command: %lu", offsetof(LQUpdatePacket, f.command)); // DLog(@"Offset of command: %lu", offsetof(LQUpdatePacket, f.command));
// DLog(@"Offset of date: %lu", offsetof(LQUpdatePacket, f.date)); // DLog(@"Offset of date: %lu", offsetof(LQUpdatePacket, f.date));


DLog(@"[Write] Sending timestamp: %d", update.f.date); // if(VERBOSE)
DLog(@"[Write] Sending location update %@", [NSData dataWithBytes:update.bytes length:sizeof(update.bytes)]);


//Swap endianness of each 16 bit int //Swap endianness of each 16 bit int
update.f.date = OSSwapHostToBigInt32(update.f.date); update.f.date = OSSwapHostToBigInt32(update.f.date);
Expand Down
5 changes: 3 additions & 2 deletions Classes/LQClient.h
Expand Up @@ -7,6 +7,7 @@
// //


#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "ASIHTTPRequest.h" #import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h" #import "ASIFormDataRequest.h"


Expand All @@ -21,7 +22,7 @@ typedef void (^LQHTTPRequestCallback)(NSError *error, NSDictionary *response);


@interface LQClient : NSObject { @interface LQClient : NSObject {
// NSMutableArray *queue; // NSMutableArray *queue;
ASIHTTPRequest *authenticationRequest; // ASIHTTPRequest *authenticationRequest;
} }


@property (nonatomic, copy) NSString *accessToken; @property (nonatomic, copy) NSString *accessToken;
Expand All @@ -34,7 +35,7 @@ typedef void (^LQHTTPRequestCallback)(NSError *error, NSDictionary *response);
- (BOOL)isLoggedIn; - (BOOL)isLoggedIn;
// - (NSString *)refreshToken; // - (NSString *)refreshToken;
- (void)sendPushToken:(NSString *)token withCallback:(LQHTTPRequestCallback)callback; - (void)sendPushToken:(NSString *)token withCallback:(LQHTTPRequestCallback)callback;
- (void)getNearbyLayers:(LQHTTPRequestCallback)callback; - (void)getNearbyLayers:(CLLocation *)location withCallback:(LQHTTPRequestCallback)callback;
- (void)createNewAccountWithEmail:(NSString *)email initials:(NSString *)initials callback:(LQHTTPRequestCallback)callback; - (void)createNewAccountWithEmail:(NSString *)email initials:(NSString *)initials callback:(LQHTTPRequestCallback)callback;
- (void)joinGame:(NSString *)layer_id withToken:(NSString *)group_token; - (void)joinGame:(NSString *)layer_id withToken:(NSString *)group_token;
- (void)logout; - (void)logout;
Expand Down
7 changes: 4 additions & 3 deletions Classes/LQClient.m
Expand Up @@ -38,7 +38,6 @@ - (id) init
} }


- (void)dealloc { - (void)dealloc {
// [queue release];
[super dealloc]; [super dealloc];
} }


Expand Down Expand Up @@ -260,8 +259,10 @@ - (void)sendPushToken:(NSString *)token withCallback:(LQHTTPRequestCallback)call
[self runRequest:request callback:callback]; [self runRequest:request callback:callback];
} }


- (void)getNearbyLayers:(LQHTTPRequestCallback)callback { - (void)getNearbyLayers:(CLLocation *)location withCallback:(LQHTTPRequestCallback)callback {
NSURL *url = [self urlWithPath:[NSString stringWithFormat:@"layer/nearby?latitude=45.5246&longitude=-122.6843&application_id=%@", MapAttackAppID]]; NSURL *url = [self urlWithPath:[NSString stringWithFormat:@"layer/nearby?latitude=%f&longitude=%f&application_id=%@",
//45.5246, -122.6843, MapAttackAppID]];
location.coordinate.latitude, location.coordinate.longitude, MapAttackAppID]];
__block ASIHTTPRequest *request; __block ASIHTTPRequest *request;
if([self isLoggedIn]) { if([self isLoggedIn]) {
request = [self userRequestWithURL:url]; request = [self userRequestWithURL:url];
Expand Down
29 changes: 29 additions & 0 deletions Classes/LQPushHandler.h
@@ -0,0 +1,29 @@
//
// LQPushHandler.h
// Geoloqi
//
// Created by Aaron Parecki on 12/23/10.
// Copyright 2010 Geoloqi.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

enum {
kLQPushAlertGeonote = 0,
kLQPushAlertShutdown,
kLQPushAlertStart
};

@interface LQPushHandler : NSObject <UIAlertViewDelegate> {
NSString *lastAlertURL;
}

@property (nonatomic, retain) NSString *lastAlertURL;

- (id)myInit;
- (void)handlePush:(UIApplication *)application notification:(NSDictionary *)userInfo;
- (void)handleLocalNotificationFromApp:(UIApplication *)app notif:(UILocalNotification *)notif;
- (void)handleLaunch:(NSDictionary *)launchOptions;

@end

0 comments on commit 5823c9a

Please sign in to comment.