예제 #1
0
        private static void FillPlayerConfig(MilleBornesEntities context, Room roomEntity, Game newGame)
        {
            // Essayer en premier lieu avec le service, sinon
            // regarder directement dans la base de données.

            List<PlayerGame> playerGameSource = new List<PlayerGame>();
#if false
            try
            {
                using (var client = new LobbyGameProxy.LobbyServiceClient())
                {
                    var configEntries = client.GetPlayerConfig(roomEntity.Token);

                    foreach (var entry in configEntries)
                    {
                        var user = context.User
                            .Where(us => us.LoggedInUser != null)
                            .SingleOrDefault(us => us.LoggedInUser.Token == entry.UserToken);

                        if (user == null)
                        {
                            continue;
                        }

                        var playerGame = new PlayerGame()
                        {
                            Game = newGame,
                            UserId = user.UserId,
                            User = user,
                            LastHeartbeat = DateTime.UtcNow,
                            Order = entry.Order,
                            Team = entry.Team + 1,
                            HasJoined = user.UserId == roomEntity.MasterUserId
                        };
                    }
                }
            }
#endif
            //catch (Exception ex)
            //{
                playerGameSource = roomEntity.PlayerRoomState.Select((prs, inx) => new PlayerGame()
                {
                    Game = newGame,
                    UserId = prs.UserId,
                    User = prs.User,
                    LastHeartbeat = DateTime.UtcNow,
                    Order = inx,
                    Team = prs.Team + 1,
                    HasJoined = prs.UserId == roomEntity.MasterUserId
                })
                .ToList();
            //}

            newGame.PlayerGame = playerGameSource;
        }
예제 #2
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;
            }
        }