MOGiftInfo.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // MOGiftInfo.m
  3. //
  4. // Created by SuperCabbage on 2023/11/24
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOGiftInfo.h"
  8. #import "MOEffect.h"
  9. #import "MOGiftCornerMark.h"
  10. #import "MOStripe.h"
  11. #import "MOBlindboxLottery.h"
  12. NSString *const kMOGiftInfoEffect = @"effect";
  13. NSString *const kMOGiftInfoGiftPath = @"giftPath";
  14. NSString *const kMOGiftInfoId = @"id";
  15. NSString *const kMOGiftInfoLuckyGift = @"luckyGift";
  16. NSString *const kMOGiftInfoGiftSource = @"giftSource";
  17. NSString *const kMOGiftInfoGiftName = @"giftName";
  18. NSString *const kMOGiftInfoDiamond = @"diamond";
  19. NSString *const kMOGiftInfoGiftCode = @"giftCode";
  20. //NSString *const kMOGiftInfoAnimation = @"animation";
  21. NSString *const kMOGiftInfoGiftCornerMark = @"cornerMark";
  22. NSString *const kMOGiftInfoEffectType = @"effectType";
  23. NSString *const kMOGiftInfoSkin = @"skin";
  24. NSString *const kMOGiftInfoSkins = @"skins";
  25. NSString *const kMOGiftInfoDiyGiftNumberMax = @"diyGiftNumberMax";
  26. NSString *const kMOGiftInfoValueLevel = @"valueLevel";
  27. NSString *const kMOGiftInfoStripe = @"stripe";
  28. NSString *const kMOGiftInfoBlindBoxGift = @"blindBoxGift";
  29. NSString *const kMOGiftInfoAntiDrillCritGift = @"antiDrillCritGift";
  30. @interface MOGiftInfo ()
  31. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  32. @end
  33. @implementation MOGiftInfo
  34. @synthesize effect = _effect;
  35. @synthesize giftPath = _giftPath;
  36. @synthesize id = _id;
  37. @synthesize luckyGift = _luckyGift;
  38. @synthesize giftSource = _giftSource;
  39. @synthesize giftName = _giftName;
  40. @synthesize diamond = _diamond;
  41. @synthesize giftCode = _giftCode;
  42. //@synthesize animation = _animation;
  43. @synthesize giftCornerMark = _giftCornerMark;
  44. @synthesize effectType = _effectType;
  45. @synthesize skin = _skin;
  46. @synthesize skins = _skins;
  47. @synthesize diyGiftNumberMax = _diyGiftNumberMax;
  48. @synthesize valueLevel = _valueLevel;
  49. @synthesize stripe = _stripe;
  50. @synthesize blindBoxGift = _blindBoxGift;
  51. @synthesize antiDrillCritGift = _antiDrillCritGift;
  52. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  53. return [[self alloc] initWithDictionary:dict];
  54. }
  55. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  56. self = [super init];
  57. // This check serves to make sure that a non-NSDictionary object
  58. // passed into the model class doesn't break the parsing.
  59. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  60. self.effect = [MOEffect modelObjectWithDictionary:[dict objectForKey:kMOGiftInfoEffect]];
  61. self.giftPath = [self objectOrNilForKey:kMOGiftInfoGiftPath fromDictionary:dict];
  62. self.id = [self objectOrNilForKey:kMOGiftInfoId fromDictionary:dict];
  63. self.luckyGift = [[self objectOrNilForKey:kMOGiftInfoLuckyGift fromDictionary:dict] boolValue];
  64. self.giftSource = [self objectOrNilForKey:kMOGiftInfoGiftSource fromDictionary:dict];
  65. self.giftName = [self objectOrNilForKey:kMOGiftInfoGiftName fromDictionary:dict];
  66. self.diamond = [[self objectOrNilForKey:kMOGiftInfoDiamond fromDictionary:dict] doubleValue];
  67. self.giftCode = [[self objectOrNilForKey:kMOGiftInfoGiftCode fromDictionary:dict] doubleValue];
  68. // self.animation = [[self objectOrNilForKey:kMOGiftInfoAnimation fromDictionary:dict] boolValue];
  69. self.giftCornerMark = [MOGiftCornerMark modelObjectWithDictionary:[dict objectForKey:kMOGiftInfoGiftCornerMark]];
  70. self.effectType = [[self objectOrNilForKey:kMOGiftInfoEffectType fromDictionary:dict] doubleValue];
  71. self.skin = [MOEffect modelObjectWithDictionary:[dict objectForKey:kMOGiftInfoSkin]];
  72. self.blindBoxGift = [[self objectOrNilForKey:kMOGiftInfoBlindBoxGift fromDictionary:dict] boolValue];
  73. self.antiDrillCritGift = [[self objectOrNilForKey:kMOGiftInfoAntiDrillCritGift fromDictionary:dict] boolValue];
  74. NSObject *receivedMOGiftlist = [dict objectForKey:kMOGiftInfoSkins];
  75. NSMutableArray *parsedMOGiftlist = [NSMutableArray array];
  76. if ([receivedMOGiftlist isKindOfClass:[NSArray class]]) {
  77. for (NSDictionary *item in (NSArray *)receivedMOGiftlist) {
  78. if ([item isKindOfClass:[NSDictionary class]]) {
  79. [parsedMOGiftlist addObject:[MOEffect modelObjectWithDictionary:item]];
  80. }
  81. }
  82. } else if ([receivedMOGiftlist isKindOfClass:[NSDictionary class]]) {
  83. [parsedMOGiftlist addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOGiftlist]];
  84. }
  85. self.skins = [NSArray arrayWithArray:parsedMOGiftlist];
  86. self.diyGiftNumberMax = [[self objectOrNilForKey:kMOGiftInfoDiyGiftNumberMax fromDictionary:dict] doubleValue];
  87. self.valueLevel = [[self objectOrNilForKey:kMOGiftInfoValueLevel fromDictionary:dict] doubleValue];
  88. self.stripe = [MOStripe modelObjectWithDictionary:[dict objectForKey:kMOGiftInfoStripe]];
  89. if(self.giftPath.length == 0){
  90. self.giftPath = self.skin.path;
  91. }
  92. if(self.giftName.length == 0){
  93. self.giftName = self.skin.name;
  94. }
  95. if(self.giftCode == 0){
  96. self.giftCode = self.skin.code;
  97. }
  98. if(self.giftSource.length == 0){
  99. self.giftSource = self.skin.source;
  100. }
  101. if(self.effectType == 0){
  102. if(self.skin){
  103. self.effectType = self.skin.effectType;
  104. }
  105. }
  106. }
  107. return self;
  108. }
  109. - (NSDictionary *)dictionaryRepresentation {
  110. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  111. [mutableDict setValue:[self.effect dictionaryRepresentation] forKey:kMOGiftInfoEffect];
  112. [mutableDict setValue:self.giftPath forKey:kMOGiftInfoGiftPath];
  113. [mutableDict setValue:self.id forKey:kMOGiftInfoId];
  114. [mutableDict setValue:[NSNumber numberWithBool:self.luckyGift] forKey:kMOGiftInfoLuckyGift];
  115. [mutableDict setValue:self.giftSource forKey:kMOGiftInfoGiftSource];
  116. [mutableDict setValue:self.giftName forKey:kMOGiftInfoGiftName];
  117. [mutableDict setValue:[NSNumber numberWithDouble:self.diamond] forKey:kMOGiftInfoDiamond];
  118. [mutableDict setValue:[NSNumber numberWithDouble:self.giftCode] forKey:kMOGiftInfoGiftCode];
  119. // [mutableDict setValue:[NSNumber numberWithBool:self.animation] forKey:kMOGiftInfoAnimation];
  120. [mutableDict setValue:[self.giftCornerMark dictionaryRepresentation] forKey:kMOGiftInfoGiftCornerMark];
  121. [mutableDict setValue:[NSNumber numberWithDouble:self.effectType] forKey:kMOGiftInfoEffectType];
  122. [mutableDict setValue:[self.skin dictionaryRepresentation] forKey:kMOGiftInfoSkin];
  123. [mutableDict setValue:[NSNumber numberWithBool:self.blindBoxGift] forKey:kMOGiftInfoBlindBoxGift];
  124. [mutableDict setValue:[NSNumber numberWithBool:self.antiDrillCritGift] forKey:kMOGiftInfoAntiDrillCritGift];
  125. NSMutableArray *tempArrayForGiftlist = [NSMutableArray array];
  126. for (NSObject *subArrayObject in self.skins) {
  127. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  128. // This class is a model object
  129. [tempArrayForGiftlist addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  130. } else {
  131. // Generic object
  132. [tempArrayForGiftlist addObject:subArrayObject];
  133. }
  134. }
  135. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftlist] forKey:kMOGiftInfoSkins];
  136. [mutableDict setValue:[NSNumber numberWithDouble:self.diyGiftNumberMax] forKey:kMOGiftInfoDiyGiftNumberMax];
  137. [mutableDict setValue:[NSNumber numberWithDouble:self.valueLevel] forKey:kMOGiftInfoValueLevel];
  138. [mutableDict setValue:[self.stripe dictionaryRepresentation] forKey:kMOGiftInfoStripe];
  139. return [NSDictionary dictionaryWithDictionary:mutableDict];
  140. }
  141. - (NSString *)description {
  142. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  143. }
  144. #pragma mark - Helper Method
  145. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  146. id object = [dict objectForKey:aKey];
  147. return [object isEqual:[NSNull null]] ? nil : object;
  148. }
  149. #pragma mark - NSCoding Methods
  150. - (id)initWithCoder:(NSCoder *)aDecoder {
  151. self = [super init];
  152. self.effect = [aDecoder decodeObjectForKey:kMOGiftInfoEffect];
  153. self.giftPath = [aDecoder decodeObjectForKey:kMOGiftInfoGiftPath];
  154. self.id = [aDecoder decodeObjectForKey:kMOGiftInfoId];
  155. self.luckyGift = [aDecoder decodeBoolForKey:kMOGiftInfoLuckyGift];
  156. self.giftSource = [aDecoder decodeObjectForKey:kMOGiftInfoGiftSource];
  157. self.giftName = [aDecoder decodeObjectForKey:kMOGiftInfoGiftName];
  158. self.diamond = [aDecoder decodeDoubleForKey:kMOGiftInfoDiamond];
  159. self.giftCode = [aDecoder decodeDoubleForKey:kMOGiftInfoGiftCode];
  160. // self.animation = [aDecoder decodeBoolForKey:kMOGiftInfoAnimation];
  161. self.giftCornerMark = [aDecoder decodeObjectForKey:kMOGiftInfoGiftCornerMark];
  162. self.effectType = [aDecoder decodeDoubleForKey:kMOGiftInfoEffectType];
  163. self.skin = [aDecoder decodeObjectForKey:kMOGiftInfoSkin];
  164. self.skins = [aDecoder decodeObjectForKey:kMOGiftInfoSkins];
  165. self.diyGiftNumberMax = [aDecoder decodeDoubleForKey:kMOGiftInfoDiyGiftNumberMax];
  166. self.valueLevel = [aDecoder decodeDoubleForKey:kMOGiftInfoValueLevel];
  167. self.stripe = [aDecoder decodeObjectForKey:kMOGiftInfoStripe];
  168. self.blindBoxGift = [aDecoder decodeBoolForKey:kMOGiftInfoBlindBoxGift];
  169. self.antiDrillCritGift = [aDecoder decodeBoolForKey:kMOGiftInfoAntiDrillCritGift];
  170. return self;
  171. }
  172. - (void)encodeWithCoder:(NSCoder *)aCoder
  173. {
  174. [aCoder encodeObject:_effect forKey:kMOGiftInfoEffect];
  175. [aCoder encodeObject:_giftPath forKey:kMOGiftInfoGiftPath];
  176. [aCoder encodeObject:_id forKey:kMOGiftInfoId];
  177. [aCoder encodeBool:_luckyGift forKey:kMOGiftInfoLuckyGift];
  178. [aCoder encodeObject:_giftSource forKey:kMOGiftInfoGiftSource];
  179. [aCoder encodeObject:_giftName forKey:kMOGiftInfoGiftName];
  180. [aCoder encodeDouble:_diamond forKey:kMOGiftInfoDiamond];
  181. [aCoder encodeDouble:_giftCode forKey:kMOGiftInfoGiftCode];
  182. // [aCoder encodeBool:_animation forKey:kMOGiftInfoAnimation];
  183. [aCoder encodeObject:_giftCornerMark forKey:kMOGiftInfoGiftCornerMark];
  184. [aCoder encodeDouble:_effectType forKey:kMOGiftInfoEffectType];
  185. [aCoder encodeObject:_skin forKey:kMOGiftInfoSkin];
  186. [aCoder encodeObject:_skins forKey:kMOGiftInfoSkins];
  187. [aCoder encodeDouble:_diyGiftNumberMax forKey:kMOGiftInfoDiyGiftNumberMax];
  188. [aCoder encodeDouble:_valueLevel forKey:kMOGiftInfoValueLevel];
  189. [aCoder encodeObject:_stripe forKey:kMOGiftInfoStripe];
  190. [aCoder encodeBool:_blindBoxGift forKey:kMOGiftInfoBlindBoxGift];
  191. [aCoder encodeBool:_antiDrillCritGift forKey:kMOGiftInfoAntiDrillCritGift];
  192. }
  193. - (id)copyWithZone:(NSZone *)zone {
  194. MOGiftInfo *copy = [[MOGiftInfo alloc] init];
  195. if (copy) {
  196. copy.effect = [self.effect copyWithZone:zone];
  197. copy.giftPath = [self.giftPath copyWithZone:zone];
  198. copy.id = [self.id copyWithZone:zone];
  199. copy.luckyGift = self.luckyGift;
  200. copy.giftSource = [self.giftSource copyWithZone:zone];
  201. copy.giftName = [self.giftName copyWithZone:zone];
  202. copy.diamond = self.diamond;
  203. copy.giftCode = self.giftCode;
  204. // copy.animation = self.animation;
  205. copy.giftCornerMark = [self.giftCornerMark copyWithZone:zone];
  206. copy.effectType = self.effectType;
  207. copy.skin = [self.skin copyWithZone:zone];
  208. copy.skins = [self.skins copyWithZone:zone];
  209. copy.diyGiftNumberMax = self.diyGiftNumberMax;
  210. copy.valueLevel = self.valueLevel;
  211. copy.stripe = [self.stripe copyWithZone:zone];
  212. copy.selectSkin = [self.selectSkin copyWithZone:zone];
  213. copy.blindBoxGift = self.blindBoxGift;
  214. copy.antiDrillCritGift = self.antiDrillCritGift;
  215. }
  216. return copy;
  217. }
  218. - (BOOL)isEqual:(id)object {
  219. if (self == object) return YES;
  220. if (![object isKindOfClass:[self class]]) return NO;
  221. MOGiftInfo *other = (MOGiftInfo *)object;
  222. return self.id == other.id;
  223. }
  224. @end