85 lines
3.2 KiB
C#
85 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)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, object> users = Statics.redis.GetAllHash("LoginUsers");
|
|
logger.Info($"New User : {user.nickname}, session : {loginUUID}");
|
|
foreach (var item in users)
|
|
{
|
|
if(item.Value == (object)(StackExchange.Redis.RedisValue)user.id)
|
|
{
|
|
Statics.redis.RemoveHash(item.Key);
|
|
Statics.redis.SetHash(loginUUID, user.id, "LoginUsers");
|
|
updateSession(loginUUID);
|
|
return loginUUID;
|
|
}
|
|
}
|
|
|
|
Statics.redis.SetHash(loginUUID, user.id, "LoginUsers");
|
|
Dictionary<string, object> userInfo = new Dictionary<string, object>();
|
|
userInfo.Add("id", user.id);
|
|
userInfo.Add("uuid", user.uuid);
|
|
userInfo.Add("mail", user.mail);
|
|
userInfo.Add("nickname", user.nickname);
|
|
userInfo.Add("gold", user.gold);
|
|
userInfo.Add("free_cash", user.free_cash);
|
|
userInfo.Add("pay_cash", user.pay_cash);
|
|
userInfo.Add("end_login", DateTime.Now);
|
|
Statics.redis.SetHash(userInfo, "UserInfo", user.id);
|
|
|
|
return loginUUID;
|
|
}
|
|
|
|
//세션 업데이트
|
|
public static void updateSession(string UUID)
|
|
{
|
|
int id = (int)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)Statics.redis.GetHash(UUID, "LoginUsers");
|
|
Dictionary<string, object> userHash= Statics.redis.GetAllHash("UserInfo", id.ToString());
|
|
|
|
|
|
user.id = (int)userHash["id"];
|
|
user.uuid = (string)userHash["uuid"];
|
|
user.mail = (string)userHash["mail"];
|
|
user.nickname = (string)userHash["nickname"];
|
|
user.gold = (int)userHash["gold"];
|
|
user.free_cash = (int)userHash["free_cash"];
|
|
user.pay_cash = (int)userHash["pay_cash"];
|
|
Statics.redis.SetHash("end_login", DateTime.Now.ToString(), "UserInfo", id.ToString());
|
|
return user;
|
|
}
|
|
}
|
|
}
|