Core data model, game has a winner and a loser, a player has multiple games?

断了今生、忘了曾经 提交于 2020-01-06 12:53:17

问题


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.

I have the following:

Entity: Player

Attributes: wins, losses, name

Relationships: games Destination:Game inverse: ??? winner or loser???

Entity: Game

Attributes: losingScore, winningScore,

Relationships: loser destination:Player Inverse:games, winner destination:Player Inverse:games

How can I set this up??

Thanks!


回答1:


Why not just:

  • player.gamesWon<->game.winner (game:to-one)
  • player.gamesLost<->game.loser (game:to-one)
  • player.games<->game.players (game:to-many)

You could incidentally then have wins/losses as gamesWon.count and gamesLost.count and not as attributes.




回答2:


Consider this...

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.

My suggestion...


  • Entity: Player
  • Attribute: NSString *name
  • Relationship: playerGames one-to-many Game

  • Entity: Game
  • Attribute: NSString *reference (e.g. Game "1")
  • Attribute: NSNumber *scorePlayer1
  • Attribute: NSNumber *scorePlayer2
  • (Attribute: NSDate *timeStamp) option?
  • Relationship: gamePlayer1 many-to-one Player
  • Relationship: gamePlayer2 many-to-one Player (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;

Hope this helps.



来源:https://stackoverflow.com/questions/23318382/core-data-model-game-has-a-winner-and-a-loser-a-player-has-multiple-games

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!