Beispiel #1
0
        /// <summary>
        /// Crée la room avec le master associé.
        /// </summary>
        /// <param name="name">Nom de la salle</param>
        /// <param name="roomMaster">Guid du créateur.</param>
        /// <returns>Le guid de la room. </returns>
        public Guid? CreateRoom(string name, Guid roomMaster)
        {
            try
            {
                using (MilleBornesEntities context = new MilleBornesEntities())
                {
                    // Fetch le master id. 
                    int mId = context.LoggedInUser.First(p => p.Token == roomMaster).UserId;
                    // Vérifie si déjà master d'une room ou membre d'une room.
                    bool alreadyInRoom = context.Room
                        .Where(p => p.MasterUserId == mId)
                        .Count() > 0;

                    if (alreadyInRoom)
                        return null;

                    alreadyInRoom = context.PlayerRoomState
                        .Where(p => p.UserId == mId)
                        .Count() > 0;

                    if (alreadyInRoom)
                        return null;

                    Guid roomGuid = Guid.NewGuid();
                    Room r = new Room();
                    r.Name = name;
                    r.Started = false;
                    r.Token = roomGuid;
                    r.MasterUserId = mId;
                    r.GameInProgressId = null;

                    // Crée le playerroomstate du roommaster
                    PlayerRoomState prs = new PlayerRoomState();
                    prs.Room = r;
                    prs.UserId = mId;
                    prs.IsReady = false;
                    prs.Order = 0;
                    prs.Team = 0;
                    prs.LastHeartbeat = DateTime.UtcNow;

                    context.Room.Add(r);
                    context.PlayerRoomState.Add(prs);
                    context.SaveChanges();

                    return roomGuid;
                }
            }
            catch
            {
                return null;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Permet de joindre une room!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        /// </summary>
        /// <param name="player"></param>
        /// <param name="room"></param>
        /// <returns>True si succès, false autrement.</returns>
        public bool JoinRoom(Guid player, Guid room)
        {
            try
            {
                using (MilleBornesEntities context = new MilleBornesEntities())
                {
                    LoggedInUser liu = context.LoggedInUser.First(p => p.Token == player);

                    // On vérifie si le user est dans la room... 
                    // vérifie si déjà dans une room
                    bool alreadyInRoom = context.PlayerRoomState
                        .Where(p => p.UserId == liu.UserId)
                        .Count() > 0;

                    Room r = context.Room.First(p => p.Token == room);

                    // Si il est dans une autre room.
                    if (alreadyInRoom)
                    {
                        if (r.MasterUserId == liu.UserId || r.PlayerRoomState.First(p => p.UserId == liu.UserId).Room.Token == room)
                        {
                            SendRoomMessage(Guid.ParseExact("00000000-0000-0000-0000-000000000000", "D"), room, "**Le joueur " + liu.User.Name + " revenu dans la partie ☺**");
                            return true;
                        }

                        return false;
                    }

                    PlayerRoomState prsUser = new PlayerRoomState();
                    prsUser.UserId = liu.UserId;
                    prsUser.RoomId = r.RoomId;
                    prsUser.IsReady = false;
                    prsUser.Order = 0;
                    prsUser.Team = 0;
                    prsUser.LastHeartbeat = DateTime.UtcNow;

                    context.PlayerRoomState.Add(prsUser);
                    context.SaveChanges();
                    SendRoomMessage(Guid.ParseExact("00000000-0000-0000-0000-000000000000", "D"), room, "**Le joueur " + liu.User.Name + " a join la partie ☺**");
                }

                return true;
            }
            catch
            {
                return false;
            }
        }