83 lines
3.2 KiB
C#
83 lines
3.2 KiB
C#
using NLog;
|
|
using Server.SQL;
|
|
using Server.System;
|
|
|
|
namespace Server.Scheduler
|
|
{
|
|
|
|
public class SessionScheduler : System.Scheduler
|
|
{
|
|
public override void Process()
|
|
{
|
|
List<string> users = Statics.redis.GetList<string>("LoginUser");
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
for (int n = users.Count - 1; n >= 0; n--)
|
|
{
|
|
if ((now - DateTime.Parse(Statics.redis.GetHash("end_login", "UserInfo", users[n]))).TotalMinutes >= 10)
|
|
{
|
|
Statics.redis.RemoveList(n, users[n], "LoginUser");
|
|
Statics.redis.RemoveKey("UserSession", users[n]);
|
|
}
|
|
}
|
|
}
|
|
private static readonly NLog.ILogger logger = LogManager.GetCurrentClassLogger();
|
|
//신규 로그인
|
|
public static string addSession(User user)
|
|
{
|
|
//기존 세션 확인
|
|
string loginUUID = Guid.NewGuid().ToString();
|
|
Dictionary<string, string> users = Statics.redis.GetAllHash("LoginUsers");
|
|
logger.Info($"New User : {user.nickname}, session : {loginUUID}");
|
|
foreach (var item in users)
|
|
{
|
|
if(int.Parse(item.Value) == user.id)
|
|
{
|
|
Statics.redis.RemoveHash(item.Key);
|
|
Statics.redis.SetHash(loginUUID, user.id.ToString(), "LoginUsers");
|
|
updateSession(loginUUID);
|
|
return loginUUID;
|
|
}
|
|
}
|
|
|
|
Statics.redis.SetHash(loginUUID, user.id.ToString(), "LoginUsers");
|
|
Dictionary<string, string> userInfo = new Dictionary<string, string>();
|
|
userInfo.Add("id", user.id.ToString());
|
|
userInfo.Add("uuid", user.uuid);
|
|
userInfo.Add("mail", user.mail);
|
|
userInfo.Add("nickname ", user.nickname);
|
|
userInfo.Add("gold ", user.gold.ToString());
|
|
userInfo.Add("free_cash", user.free_cash.ToString());
|
|
userInfo.Add("pay_cash", user.pay_cash.ToString());
|
|
userInfo.Add("end_login", DateTime.Now.ToString());
|
|
Statics.redis.SetHash(userInfo, "UserInfo", user.id.ToString());
|
|
|
|
return loginUUID;
|
|
}
|
|
|
|
//세션 업데이트
|
|
public static void updateSession(string UUID)
|
|
{
|
|
int id = int.Parse(Statics.redis.GetHash(UUID, "LoginUsers"));
|
|
Statics.redis.SetHash("end_login", DateTime.Now.ToString(), "UserInfo", id.ToString());
|
|
}
|
|
|
|
public static User GetUser(string UUID)
|
|
{
|
|
User user = new User();
|
|
int id = int.Parse(Statics.redis.GetHash(UUID, "LoginUsers"));
|
|
Dictionary<string, string> userHash= Statics.redis.GetAllHash("UserInfo", id.ToString());
|
|
user.id = int.Parse(userHash["id"]);
|
|
user.uuid = userHash["uuid"];
|
|
user.mail = userHash["mail"];
|
|
user.nickname = userHash["nickname"];
|
|
user.gold = int.Parse(userHash["gold"]);
|
|
user.free_cash = int.Parse(userHash["free_cash"]);
|
|
user.pay_cash = int.Parse(userHash["pay_cash"]);
|
|
Statics.redis.SetHash("end_login", DateTime.Now.ToString(), "UserInfo", id.ToString());
|
|
return user;
|
|
}
|
|
}
|
|
}
|