Ejemplo n.º 1
0
 public async Task <BasePlayer> LetmePlay(SEplayer sepl)
 {
     if (sepl != null)
     {
         if (!MMplayers.ContainsKey(sepl.Name))
         {
             MMplayer mm = new MMplayer(sepl);
             MMplayers.TryAdd(sepl.Name, mm);
         }
         else
         {
             ExitQ(sepl.Name);
             MMplayers[sepl.Name].Mode   = sepl.Mode;
             MMplayers[sepl.Name].Mode2  = sepl.Mode2;
             MMplayers[sepl.Name].Server = sepl.Server;
             MMplayers[sepl.Name].Random = sepl.Random;
         }
         lock (QMMplayers)
         {
             QMMplayers.Add(MMplayers[sepl.Name]);
         }
         Save();
         return(new BasePlayer(MMplayers[sepl.Name]));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 2
0
        void QplayersChanged(object aSender, NotifyCollectionChangedEventArgs aArgs)
        {
            if (aArgs.Action == NotifyCollectionChangedAction.Add)
            {
                MMplayer qpl = new MMplayer();
                qpl = aArgs.NewItems[0] as MMplayer;

                int lobbysize = int.Parse(qpl.Mode2.Substring(qpl.Mode2.Length - 1, 1)) * 2;
                qpl.Lobbysize = lobbysize;
                MMplayers[qpl.Name].Lobbysize = lobbysize;
                if (!Lobby.ContainsKey(qpl.Mode + qpl.Mode2))
                {
                    Lobby.TryAdd(qpl.Mode + qpl.Mode2, new ConcurrentDictionary <MMplayer, byte>());
                }
                Lobby[qpl.Mode + qpl.Mode2].TryAdd(MMplayers[qpl.Name], 0);

                //qpl.Lobby = Lobby[qpl.Mode + qpl.Mode2];
                MMplayers[qpl.Name].Lobby = Lobby[qpl.Mode + qpl.Mode2];
                if (LobbyCheck == false)
                {
                    LobbyCheck = true;
                    CheckLobbyJob();
                }
            }
        }
Ejemplo n.º 3
0
 public async Task <string> Deleteme(string name)
 {
     ExitQ(name);
     if (MMplayers.ContainsKey(name))
     {
         MMplayer mm = new MMplayer();
         MMplayers.TryRemove(name, out mm);
         Save();
     }
     return("Ok.");
 }
Ejemplo n.º 4
0
        public async Task <MMgame> Report(dsreplay replay, string id)
        {
            MMgame mmgame = new MMgame();
            int    gid    = int.Parse(id);

            if (Replays.ContainsKey(gid))
            {
                Replays[gid].Add(replay);
            }
            else
            {
                Replays.TryAdd(gid, new ConcurrentBag <dsreplay>());
                Replays[gid].Add(replay);
            }
            Save(Games[gid], replay);
            lock (Games)
            {
                if (Games.ContainsKey(gid))
                {
                    if (Games[gid].Reported == true)
                    {
                        return(Games[gid]);
                    }
                }

                replay.GenHash();
                if (ReplayHash.Contains(replay.HASH))
                {
                    //return null;
                }
                else
                {
                    ReplayHash.Add(replay.HASH);
                }

                List <MMplayer> team1 = new List <MMplayer>();
                List <MMplayer> team2 = new List <MMplayer>();
                foreach (var pl in replay.PLAYERS)
                {
                    MMplayer mmpl = new MMplayer();
                    if (MMplayers.ContainsKey(pl.NAME))
                    {
                        mmpl = MMplayers[pl.NAME];
                    }
                    else
                    {
                        mmpl.Name = "Dummy";
                    }

                    if (pl.TEAM == replay.WINNER)
                    {
                        team1.Add(mmpl);
                    }
                    else
                    {
                        team2.Add(mmpl);
                    }
                }
                MMgame repgame = new MMgame();
                repgame          = MMrating.RateGame(team1, team2);
                repgame.ID       = Games[gid].ID;
                repgame.Quality  = Games[gid].Quality;
                repgame.Server   = Games[gid].Server;
                repgame.Reported = true;
                Games[gid]       = repgame;
            }
            Save();
            return(Games[gid]);
        }
Ejemplo n.º 5
0
        MMgame CheckLobby(ConcurrentDictionary <MMplayer, byte> lobby)
        {
            int lobbysize = 0;

            if (lobby.Count > 0)
            {
                lobbysize = lobby.Keys.First().Lobbysize;
            }

            var rng      = lobby.Keys.Where(x => x.Random == true);
            int minticks = lobby.OrderBy(o => o.Key.Ticks).First().Key.Ticks;

            if (rng.Count() >= 2 && lobby.Count < lobbysize && minticks > Minticks)
            {
                int i = 0;
                while (lobby.Count < lobbysize)
                {
                    i++;
                    MMplayer mm = new MMplayer();
                    mm.Name      = "Random" + i;
                    mm.Ticks     = Minticks * 3 + 1;
                    mm.Accepted  = true;
                    mm.Lobbysize = lobbysize;
                    lobby.TryAdd(mm, 0);
                }
            }

            MMgame game = new MMgame();

            lock (lobby)
            {
                if (lobbysize > 0 && lobby.Count >= lobbysize)
                {
                    if (minticks > Minticks || lobby.Count > lobbysize)
                    {
                        _logger.LogInformation("Generating Matchup .. " + minticks);
                        game = GenMatchup(lobby, lobbysize).Result;
                        if (game != null)
                        {
                            Interlocked.Increment(ref MMID);
                            game.ID = MMID;
                            _logger.LogInformation("Game found: {0} ({1}) ", MMID, lobby.Count);

                            Save(game);
                            byte zero = 0;
                            var  dic  = GameList(game).ToDictionary(x => new MMplayer(x), x => zero);

                            Dictionary <string, int> Server = new Dictionary <string, int>();
                            foreach (var pl in GameList(game))
                            {
                                Match m = rx_rng.Match(pl.Name);
                                if (m.Success)
                                {
                                    continue;
                                }
                                lock (MMplayers[pl.Name])
                                {
                                    MMplayers[pl.Name].Game  = game;
                                    MMplayers[pl.Name].Lobby = new ConcurrentDictionary <MMplayer, byte>(dic);
                                }
                                lobby.TryRemove(MMplayers[pl.Name], out zero);

                                if (lobby.Count == 0)
                                {
                                    int p = 0;
                                    foreach (var ent in Lobby.Values)
                                    {
                                        if (ent != null && ent.Count > 0)
                                        {
                                            p++;
                                        }
                                    }
                                    if (p == 0)
                                    {
                                        LobbyCheck = false;
                                    }
                                }

                                if (!Server.ContainsKey(MMplayers[pl.Name].Server))
                                {
                                    Server.Add(MMplayers[pl.Name].Server, 1);
                                }
                                else
                                {
                                    Server[MMplayers[pl.Name].Server]++;
                                }
                            }
                            game.Server = Server.OrderByDescending(o => o.Value).First().Key;
                            Games.TryAdd(MMID, game);
                        }
                    }
                }
                // fail safe
                foreach (var ent in lobby.Keys)
                {
                    Match m    = rx_rng.Match(ent.Name);
                    byte  zero = 0;
                    if (m.Success)
                    {
                        lobby.TryRemove(ent, out zero);
                    }
                }
            }
            return(game);
        }
Ejemplo n.º 6
0
        public MMrepository(ILogger <MMrepository> logger)
        {
            _logger = logger;

            if (File.Exists(data_json))
            {
                TextReader reader = new StreamReader(data_json, Encoding.UTF8);
                string     fileContents;
                while ((fileContents = reader.ReadLine()) != null)
                {
                    var player = JsonConvert.DeserializeObject <BasePlayer>(fileContents);
                    if (player != null && player.Name != null)
                    {
                        MMplayer pl = new MMplayer();
                        pl = new MMplayer(player);
                        MMplayers.TryAdd(player.Name, pl);
                    }
                }
                reader.Close();
            }
            _logger.LogInformation("Initialized MMplayers with {0} ents.", MMplayers.Count());

            foreach (var file in Directory.EnumerateFiles(WorkDir + "/games", "*_found.json"))
            {
                var   ent = Path.GetFileName(file);
                Match m   = rx_mmid.Match(ent);
                if (m.Success)
                {
                    int mmid = int.Parse(m.Groups[1].ToString());
                    if (mmid > MMID)
                    {
                        MMID = mmid;
                    }
                }

                try
                {
                    var json = JsonConvert.DeserializeObject <MMgame>(File.ReadAllText(file));
                    Games.TryAdd(json.ID, json);
                } catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            _logger.LogInformation("Setting MMID to " + MMID);

            foreach (var file in Directory.EnumerateFiles(WorkDir + "/games", "*_report.json"))
            {
                try
                {
                    var json = JsonConvert.DeserializeObject <MMgame>(file);
                    Games[json.ID] = json;
                }
                catch { }
            }

            foreach (var file in Directory.EnumerateFiles(WorkDir + "/games", "*_replay_*.json"))
            {
                try
                {
                    var json = JsonConvert.DeserializeObject <dsreplay>(file);
                    ReplayHash.Add(json.HASH);
                }
                catch { }
            }

            /**
             * var options = new JsonSerializerOptions
             * {
             *  WriteIndented = true
             * };
             * var ladder = System.Text.Json.JsonSerializer.Serialize(laddergames, options);
             * File.WriteAllText(WorkDir + "/ladder.json", ladder);
             **/
            GetLadder();
            QMMplayers.CollectionChanged += QplayersChanged;
        }