核心数据模型,游戏有赢家和输家,玩家有多个游戏? [英] Core data model, game has a winner and a loser, a player has multiple games?
问题描述
我正在尝试设置我的核心数据模型.我想要一款拥有一个输家和一个赢家的游戏.我需要一个拥有多个游戏的播放器.
I'm trying to setup my core data model. I want to have a game that has one loser and one winner. And I need a Player that has multiple games.
我有以下内容:
实体:播放器
属性:获胜损失,名称
关系:游戏目的地:游戏逆向:???赢家或输家?
Relationships: games Destination:Game inverse: ??? winner or loser???
实体:游戏
属性:missingScore,WinningScore,
Attributes: losingScore, winningScore,
关系:失败者目的地:玩家逆向:游戏,获胜者目的地:逆向玩家:游戏
Relationships: loser destination:Player Inverse:games, winner destination:Player Inverse:games
我该如何设置?
谢谢!
推荐答案
考虑一下...
游戏有玩家.游戏中的每个玩家都得分.他们是赢是输,这取决于他们与该游戏中其他玩家的得分相比的得分.
A game has players. Each player in the game achieves a score. Whether they win or lose depends on their score compared to the other player's score for that game.
我的建议...
- 实体:
播放器
- 属性:
NSString * name
- 关系:
playerGames
一对多Game
- Entity:
Player
- Attribute:
NSString *name
- Relationship:
playerGames
one-to-manyGame
- 实体:
Game
- 属性:
NSString * reference
(例如游戏"1") - 属性:
NSNumber * scorePlayer1
- 属性:
NSNumber * scorePlayer2
- (属性:
NSDate * timeStamp
)选项? - 关系:
gamePlayer1
多对一Player
- 关系:
gamePlayer2
多对一Player
(其中玩家2不能等于玩家1)
- Entity:
Game
- Attribute:
NSString *reference
(e.g. Game "1") - Attribute:
NSNumber *scorePlayer1
- Attribute:
NSNumber *scorePlayer2
- (Attribute:
NSDate *timeStamp
) option? - Relationship:
gamePlayer1
many-to-onePlayer
- Relationship:
gamePlayer2
many-to-onePlayer
(where player 2 cannot equal player 1)
那么我们可以拥有...
So then we can have...
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;
希望这会有所帮助.
这篇关于核心数据模型,游戏有赢家和输家,玩家有多个游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!