W3C

  • Flux RSS des articles

Recherche

technique

Lundi 22 mai 2006 1 22 05 2006 16:44
Afin de comprendre comment est codé le jeu (bientôt je mettrais les fichiers sources quelquepart), il faut comprendre comment je l'ai organisé :

Les joueurs inscrits peuvent jouer contre d'autres joueurs, et faire plusieurs parties à la fois, une partie à l'avenir pourra avoir plus de deux joueurs, il faut séparer les objets "joueur_inscrit", "partie_de_jeu" et "participant_a_une_partie".

Les noms des tables sont donc :

joueurs
confrontations
participants

J'ai évidemment créé les modèles de classes correspondantes.
Pour relier ces tables là, j'utilise les déclarations has_many, belongs_to et has_and_belongs_to_many de la sorte :
  1. Un joueur a plusieurs participants.
  2. Une confrontation a plusieurs participants.
  3. Une confrontation a plusieurs joueurs.
  4. Un joueur a plusieurs participants.
1 et 2 sont deux dépendances 1 vers n (has_many).
3 et 4 forment une dépendance n vers n (has_and_belongs_to_many).

class Confrontation < ActiveRecord::Base
    has_and_belongs_to_many :joueurs
    has_many                :participants
end

class Joueur < ActiveRecord::Base
  has_and_belongs_to_many :confrontations
  has_many :participants
end

class Participant < ActiveRecord::Base
    belongs_to :confrontation
    belongs_to :joueur
end

Du côté mysql, j'ai opté pour un fichier create.sql (méthode utilisée dans le livre ruby on rails).
Les lignes intéressantes sont :

create table participants (
    id                          int             not null    auto_increment,
    joueur_id               int             not null,
    confrontation_id    int             not null,
    constraint  fk_confrontation foreign key (confrontation_id)  references  confrontations(id),
    constraint  fk_joueur        foreign key (joueur_id)         references  joueurs(id),
    primary key (id)
    );

où l'on voit apparaître les lignes "joueur_id" et "confrontation_id"

Ainsi que la table de jointure permettant à rails de faire la passerelle entre les joueurs et les confrontation :

create table confrontations_joueurs (
    confrontation_id    int not null,
    joueur_id               int not null,
    constraint  fk_cp_confrontation foreign key (confrontation_id)  references  confrontations(id),
    constraint  fk_cp_joueur        foreign key (joueur_id)         references  joueurs(id),
    );
Par FrihD
Ecrire un commentaire - Voir les 0 commentaires - Recommander

Recommander

Calendrier

Décembre 2009
L M M J V S D
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
<< < > >>
Créer un blog sur over-blog.com - Contact - C.G.U. - Rémunération en droits d'auteur - Signaler un abus