MOPkUserList.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // MOPkUserList.m
  3. //
  4. // Created by SuperCabbage on 2023/12/18
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOPkUserList.h"
  8. #import "MOUserProfile.h"
  9. NSString *const kMOPkUserListRoomId = @"roomId";
  10. NSString *const kMOPkUserListWinNum = @"winNum";
  11. NSString *const kMOPkUserListPkNum = @"pkNum";
  12. NSString *const kMOPkUserListWinRate = @"winRate";
  13. NSString *const kMOPkUserListUserProfile = @"user";
  14. NSString *const kMOPkUserListPkSecret = @"pkSecret";
  15. NSString *const kMOPkUserListAmount = @"amount";
  16. NSString *const kMOPkUserListInvite = @"invite";
  17. @interface MOPkUserList ()
  18. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  19. @end
  20. @implementation MOPkUserList
  21. @synthesize roomId = _roomId;
  22. @synthesize winNum = _winNum;
  23. @synthesize pkNum = _pkNum;
  24. @synthesize winRate = _winRate;
  25. @synthesize userProfile = _userProfile;
  26. @synthesize pkSecret = _pkSecret;
  27. @synthesize amount = _amount;
  28. @synthesize invite = _invite;
  29. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  30. return [[self alloc] initWithDictionary:dict];
  31. }
  32. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  33. self = [super init];
  34. // This check serves to make sure that a non-NSDictionary object
  35. // passed into the model class doesn't break the parsing.
  36. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  37. self.roomId = [self objectOrNilForKey:kMOPkUserListRoomId fromDictionary:dict];
  38. self.winNum = [[self objectOrNilForKey:kMOPkUserListWinNum fromDictionary:dict] doubleValue];
  39. self.pkNum = [[self objectOrNilForKey:kMOPkUserListPkNum fromDictionary:dict] doubleValue];
  40. self.winRate = [[self objectOrNilForKey:kMOPkUserListWinRate fromDictionary:dict] doubleValue];
  41. self.userProfile = [MOUserProfile modelObjectWithDictionary:[dict objectForKey:kMOPkUserListUserProfile]];
  42. self.pkSecret = [self objectOrNilForKey:kMOPkUserListPkSecret fromDictionary:dict];
  43. self.amount = [[self objectOrNilForKey:kMOPkUserListAmount fromDictionary:dict] doubleValue];
  44. self.invite = [self objectOrNilForKey:kMOPkUserListInvite fromDictionary:dict];
  45. }
  46. return self;
  47. }
  48. - (NSDictionary *)dictionaryRepresentation {
  49. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  50. [mutableDict setValue:self.roomId forKey:kMOPkUserListRoomId];
  51. [mutableDict setValue:[NSNumber numberWithDouble:self.winNum] forKey:kMOPkUserListWinNum];
  52. [mutableDict setValue:[NSNumber numberWithDouble:self.pkNum] forKey:kMOPkUserListPkNum];
  53. [mutableDict setValue:[NSNumber numberWithDouble:self.winRate] forKey:kMOPkUserListWinRate];
  54. [mutableDict setValue:[self.userProfile dictionaryRepresentation] forKey:kMOPkUserListUserProfile];
  55. [mutableDict setValue:self.pkSecret forKey:kMOPkUserListPkSecret];
  56. [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kMOPkUserListAmount];
  57. [mutableDict setValue:self.invite forKey:kMOPkUserListInvite];
  58. return [NSDictionary dictionaryWithDictionary:mutableDict];
  59. }
  60. - (NSString *)description {
  61. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  62. }
  63. #pragma mark - Helper Method
  64. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  65. id object = [dict objectForKey:aKey];
  66. return [object isEqual:[NSNull null]] ? nil : object;
  67. }
  68. #pragma mark - NSCoding Methods
  69. - (id)initWithCoder:(NSCoder *)aDecoder {
  70. self = [super init];
  71. self.roomId = [aDecoder decodeObjectForKey:kMOPkUserListRoomId];
  72. self.winNum = [aDecoder decodeDoubleForKey:kMOPkUserListWinNum];
  73. self.pkNum = [aDecoder decodeDoubleForKey:kMOPkUserListPkNum];
  74. self.winRate = [aDecoder decodeDoubleForKey:kMOPkUserListWinRate];
  75. self.userProfile = [aDecoder decodeObjectForKey:kMOPkUserListUserProfile];
  76. self.pkSecret = [aDecoder decodeObjectForKey:kMOPkUserListPkSecret];
  77. self.amount = [aDecoder decodeDoubleForKey:kMOPkUserListAmount];
  78. self.invite = [aDecoder decodeObjectForKey:kMOPkUserListInvite];
  79. return self;
  80. }
  81. - (void)encodeWithCoder:(NSCoder *)aCoder
  82. {
  83. [aCoder encodeObject:_roomId forKey:kMOPkUserListRoomId];
  84. [aCoder encodeDouble:_winNum forKey:kMOPkUserListWinNum];
  85. [aCoder encodeDouble:_pkNum forKey:kMOPkUserListPkNum];
  86. [aCoder encodeDouble:_winRate forKey:kMOPkUserListWinRate];
  87. [aCoder encodeObject:_userProfile forKey:kMOPkUserListUserProfile];
  88. [aCoder encodeObject:_pkSecret forKey:kMOPkUserListPkSecret];
  89. [aCoder encodeDouble:_amount forKey:kMOPkUserListAmount];
  90. [aCoder encodeObject:_invite forKey:kMOPkUserListInvite];
  91. }
  92. - (id)copyWithZone:(NSZone *)zone {
  93. MOPkUserList *copy = [[MOPkUserList alloc] init];
  94. if (copy) {
  95. copy.roomId = [self.roomId copyWithZone:zone];
  96. copy.winNum = self.winNum;
  97. copy.pkNum = self.pkNum;
  98. copy.winRate = self.winRate;
  99. copy.userProfile = [self.userProfile copyWithZone:zone];
  100. copy.pkSecret = [self.pkSecret copyWithZone:zone];
  101. copy.amount = self.amount;
  102. copy.invite = [self.invite copyWithZone:zone];
  103. copy.isSelect = self.isSelect;
  104. }
  105. return copy;
  106. }
  107. @end