public RoomOperationResult EnterRoom(int roomId, bool spectate, string password, out Room room)
        {
            room = null;
            if (currentAccount == null)
            {
                return(RoomOperationResult.NotAutheticated);
            }
            Trace.TraceInformation("{1} Enter room {0}", roomId, currentAccount.Account.UserName);
            if (currentAccount.CurrentRoom != null)
            {
                return(RoomOperationResult.Locked);
            }

            ServerRoom serverRoom = null;
            Room       clientRoom = null;

            lock (rooms)
            {
                if (!rooms.ContainsKey(roomId))
                {
                    return(RoomOperationResult.Invalid);
                }
                else
                {
                    serverRoom = rooms[roomId];
                    clientRoom = serverRoom.Room;
                }
            }

            lock (clientRoom)
            {
                if (clientRoom.IsEmpty || clientRoom.State == RoomState.Gaming)
                {
                    return(RoomOperationResult.Locked);
                }
                int seatNo = 0;
                foreach (var seat in clientRoom.Seats)
                {
                    Trace.TraceInformation("Testing seat {0}", seatNo);
                    if (seat.Account == null && seat.State == SeatState.Empty)
                    {
                        currentAccount.CurrentRoom = serverRoom;
                        currentAccount.LastAction  = DateTime.Now;
                        seat.Account = currentAccount.Account;
                        seat.State   = SeatState.GuestTaken;
                        _NotifyRoomLayoutChanged(clientRoom);
                        Trace.TraceInformation("Seat {0}", seatNo);
                        _Unspectate(currentAccount);
                        room = clientRoom;
                        return(RoomOperationResult.Success);
                    }
                    seatNo++;
                }
                Trace.TraceInformation("Full");
            }
            return(RoomOperationResult.Full);
        }
Esempio n. 2
0
 private static bool _DestroyRoomIfEmpty(ServerRoom room)
 {
     if (!room.Room.IsEmpty)
     {
         return(false);
     }
     else
     {
         _DestroyRoom(room.Room.Id);
         return(true);
     }
 }
        public Room CreateRoom(RoomSettings settings, string password = null)
        {
            if (currentAccount == null)
            {
                return(null);
            }
            if (currentAccount.CurrentRoom != null)
            {
                return(null);
            }

            lock (rooms)
            {
                while (rooms.ContainsKey(newRoomId))
                {
                    newRoomId++;
                }
                Room room     = new Room();
                int  maxSeats = settings.GameType == GameType.Pk1v1 ? 2 : 8;
                for (int i = 0; i < maxSeats; i++)
                {
                    room.Seats.Add(new Seat()
                    {
                        State = SeatState.Empty
                    });
                }
                room.Seats[0].Account = currentAccount.Account;
                room.Seats[0].State   = SeatState.Host;
                room.Id       = newRoomId;
                room.OwnerId  = 0;
                room.Settings = settings;
                var srvRoom = new ServerRoom()
                {
                    Room = room
                };
                rooms.Add(newRoomId, srvRoom);
                currentAccount.CurrentRoom = srvRoom;
                currentAccount.LastAction  = DateTime.Now;
                Trace.TraceInformation("created room {0}", newRoomId);
                return(room);
            }
        }
Esempio n. 4
0
 private static bool _DestroyRoomIfEmpty(ServerRoom room)
 {
     if (!room.Room.IsEmpty)
     {
         return false;
     }
     else
     {
         _DestroyRoom(room.Room.Id);
         return true;
     }
 }
Esempio n. 5
0
        public Room CreateRoom(RoomSettings settings, string password = null)
        {
            if (currentAccount == null) return null;
            if (currentAccount.CurrentRoom != null)
            {
                return null;
            }

            lock (rooms)
            {
                while (rooms.ContainsKey(newRoomId))
                {
                    newRoomId++;
                }
                Room room = new Room();
                int maxSeats = settings.GameType == GameType.Pk1v1 ? 2 : 8;
                for (int i = 0; i < maxSeats; i++)
                {
                    room.Seats.Add(new Seat() { State = SeatState.Empty });
                }
                room.Seats[0].Account = currentAccount.Account;
                room.Seats[0].State = SeatState.Host;
                room.Id = newRoomId;
                room.OwnerId = 0;
                room.Settings = settings;
                var srvRoom = new ServerRoom() { Room = room };
                rooms.Add(newRoomId, srvRoom);
                currentAccount.CurrentRoom = srvRoom;
                Trace.TraceInformation("created room {0}", newRoomId);
                return room;
            }
        }
        private void _OnGameEnds(int roomId)
        {
            if (accountContext != null)
            {
                try
                {
                    lock (accountContext)
                    {
                        accountContext.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    var crashReport = new StreamWriter(FileRotator.CreateFile("./Crash", "crash", ".dmp", 1000));
                    crashReport.WriteLine(e);
                    crashReport.WriteLine(e.Message);
                    crashReport.Close();
                    accountContext = new AccountContext();
                }
            }
            ServerRoom room = null;

            lock (rooms)
            {
                if (!rooms.ContainsKey(roomId))
                {
                    return;
                }
                room = rooms[roomId];
            }
            Trace.Assert(room != null);
            if (room == null)
            {
                return;
            }
            lock (room.Room)
            {
                room.Room.State = RoomState.Waiting;
                foreach (var seat in room.Room.Seats)
                {
                    if (seat.Account == null)
                    {
                        continue;
                    }
                    lock (loggedInAccounts)
                    {
                        if (loggedInAccounts.ContainsKey(seat.Account.UserName))
                        {
                            try
                            {
                                loggedInAccounts[seat.Account.UserName].CallbackChannel.Ping();
                            }
                            catch (Exception)
                            {
                                _Logout(loggedInAccounts[seat.Account.UserName], true);
                                seat.Account = null;
                                seat.State   = SeatState.Empty;
                                continue;
                            }
                        }
                        else
                        {
                            seat.State   = SeatState.Empty;
                            seat.Account = null;
                        }

                        if (seat.State != SeatState.Host)
                        {
                            seat.State = SeatState.GuestTaken;
                        }

                        if (seat.Account != null && (loggedInAccounts.ContainsKey(seat.Account.UserName) && loggedInAccounts[seat.Account.UserName].CurrentRoom != rooms[roomId]))
                        {
                            seat.Account = null;
                            seat.State   = SeatState.Empty;
                        }
                    }
                }

                if (_DestroyRoomIfEmpty(room))
                {
                    return;
                }
                if (!room.Room.Seats.Any(st => st.State == SeatState.Host))
                {
                    var f = room.Room.Seats.First(st => st.State == SeatState.GuestTaken);
                    f.State = SeatState.Host;
                }
            }
            Trace.Assert(room != null);
            _NotifyRoomLayoutChanged(room.Room);
        }