Skip to content

Commit

Permalink
Updated the database to Mosaic 1.0. Improvements to the Contract feat…
Browse files Browse the repository at this point in the history
…ure to update contracts that are Outstanding, but are not in the default API results.
  • Loading branch information
Bleyddyn committed Apr 28, 2015
1 parent 4b52ec9 commit 8f69f99
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 19 deletions.
4 changes: 2 additions & 2 deletions dbscripts/build_dbexport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
DBEXPORT=database.sql

# This should match what's in the application's user defaults under UD_DATABASE_MIN_VERSION
DBVERSION=22
DBEXPANSION="Scylla 1.1"
DBVERSION=23
DBEXPANSION="Mosaic 1.0"

VERQUERY="INSERT INTO version VALUES ($DBVERSION,'$DBEXPANSION');"

Expand Down
1 change: 1 addition & 0 deletions src/Contracts/Contract.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ - (void)preloadItems
NSMutableArray *tempItems = [self loadContractItems];
if( [tempItems count] > 0 )
{
[[self items] removeAllObjects];
[[self items] addObjectsFromArray:tempItems];
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/Contracts/Contracts.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@

- (IBAction)reload:(id)sender;
- (void)sortUsingDescriptors:(NSArray *)descriptors;
- (void)requestContract:(NSNumber *)contractID;
@end
75 changes: 66 additions & 9 deletions src/Contracts/Contracts.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#import "XMLDownloadOperation.h"
#import "XMLParseOperation.h"
#import "METURLRequest.h"

#include <assert.h>

Expand Down Expand Up @@ -154,8 +155,14 @@ - (void) parserOperationDone:(id)ignore errors:(NSArray *)errors
NSLog(@"Failed to read %@",[self xmlPath]);
return;
}
[self parseXmlContracts:doc];
[[self contracts] removeAllObjects];
[[self contracts] addObjectsFromArray:[self parseXmlContracts:doc]];
xmlFreeDoc(doc);

if( [[self delegate] respondsToSelector:@selector(contractsFinishedUpdating:)] )
{
[[self delegate] performSelector:@selector(contractsFinishedUpdating:) withObject:[self contracts]];
}
}

/* Sample xml for contracts:
Expand All @@ -172,7 +179,7 @@ - (void) parserOperationDone:(id)ignore errors:(NSArray *)errors
<cachedUntil>2011-07-30 05:44:30</cachedUntil>
</eveapi>
*/
-(BOOL) parseXmlContracts:(xmlDoc*)document
-(NSArray *) parseXmlContracts:(xmlDoc*)document
{
xmlNode *root_node;
xmlNode *result;
Expand All @@ -190,7 +197,7 @@ -(BOOL) parseXmlContracts:(xmlDoc*)document
{
NSLog( @"%@", [NSString stringWithString:getNodeText(xmlErrorMessage)] );
}
return NO;
return nil;
}

rowset = findChildNode(result,(xmlChar*)"rowset");
Expand All @@ -203,10 +210,10 @@ -(BOOL) parseXmlContracts:(xmlDoc*)document
{
NSLog( @"%@", [NSString stringWithString:getNodeText(xmlErrorMessage)] );
}
return NO;
return nil;
}

[[self contracts] removeAllObjects];
NSMutableArray *localContracts = [NSMutableArray array];

for( xmlNode *cur_node = rowset->children;
NULL != cur_node;
Expand Down Expand Up @@ -318,7 +325,7 @@ -(BOOL) parseXmlContracts:(xmlDoc*)document
[contract setAcceptorID:[value integerValue]];
}
}
[[self contracts] addObject:contract];
[localContracts addObject:contract];
}
}

Expand All @@ -333,11 +340,61 @@ -(BOOL) parseXmlContracts:(xmlDoc*)document

}

if( [[self delegate] respondsToSelector:@selector(contractsFinishedUpdating)] )
return localContracts;
}

- (void)requestContract:(NSNumber *)contractID
{
if( ![self character] )
return;

CharacterTemplate *template = [[self character] template];
if( !template )
return;

NSString *apiUrl = [Config getApiUrl:XMLAPI_CHAR_CONTRACTS
keyID:[template accountId]
verificationCode:[template verificationCode]
charId:[template characterId]];
if( contractID )
apiUrl = [apiUrl stringByAppendingFormat:@"&contractID=%ld", (unsigned long)[contractID unsignedIntegerValue]];
NSURL *url = [NSURL URLWithString:apiUrl];
METURLRequest *request = [METURLRequest requestWithURL:url];
[request setDelegate:self];
[NSURLConnection connectionWithRequest:request delegate:request];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection withError:(NSError *)error
{
if( error )
{
NSLog( @"Error requesting a contract: %@", [error localizedDescription] );
return;
}

METURLRequest *request = (METURLRequest *)[connection originalRequest];
NSMutableData *data = [request data];
const char *ptr = [data bytes];
NSInteger length = [data length];

if(length == 0){
NSLog(@"Zero bytes returned for contract data");
return;
}

xmlDoc *doc = xmlReadMemory(ptr, (int)length, NULL, NULL, 0);
if( doc == NULL )
{
NSLog(@"Failed to read contract data");
return;
}
NSArray *newContracts = [self parseXmlContracts:doc];
xmlFreeDoc(doc);

if( [[self delegate] respondsToSelector:@selector(contractFinishedUpdating:)] )
{
[[self delegate] performSelector:@selector(contractsFinishedUpdating)];
[[self delegate] performSelector:@selector(contractFinishedUpdating:) withObject:newContracts];
}
return YES;
}

@end
66 changes: 60 additions & 6 deletions src/Contracts/ContractsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,30 @@ -(NSMenuItem*) menuItems
return nil;
}

- (void)contractsFinishedUpdating
- (void)updateContracts:(NSArray *)newContracts andClose:(BOOL)close
{
[self saveContracts:[contracts contracts]];
[self saveContracts:newContracts];
[self setDbContracts:[self loadContracts]];
NSArray *newDescriptors = [contractsTable sortDescriptors];
[contracts sortUsingDescriptors:newDescriptors];
[[self dbContracts] sortUsingDescriptors:newDescriptors];
if( close )
[self closeOlderContracts:newContracts];
[contractsTable reloadData];
[app setToolbarMessage:NSLocalizedString(@"Finished Updating Contracts…",@"Finished Updating Contracts status line") time:5];
[app stopLoadingAnimation];
}

- (void)contractsFinishedUpdating:(NSArray *)newContracts
{
[self updateContracts:newContracts andClose:YES];
}

- (void)contractFinishedUpdating:(NSArray *)newContracts
{
[self updateContracts:newContracts andClose:NO];
}

- (void)contractsSkippedUpdating
{
[app setToolbarMessage:NSLocalizedString(@"Using Cached Contracts…",@"Using Cached Contracts status line") time:5];
Expand Down Expand Up @@ -318,10 +330,10 @@ - (NSMutableArray *)loadContracts
[aContract setIssuerCorpID:sqlite3_column_nsint( read_stmt, 11 )];
[aContract setAssigneeID:sqlite3_column_nsint( read_stmt, 12 )];
[aContract setAcceptorID:sqlite3_column_nsint( read_stmt, 13 )];
[aContract setIssued:[NSDate dateWithTimeIntervalSince1970:sqlite3_column_nsint(read_stmt,14)]];
[aContract setExpired:[NSDate dateWithTimeIntervalSince1970:sqlite3_column_nsint(read_stmt,15)]];
[aContract setAccepted:[NSDate dateWithTimeIntervalSince1970:sqlite3_column_nsint(read_stmt,16)]];
[aContract setCompleted:[NSDate dateWithTimeIntervalSince1970:sqlite3_column_nsint(read_stmt,17)]];
[aContract setIssued:sqlite3_column_nsdate(read_stmt,14)];
[aContract setExpired:sqlite3_column_nsdate(read_stmt,15)];
[aContract setAccepted:sqlite3_column_nsdate(read_stmt,16)];
[aContract setCompleted:sqlite3_column_nsdate(read_stmt,17)];
[aContract setAvailability:sqlite3_column_nsstr(read_stmt,18)];
[aContract setTitle:sqlite3_column_nsstr(read_stmt,19)];
[aContract setDays:sqlite3_column_nsint(read_stmt,20)];
Expand Down Expand Up @@ -420,4 +432,46 @@ - (BOOL)saveContracts:(NSArray *)newOrders
return success;
}

- (BOOL)contracts:(NSArray *)localContracts containContractID:(NSInteger)contractID
{
NSUInteger index = [localContracts indexOfObjectPassingTest:^BOOL (id el, NSUInteger i, BOOL *stop)
{
BOOL res = [(Contract *)el contractID] == contractID;
if( res )
*stop = YES;
return res;
}];
return NSNotFound != index;
}

// Try to find orders that slipped through the cracks and see if they are closed or expired
- (BOOL)closeOlderContracts:(NSArray *)newOrders
{
NSMutableArray *changes = [NSMutableArray array];
// filter dbOrders for ones that are open
// for each, if it is not in newOrders, then request that order specifically from the API
NSIndexSet *indexes = [[self dbContracts] indexesOfObjectsPassingTest:^BOOL (id el, NSUInteger i, BOOL *stop)
{
return [@"Outstanding" isEqualToString:[(Contract *)el status]];
}];
NSArray *openOrders = [[self dbContracts] objectsAtIndexes:indexes];

for( Contract *aContract in openOrders )
{
BOOL res = [self contracts:newOrders containContractID:[aContract contractID]];
if( !res )
{
// request this market order by id
[aContract setStatus:@"Unknown"]; // for now just set it to unknown
[contracts requestContract:[NSNumber numberWithInteger:[aContract contractID]]];
[changes addObject:aContract];
}
}

// if( [changes count] > 0 )
// [self saveMarketOrders:changes];

return YES;
}

@end
12 changes: 12 additions & 0 deletions src/Core/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ CGFloat skillPercentCompleted(NSInteger startingPoints, NSInteger finishingPoint
*/
NSString* sqlite3_column_nsstr(void *stmt, int col);

/**
Wrapper for `sqlite3_column_nsint` that returns an `NSDate`.
@param stmt The SQLite prepared statement being evaluated.
@param col The index of the text column to return.
@return The contents of a datetime column as a Unix date, returned as an NSDate.
A database value of zero is considered to be NULL and a nil pointer will be returned.
*/
NSDate *sqlite3_column_nsdate( void *stmt, int col );

/**
Returns the language corresponding to a `DatabaseLanguage` member.
Expand Down
8 changes: 8 additions & 0 deletions src/Core/Helpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ CGFloat skillPercentCompleted(NSInteger startingPoints, NSInteger finishingPoint
}
}

NSDate *sqlite3_column_nsdate( void *stmt, int col )
{
NSInteger value = sqlite3_column_nsint( stmt, col );
if( 0 == value )
return nil;
return [NSDate dateWithTimeIntervalSince1970:value];
}

NSString* languageForId(enum DatabaseLanguage lang) {
NSString *str;

Expand Down
4 changes: 2 additions & 2 deletions src/vitality.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.3.3b</string>
<string>0.3.4b</string>
<key>CFBundleVersion</key>
<string>0.3.3b</string>
<string>0.3.4b</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSMainNibFile</key>
Expand Down

0 comments on commit 8f69f99

Please sign in to comment.