using System;
using System.Text;
using System.Collections;
using Duck.Managers;
using Duck.Virtual.Rooms;
namespace Duck
{
///
/// Provides alot of functions which do things. doh.
///
public static class Access
{
#region Username checks, passwords etc
///
/// Returns a boolean that indicates if a virtual user with a certain username exists in the 'users' table of the database.
///
/// The username to check. This method does not stripslash it.
public static bool userExists(string Username)
{
return DB.checkExists("SELECT id FROM users WHERE username = '" + Username + "'");
}
///
/// Returns the ID of the error at a name check of a username/new pet. If no errors, then 0 is returned.
///
/// Specifies if this namecheck is for a pet purchase.
/// The name to check.
public static int getNameCheckError(bool Pet, string Name)
{
string sName = DB.Stripslash(Name);
if (sName != Name || Name.Length > 15)
return 2;
else
{
if (Pet == false && userExists(Name))
return 4; // Username already taken
else
return 0; // OK
}
}
///
/// Returns a boolean that indicates if a string is valid as a password for a virtual user. The string shouldn't be the same as the username, it should be 6-9 characters long, it should contain at least one number and no 'stripslashed' characters.
///
/// The username for this password.
/// The password to check.
public static bool passwordOK(string Username, string Password)
{
return (Username != Password && (Password.Length > 5 && Password.Length < 10) && stringManager.hasNumbers(Password) && DB.Stripslash(Password) == Password);
}
///
/// Returns a boolean that indicates if an email address is valid.
///
/// The email address to check.
public static bool emailOK(string Email)
{
try
{
// mike.fails@script-o-matic.net :]
string[] Parts = Email.Split('@');
return (Parts.Length == 2 && Parts[1].Contains(".") && DB.Stripslash(Email) == Email);
}
catch { return false; }
}
#endregion
#region FUSE rights
#region Declares
///
/// Contains the accessRank objects.
///
private static Hashtable _accessRanks = new Hashtable();
#endregion
#region Methods
///
/// Initializes the access ranks for virtual users.
///
public static void initAccessRanks()
{
IO.WriteLine("Initializing access ranks...");
for (byte i = 0; i <= 6; i++)
_accessRanks.Add(i, new accessRank(i));
string[] Subscriptions = { "club_habbo" };
foreach (string Subscription in Subscriptions)
_accessRanks.Add(Subscription, new accessRank(Subscription));
IO.WriteLine("Access ranks initialized.");
}
///
/// Returns a boolean that indicates if a certain access rank contains a certain FUSE right.
///
/// The ID of the main rank.
/// The subscriptions that the virtual user has.
/// The FUSE right to check.
public static bool accessRankHasFUSERight(byte rankID, string[] Subscriptions, string Right)
{
try
{
foreach (string acRight in ((accessRank)_accessRanks[rankID]).Rights)
if (acRight == Right)
return true;
foreach (string Subscription in Subscriptions)
foreach (string acRight in ((accessRank)_accessRanks[Subscription]).Rights)
if (acRight == Right)
return true;
return false;
}
catch { return false; }
}
///
/// Returns all the FUSE rights for a certain user rank + subscription as a string. The rights are separated by \x02.
///
/// The user rank of the virtual user.
/// The subscriptions that the virtual user has.
public static string getFUSERights(byte rankID, string[] Subscriptions)
{
StringBuilder Rights = new StringBuilder();
try
{
foreach (string Right in ((accessRank)_accessRanks[rankID]).Rights)
Rights.Append(Right + Convert.ToChar(2));
foreach (string Subscription in Subscriptions)
foreach (string Right in ((accessRank)_accessRanks[Subscription]).Rights)
Rights.Append(Right + Convert.ToChar(2));
return Rights.ToString();
}
catch { return Rights.ToString(); }
}
#endregion
#region Private members
///
/// Represents an access rank for virtual users, for a user rank or a subscription. Contains the FUSE rights that this rank provides.
///
private struct accessRank
{
///
/// The FUSE rights of this access rank.
///
internal string[] Rights;
///
/// Initializes a user rank.
///
/// The ID of the user rank to initialize.
internal accessRank(byte rankID)
{
this.Rights = DB.runReadColumn("SELECT fuseright FROM duck_fuse WHERE minrank <= " + rankID + "", 0);
}
///
/// Initializes a subscription rank.
///
/// The subscription rank to initialize, eg, 'club_habbo'.
internal accessRank(string Subscription)
{
this.Rights = DB.runReadColumn("SELECT fuseright FROM duck_subscriptions_fuse WHERE subscription = '" + Subscription + "'", 0);
}
}
#endregion
#endregion
#region Financial
///
/// Writes a transaction (eg, furniture purchase) by a virtual user into the database, together with the current date + time.
///
/// The database ID of the user that did the transaction.
/// The type of the transaction, eg, 'stuff_store'.
/// The activity of the transaction, eg, '-5' or '10'.
public static void logTransaction(int userID, string Type, int Activity)
{
DB.runQuery("INSERT INTO users_transactions(userid,date,time,type,activity) VALUES ('" + userID + "','" + DateTime.Today.ToShortDateString() + "','" + DateTime.Now.ToShortTimeString() + "','" + Type + "','" + Activity + "')");
}
#endregion
#region Rooms
///
/// Converts an access level of a guestroom to it's virtualRoom.accessFlag equivalent.
///
/// The access level as an integer.
public static virtualRoom.accessFlags getRoomAccessFlag(int Level)
{
return (virtualRoom.accessFlags)Level;
}
///
/// Converts an access level of a guestroom to it's virtualRoom.accessFlag equivalent.
///
/// The access level as a string.
public static virtualRoom.accessFlags getRoomAccessFlag(string Level)
{
try { return (virtualRoom.accessFlags)int.Parse(Level); }
catch { return virtualRoom.accessFlags.open; }
}
public static int convertRoomAccessFlag(string accessFlag)
{
try { return (int)Enum.Parse(typeof(virtualRoom.accessFlags), accessFlag, false); }
catch { return 0; }
}
#endregion
}
}