using System;
using System.Collections.Generic;
using Woodpecker.Core;
using Woodpecker.Data;
using Woodpecker.Game.Users;
using Woodpecker.Net.Game;
using Woodpecker.Net.Game.Messages;
namespace Woodpecker.Sessions
{
///
/// Provides management for login and logged in users.
///
public static class sessionManager
{
#region Declares
///
/// A keyed collection with the userSession objects of the logged in users.
///
private static Dictionary _Sessions = new Dictionary();
#endregion
#region Properties
///
/// The amount of sessions (aka, logged in users) in the session manager.
///
internal static int Count
{
get
{
return _Sessions.Count;
}
}
#endregion
#region Methods
public static bool userExists(string Username)
{
return Database.checkExists("users", "name", Username);
}
public static void attemptLogin(string Username, string Password, gameMessageHandler m)
{
string[] Data = Database.runReadRowStrings(new string[] {"username"}, new object[] {Username},"SELECT id,username,password,rank FROM users WHERE username = @username");
if(Data.Length > 0)
{
if(Data[1] == Password)
{
userInfo Info = new userInfo();
Info.userID = int.Parse(Data[0]);
Info.Username = Data[1];
Info.Rank = byte.Parse(Data[3]);
Info.Subscriptions = Database.runReadColumnStrings("SELECT subscription FROM users_subscriptions WHERE userid = '" + Info.userID + "' AND active = '1'",0);
userSession Session = new userSession(m.Connection,Info);
m.Connection.Session = Session;
_Sessions.Add(Info.userID, Session);
serverMessage Message = new serverMessage(2); // @B
Message.Append(Access.getFUSERights(Info.Rank, Info.Subscriptions));
m.Connection.sendMessage(Message);
Message = new serverMessage(3); // @C
m.Connection.sendMessage(Message);
return;
}
else
m.sendLocalError("login incorrect: Wrong password");
}
else
m.sendLocalError("login incorrect: Wrong username");
gameConnectionManager.endConnection(m.Connection);
}
#endregion
}
}