Browse Source

Add timer to Player.cs, controlling active time

Veloe 4 years ago
parent
commit
b639aba872

+ 10 - 0
MafiaTelegramBot/Controllers/RoomController.cs

@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Game;
 using MafiaTelegramBot.Game.GameRoles;
 using MafiaTelegramBot.Game.GameRooms;
@@ -25,6 +26,9 @@ namespace MafiaTelegramBot.Controllers
                     : new NormalGameRoom {Owner = creator, RoomName = roomName, IsPrivate = isPrivate};
                 OpenedGames.Add(roomKey, room);
                 room.Players.Add(creator.Id, creator);
+
+                UserDao.GetPlayerById(creator.Id).Result.StopTimer();
+                
                 return ResultCode.CodeOk;
             });
         }
@@ -39,6 +43,9 @@ namespace MafiaTelegramBot.Controllers
                 if (OpenedGames[roomKey].IsFilled()) return ResultCode.RoomIsFilled;
                 if (!player.SetRoomName(roomName)) return ResultCode.UserAlreadyInGame;
                 OpenedGames[roomKey].Players.Add(player.Id, player);
+                
+                UserDao.GetPlayerById(player.Id).Result.StopTimer();
+                
                 await OpenedGames[roomKey].PlayersCh.SendExcept(player.Id, $"{player.NickName} {strings.connected_to_game}");
                 return ResultCode.CodeOk;
             });
@@ -62,6 +69,9 @@ namespace MafiaTelegramBot.Controllers
                 player.CurrentRole = new NoneRole();
                 player.TurnOrder = -1;
                 OpenedGames[roomKey].Players.Remove(player.Id);
+                
+                UserDao.GetPlayerById(player.Id).Result.StartTimer();
+                
                 var message = OpenedGames[roomKey].Owner.Id == player.Id
                     ? $"{player.NickName} \\({strings.room_owner}\\) {strings.leave_from_game}"
                     : $"{player.NickName} {strings.leave_from_game}";

+ 1 - 0
MafiaTelegramBot/DataBase/EntityDao/UserDao.cs

@@ -18,6 +18,7 @@ namespace MafiaTelegramBot.DataBase.EntityDao
             var player = Player.FromUserEntity(user);
             player.OpenedRoles = await OpenedRolesDao.GetOpenedRolesById(id);
             ActiveUsers.Add(user.Id, player);
+            ActiveUsers[user.Id].SetTimer();
             return player;
         }
         

+ 4 - 0
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -4,6 +4,7 @@ using System.Threading;
 using System.Threading.Tasks;
 using MafiaTelegramBot.Controllers;
 using MafiaTelegramBot.CustomCollections.Extensions;
+using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Resources;
 using Telegram.Bot.Types;
@@ -271,6 +272,9 @@ namespace MafiaTelegramBot.Game.GameRooms
                     rolesMessage += $"\n\\({player.TurnOrder}\\) {player.NickName} - {player.GetRoleName()}";
                     player.ResetState();
                 }
+
+                //var dblist = UserDao.DataBase.Statistics.Where(s => sortedPLayers.Contains(s.UserId, s.Role.ToString())).ToList();
+                
                 await PlayersCh.Send(rolesMessage);
                 IsRunning = false;
                 IsDay = false;

+ 45 - 1
MafiaTelegramBot/Game/Player.cs

@@ -7,6 +7,7 @@ using MafiaTelegramBot.CustomCollections;
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.DataBase.EntityDao;
 using MafiaTelegramBot.Game.GameRoles;
+using MafiaTelegramBot.Models;
 using MafiaTelegramBot.Resources;
 using Microsoft.EntityFrameworkCore;
 using Newtonsoft.Json;
@@ -18,10 +19,13 @@ namespace MafiaTelegramBot.Game
         public GameRooms.GameRoom.Role CurrentRole = new NoneRole();
         public int TurnOrder = -1;
         private string _roomName = "";
-        public bool IsAlive = true;
+
+        private static System.Timers.Timer ActiveTime;
         public bool IsSpeaker;
         public bool IsPlaying;
         public bool IsFirst;
+        
+        public bool IsAlive = true;
         public bool CanBeHealed = true;
         public bool IsBlocked = false;
         public bool CanBeBlockedNight = true;
@@ -40,6 +44,46 @@ namespace MafiaTelegramBot.Game
             }
             return true;
         }
+
+        public async Task SetTimer()
+        {
+            ActiveTime = new System.Timers.Timer(3600000);
+            ActiveTime.Elapsed += async (x , y) =>
+            {
+                await Remove();
+            };
+            ActiveTime.Enabled = true;
+        }
+
+        public async Task Restart()
+        {
+            ActiveTime.Stop();
+            ActiveTime.Start();
+        }
+
+        public async Task StopTimer()
+        {
+            ActiveTime.Stop();
+        }
+
+        public async Task StartTimer()
+        {
+            ActiveTime.Start();
+        }
+
+        private async Task Remove()
+        {
+            try
+            {
+                UserDao.ActiveUsers.Remove(Id);
+                ActiveTime.Dispose();
+            }
+            catch (Exception)
+            {
+                await Console.Out.WriteLineAsync("Cant delete user!");
+            }
+            //await Bot.SendHyperLink(ChatId, "Inactive now!");
+        }
         
         public static Player FromUserEntity(UserEntity b)
         {

+ 1 - 0
MafiaTelegramBot/Models/Commands/Command.cs

@@ -34,6 +34,7 @@ namespace MafiaTelegramBot.Models.Commands
             var startCommand = new StartCommand();
             //TODO refactor later
             if (message.Contains(startCommand.Name)) return await ((Command?) startCommand.Clone(chatId, userId))!.Execute(update);
+            await UserDao.GetPlayerById(userId).Result.Restart();
             var user = await UserDao.GetPlayerById(userId);
             if (user.IsPlaying)
             {