示例#1
0
        public void LoadSampleData()
        {
            string samplejson = _startUp.Conf.ExeDir + "/Json/sample.json";

            if (File.Exists(samplejson))
            {
                lock (Replays)
                {
                    Replays.Clear();
                    foreach (string fileContents in File.ReadLines(samplejson))
                    {
                        dsreplay rep = null;
                        try
                        {
                            rep = JsonSerializer.Deserialize <dsreplay>(fileContents);
                            if (rep != null)
                            {
                                rep.Init();
                                Replays.Add(rep);
                            }
                        }
                        catch { }
                    }
                }
                _startUp.Conf.Players.Add("player");
                _dsdata.Init(Replays);
            }
        }
示例#2
0
        public static List <DSReplay> ReadJson(string file)
        {
            List <DSReplay>  DSReplays  = new List <DSReplay>();
            HashSet <string> ReplayHash = new HashSet <string>();

            string plhash = Path.GetFileNameWithoutExtension(file);

            Console.WriteLine("Working on " + plhash);

            if (File.Exists(file))
            {
                foreach (string line in File.ReadAllLines(file))
                {
                    dsreplay rep = JsonSerializer.Deserialize <dsreplay>(line);
                    if (ReplayHash.Contains(rep.REPLAY))
                    {
                        continue;
                    }
                    DSReplay dsrep = Map.Rep(rep, plhash);
                    ReplayHash.Add(rep.REPLAY);
                    DSReplays.Add(dsrep);
                }
            }
            return(DSReplays);
        }
示例#3
0
        public static void SetUnits(dsreplay rep)
        {
            foreach (dsplayer pl in rep.PLAYERS)
            {
                foreach (int gl in pl.SPAWNS.Keys)
                {
                    Dictionary <string, int> units = pl.SPAWNS[gl];

                    if (gl >= 20640 && gl <= 22080)
                    {
                        SetBp("MIN15", MIN15, gl, pl, units);
                    }
                    else if (gl >= 13440 && gl <= 14880)
                    {
                        SetBp("MIN10", MIN10, gl, pl, units);
                    }
                    else if (gl >= 6240 && gl <= 7680)
                    {
                        SetBp("MIN5", MIN5, gl, pl, units);
                    }
                }
                if (pl.SPAWNS.Count() > 0)
                {
                    pl.UNITS["ALL"] = pl.SPAWNS.ElementAt(pl.SPAWNS.Count() - 1).Value;
                }

                if (pl.STATS.Count() > 0)
                {
                    pl.KILLSUM = pl.STATS.ElementAt(pl.STATS.Count() - 1).Value.m_scoreValueMineralsKilledArmy;
                }

                pl.INCOME = Math.Round(pl.INCOME, 2);
            }
        }
示例#4
0
        public async Task <dsreplay> myDecode(string rep, int mmid, bool saveit = true)
        {
            return(await Task.Run(() =>
            {
                dsreplay replay = null;
                lock (dec_lock)
                {
                    //_s2dec.REPID = mmid - 1;
                    replay = _s2dec.DecodePython(rep, false, true);
                    if (replay != null)
                    {
                        replay.ID = mmid;
                        replay.REPLAY = rep;
                        var json = JsonSerializer.Serialize(replay);
                        File.AppendAllText(Program.myReplays_file, json + Environment.NewLine);
                        _startUp.MyReplays.Add(replay);
                        string dest = _startUp.Exedir + "/replays/" + Path.GetFileName(rep);
                        if (!File.Exists(dest))
                        {
                            File.Copy(rep, dest);
                        }

                        SaveDetails(replay);
                    }
                }
                return replay;
            }));
        }
示例#5
0
        public async Task SaveDetails(dsreplay replay, string detaildir = Program.detaildir)
        {
            string mydir = detaildir + "/" + replay.ID.ToString();

            if (!Directory.Exists(mydir))
            {
                Directory.CreateDirectory(mydir);
                WriteToBinaryFile(mydir + "/mid.bin", replay.MIDDLE);

                foreach (dsplayer pl in replay.PLAYERS)
                {
                    PlStats st = new PlStats();
                    st.Loops = new List <int>(pl.STATS.Keys.ToList());
                    st.Stats = new List <M_stats>(pl.STATS.Values.ToList());
                    try
                    {
                        WriteToBinaryFile(mydir + "/" + pl.REALPOS.ToString() + "_stats.bin", st);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
        }
示例#6
0
        public static void SaveDS(string out_file, dsreplay rep)
        {
            if (out_file == null)
            {
                return;
            }

            TextWriter writer = null;

            _readWriteLock.EnterWriteLock();
            try
            {
                var repjson = JsonSerializer.Serialize(rep);
                writer = new StreamWriter(out_file, true, Encoding.UTF8);
                writer.Write(repjson + Environment.NewLine);
            }
            catch (Exception e)
            {
                AddLog("Failed writing to json :( " + e.Message);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            _readWriteLock.ExitWriteLock();
        }
示例#7
0
        public async Task RemoveUserReplay(int myrepid)
        {
            dsreplay rep = _startUp.MyReplays.Where(x => x.ID == myrepid).FirstOrDefault();

            if (rep != null)
            {
                _startUp.MyReplays.Remove(rep);
                try
                {
                    File.Delete(rep.REPLAY);
                }
                catch { }

                if (Directory.Exists(Program.detaildir + "/" + rep.ID))
                {
                    try
                    {
                        Directory.Delete(Program.detaildir + "/" + rep.ID, true);
                    }
                    catch { }
                }

                string removeline = JsonSerializer.Serialize(rep).Trim();
                var    lines      = File.ReadAllLines(Program.myReplays_file).Where(line => line.Trim() != removeline).ToArray();
                File.WriteAllLines(Program.myReplays_file, lines);
                await _startUp.Reload();
            }
        }
示例#8
0
        public void SaveDS(string out_file, dsreplay rep)
        {
            TextWriter writer = null;

            _readWriteLock.EnterWriteLock();
            try
            {
                //var repjson = JsonConvert.SerializeObject(rep);
                var repjson = JsonSerializer.Serialize(rep);
                writer = new StreamWriter(out_file, true, Encoding.UTF8);
                writer.Write(repjson + Environment.NewLine);
            }
            catch (Exception e)
            {
                Program.Log("Failed writing to json :(");
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            _readWriteLock.ExitWriteLock();
        }
示例#9
0
        public static (MMgameNG, MMgameNG) RateGame(dsreplay replay, StartUp _mm, string lobby)
        {
            var team1 = new Team();
            var team2 = new Team();

            var rteam1 = new Team();
            var rteam2 = new Team();

            //string lobby = _mm.Games.Where(x => x.ID == replay.ID).FirstOrDefault().Lobby;
            if (lobby == null || lobby == "")
            {
                return(null, null);
            }
            int i = 0;

            foreach (var pl in replay.PLAYERS)
            {
                MMplayerNG mpl = new MMplayerNG();
                if (_mm.MMplayers.ContainsKey(pl.NAME))
                {
                    mpl = _mm.MMplayers[pl.NAME];
                }
                else
                {
                    mpl.Name = "Dummy" + i;
                }

                MMplayerNG rpl   = _mm.MMraces[pl.RACE];
                MMPlRating plrat = mpl.Rating[lobby].LastOrDefault();
                if (plrat == null)
                {
                    plrat = new MMPlRating();
                    mpl.Rating[lobby].Add(plrat);
                }
                MMPlRating cmdrrat = rpl.Rating[lobby].LastOrDefault();
                if (cmdrrat == null)
                {
                    cmdrrat = new MMPlRating();
                    rpl.Rating[lobby].Add(cmdrrat);
                }

                if (pl.TEAM == replay.WINNER)
                {
                    team1.AddPlayer(new Player(mpl.Name), new Rating(plrat.MU, plrat.SIGMA));
                    rteam1.AddPlayer(new Player(pl.RACE), new Rating(cmdrrat.MU, cmdrrat.SIGMA));
                }
                else
                {
                    team2.AddPlayer(new Player(mpl.Name), new Rating(plrat.MU, plrat.SIGMA));
                    rteam2.AddPlayer(new Player(pl.RACE), new Rating(cmdrrat.MU, cmdrrat.SIGMA));
                }
                i++;
            }
            return(RateGame(team1, team2, lobby, _mm), RateGame(rteam1, rteam2, lobby, _mm, true));
        }
示例#10
0
        public static void FixPos(dsreplay replay)
        {
            foreach (dsplayer pl in replay.PLAYERS)
            {
                if (pl.REALPOS == 0)
                {
                    for (int j = 1; j <= 6; j++)
                    {
                        if (replay.PLAYERCOUNT == 2 && (j == 2 || j == 3 || j == 5 || j == 6))
                        {
                            continue;
                        }
                        if (replay.PLAYERCOUNT == 4 && (j == 3 || j == 6))
                        {
                            continue;
                        }

                        List <dsplayer> temp = new List <dsplayer>(replay.PLAYERS.Where(x => x.REALPOS == j).ToList());
                        if (temp.Count == 0)
                        {
                            pl.REALPOS = j;
                            Program.Log("Fixing missing playerid for " + pl.POS + "|" + pl.REALPOS + " => " + j);
                        }
                    }
                    if (new List <dsplayer>(replay.PLAYERS.Where(x => x.REALPOS == pl.POS).ToList()).Count == 0)
                    {
                        pl.REALPOS = pl.POS;
                    }
                }

                if (new List <dsplayer>(replay.PLAYERS.Where(x => x.REALPOS == pl.REALPOS).ToList()).Count > 1)
                {
                    Console.WriteLine("Found double playerid for " + pl.POS + "|" + pl.REALPOS);

                    for (int j = 1; j <= 6; j++)
                    {
                        if (replay.PLAYERCOUNT == 2 && (j == 2 || j == 3 || j == 5 || j == 6))
                        {
                            continue;
                        }
                        if (replay.PLAYERCOUNT == 4 && (j == 3 || j == 6))
                        {
                            continue;
                        }
                        if (new List <dsplayer>(replay.PLAYERS.Where(x => x.REALPOS == j).ToList()).Count == 0)
                        {
                            pl.REALPOS = j;
                            Program.Log("Fixing double playerid for " + pl.POS + "|" + pl.REALPOS + " => " + j);
                            break;
                        }
                    }
                }
            }
        }
示例#11
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _options.Decoding = true;
            BulkInsertArgs arg = new BulkInsertArgs();
            await Task.Run(() =>
            {
                DateTime t = DateTime.UtcNow;

                var jsonfile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\sc2dsstats_web\\data.json";

                if (!File.Exists(jsonfile))
                {
                    return;
                }

                using (FileStream fs = File.OpenRead(jsonfile))
                {
                    arg.Total = CountLinesMaybe(fs);
                }

                using (var md5 = MD5.Create())
                {
                    foreach (var line in File.ReadAllLines(jsonfile))
                    {
                        dsreplay rep = JsonSerializer.Deserialize <dsreplay>(line);
                        if (rep != null)
                        {
                            rep.Init();
                            rep.GenHash();

                            string reppath = Status.ReplayFolder.Where(x => x.Value == rep.REPLAY.Substring(0, 47)).FirstOrDefault().Key;
                            reppath       += "/" + rep.REPLAY.Substring(48);
                            reppath       += ".SC2Replay";
                            rep.REPLAY     = reppath;
                            DSReplay Rep   = Map.Rep(rep);

                            _db.SaveReplay(Rep, true);

                            arg.Count++;
                            OnReplayProcessed(arg);
                        }
                    }
                }
                _db.SaveContext();
                Console.WriteLine((DateTime.UtcNow - t).TotalSeconds);
            });

            arg.Done = true;
            OnReplayProcessed(arg);
            _options.Decoding = false;
        }
示例#12
0
        public async Task ReScan(string tdir)
        {
            string jsonfile = tdir + "/treplays.json";

            if (File.Exists(jsonfile))
            {
                File.Delete(jsonfile);
            }
            File.Create(jsonfile).Dispose();

            if (!_startUp.TournamentReplays.ContainsKey(Path.GetFileName(tdir)))
            {
                _startUp.TournamentReplays.Add(Path.GetFileName(tdir), new List <dsreplay>());
            }
            else
            {
                _startUp.TournamentReplays[Path.GetFileName(tdir)].Clear();
            }

            if (Directory.Exists(tdir + "/replays"))
            {
                int i = 0;
                foreach (string file in Directory.EnumerateFiles(tdir + "/replays"))
                {
                    i++;
                    dsreplay replay = _s2dec.DecodePython(file, false, true);
                    if (replay != null)
                    {
                        replay.ID     = i;
                        replay.REPLAY = file;
                        var json = JsonSerializer.Serialize(replay);
                        File.AppendAllText(jsonfile, json + Environment.NewLine);
                        _startUp.TournamentReplays[Path.GetFileName(tdir)].Add(replay);
                        string dest = _startUp.Exedir + "/treplays/" + Path.GetFileName(tdir) + "/" + Path.GetFileName(file);
                        if (!Directory.Exists(Path.GetDirectoryName(dest)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(dest));
                        }
                        if (!File.Exists(dest))
                        {
                            File.Copy(file, dest);
                        }

                        SaveDetails(replay, tdir);
                    }
                }
            }
        }
示例#13
0
        public static KeyValuePair <int, int> GetMiddle(dsreplay replay, bool current = false)
        {
            double team1 = 0;
            double team2 = 0;
            KeyValuePair <int, int>         lastmid = new KeyValuePair <int, int>();
            List <KeyValuePair <int, int> > mymid   = new List <KeyValuePair <int, int> >(replay.MIDDLE);

            if (mymid.Count() >= 1)
            {
                int finalmid = mymid.ElementAt(mymid.Count() - 1).Value;
                lastmid = new KeyValuePair <int, int>(0, mymid[0].Value);
                mymid.RemoveAt(0);
                mymid.Add(new KeyValuePair <int, int>(replay.DURATION, finalmid));
            }

            foreach (var ent in mymid)
            {
                if (lastmid.Value == 0)
                {
                    team1 += ent.Key - lastmid.Key;
                }
                else
                {
                    team2 += ent.Key - lastmid.Key;
                }

                lastmid = ent;
            }

            KeyValuePair <int, int> currentMid = new KeyValuePair <int, int>();

            int mSec  = 0;
            int mTeam = 0;

            if (mymid.Count() > 2)
            {
                mSec       = mymid.ElementAt(mymid.Count() - 2).Key - mymid.ElementAt(mymid.Count() - 3).Key;
                mTeam      = mymid.ElementAt(mymid.Count() - 2).Value;
                currentMid = new KeyValuePair <int, int>(mSec, mTeam);
            }
            if (current == true)
            {
                //double midt1 = Math.Round(team1 * 100 / (double)replay.DURATION, 2);
                //double midt2 = Math.Round(team2 * 100 / (double)replay.DURATION, 2);
                return(new KeyValuePair <int, int>((int)team1, (int)team2));
            }
            return(currentMid);
        }
示例#14
0
        public static void FixWinner(dsreplay replay)
        {
            int lastmid = GetMiddle(replay).Value;

            foreach (dsplayer pl in replay.PLAYERS)
            {
                if (pl.TEAM == lastmid)
                {
                    replay.WINNER = pl.TEAM;
                    pl.RESULT     = 1;
                }
                else
                {
                    pl.RESULT = 2;
                }
            }
        }
示例#15
0
        public async Task <int> CheckValid(dsreplay replay, MMgameNG game)
        {
            int valid = 0;

            return(await Task.Run(() =>
            {
                HashSet <string> game_Names = game.GetPlayers().Select(s => s.Name).ToHashSet();
                foreach (dsplayer dspl in replay.PLAYERS)
                {
                    if (game_Names.Contains(dspl.NAME))
                    {
                        valid++;
                        int rep_team = dspl.TEAM + 1;
                        List <MMplayerNG> game_team = new List <MMplayerNG>();
                        if (game.Team1.Select(s => s.Name == dspl.NAME).Count() > 0)
                        {
                            game_team = game.Team1;
                        }
                        else
                        {
                            game_team = game.Team2;
                        }

                        foreach (dsplayer tdspl in replay.PLAYERS.Where(x => x.TEAM == dspl.TEAM))
                        {
                            if (tdspl.NAME == dspl.NAME)
                            {
                                continue;
                            }
                            if (game_Names.Contains(tdspl.NAME))
                            {
                                if (game_team.Where(x => x.Name == tdspl.NAME).Count() > 0)
                                {
                                    valid++;
                                }
                            }
                        }
                    }
                }
                return valid;
            }));
        }
示例#16
0
 public async Task <dsreplay> Decode(string rep, int mmid)
 {
     return(await Task.Run(() =>
     {
         dsreplay replay = null;
         lock (dec_lock)
         {
             replay = _s2dec.DecodePython(rep, mmid);
             if (_startUp.replays.ContainsKey(mmid))
             {
                 _startUp.replays[mmid].Add(replay);
             }
             else
             {
                 _startUp.replays.TryAdd(mmid, new List <dsreplay>());
                 _startUp.replays[mmid].Add(replay);
             }
         }
         return replay;
     }));
 }
示例#17
0
        public void InitDB(int count = 0)
        {
            int i = 0;

            using (var md5 = MD5.Create())
            {
                foreach (string line in File.ReadAllLines(@"C:\Users\pax77\AppData\Local\sc2dsstats_web\data.json"))
                {
                    dsreplay rep = JsonSerializer.Deserialize <dsreplay>(line);
                    rep.Init();
                    rep.GenHash();

                    string reppath = ReplayFolder.Where(x => x.Value == rep.REPLAY.Substring(0, 47)).FirstOrDefault().Key;
                    reppath += "/" + rep.REPLAY.Substring(48);
                    reppath += ".SC2Replay";
                    string reppathhash = rep.REPLAY;
                    if (File.Exists(reppath))
                    {
                        string dirHash  = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(Path.GetDirectoryName(reppath)))).Replace("-", "").ToLowerInvariant();
                        string fileHash = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(Path.GetFileName(reppath)))).Replace("-", "").ToLowerInvariant();
                        reppathhash = dirHash + fileHash;
                    }

                    //InsertdsDesktopReplay(_context, rep, reppathhash, reppath);

                    i++;
                    if (i % 100 == 0)
                    {
                        Console.WriteLine(i);
                        if (count > 0 && i > count)
                        {
                            break;
                        }
                    }
                }
            }
        }
示例#18
0
        public void Save(MMgame game, dsreplay replay)
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    var json = JsonConvert.SerializeObject(game, Formatting.Indented);
                    if (!File.Exists(WorkDir + "/games/" + game.ID + "_report.json"))
                    {
                        File.WriteAllText(WorkDir + "/games/" + game.ID + "_report.json", json);
                    }
                }
                catch
                {
                    _logger.LogError("Failed writing report game " + game.ID);
                }

                replay.GenHash();
                ReplayHash.Add(replay.HASH);
                try
                {
                    var json           = JsonConvert.SerializeObject(replay, Formatting.Indented);
                    string replay_file = WorkDir + "/games/" + game.ID + "_replay.json";
                    int i = 1;
                    while (File.Exists(replay_file))
                    {
                        replay_file = WorkDir + "/games/" + game.ID + "_replay_" + i.ToString() + ".json";
                        i++;
                    }
                    File.WriteAllText(replay_file, json);
                }
                catch
                {
                    _logger.LogError("Failed writing report replay " + game.ID);
                }
            });
        }
示例#19
0
        public async Task GetDetails(dsreplay replay, string detaildir = Program.detaildir)
        {
            await Task.Run(() =>
            {
                // /data/tournaments/20190921/replays/Team5_vs_Team6_1.SC2Replay
                // /data/tournaments/20190921/7
                string mydir = detaildir + "/" + replay.ID.ToString();
                if (replay.REPLAY.Contains("tournaments"))
                {
                    mydir = Path.GetDirectoryName(Path.GetDirectoryName(replay.REPLAY)) + "/" + replay.ID;
                }

                if (Directory.Exists(mydir))
                {
                    if (File.Exists(mydir + "/mid.bin"))
                    {
                        replay.MIDDLE = ReadFromBinaryFile <List <KeyValuePair <int, int> > >(mydir + "/mid.bin");
                    }

                    foreach (dsplayer pl in replay.PLAYERS)
                    {
                        PlStats st = new PlStats();
                        if (File.Exists(mydir + "/" + pl.REALPOS.ToString() + "_stats.bin"))
                        {
                            st = ReadFromBinaryFile <PlStats>(mydir + "/" + pl.REALPOS.ToString() + "_stats.bin");
                        }

                        int i = 0;
                        foreach (var ent in st.Loops)
                        {
                            pl.STATS[ent] = st.Stats.ElementAt(i);
                            i++;
                        }
                    }
                }
            });
        }
示例#20
0
文件: StartUp.cs 项目: Mnuzz/dsmm_web
        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);
             * }
             **/
        }
示例#21
0
        public async Task <bool> GetAutoFile(string id, string myfile)
        {
            DSRestPlayer player = _context.DSRestPlayers.Include(p => p.Uploads).FirstOrDefault(f => f.Name == id);

            if (player == null)
            {
                return(false);
            }

            player.LastUpload = DateTime.UtcNow;

            string mypath = SharedDir + "/" + id;
            string mysum  = SharedDir + "/sum/" + id + ".json";

            if (!Directory.Exists(mypath))
            {
                try
                {
                    Directory.CreateDirectory(mypath);
                }
                catch (Exception e)
                {
                    _logger.LogError("Could not create directory " + mypath + " " + e.Message);
                    return(false);
                }
            }
            if (File.Exists(myfile))
            {
                if (!File.Exists(mysum))
                {
                    File.Create(mysum).Dispose();
                }
                string myjson = "";
                return(await Task.Run(() => myjson = Decompress(new FileInfo(myfile), mypath, id, _logger))
                       .ContinueWith(task =>
                {
                    if (myjson == "")
                    {
                        return false;
                    }
                    if (File.Exists(myjson) && new FileInfo(myjson).Length > 0)
                    {
                        using (StreamWriter sw = File.AppendText(mysum))
                        {
                            foreach (string line in File.ReadLines(myjson))
                            {
                                if (!line.StartsWith(@"{"))
                                {
                                    return false;
                                }
                                sw.WriteLine(line);
                                dsreplay replay = null;
                                try
                                {
                                    replay = JsonSerializer.Deserialize <dsreplay>(line);
                                    if (replay != null)
                                    {
                                        DSReplay dsreplay = Map.Rep(replay);
                                        DSPlayer dsplayer = dsreplay.DSPlayer.FirstOrDefault(f => f.NAME == "player");
                                        if (dsplayer != null)
                                        {
                                            dsplayer.NAME = id;
                                        }
                                        dsreplay.Upload = player.LastUpload;
                                        DSReplays.Add(dsreplay);
                                        player.Data++;
                                    }
                                } catch (Exception e)
                                {
                                    _logger.LogError("Could not Deserialize and map replay " + e.Message);
                                }
                            }
                        }

                        DSRestUpload upload = new DSRestUpload();
                        upload.Upload = player.LastUpload;
                        upload.DSRestPlayer = player;
                        _context.DSRestUploads.Add(upload);

                        DSinfo info = Infos.FirstOrDefault(f => f.Key == id).Value;
                        if (info != null)
                        {
                            DateTime LastRep = DateTime.MinValue;
                            if (info.LastRep.Length == 14)
                            {
                                int year = int.Parse(info.LastRep.Substring(0, 4));
                                int month = int.Parse(info.LastRep.Substring(4, 2));
                                int day = int.Parse(info.LastRep.Substring(6, 2));
                                int hour = int.Parse(info.LastRep.Substring(8, 2));
                                int min = int.Parse(info.LastRep.Substring(10, 2));
                                int sec = int.Parse(info.LastRep.Substring(12, 2));
                                LastRep = new DateTime(year, month, day, hour, min, sec);
                            }
                            player.LastRep = LastRep;
                            player.Json = info.Json;
                            player.Total = info.Total;
                            player.Version = info.Version;
                        }

                        lock (dblock)
                        {
                            _context.SaveChanges();
                        }
                        InsertDSReplays();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }));
            }
            else
            {
                return(false);
            }
        }
示例#22
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();
        }
示例#23
0
        public async Task Reload()
        {
            replays.Clear();
            await Task.Run(() => {
                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);
                    }
                }
            });

            MyReplays.Clear();
            await Task.Run(() => {
                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);
                    }
                }
            });

            TournamentReplays.Clear();
            await Task.Run(() => {
                foreach (var dir in Directory.EnumerateDirectories(Program.workdir + "/tournaments"))
                {
                    if (File.Exists(dir + "/treplays.json"))
                    {
                        string tourny = Path.GetFileName(dir);
                        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);
                            }
                        }
                    }
                }
            });
        }
示例#24
0
        // WebDatabase Replay
        // LocalDatabase Replay
        // DecodedReplay
        // OldReplay
        public static DSReplay Rep(dsreplay rep, string id = "player")
        {
            DSReplay dbrep = new DSReplay();

            dbrep.REPLAYPATH = rep.REPLAY;
            using (var md5 = MD5.Create())
            {
                string dirHash  = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(Path.GetDirectoryName(rep.REPLAY)))).Replace("-", "").ToLowerInvariant();
                string fileHash = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(Path.GetFileName(rep.REPLAY)))).Replace("-", "").ToLowerInvariant();
                dbrep.REPLAY = dirHash + fileHash;
            }
            string   gametime = rep.GAMETIME.ToString();
            int      year     = int.Parse(gametime.Substring(0, 4));
            int      month    = int.Parse(gametime.Substring(4, 2));
            int      day      = int.Parse(gametime.Substring(6, 2));
            int      hour     = int.Parse(gametime.Substring(8, 2));
            int      min      = int.Parse(gametime.Substring(10, 2));
            int      sec      = int.Parse(gametime.Substring(12, 2));
            DateTime gtime    = new DateTime(year, month, day, hour, min, sec);

            dbrep.GAMETIME = gtime;
            dbrep.WINNER   = (sbyte)rep.WINNER;
            TimeSpan d = TimeSpan.FromSeconds(rep.DURATION / 22.4);

            dbrep.DURATION    = (int)d.TotalSeconds;
            dbrep.MINKILLSUM  = rep.MINKILLSUM;
            dbrep.MAXKILLSUM  = rep.MAXKILLSUM;
            dbrep.MINARMY     = rep.MINARMY;
            dbrep.MININCOME   = (int)rep.MININCOME;
            dbrep.PLAYERCOUNT = (byte)rep.PLAYERCOUNT;
            dbrep.REPORTED    = (byte)rep.REPORTED;
            dbrep.ISBRAWL     = rep.ISBRAWL;
            dbrep.GAMEMODE    = rep.GAMEMODE;
            dbrep.VERSION     = rep.VERSION;
            dbrep.MAXLEAVER   = 0;

            List <DSPlayer> pls = new List <DSPlayer>();

            foreach (dsplayer pl in rep.PLAYERS)
            {
                DSPlayer dbpl = new DSPlayer();
                dbpl.POS     = (byte)pl.POS;
                dbpl.REALPOS = (byte)pl.REALPOS;
                if (pl.NAME == "player")
                {
                    dbpl.NAME = id;
                }
                else
                {
                    dbpl.NAME = pl.NAME;
                }
                dbpl.RACE = pl.RACE;
                if (pl.TEAM == rep.WINNER)
                {
                    dbpl.WIN = true;
                }
                dbpl.TEAM      = (byte)pl.TEAM;
                dbpl.KILLSUM   = pl.KILLSUM;
                dbpl.INCOME    = (int)pl.INCOME;
                dbpl.PDURATION = (int)TimeSpan.FromSeconds(pl.PDURATION / 22.4).TotalSeconds;
                dbpl.ARMY      = pl.ARMY;
                dbpl.GAS       = (byte)pl.GAS;
                dsplayer opp = rep.GetOpp(pl.REALPOS);
                if (opp != null)
                {
                    dbpl.OPPRACE = opp.RACE;
                }
                int diff = dbrep.DURATION - dbpl.PDURATION;
                if (diff > dbrep.MAXLEAVER)
                {
                    dbrep.MAXLEAVER = diff;
                }

                dbpl.DSReplay = dbrep;
                pls.Add(dbpl);

                List <DbBreakpoint> bps = new List <DbBreakpoint>();
                foreach (var bp in pl.UNITS.Keys)
                {
                    DbBreakpoint dbbp = new DbBreakpoint();
                    dbbp.Breakpoint    = bp;
                    dbbp.dsUnitsString = "";

                    foreach (var name in pl.UNITS[bp].Keys)
                    {
                        if (name == "Gas")
                        {
                            dbbp.Gas = pl.UNITS[bp][name];
                        }
                        else if (name == "Upgrades")
                        {
                            dbbp.Upgrades = pl.UNITS[bp][name];
                        }
                        else if (name == "Mid")
                        {
                            dbbp.Mid = pl.UNITS[bp][name];
                        }
                        else
                        {
                            UnitModelBase unit = DSdata.Units.FirstOrDefault(f => f.Race == pl.RACE && f.Name == Fix.UnitName(name));
                            if (unit != null)
                            {
                                dbbp.dsUnitsString += unit.ID + "," + pl.UNITS[bp][name] + "|";
                            }
                            else
                            {
                                dbbp.dsUnitsString += name + "," + pl.UNITS[bp][name] + "|";
                            }
                        }
                    }
                    if (!String.IsNullOrEmpty(dbbp.dsUnitsString))
                    {
                        dbbp.dsUnitsString = dbbp.dsUnitsString.Remove(dbbp.dsUnitsString.Length - 1);
                    }
                    bps.Add(dbbp);
                }
                dbpl.Breakpoints = bps;
            }
            dbrep.DSPlayer = pls as ICollection <DSPlayer>;
            dbrep.HASH     = dbrep.GenHash();
            dbrep.Upload   = DateTime.UtcNow;
            return(dbrep);
        }
示例#25
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]);
        }
示例#26
0
        public static void ReadJson(string file)
        {
            Console.WriteLine("Working on " + Path.GetFileName(file));
            string filename = Path.GetFileNameWithoutExtension(file);
            string plhash   = filename.Substring(filename.Length - 64);
            int    i        = 0;
            int    j        = 0;

            foreach (var line in File.ReadAllLines(file, Encoding.UTF8))
            {
                dsreplay replay = System.Text.Json.JsonSerializer.Deserialize <dsreplay>(line);
                if (replay != null)
                {
                    i++;
                    replay.Init();
                    replay.GenHash();

                    dsplayer pl = replay.PLAYERS.FirstOrDefault(s => s.NAME == "player");
                    if (pl != null)
                    {
                        pl.NAME = plhash;
                        replay.PLDupPos[plhash] = pl.REALPOS;
                    }

                    dsreplay crep = sReplays.SingleOrDefault(s => s.HASH == replay.HASH);

                    if (crep == null)
                    {
                        sReplays.Add(replay);
                        Hashs.Add(replay.HASH);
                    }
                    else
                    {
                        j++;
                        if (new Version(replay.VERSION) >= new Version(crep.VERSION))
                        {
                            foreach (var ent in crep.PLAYERS.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && replay.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        replay.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch
                                {
                                    Console.WriteLine("???");
                                }
                            }
                            sReplays.Remove(crep);
                            sReplays.Add(replay);
                        }
                        else
                        {
                            foreach (var ent in replay.PLAYERS.Select(s => new { s.NAME, s.REALPOS }))
                            {
                                try
                                {
                                    if (ent.NAME.Length == 64 && crep.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME.Length < 64)
                                    {
                                        crep.PLAYERS.Single(x => x.REALPOS == ent.REALPOS).NAME = ent.NAME;
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(":( " + e.Message);
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine($"{i} ({j})");
        }
示例#27
0
        public static dsreplay GetDetails(string replay_file, dynamic details_dec)
        {
            string id             = Path.GetFileNameWithoutExtension(replay_file);
            string reppath        = Path.GetDirectoryName(replay_file);
            var    plainTextBytes = Encoding.UTF8.GetBytes(reppath);
            MD5    md5            = new MD5CryptoServiceProvider();
            string reppath_md5    = System.BitConverter.ToString(md5.ComputeHash(plainTextBytes));
            string repid          = reppath_md5 + "/" + id;


            dsreplay replay = new dsreplay();

            replay.REPLAY = repid;
            Program.Log("Replay id: " + repid);
            int failsafe_pos = 0;

            foreach (var player in details_dec["m_playerList"])
            {
                if (player["m_observe"] > 0)
                {
                    continue;
                }

                failsafe_pos++;
                string name = "";
                Bytes  bab  = null;
                try
                {
                    bab = player["m_name"];
                }
                catch { }

                if (bab != null)
                {
                    name = Encoding.UTF8.GetString(bab.ToByteArray());
                }
                else
                {
                    name = player["m_name"].ToString();
                }

                Match m2 = s2parse.rx_subname.Match(name);
                if (m2.Success)
                {
                    name = m2.Groups[1].Value;
                }
                Program.Log("Replay playername: " + name);
                dsplayer pl = new dsplayer();

                pl.NAME = name;
                pl.RACE = player["m_race"].ToString();
                Program.Log("Replay race: " + pl.RACE);
                pl.RESULT = int.Parse(player["m_result"].ToString());
                pl.TEAM   = int.Parse(player["m_teamId"].ToString());
                try
                {
                    //pl.POS = int.Parse(player["m_workingSetSlotId"].ToString()) + 1;
                    pl.POS = failsafe_pos;
                }
                catch
                {
                    pl.POS = failsafe_pos;
                }
                replay.PLAYERS.Add(pl);
            }

            replay.PLAYERCOUNT = replay.PLAYERS.Count();

            long offset  = long.Parse(details_dec["m_timeLocalOffset"].ToString());
            long timeutc = long.Parse(details_dec["m_timeUTC"].ToString());

            long     georgian = timeutc + offset;
            DateTime gametime = DateTime.FromFileTime(georgian);

            replay.GAMETIME = double.Parse(gametime.ToString("yyyyMMddhhmmss"));
            Program.Log("Replay gametime: " + replay.GAMETIME);

            return(replay);
        }
示例#28
0
        public static (Dictionary <int, List <Unit> >, Dictionary <int, HashSet <int> >) GetUnits(dsreplay replay, GameHistory game)
        {
            //var json = File.ReadAllText("/data/unitst1p3.json");
            //var bab = JsonSerializer.Deserialize<List<UnitEvent>>(json);

            List <Unit>      Units      = new List <Unit>();
            List <Vector2>   vecs       = new List <Vector2>();
            List <UnitEvent> UnitEvents = replay.UnitBorn;

            int maxdiff  = 0;
            int temploop = 0;
            Dictionary <int, List <Unit> >   spawns   = new Dictionary <int, List <Unit> >();
            Dictionary <int, HashSet <int> > plspawns = new Dictionary <int, HashSet <int> >();

            foreach (var unit in UnitEvents)
            {
                int diff = unit.Gameloop - temploop;

                if (temploop == 0)
                {
                    spawns.Add(unit.Gameloop, new List <Unit>());
                }
                else if (diff > 3)
                {
                    spawns.Add(unit.Gameloop, new List <Unit>());
                }

                if (unit.Gameloop - temploop > maxdiff)
                {
                    maxdiff = unit.Gameloop - temploop;
                }

                temploop = unit.Gameloop;

                int pos     = unit.PlayerId;
                int realpos = replay.PLAYERS.SingleOrDefault(x => x.POS == pos).REALPOS;

                /*
                 * if (unit.PlayerId > 3)
                 *  pos = unit.PlayerId - 3;
                 * else if (unit.PlayerId <= 3)
                 *  pos = unit.PlayerId + 3;
                 */
                spawns.Last().Value.Add(UnitEventToUnit(unit, realpos, game));

                if (!plspawns.ContainsKey(pos))
                {
                    plspawns[pos] = new HashSet <int>();
                }

                plspawns[pos].Add(spawns.Last().Key);
            }

            return(spawns, plspawns);
        }
示例#29
0
        public static Dictionary <int, Dictionary <int, List <UnitUpgrade> > > GetUpgrades(dsreplay replay)
        {
            Dictionary <int, Dictionary <int, List <UnitUpgrade> > > Upgrades = new Dictionary <int, Dictionary <int, List <UnitUpgrade> > >();

            foreach (dsplayer pl in replay.PLAYERS)
            {
                Upgrades[pl.POS] = new Dictionary <int, List <UnitUpgrade> >();
                foreach (var ent in pl.Upgrades)
                {
                    int gameloop = ent.Key;
                    foreach (var upgrades in ent.Value)
                    {
                        UnitUpgrade u = UpgradePool.Map(upgrades);
                        if (u != null)
                        {
                            if (!Upgrades[pl.POS].ContainsKey(gameloop))
                            {
                                Upgrades[pl.POS][gameloop] = new List <UnitUpgrade>();
                            }
                            Upgrades[pl.POS][gameloop].Add(u);
                        }
                    }
                }
            }
            return(Upgrades);
        }
示例#30
0
        public static int GetPlayer(GameMapModel _map, dsreplay replay, Player pl, int gameloop = 0)
        {
            dsplayer dspl = replay.PLAYERS.SingleOrDefault(x => x.REALPOS == pl.Pos);

            if (dspl == null)
            {
                return(gameloop);
            }

            pl.SoftReset();
            if (pl.Name == "")
            {
                pl.Name = dspl.NAME;
                pl.Pos  = dspl.REALPOS;
                UnitRace race = UnitRace.Terran;
                if (dspl.RACE == "Protoss")
                {
                    race = UnitRace.Protoss;
                }
                else if (dspl.RACE == "Zerg")
                {
                    race = UnitRace.Zerg;
                }
                pl.Race  = race;
                pl.Units = UnitPool.Units.Where(x => x.Race == race && x.Cost > 0).ToList();
            }

            if (gameloop == 0)
            {
                gameloop = _map.plSpawns[pl.Pos].OrderBy(o => o).First();
            }

            foreach (var unit in _map.Spawns[gameloop].Where(x => x.Owner == pl.Pos))
            {
                Unit myunit = unit.DeepCopy();
                if (pl.Pos <= 3)
                {
                    //myunit.PlacePos = UnitService.mirrorImage(myunit.PlacePos);
                    //myunit.PlacePos = new Vector2(Battlefield.Xmax - myunit.PlacePos.X, myunit.PlacePos.Y);
                    myunit.PlacePos = new Vector2(myunit.PlacePos.X, 2 * 5 - myunit.PlacePos.Y - 1);
                }
                UnitService.NewUnit(pl, myunit);
                pl.Units.Add(myunit);
                pl.MineralsCurrent -= myunit.Cost;
            }

            foreach (var dic in _map.Upgrades[pl.Pos].OrderBy(x => x.Key))
            {
                if (dic.Key > gameloop)
                {
                    break;
                }
                foreach (var upgrade in dic.Value)
                {
                    pl.MineralsCurrent -= UnitService.UpgradeUnit(upgrade.Upgrade, pl);
                }
            }
            foreach (var dic in _map.AbilityUpgrades[pl.Pos].OrderBy(x => x.Key))
            {
                if (dic.Key > gameloop)
                {
                    break;
                }
                foreach (var upgrade in dic.Value)
                {
                    pl.MineralsCurrent -= UnitService.AbilityUpgradeUnit(upgrade, pl);
                }
            }
            pl.MineralsCurrent = 10000;
            return(gameloop);
        }