// // IJInventoryItem.m // InsideJob // // Created by Adam Preble on 10/7/10. // Copyright 2010 Adam Preble. All rights reserved. // #import "IJInventoryItem.h" @implementation IJInventoryItem @synthesize itemId, slot, damage, count; + (id)emptyItemWithSlot:(uint8_t)slot { IJInventoryItem *obj = [[[[self class] alloc] init] autorelease]; obj.slot = slot; return obj; } - (NSString *)itemName { NSString *name = [[IJInventoryItem itemIdLookup] objectForKey:[NSNumber numberWithShort:self.itemId]]; if (name) return name; else return [NSString stringWithFormat:@"%d", self.itemId]; } + (NSDictionary *)itemIdLookup { static NSDictionary *lookup = nil; if (!lookup) { NSError *error = nil; NSString *lines = [NSString stringWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Items" withExtension:@"csv"] encoding:NSUTF8StringEncoding error:&error]; NSMutableDictionary *building = [NSMutableDictionary dictionary]; [lines enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) { NSArray *components = [line componentsSeparatedByString:@","]; NSNumber *itemId = [NSNumber numberWithShort:[[components objectAtIndex:0] intValue]]; NSString *name = [components objectAtIndex:1]; [building setObject:name forKey:itemId]; }]; lookup = [[NSDictionary alloc] initWithDictionary:building]; } return lookup; } @end