0

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121

2 Answers2

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.

stevesliva
  • 5,351
  • 1
  • 16
  • 39
1

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.

andrewbuilder
  • 3,629
  • 2
  • 24
  • 46