核心数据模型,游戏有赢家和输家,一个玩家有多场比赛?
Posted
技术标签:
【中文标题】核心数据模型,游戏有赢家和输家,一个玩家有多场比赛?【英文标题】:Core data model, game has a winner and a loser, a player has multiple games? 【发布时间】:2014-04-27 01:32:31 【问题描述】:我正在尝试设置我的核心数据模型。我想要一个有一个输家和一个赢家的游戏。我需要一个有多个游戏的玩家。
我有以下几点:
实体: 播放器
属性: 赢了, 损失, 名字
关系: 游戏目的地:游戏逆:???赢家还是输家???
实体: 游戏
属性: 失分, 获胜分数,
关系: 失败者目的地:玩家逆:游戏, 获胜者目的地:Player Inverse:游戏
我该如何设置??
谢谢!
【问题讨论】:
【参考方案1】:为什么不只是:
player.gamesWongame.winner (game:to-one) player.gamesLostgame.loser (game:to-one) player.gamesgame.players (game:to-many)您可以顺便将赢/输作为 gamesWon.count 和 gamesLost.count 而不是作为属性。
【讨论】:
【参考方案2】:考虑一下...
游戏有玩家。游戏中的每个玩家都会获得一个分数。他们的输赢取决于他们的得分与其他玩家在该游戏中的得分相比。
我的建议...
实体:
Player
属性:NSString *name
关系:playerGames
一对多Game
实体:
Game
属性:NSString *reference
(例如游戏“1”)
属性:NSNumber *scorePlayer1
属性:NSNumber *scorePlayer2
(属性:NSDate *timeStamp
)选项?
关系:gamePlayer1
多对一 Player
关系:gamePlayer2
多对一 Player
(其中玩家 2 不能等于玩家 1)
那么我们可以...
Game *game = [[Game alloc] init...];
if (game.scorePlayer1 > game.scorePlayer2)
NSLog("%@ is winner and %@ is loser with score %@-%@", game.gamePlayer1.name, gamePlayer2.name, scorePlayer1, scorePlayer2);
else if (game.scorePlayer2 > game.scorePlayer1)
NSLog("%@ is winner and %@ is loser with score %@-%@", game.gamePlayer2.name, gamePlayer1.name, scorePlayer2, scorePlayer1);
else
NSLog("Players %@ and %@ drew with score %@-%@", game.gamePlayer1.name, gamePlayer2.name, scorePlayer1, scorePlayer2);
Player *player = [[Player alloc] init...];
NSFetchRequest *requestWins = [[NSFetchRequest alloc] initWithEntity:@"Game"];
NSPredicate *predicateAsPlayer1 = [NSPredicate predicateWithFormat: @"(%@ >= %@) && (game.gamePlayer1.name == %@)", game.scorePlayer1, game.scorePlayer2, player];
NSPredicate *predicateAsPlayer2 = [NSPredicate predicateWithFormat: @"(%@ >= %@) && (game.gamePlayer2.name == %@)", game.scorePlayer2, game.scorePlayer1, player];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateAsPlayer1, predicateAsPlayer2]];
[requestWins setPredicate:predicate];
NSArray *arrayWins = [managedObjectContext executeFetchRequest:requestWins];
NSFetchRequest *requestLosses = [[NSFetchRequest alloc] initWithEntity:@"Game"];
...<repeat similar to above>...
NSArray *arrayLosses = [managedObjectContext executeFetchRequest:requestLosses];
NSInteger gamesPlayed = player.playerGames.count;
NSInteger gamesWon = arrayWins.count;
NSInteger gamesLost = arrayLosses.count;
NSInteger gamesDrawn = gamesPlayed - gamesWon - gamesLost;
希望这会有所帮助。
【讨论】:
以上是关于核心数据模型,游戏有赢家和输家,一个玩家有多场比赛?的主要内容,如果未能解决你的问题,请参考以下文章