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
 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.º 3
0
 public async Task <string> Random(string name)
 {
     if (MMplayers.ContainsKey(name))
     {
         MMplayers[name].Random = !MMplayers[name].Random;
         if (MMplayers[name].Random == true)
         {
             MMplayers[name].Ticks = 0;
         }
     }
     return("Ok.");
 }
Ejemplo n.º 4
0
        public void Ladder()
        {
            string bab = "";

            foreach (var ent in MMplayers.OrderByDescending(o => o.Value.EXP))
            {
                Match m = rx_rng.Match(ent.Value.Name);
                if (m.Success)
                {
                    continue;
                }
                bab += String.Format("{0} => {1} => {2} => {3} => {4}", ent.Value.Name, ent.Value.EXP, ent.Value.MU, ent.Value.SIGMA, ent.Value.Games) + Environment.NewLine;
            }
            _readWriteLock.EnterWriteLock();
            File.WriteAllText(data_json + "_ladder.txt", bab);
            _readWriteLock.ExitWriteLock();
        }
Ejemplo n.º 5
0
        public async Task <string> ExitQ(string name)
        {
            if (MMplayers.ContainsKey(name))
            {
                if (QMMplayers.Contains(MMplayers[name]))
                {
                    if (MMplayers[name].Lobby.ContainsKey(MMplayers[name]))
                    {
                        foreach (var pl in MMplayers[name].Lobby.Keys)
                        {
                            lock (MMplayers[pl.Name])
                            {
                                byte zero = 0;
                                MMplayers[pl.Name].Lobby.TryRemove(MMplayers[name], out zero);
                            }
                        }
                    }

                    lock (QMMplayers)
                    {
                        QMMplayers.Remove(MMplayers[name]);
                    }
                }
                ResetMMplayer(name);
            }

            // fail safe
            foreach (var lobby in Lobby.Values)
            {
                lock (lobby)
                {
                    foreach (var pl in lobby)
                    {
                        if (pl.Key.Name == name)
                        {
                            byte zero = 0;
                            lobby.TryRemove(MMplayers[name], out zero);
                        }
                    }
                }
            }

            return("Ok");
        }
Ejemplo n.º 6
0
        public StartUp(DbContextOptions <MMdb> mmdb)
        {
            _mmdb = mmdb;
            _db   = new MMdb(_mmdb);
            foreach (string cmdr in DSdata.s_races)
            {
                MMraces.TryAdd(cmdr, new MMplayerNG(cmdr));
            }

            /**
             * using (var db = new MMdb(_mmdb))
             * {
             * foreach (var ent in db.MMdbPlayers)
             *     db.MMdbPlayers.Remove(ent);
             * foreach (var ent in db.MMdbRatings)
             *     db.MMdbRatings.Remove(ent);
             * foreach (var ent in db.MMdbRaceRatings)
             *     db.MMdbRaceRatings.Remove(ent);
             *
             * db.SaveChanges();
             * }
             **/

            // /**
            using (var db = new MMdb(_mmdb))
            {
                foreach (var ent in db.MMdbPlayers)
                {
                    MMplayers[ent.Name] = new MMplayerNG(ent);
                }

                foreach (var ent in db.MMdbRaces)
                {
                    MMraces[ent.Name] = new MMplayerNG(ent);
                }

                foreach (var ent in db.MMdbRatings.OrderBy(o => o.Games))
                {
                    if (MMplayers.ContainsKey(ent.MMdbPlayer.Name))
                    {
                        if (MMplayers[ent.MMdbPlayer.Name].AuthName == ent.MMdbPlayer.AuthName)
                        {
                            MMplayers[ent.MMdbPlayer.Name].Rating[ent.Lobby].Add(new MMPlRating(ent));
                        }
                    }
                }

                foreach (var ent in db.MMdbRaceRatings.OrderBy(o => o.Games))
                {
                    if (MMraces.ContainsKey(ent.MMdbRace.Name))
                    {
                        MMraces[ent.MMdbRace.Name].Rating[ent.Lobby].Add(new MMPlRating(ent));
                    }
                }
            }
            // **/
            foreach (string name in MMplayers.Keys)
            {
                if (MMplayers[name].AuthName != null && MMplayers[name].AuthName != "")
                {
                    Players.Add(MMplayers[name].Name);
                    Auth.Add(MMplayers[name].AuthName, MMplayers[name].Name);
                }
            }

            if (!File.Exists(Program.myJson_file))
            {
                File.Create(Program.myJson_file).Dispose();
            }

            foreach (var line in File.ReadAllLines(Program.myJson_file))
            {
                dsreplay rep = null;
                try
                {
                    rep = JsonSerializer.Deserialize <dsreplay>(line);
                } catch { }
                if (rep != null)
                {
                    //rep.Init();
                    repHash.Add(rep.HASH);
                    if (!replays.ContainsKey(rep.ID))
                    {
                        replays[rep.ID] = new List <dsreplay>();
                    }
                    replays[rep.ID].Add(rep);
                }
            }

            if (!File.Exists(Program.myReplays_file))
            {
                File.Create(Program.myReplays_file).Dispose();
            }

            foreach (var line in File.ReadAllLines(Program.myReplays_file))
            {
                dsreplay rep = null;
                try
                {
                    rep = JsonSerializer.Deserialize <dsreplay>(line);
                }
                catch { }
                if (rep != null)
                {
                    MyReplays.Add(rep);
                }
            }

            string exedir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            Exedir = exedir;
            foreach (var file in Directory.EnumerateFiles(Program.replaydir))
            {
                string dest = exedir + "/replays/" + Path.GetFileName(file);
                if (!File.Exists(dest))
                {
                    File.Copy(file, dest);
                }
            }
            foreach (var file in Directory.EnumerateFiles(Program.myreplaydir))
            {
                string dest = exedir + "/replays/" + Path.GetFileName(file);
                if (!File.Exists(dest))
                {
                    File.Copy(file, dest);
                }
            }

            foreach (var dir in Directory.EnumerateDirectories(Program.workdir + "/tournaments"))
            {
                string tourny = Path.GetFileName(dir);
                if (File.Exists(dir + "/treplays.json"))
                {
                    TournamentReplays.Add(tourny, new List <dsreplay>());
                    foreach (var line in File.ReadAllLines(dir + "/treplays.json"))
                    {
                        dsreplay rep = null;
                        try
                        {
                            rep = JsonSerializer.Deserialize <dsreplay>(line);
                        }
                        catch { }
                        if (rep != null)
                        {
                            TournamentReplays[tourny].Add(rep);
                        }
                    }

                    if (!Directory.Exists(Exedir + "/treplays/" + tourny))
                    {
                        Directory.CreateDirectory(Exedir + "/treplays/" + tourny);
                    }

                    foreach (var file in Directory.EnumerateFiles(dir + "/replays"))
                    {
                        string dest = Exedir + "/treplays/" + tourny + "/" + Path.GetFileName(file);
                        if (!File.Exists(dest))
                        {
                            File.Copy(file, dest);
                        }
                    }
                }
                if (File.Exists(dir + "/info.json"))
                {
                    TournamentInfo t = JsonSerializer.Deserialize <TournamentInfo>(File.ReadAllText(dir + "/info.json"));
                    TournamentInfo[tourny] = t;
                }
            }

            foreach (var file in Directory.EnumerateFiles(Program.commentdir))
            {
                GameComment com = JsonSerializer.Deserialize <GameComment>(File.ReadAllText(file));
                if (com != null)
                {
                    GameComments[com.RepId] = com;
                }
            }

            // ladder init

            //LadderInit();
        }
Ejemplo n.º 7
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.º 8
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;
        }
Ejemplo n.º 9
0
        public StartUp(DbContextOptions <MMdb> mmdb)
        {
            _mmdb = mmdb;

            foreach (string cmdr in DSdata.s_races)
            {
                MMraces.TryAdd(cmdr, new MMplayerNG(cmdr));
            }

            /**
             * using (var db = new MMdb(_mmdb))
             * {
             *  foreach (var ent in db.MMdbPlayers)
             *      db.MMdbPlayers.Remove(ent);
             *  foreach (var ent in db.MMdbRatings)
             *      db.MMdbRatings.Remove(ent);
             *  foreach (var ent in db.MMdbRaceRatings)
             *      db.MMdbRaceRatings.Remove(ent);
             *
             *  db.SaveChanges();
             * }
             **/

            // /**
            using (var db = new MMdb(_mmdb))
            {
                foreach (var ent in db.MMdbPlayers)
                {
                    MMplayers[ent.Name] = new MMplayerNG(ent);
                }

                foreach (var ent in db.MMdbRaces)
                {
                    MMraces[ent.Name] = new MMplayerNG(ent);
                }

                foreach (var ent in db.MMdbRatings)
                {
                    if (MMplayers.ContainsKey(ent.MMdbPlayer.Name))
                    {
                        if (MMplayers[ent.MMdbPlayer.Name].AuthName == ent.MMdbPlayer.AuthName)
                        {
                            MMplayers[ent.MMdbPlayer.Name].Rating[ent.Lobby] = new MMPlRating(ent);
                        }
                    }
                }

                foreach (var ent in db.MMdbRaceRatings)
                {
                    if (MMraces.ContainsKey(ent.MMdbRace.Name))
                    {
                        MMraces[ent.MMdbRace.Name].Rating[ent.Lobby] = new MMPlRating(ent);
                    }
                }
            }
            // **/
            foreach (string name in MMplayers.Keys)
            {
                if (MMplayers[name].AuthName != null && MMplayers[name].AuthName != "")
                {
                    Players.Add(MMplayers[name].Name);
                    Auth.Add(MMplayers[name].AuthName, MMplayers[name].Name);
                }
            }

            if (!File.Exists(Program.myJson_file))
            {
                File.Create(Program.myJson_file).Dispose();
            }

            foreach (var line in File.ReadAllLines(Program.myJson_file))
            {
                dsreplay rep = null;
                try
                {
                    rep = JsonSerializer.Deserialize <dsreplay>(line);
                } catch { }
                if (rep != null)
                {
                    //rep.Init();
                    repHash.Add(rep.HASH);
                    if (!replays.ContainsKey(rep.ID))
                    {
                        replays[rep.ID] = new List <dsreplay>();
                    }
                    replays[rep.ID].Add(rep);
                }
            }

            // ladder init

            /**
             * Save();
             * List<string> LadderGames = new List<string>();
             * if (File.Exists(Program.ladder_file))
             * {
             *  LadderGames = JsonSerializer.Deserialize<List<string>>(File.ReadAllText(Program.ladder_file));
             * }
             * foreach (var ent in LadderGames)
             * {
             *  MMgameNG game;
             *  MMgameNG racegame;
             *  (game, racegame) = MMrating.RateGame(ent, "Commander3v3True", this);
             *  Save();
             *  Save(game);
             *  SaveRace(racegame);
             * }
             **/
        }