浏览代码

Merge remote-tracking branch 'origin/develop' into develop

Tigran 4 年之前
父节点
当前提交
fa17c22b2d

+ 2 - 2
MafiaTelegramBot/DataBase/Entity/StatisticsEntity.cs

@@ -11,9 +11,9 @@ namespace MafiaTelegramBot.DataBase.Entity
     public class StatisticsEntity
     {
         [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("id") , MaxLength(127)]
-        public long UserId { get; init; }
+        public long UserId { get; set; }
         [DatabaseGenerated(DatabaseGeneratedOption.None)] [Column("role") , MaxLength(127)]
-        public string Role { get; init; }
+        public string Role { get; set; }
         
         [NotMapped]
         private int _games = 0;

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

@@ -1,8 +1,10 @@
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
 using MafiaTelegramBot.DataBase.Entity;
 using MafiaTelegramBot.Game;
+using MafiaTelegramBot.Resources;
 using Microsoft.EntityFrameworkCore;
 
 namespace MafiaTelegramBot.DataBase.EntityDao
@@ -35,6 +37,31 @@ namespace MafiaTelegramBot.DataBase.EntityDao
             await DataBase.SaveChangesAsync();
         }
 
+        public static async Task CreateOrUpdateStats(Player player)
+        {
+            var actualPlayerStats = await UserDao.DataBase.Statistics.Where(s => s.UserId == player.Id).ToListAsync();
+
+            var missingRoles = Enum.GetValues(typeof(Roles)).Cast<Roles>().ToList();
+            
+            for (int i = 0; i < actualPlayerStats.Count; i++)
+            {
+                missingRoles.Remove((Roles) Enum.Parse(typeof(Roles),actualPlayerStats[i].Role));
+            }
+            
+            for (int i = 0; i < missingRoles.Count; i++)
+            {
+                var newStatsRow = new StatisticsEntity();
+                newStatsRow.UserId = player.Id;
+                newStatsRow.Role = missingRoles[i].ToString();
+                newStatsRow.Games = 0;
+                newStatsRow.Wins = 0;
+
+                await DataBase.Statistics.AddAsync(newStatsRow);
+            }
+
+            await DataBase.SaveChangesAsync();
+        }
+
         public static async Task<bool> UserExists(long id)
         {
             if (ActiveUsers.ContainsKey(id)) return true;

+ 41 - 1
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -1,11 +1,14 @@
+using System;
 using System.Collections.Generic;
 using System.Linq;
 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 Microsoft.EntityFrameworkCore;
 using Telegram.Bot.Types;
 using Timer = System.Timers.Timer;
 
@@ -384,13 +387,50 @@ namespace MafiaTelegramBot.Game.GameRooms
                 var rolesMessage = strings.in_this_game_roles;
                 var sortedPLayers = Players.Values.ToList();
                 sortedPLayers.Sort((x, y) => x.TurnOrder - y.TurnOrder);
+                
+                var statsQuery = "SELECT * FROM mafia.statistics WHERE";
+                
                 foreach (var player in sortedPLayers)
                 {
                     rolesMessage += $"\n({player.TurnOrder}) {player.NickName} - {player.GetRoleName()}";
+                    statsQuery += $" id = {player.Id} AND role = '{player.GetRole().ToString()}' OR role = 'All' OR";
                     player.ResetState();
                 }
 
-                //var dblist = UserDao.DataBase.Statistics.Where(s => sortedPLayers.Contains(s.UserId, s.Role.ToString())).ToList();
+                statsQuery = statsQuery.Substring(0, statsQuery.Length - 2);
+
+                try
+                {
+                    var statsList = UserDao.DataBase.Statistics.FromSqlRaw(statsQuery).ToArrayAsync().Result;
+
+                    foreach (var player in statsList)
+                    {
+
+                        player.Games++;
+                        
+                        //Yellow get Wins++
+
+                        if (aliveMafia.Count() == 0)
+                        {
+                            //then civil roles get Wins++;
+                        }
+                        else
+                        {
+                            //then civil rols get Wins++;
+                        }
+                        
+                        //Check Fool Wins++
+                    }
+
+                    UserDao.DataBase.Statistics.UpdateRange(statsList);
+
+                    await UserDao.DataBase.SaveChangesAsync();
+                
+                }
+                catch (Exception e)
+                {
+                    await Console.Out.WriteLineAsync(e.Message);
+                }
                 
                 await PlayersCh.Send(rolesMessage);
                 IsRunning = false;

+ 2 - 0
MafiaTelegramBot/Models/Commands/StartCommand.cs

@@ -18,6 +18,7 @@ namespace MafiaTelegramBot.Models.Commands
             if (await UserDao.UserExists(UserId))
             {
                 var player = await UserDao.GetPlayerById(UserId);
+                await UserDao.CreateOrUpdateStats(player);
                 if (player.GetRoomName() != "")
                     return await Bot.SendWithMarkdown2(ChatId, $"{strings.prefer_leave_from_room} {player.GetRoomName()}");
                 if (command.Length <= 1)
@@ -41,6 +42,7 @@ namespace MafiaTelegramBot.Models.Commands
                 Id = UserId, ChatId = ChatId, Username = username, NickName =  nickName
             };
             await UserDao.Update(user);
+            await UserDao.CreateOrUpdateStats(user);
             UserDao.ActiveUsers.Add(user.Id, user);
             if (command.Length <= 1)
                 return await Bot.SendWithMarkdown2(ChatId, strings.start_message, Keyboard.MainMenu);