2 // HMChangeHenseiCommand.m
5 // Created by Hori,Masaki on 2014/06/15.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMChangeHenseiCommand.h"
11 #import "HMServerDataStore.h"
13 @interface HMChangeHenseiCommand ()
14 @property (nonatomic, strong) HMServerDataStore *store;
17 @implementation HMChangeHenseiCommand
20 static dispatch_once_t onceToken;
21 dispatch_once(&onceToken, ^{
22 [HMJSONCommand registerClass:self];
26 + (BOOL)canExcuteAPI:(NSString *)api
28 return [api isEqualToString:@"/kcsapi/api_req_hensei/change"];
31 - (HMServerDataStore *)store
33 if(_store) return _store;
35 _store = [HMServerDataStore oneTimeEditor];
42 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
43 NSArray *decks = [self.store objectsWithEntityName:@"Deck"
44 sortDescriptors:@[sortDescriptor]
47 // TODO: error handling
49 NSMutableArray *ships = [NSMutableArray new];
50 for(id deck in decks) {
51 for(NSUInteger i = 0; i < 6; i++) {
52 [ships addObject:[deck valueForKey:[NSString stringWithFormat:@"ship_%ld", i]]];
56 for(int i = 0; i < 4; i++) {
57 fprintf(stderr, "Fleet %d\n", i+1);
58 for(int j = 0; j < 6; j++) {
59 fprintf(stderr, "%d -> %ld\n", j+1, [ships[i*6+j] integerValue]);
62 fprintf(stderr, "\n");
68 NSArray *decks = [self.store objectsWithEntityName:@"Deck"
71 // TODO: error handling
73 for(id deck in decks) {
75 for(NSInteger i = 0; i < 6; i++) {
76 NSInteger shipId = [[deck valueForKey:[NSString stringWithFormat:@"ship_%ld", i]] integerValue];
77 if(!needsPack && shipId == -1) {
82 [deck setValue:@(shipId) forKey:[NSString stringWithFormat:@"ship_%ld", i - 1]];
85 [deck setValue:@(-1) forKey:@"ship_5"];
92 - (void)excludeShipsWithoutFlag
94 NSInteger deckNumber = [[self.arguments valueForKey:@"api_id"] integerValue];
97 NSArray *decks = [self.store objectsWithEntityName:@"Deck"
99 predicateFormat:@"id = %ld", deckNumber];
100 // TODO: error handling
103 for(NSInteger i = 1; i < 6; i++) {
104 [deck setValue:@(-1) forKey:[NSString stringWithFormat:@"ship_%ld", i]];
110 NSInteger deckNumber = [[self.arguments valueForKey:@"api_id"] integerValue];
111 NSInteger shipId = [[self.arguments valueForKey:@"api_ship_id"] integerValue];
112 NSInteger shipIndex = [[self.arguments valueForKey:@"api_ship_idx"] integerValue];
115 [self excludeShipsWithoutFlag];
119 NSError *error = nil;
120 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:YES];
121 NSArray *decks = [self.store objectsWithEntityName:@"Deck"
122 sortDescriptors:@[sortDescriptor]
125 // TODO: error handling
127 NSMutableArray *ships = [NSMutableArray new];
128 for(id deck in decks) {
129 for(NSUInteger i = 0; i < 6; i++) {
130 [ships addObject:[deck valueForKey:[NSString stringWithFormat:@"ship_%ld", i]]];
135 BOOL alreadyInFleet = [ships containsObject:@(shipId)];
136 NSInteger shipDeckNumber = -1;
137 NSInteger shipDeckIndex = -1;
139 NSInteger index = [ships indexOfObject:@(shipId)];
140 shipDeckNumber = index / 6;
141 shipDeckIndex = index % 6;
144 NSInteger replaceShipId = [ships[(deckNumber - 1) * 6 + shipIndex] integerValue];
147 id deck = decks[deckNumber - 1];
148 [deck setValue:@(shipId) forKey:[NSString stringWithFormat:@"ship_%ld", shipIndex]];
151 if(alreadyInFleet && shipId != -1) {
152 id deck = decks[shipDeckNumber];
153 [deck setValue:@(replaceShipId) forKey:[NSString stringWithFormat:@"ship_%ld", shipDeckIndex]];