Пример #1
0
        void GeneratePlayerSection(List<UserBattleStatus> playersExport,
            User localUser,
            SpringBattleStartSetup startSetup,
            List<UserBattleStatus> users,
            StringBuilder script,
            List<StartPos> positions,
            List<BotBattleStatus> bots)
        {
            if (mod != null && mod.IsMission) // mission stuff
            {
                var aiNum = 0;
                var declaredTeams = new HashSet<int>();
                var orderedUsers = users.OrderBy(x => x.TeamNumber).ToList();
                for (var i = 0; i < orderedUsers.Count; i++)
                {
                    var u = orderedUsers[i];
                    ScriptAddUser(script, i, playersExport, startSetup, u.TeamNumber, u);
                    if (!u.IsSpectator && !declaredTeams.Contains(u.TeamNumber))
                    {
                        ScriptAddTeam(script, u.TeamNumber, positions, i, u,mod,Details);
                        declaredTeams.Add(u.TeamNumber);
                    }
                }

                for (var i = 0; i < orderedUsers.Count; i++)
                {
                    var u = orderedUsers[i];
                    foreach (var b in bots.Where(x => x.owner == u.Name))
                    {
                        ScriptAddBot(script, aiNum++, b.TeamNumber, i, b);
                        if (!declaredTeams.Contains(b.TeamNumber))
                        {
                            ScriptAddTeam(script, b.TeamNumber, positions, i, b,mod,Details);
                            declaredTeams.Add(b.TeamNumber);
                        }
                    }
                }
            }
            else
            {
                // ordinary battle stuff

                var userNum = 0;
                var teamNum = 0;
                var aiNum = 0;
                //players is excluding self (so "springie doesn't appear as spec ingame") & excluding bots (bots is added later for each owner)
                foreach (var u in users.Where(u => !bots.Any(b => b.Name == u.Name)).OrderBy(x => x.TeamNumber).Where(x => x.Name != localUser.Name)) 
                {
                    ScriptAddUser(script, userNum, playersExport, startSetup, teamNum, u);

                    if (!u.IsSpectator)
                    {
                        ScriptAddTeam(script, teamNum, positions, userNum, u,mod,Details);
                        teamNum++;
                    }

                    foreach (var b in bots.Where(x => x.owner == u.Name))
                    {
                        ScriptAddBot(script, aiNum, teamNum, userNum, b);
                        aiNum++;
                        ScriptAddTeam(script, teamNum, positions, userNum, b,mod,Details);
                        teamNum++;
                    }
                    userNum++;
                }
            }

            // ALLIANCES
            script.AppendLine();
            foreach (var allyNumber in
                users.Where(x => !x.IsSpectator).Select(x => x.AllyNumber).Union(bots.Select(x => x.AllyNumber)).Union(Rectangles.Keys).Distinct())
            {
                // get allies from each player, bot and rectangles (for koth)
                script.AppendFormat("[ALLYTEAM{0}]\n", allyNumber);
                script.AppendLine("{");
                script.AppendFormat("     NumAllies={0};\n", 0);
                double left = 0, top = 0, right = 1, bottom = 1;
                BattleRect rect;
                if (Rectangles.TryGetValue(allyNumber, out rect)) rect.ToFractions(out left, out top, out right, out bottom);
                script.AppendFormat(CultureInfo.InvariantCulture,"     StartRectLeft={0};\n", left);
                script.AppendFormat(CultureInfo.InvariantCulture,"     StartRectTop={0};\n", top);
                script.AppendFormat(CultureInfo.InvariantCulture,"     StartRectRight={0};\n", right);
                script.AppendFormat(CultureInfo.InvariantCulture,"     StartRectBottom={0};\n", bottom);
                script.AppendLine("}");
            }

            script.AppendLine();
            script.AppendFormat("  NumRestrictions={0};\n", DisabledUnits.Count);
            script.AppendLine();

            if (!mod.IsMission)
            {
                script.AppendLine("  [RESTRICT]");
                script.AppendLine("  {");
                for (var i = 0; i < DisabledUnits.Count; ++i)
                {
                    script.AppendFormat("    Unit{0}={1};\n", i, DisabledUnits[i]);
                    script.AppendFormat("    Limit{0}=0;\n", i);
                }
                script.AppendLine("  }");

                script.AppendLine("  [MODOPTIONS]");
                script.AppendLine("  {");

                var options = new Dictionary<string, string>();

                // put standard modoptions to options dictionary
                foreach (var o in mod.Options.Where(x => x.Type != OptionType.Section))
                {
                    var v = o.Default;
                    if (ModOptions.ContainsKey(o.Key)) v = ModOptions[o.Key];
                    options[o.Key] = v;
                }

                // replace/add custom modoptions from startsetup (if they exist)
                if (startSetup != null && startSetup.ModOptions != null) foreach (var entry in startSetup.ModOptions) options[entry.Key] = entry.Value;

                // write final options to script
                foreach (var kvp in options) script.AppendFormat("    {0}={1};\n", kvp.Key, kvp.Value);

                script.AppendLine("  }");
            }

            script.AppendLine("}");
        }
Пример #2
0
 public int GetState(User founder)
 {
     var battleState = 0;
     if (founder.IsInGame) battleState += 2;
     if (IsFull) battleState++;
     if (IsPassworded) battleState += 3;
     if (IsReplay) battleState += 6;
     return battleState;
 }
Пример #3
0
        /// <summary>
        /// Generates script
        /// </summary>
        /// <param name="playersExport">list of players</param>
        /// <param name="localUser">myself</param>
        /// <param name="loopbackListenPort">listen port for autohost interface</param>
        /// <param name="zkSearchTag">hackish search tag</param>
        /// <param name="startSetup">structure with custom extra data</param>
        /// <returns></returns>
        public string GenerateScript(out List<UserBattleStatus> playersExport,
                                     User localUser,
                                     int loopbackListenPort,
                                     string zkSearchTag,
                                     SpringBattleStartSetup startSetup)
        {
            var previousCulture = Thread.CurrentThread.CurrentCulture;
            try
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

                playersExport = new List<UserBattleStatus>();
                var isHost = localUser.Name == Founder.Name;
                var myUbs = Users.SingleOrDefault(x => x.Name == localUser.Name);
                if (!isHost)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine("[GAME]");
                    sb.AppendLine("{");
                    sb.AppendFormat("HostIP={0};\n", Ip);
                    sb.AppendFormat("HostPort={0};\n", HostPort);
                    sb.AppendLine("IsHost=0;");
                    sb.AppendFormat("MyPlayerName={0};\n", localUser.Name);
                    if (myUbs != null)
                    {
                        if (myUbs.ScriptPassword != null) sb.AppendFormat("MyPasswd={0};\n", myUbs.ScriptPassword);
                    }
                    else
                    {
                        sb.AppendFormat("MyPasswd={0};\n", localUser.Name); // used for mid-game join .. if no userbattlestatus, use own name
                    }
                    sb.AppendLine("}");
                    return sb.ToString();
                }
                else
                {
                    if (mod == null) throw new ApplicationException("Mod not downloaded yet");

                    var script = new StringBuilder();

                    script.AppendLine("[GAME]");
                    script.AppendLine("{");

                    script.AppendFormat("   ZkSearchTag={0};\n", zkSearchTag);
                    script.AppendFormat("  Mapname={0};\n", MapName);

                    if (mod.IsMission) script.AppendFormat("  StartPosType=3;\n");
                    else
                    {
                        if (Details.StartPos == BattleStartPos.Choose) script.AppendFormat("  StartPosType=2;\n");
                        else script.AppendFormat("  StartPosType=3;\n"); // workaround for random/fixed
                        // script.AppendFormat("  StartPosType={0};\n", (int)Details.StartPos);
                    }

                    script.AppendFormat("  GameType={0};\n", ModName);
                    if (ModHash.HasValue)
                        script.AppendFormat("  ModHash={0};\n", ModHash.Value != 0 ? (uint)ModHash.Value : 1);
                            // hack dont set to 1 when dedi srever is patched
                    if (MapHash.HasValue) script.AppendFormat("  MapHash={0};\n", MapHash.Value != 0 ? (uint)MapHash.Value : 1);
                    script.AppendFormat("  AutohostPort={0};\n", loopbackListenPort);
                    script.AppendLine();
                    script.AppendFormat("  HostIP={0};\n", Ip);
                    script.AppendFormat("  HostPort={0};\n", HostPort);
                    script.AppendFormat("  SourcePort={0};\n", 8300);
                    script.AppendFormat("  IsHost=1;\n");
                    script.AppendLine();

                    //script.AppendFormat("  MyPlayerName={0};\n", localUser.Name);

                    var positions = map.Positions != null ? map.Positions.ToList() : new List<StartPos>();
                    if (Details.StartPos == BattleStartPos.Random) positions = positions.Shuffle();

                    List<UserBattleStatus> users;
                    List<BotBattleStatus> bots;

                    if (startSetup != null && startSetup.BalanceTeamsResult != null && startSetup.BalanceTeamsResult.Players != null)
                    {
                        // if there is a balance results as a part of start setup, use values from this (override lobby state)
                        users = new List<UserBattleStatus>(this.Users.Select(x => x.Clone()));
                        bots = new List<BotBattleStatus>(this.Bots.Select(x => (BotBattleStatus)x.Clone()));
                        foreach (var p in startSetup.BalanceTeamsResult.Players)
                        {
                            var us = users.FirstOrDefault(x => x.Name == p.Name);
                            if (us == null)
                            {
                                us = new UserBattleStatus(p.Name, new User() {LobbyID = p.LobbyID}, Password);
                                users.Add(us);
                            }
                            us.TeamNumber = p.TeamID;
                            us.IsSpectator = p.IsSpectator;
                            us.AllyNumber = p.AllyID;

                        }
                        foreach (var p in startSetup.BalanceTeamsResult.Bots)
                        {
                            var bot = bots.FirstOrDefault(x => x.Name == p.BotName);
                            if (bot == null)
                            {
                                bot = new BotBattleStatus(p.BotName, p.Owner, p.BotAI);
                                bots.Add(bot);
                            }
                            bot.AllyNumber = bot.AllyNumber;
                            bot.TeamNumber = bot.TeamNumber;
                        }

                        foreach (var u in users.Where(x => !startSetup.BalanceTeamsResult.Players.Any(y => y.Name == x.Name)))
                        {
                            u.IsSpectator = true;
                        } // spec those not known at the time of balance
                    }
                    else
                    {
                        users = this.Users;
                        bots = this.Bots;
                    }


                    GeneratePlayerSection(playersExport, localUser, startSetup, users, script, positions, bots);

                    return script.ToString();
                }
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = previousCulture;
            }
        }
Пример #4
0
 public static User Create(string name) {
     var u = new User();
     u.Name = name;
     return u;
 }
Пример #5
0
 public bool GetExistingUser(string name, out User u)
 {
     return ExistingUsers.TryGetValue(name, out u);
 }
Пример #6
0
 public void ChangeMyUserStatus(bool? isAway = null, bool? isInGame = null)
 {
     User u;
     if (MyUser != null) u = MyUser.Clone();
     else u = new User();
     u.FromInt(lastUserStatus);
     if (isAway != null) u.IsAway = isAway.Value;
     if (isInGame != null) u.IsInGame = isInGame.Value;
     if (MyUser == null || lastUserStatus != u.ToInt())
     {
         con.SendCommand("MYSTATUS", u.ToInt());
         lastUserStatus = u.ToInt();
     }
 }
		public UserBattleStatus(string name, User lobbyUser, string scriptPassword = null)
		{
			Name = name;
			ScriptPassword = scriptPassword;
			LobbyUser = lobbyUser;
		}