All pastes #990692 Raw Edit

Stuff

public text v1 · immutable
#990692 ·published 2008-04-20 10:50 UTC
rendered paste body
//
//  CardInfo.m
//  Browser1
//
//  Created by Yann Disser on 18.4.2008.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CardInfo.h"
#import "IKBBrowserItem.h"

static NSMutableArray* sortCriteria;

@implementation CardInfo

-(CardInfo*)init
{
  NSLog(@"simple init called");
  return self;
}

-(CardInfo*)initWithTokens:(NSArray*)tokens path:(NSString*)p
{
  [super init];
  
  NSArray* keys = [NSArray arrayWithObjects: @"number", @"name",
                   @"type", @"p/t", @"cost", @"color", @"rarity",
                   @"edition", @"artist", @"rules", nil];
  
  NSMutableArray* obj = [NSMutableArray 
                     arrayWithArray:[tokens subarrayWithRange:NSMakeRange(0,9)]];
  int i;
  NSString* rules = @"";
  for(i = 9; i < [tokens count]; ++i)
    rules = [[rules stringByAppendingString:@"\n"] 
              stringByAppendingString:[tokens objectAtIndex:i]];
  [obj addObject:rules];
  
  fields = [NSDictionary dictionaryWithObjects:obj forKeys:keys];
  
  NSString* file = [[p stringByAppendingString:[obj objectAtIndex:0]]
                       stringByAppendingString:@".jpg"];
  NSImage* img = [[NSImage alloc] initByReferencingFile:file];
  image = [[IKBBrowserItem alloc] initWithImage:img imageID:file];
  
  //NSLog(@"survive shit");
  //[fields objectForKey:@"name"];
  
  return self;
}

-(IKBBrowserItem*) getImage
{ return image; }
-(NSString*) getField:(NSString*)field
{ return [fields objectForKey:field]; }
-(id) getTypedField:(NSString*)field
{
  NSString* f = [self getField:field];
  if([field isEqual:@"number"])
    return [NSNumber numberWithInt:[f intValue]];
  
  if([field isEqual:@"cost"])
    return [NSNumber numberWithInt:([f intValue] + [f length])];
  
  if([field isEqual:@"rarity"])
  {
    f = [f stringByReplacingOccurrencesOfString:@"R" withString:@"A"];
    f = [f stringByReplacingOccurrencesOfString:@"U" withString:@"B"];
    f = [f stringByReplacingOccurrencesOfString:@"C" withString:@"C"];
    f = [f stringByReplacingOccurrencesOfString:@"L" withString:@"D"];
  }
  
  if([field isEqual:@"color"])
  {
    f = [f stringByReplacingOccurrencesOfString:@"White" withString:@"A"];
    f = [f stringByReplacingOccurrencesOfString:@"Blue" withString:@"B"];
    f = [f stringByReplacingOccurrencesOfString:@"Black" withString:@"C"];
    f = [f stringByReplacingOccurrencesOfString:@"Red" withString:@"D"];
    f = [f stringByReplacingOccurrencesOfString:@"Green" withString:@"E"];
    f = [f stringByReplacingOccurrencesOfString:@"Multicolor" withString:@"F"];
    f = [f stringByReplacingOccurrencesOfString:@"Artifact" withString:@"G"];
    f = [f stringByReplacingOccurrencesOfString:@"Land" withString:@"H"];
  }
  
  return f;
  //@"p/t"
}

-(NSComparisonResult)compare:(CardInfo*)other
{
   //crash
  //[self getField:@"name"];
  [fields objectForKey:@"name"];
  NSLog(@"kickin");
  
  if(sortCriteria == nil)
    sortCriteria = [[NSMutableArray alloc] initWithObjects: @"number", @"name",
                    @"cost", @"type", @"rarity", nil];
  
  int i;
  for(i = 0; i < [sortCriteria count]; ++i)
  {
    NSString* crit = [sortCriteria objectAtIndex:i];
    NSComparisonResult res = [[self getTypedField:crit] compare:[other getTypedField:crit]];
    if(res != NSOrderedSame)
      return res;
  }
  return NSOrderedSame;
}

+(void) changeSortCriterion:(NSString*)crit
{
  if(sortCriteria == nil)
    sortCriteria = [[NSMutableArray alloc] initWithObjects: @"number", @"name",
                                              @"cost", @"type", @"rarity", nil];
  
  [sortCriteria removeObjectIdenticalTo:crit];
  if([sortCriteria count] == 4)
    [sortCriteria insertObject:crit atIndex:0];
}

@end