using System;
using System.Data;
using Woodpecker.Storage;
using Woodpecker.Specialized.Text;
using Woodpecker.Game.Users;
namespace Woodpecker.Game.Rooms
{
///
/// Contains information about a certain room. This class cannot be inherited.
///
public sealed class roomInformation
{
#region Fields
///
/// The database ID of this room.
///
public int ID;
///
/// True if this room is a user flat.
///
public bool isUserFlat
{
get { return this.ownerID > 0; }
}
///
/// The database ID of the owner of this room.
///
public int ownerID;
///
/// The username of this room's owner.
/// ownerID
///
public string Owner = "-";
///
/// The database ID of the parent category of this room.
///
public int categoryID;
///
/// The current name of this room.
///
public string Name;
///
/// The current description of this room.
///
public string Description;
///
/// A value of the Woodpecker.Game.Rooms.roomAccessType representing the current state of the room. (locked, doorbell only etc)
///
public roomAccessType accessType;
///
/// True if the room's owner username should be displayed.
///
public bool showOwner;
///
/// True if all users in the room have rights to move furniture. (furniture CANNOT be taken out of the room)
///
public bool superUsers;
///
/// The password if this room. (if access type = 'password')
///
public string Password;
///
///
/// The current amount of visitors in an instance of this room.
///
public int currentVisitors;
///
/// The configured max amount of visitors for an instance of this room.
///
public int maxVisitors;
///
/// The model type of this room.
///
public string modelType;
///
/// Public spaces only. A string with extra casts for the room. (external .ccts etc)
///
public string CCTs;
///
/// The floor decoration value.
///
public int Floor;
///
/// The wallpaper decoration value.
///
public int Wallpaper;
#endregion
#region Methods
///
/// Parses all required fields from a given System.Data.DataRow object to a full roomInformation object.
///
/// The System.Data.DataRow object containing the required fields.
public static roomInformation Parse(DataRow dRow, bool parseOwnerName)
{
if (dRow == null)
return null;
else
{
roomInformation Room = new roomInformation();
Room.ID = (int)dRow["id"];
Room.ownerID = (int)dRow["ownerid"];
Room.categoryID = (int)dRow["category"];
if(parseOwnerName && dRow["owner"] != DBNull.Value)
Room.Owner = (string)dRow["owner"];
Room.Name = (string)dRow["roomname"];
Room.Description = (string)dRow["description"];
Room.modelType = (string)dRow["modeltype"];
if(dRow["ccts"] != DBNull.Value)
Room.CCTs = (string)dRow["ccts"];
Room.Wallpaper = int.Parse(dRow["wallpaper"].ToString());
Room.Floor = int.Parse(dRow["floor"].ToString());
Room.accessType = (roomAccessType)int.Parse(dRow["accesstype"].ToString());
Room.showOwner = (dRow["showname"].ToString() == "true");
Room.superUsers = (dRow["superusers"].ToString() == "true");
Room.Password = (string)dRow["password"];
Room.currentVisitors = int.Parse(dRow["visitors_now"].ToString());
Room.maxVisitors = int.Parse(dRow["visitors_max"].ToString());
return Room;
}
}
///
/// Parses all required fields for a user flat (guestroom) from a given System.Data.DataRow object to a flat-only roomInformation object.
///
/// The System.Data.DataRow object containing the required fields.
public static roomInformation ParseFlat(DataRow dRow)
{
if (dRow == null)
return null;
else
{
roomInformation Room = new roomInformation();
Room.ID = (int)dRow["id"];
Room.ownerID = (int)dRow["ownerid"];
Room.Owner = (string)dRow["owner"];
Room.Name = (string)dRow["roomname"];
Room.Description = (string)dRow["description"];
Room.accessType = (roomAccessType)int.Parse(dRow["accesstype"].ToString());
Room.currentVisitors = int.Parse(dRow["visitors_now"].ToString());
Room.maxVisitors = int.Parse(dRow["visitors_max"].ToString());
return Room;
}
}
///
/// Parses the generic roomInformation object to a room information string.
///
/// The userInformation object of the user that requests the flat information string.
public string ToString(userInformation viewingUser)
{
fuseStringBuilder FSB = new fuseStringBuilder();
FSB.appendWired(this.ID); // Room ID
if (!this.isUserFlat) // Public space flag
FSB.appendWired(true);
FSB.appendClosedValue(this.Name); // Room name
if (this.isUserFlat) // User flat
{
if (this.showOwner || this.ownerID == viewingUser.ID || viewingUser.hasFuseRight("fuse_see_all_roomowners"))
FSB.appendClosedValue(this.Owner);
else
FSB.Append("-");
FSB.appendClosedValue(this.accessType.ToString());
}
FSB.appendWired(this.currentVisitors);
FSB.appendWired(this.maxVisitors);
if (!this.isUserFlat)
FSB.appendWired(this.categoryID);
FSB.appendClosedValue(this.Description);
if (!this.isUserFlat)
{
FSB.appendWired(this.ID);
FSB.appendWired(false);
FSB.appendClosedValue(this.CCTs);
FSB.appendWired(false);
FSB.appendWired(true);
}
return FSB.ToString();
}
///
/// Converts the required fields from this object to a user flat information string. The 'show owner username' property is handled here as well, by checking the permissions of the given user.
///
/// The userInformation object of the user that requests the flat information string.
///
public string ToUserFlatString()
{
fuseStringBuilder FSB = new fuseStringBuilder();
FSB.appendTabbedValue(this.ID.ToString());
FSB.appendTabbedValue(this.Name);
FSB.appendTabbedValue(this.Owner);
FSB.appendTabbedValue(this.accessType.ToString());
FSB.appendTabbedValue("x");
FSB.appendTabbedValue(this.currentVisitors.ToString());
FSB.appendTabbedValue(this.maxVisitors.ToString());
FSB.appendTabbedValue("null");
FSB.appendTabbedValue(this.Description);
FSB.appendTabbedValue(this.Description);
FSB.appendChar(13);
return FSB.ToString();
}
public void updateSettings()
{
Database Database = new Database(false, true);
Database.addParameterWithValue("roomid", this.ID);
Database.addParameterWithValue("category", this.categoryID);
Database.addParameterWithValue("roomname", this.Name);
Database.addParameterWithValue("description", this.Description);
Database.addParameterWithValue("accesstype", ((int)this.accessType).ToString());
Database.addParameterWithValue("showname", this.showOwner.ToString().ToLower());
Database.addParameterWithValue("superusers", this.superUsers.ToString().ToLower());
Database.addParameterWithValue("password", this.Password);
Database.addParameterWithValue("visitors_max", this.maxVisitors);
Database.Open();
if (Database.Ready)
Database.runQuery("UPDATE rooms SET category = @category,roomname = @roomname,description = @description,accesstype = @accesstype,showname = @showname,superusers = @superusers,password = @password,visitors_max = @visitors_max WHERE id = @roomid LIMIT 1");
}
///
/// Updates the fields 'wallpaper' and 'floor' for this room.
///
public void updateFlatProperties()
{
Database dbClient = new Database(false, true);
dbClient.addParameterWithValue("roomid", this.ID);
dbClient.addParameterWithValue("wallpaper", this.Wallpaper);
dbClient.addParameterWithValue("floor", this.Floor);
dbClient.Open();
if (dbClient.Ready)
dbClient.runQuery("UPDATE rooms SET wallpaper = @wallpaper,floor = @floor WHERE id = @roomid LIMIT 1");
}
#endregion
}
}