/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Timers; using System.Linq; using System.Collections.Generic; using StockholmLibrary.Data; using StockholmLibrary.Game.Sessions; using StockholmLibrary.Game.Entities; using StockholmLibrary.Game.Entities.Components; using StockholmLibrary.Game.Packets.Server; using StockholmLibrary.Net; using StockholmLibrary.Util; namespace StockholmLibrary.Game.Flats { //------------------------------------ public class Flat { int _flatId; StockholmDbDataContext _db; Room _room; List _sessionList; List _authenticatedSessions; object _sessionListsLock = new object(); Entity _flatEntity; Timer _pulseTimer; public Room Room { get { return _room; } } //********************************************** public Flat(int flatID) { _flatId = flatID; _db = Stockholm.Get().Database; var roomQuery = from p in _db.Rooms where p.RoomID == flatID select p; _room = roomQuery.Single(); _sessionList = new List(); _authenticatedSessions = new List(); _flatEntity = new Entity("Flat"); RegisterDefaultComponentsAndInitialise(); _pulseTimer = new Timer(GameConstants.FLAT_PULSE_TIME); _pulseTimer.Elapsed += new ElapsedEventHandler(OnPulseElapsed); _pulseTimer.Enabled = true; } //********************************************** void OnPulseElapsed(object sender, ElapsedEventArgs e) { _flatEntity.Pulse(); } //********************************************** private void RegisterDefaultComponentsAndInitialise() { _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); _flatEntity.AddEC(); if (_room.RoomModel.AdditionalECs != null) { string[] ecList = _room.RoomModel.AdditionalECs.Split(",".ToCharArray()); foreach (string newEc in ecList) { _flatEntity.AddEC(newEc); } } ECFlatPhysics flatPhysics = _flatEntity.AddEC(); _flatEntity.FlatInitialise(this); flatPhysics.CalculateRoomMap(); _flatEntity.PostInitialise(); } //********************************************** public bool AddAuthenticatedSession(Session userSession, bool publicRoomAuth) { if (publicRoomAuth != _room.RoomCategory.SystemCategory) { // We're entering the wrong type of room here return false; } lock (_sessionListsLock) { _authenticatedSessions.Add(userSession); } // We should set the active room here userSession.CurrentFlat = this; Logging.LogEvent("Flat " + _flatId, "User '" + userSession.User.Username + "' just authenticated", Logging.ELogLevel.INFO); return true; } //********************************************** public void EnterFlat(Session userSession) { lock (_sessionListsLock) { if (_authenticatedSessions.Contains(userSession)) { _sessionList.Add(userSession); OnUserJoined(userSession); _authenticatedSessions.Remove(userSession); Logging.LogEvent("Flat " + _flatId, "User '" + userSession.User.Username + "' just entered", Logging.ELogLevel.INFO); } else { // The user isn't authenticated, need to refuse them userSession.Peer.EnqueuePacket(new ServerPacket(EServerPacket.HOTEL_VIEW)); Logging.LogEvent("Flat " + _flatId, "User '" + userSession.User.Username + "' was refused entrance as they were not authenticated", Logging.ELogLevel.WARNING); } } } //********************************************** public void ExitFlat(Session userSession) { _flatEntity.UserDeinitialise(userSession); lock (_sessionListsLock) { if (_authenticatedSessions.Contains(userSession)) { _authenticatedSessions.Remove(userSession); } if (_sessionList.Contains(userSession)) { _sessionList.Remove(userSession); } } userSession.CurrentFlat = null; Logging.LogEvent("Flat " + _flatId, "User '" + userSession.User.Username + "' just exited", Logging.ELogLevel.INFO); } //********************************************** public bool ExecuteRoomEntranceChecks(Session userSession, string password = null) { if (!_room.RoomCategory.IsVisible) { bool canAccessHiddenRooms = Stockholm.Get().GameManager.RoleManager.HasAccess(userSession.User.RoleID, "fuse_see_invisible_rooms"); if (!canAccessHiddenRooms) { return false; } } if (_sessionList.Count >= _room.MaxGuests) { bool canAccessFullRooms = Stockholm.Get().GameManager.RoleManager.HasAccess(userSession.User.RoleID, "fuse_enter_full_rooms"); if (!canAccessFullRooms) { return false; } } // We need to do some additional checks if it's a private room if (!_room.RoomCategory.SystemCategory) { EFlatAvailability availability = (EFlatAvailability) _room.RoomStatus; if (availability == EFlatAvailability.Passworded) { if (password != _room.Password) { return false; } } } return true; } //********************************************** void OnUserJoined(Session userSession) { _flatEntity.UserInitialise(userSession); } } }