Пример #1
0
        private GspRoot GetGspFromJson(string json)
        {
            GspRoot gsp = new GspRoot();

            // Deserialize the JSON into the GspRoot class as an object.
            gsp = JsonConvert.DeserializeObject <GspRoot>(json);

            return(gsp);
        }
Пример #2
0
        private void PopulatePlayerDropDown(GspRoot gsp)
        {
            Dictionary <string, Player> players = gsp.Result.Gamestate.Players;

            SortedDictionary <string, Player> playersSorted = new SortedDictionary <string, Player>(players);

            cbxPlayers.DataSource    = new BindingSource(playersSorted, null);
            cbxPlayers.DisplayMember = "Key";
            cbxPlayers.ValueMember   = "Value";
        }
Пример #3
0
        private GspRoot GetGsp()
        {
            // Read in the JSON for the current game state.
            string text = System.IO.File.ReadAllText(@"isleofwar.json");

            // Fix issue with gamestate being stored as a string and not JSON
            text = text.Replace("\"gamestate\":\"{", "\"gamestate\":{"); // Fix first quote.
            text = text.Replace("}\",\"height\":", "},\"height\":");     // Fix end quote.
            text = text.Replace("\\\"", "\"");                           // Fix improperly escaped quotes.
            System.IO.File.WriteAllText("isleofwar.json", text);

            GspRoot gsp = new GspRoot();

            // Deserialize the JSON into the GspRoot class as an object.
            gsp = JsonConvert.DeserializeObject <GspRoot>(text);

            return(gsp);
        }
Пример #4
0
        private void cbxPlayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            // skip 1 time
            if (skip)
            {
                skip = false; return;
            }

            // Old code that used curl
            //GspRoot gsp = GetGsp();

            // Get GSP without using curl
            string json = GetJsonRpcResponse(getCurrentState);

            Application.DoEvents();
            GspRoot gsp = GetGspFromJson(json);


            SortedDictionary <string, Player> players = new SortedDictionary <string, Player>(gsp.Result.Gamestate.Players);

            // get ComboBox from sender
            ComboBox comboBox = (ComboBox)sender;

            // get selected KVP
            KeyValuePair <string, Player> selectedEntry
                = (KeyValuePair <string, Player>)comboBox.SelectedItem;

            // get selected Key
            string name = selectedEntry.Key;

            Player player = players[name];

            StringBuilder sb = new StringBuilder();

            // set up header
            sb.AppendLine(GetHeader());
            // Set up navigation
            //sb.AppendLine(GetFloatingMenu()); // no need for a menu
            // Set up sb with basic CSS
            sb.AppendLine(GetCSS());
            IslesOfWar.UnitNames unitNames = new UnitNames();
            int pad = 20; // used to pad names & stuff

            // Should do TOTAL combat units at the top here
            sb.AppendLine(GetTotalCombatUnits(player, name, pad, gsp));
            // Next, available combat units
            sb.AppendLine(GetPlayerCombatUnits(player));

            // resources

            // Get resources for player.
            IslesOfWar.ResourceNames resourceNames = new ResourceNames();
            sb.AppendLine("<h2>PLAYER RESOURCES FOR " + name + "</h2>");
            sb.AppendLine();

            sb.AppendLine(GetPlayerResources(player, resourceNames));

            // islands & defences
            sb.AppendLine("<p></p>");
            sb.AppendLine("<h2>ISLANDS AND THEIR DEFENSES</h2>");
            sb.AppendLine(GetIslandNumbering());

            // What islands have what resources or defenses? Let's ignore resources for now as it's a bunch of arrays - just do defenses.
            sb.AppendLine("<p></p>");
            sb.AppendLine("<h3>###PLAYERISLANDCOUNT###</h3>"); // place holder to replace

            Dictionary <string, Island> islands = gsp.Result.Gamestate.Islands;

            sb.AppendLine("<p></p>");
            Dictionary <string, int> playerIslandCount = new Dictionary <string, int>();
            int islandCount = 0;

            foreach (var isle in islands)
            {
                if (isle.Value.Owner != name)
                {
                    continue;
                }

                string hexName = isle.Key;
                Island island  = isle.Value;
                sb.AppendLine("<pre>" + island.Owner + " owns " + hexName + "</pre>");
                // Add in if there's a player attacking it
                sb.AppendLine(GetAttackingPlayer(hexName, players));

                // This adds in all islands with squads deployed.
                if (island.SquadCounts != null && island.SquadPlans != null)
                {
                    // Add in defenses for each island
                    int squadPlanCount  = island.SquadPlans.Count;
                    int squadCountCount = island.SquadCounts.Count;

                    sb.AppendLine(GetDefenderSquadInfo(island, squadCountCount, unitNames, pad));
                }
                else
                {
                    // Add in for islands with no squads
                    sb.AppendLine("<pre>");
                    sb.AppendLine(GetDefenderNoSquadInfo(island));
                    sb.AppendLine("</pre>");
                }

                // There are 12 hexes per island. Each one can have a squad.
                if (playerIslandCount.Keys.Contains <string>(island.Owner))
                {
                    playerIslandCount[island.Owner]++;
                    islandCount++;
                }
                else
                {
                    playerIslandCount.Add(island.Owner, 1);
                    islandCount++;
                }
            }

            // island counts
            sb.AppendLine("<p></p>");
            sb.Replace("###PLAYERISLANDCOUNT###", name + " OWNS " + islandCount.ToString() + " ISLANDS");
            sb.AppendLine(GetFooter());

            webBrowser1.DocumentText = sb.ToString(); // "<pre>" + sb.ToString() + "</pre>";
        }
Пример #5
0
        private string GetTotalCombatUnits(Player player, string name, int pad, GspRoot gsp)
        {
            long riflemen, machinegunners, bazookamen, lighttanks, mediumtanks, heavytanks, lightfighters, mediumfighters, bombers;

            riflemen       = 0;
            machinegunners = 0;
            bazookamen     = 0;
            lighttanks     = 0;
            mediumtanks    = 0;
            heavytanks     = 0;
            lightfighters  = 0;
            mediumfighters = 0;
            bombers        = 0;

            // get all free units counted
            riflemen       += player.Units[0];
            machinegunners += player.Units[1];
            bazookamen     += player.Units[2];
            lighttanks     += player.Units[3];
            mediumtanks    += player.Units[4];
            heavytanks     += player.Units[5];
            lightfighters  += player.Units[6];
            mediumfighters += player.Units[7];
            bombers        += player.Units[8];

            // get all islands into a dictionary
            Dictionary <string, Island> dic = gsp.Result.Gamestate.Islands;

            foreach (var isle in dic)
            {
                if (isle.Value.Owner == name)
                {
                    if (isle.Value.SquadCounts == null)
                    {
                        continue;
                    }

                    foreach (List <long> v in isle.Value.SquadCounts)
                    {
                        riflemen       += v[0];
                        machinegunners += v[1];
                        bazookamen     += v[2];
                        lighttanks     += v[3];
                        mediumtanks    += v[4];
                        heavytanks     += v[5];
                        lightfighters  += v[6];
                        mediumfighters += v[7];
                        bombers        += v[8];
                    }
                }
            }

            // assemble string of TOTAL units - free and deployed
            StringBuilder sb = new StringBuilder();

            IslesOfWar.UnitNames unitNames = new UnitNames();

            sb.AppendLine("<h2>TOTAL COMBAT UNITS FOR " + name + "</h2>");
            sb.AppendLine("<p></p>");

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.Riflemen.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", riflemen).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.MachineGunners.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", machinegunners).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.Bazookamen.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", bazookamen).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.LightTanks.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", lighttanks).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.MediumTanks.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", mediumtanks).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.HeavyTanks.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", heavytanks).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.LightFighters.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", lightfighters).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.MediumFighters.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", mediumfighters).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            sb.Append("<pre>&nbsp;&nbsp;&nbsp;&nbsp;" + unitNames.Bombers.PadRight(pad, ' '));
            sb.Append("=" + string.Format("{0:n0}", bombers).ToString().PadLeft(12, ' ') + "</pre>" + Environment.NewLine);

            return(sb.ToString());
        }
Пример #6
0
        private void btnGetGameState_Click(object sender, EventArgs e)
        {
            // This is old code that used curl.
            //string s = Blah("getcurrentstate.bat");
            //Application.DoEvents();
            //GspRoot gsp = GetGsp();

            // Get GSP without using curl
            string json = GetJsonRpcResponse(getCurrentState);

            Application.DoEvents();
            GspRoot gsp = GetGspFromJson(json);

            Dictionary <string, Player>       playersU = gsp.Result.Gamestate.Players;
            SortedDictionary <string, Player> players  = new SortedDictionary <string, Player>(playersU);

            PopulatePlayerDropDown(gsp);

            StringBuilder sb = new StringBuilder();

            // set up header
            sb.AppendLine(GetHeader());
            // Set up navigation
            sb.AppendLine(GetFloatingMenu());
            // Set up sb with basic CSS
            sb.AppendLine(GetCSS());

            IslesOfWar.UnitNames unitNames = new UnitNames();
            int pad = 20; // used to pad names & stuff

            // Some basic overview stats first
            sb.AppendLine("<p>Player count = " + players.Count.ToString() + "</p>");
            sb.AppendLine("<p>Island count = " + gsp.Result.Gamestate.Islands.Count.ToString() + "</p>");
            sb.AppendLine("<p></p>");

            // Display resource pools.

            sb.AppendLine("<h1><a id=\"Pools\"></a>POOLS</h1>");
            sb.AppendLine("<pre>Oil: ".PadRight(16) + gsp.Result.Gamestate.ResourcePools[0].ToString("n0").PadLeft(20) + "</pre>");
            sb.AppendLine("<pre>Metal: ".PadRight(16) + gsp.Result.Gamestate.ResourcePools[1].ToString("n0").PadLeft(20) + "</pre>");
            sb.AppendLine("<pre>Concrete: ".PadRight(16) + gsp.Result.Gamestate.ResourcePools[2].ToString("n0").PadLeft(20) + "</pre>");
            sb.AppendLine("<pre>Warbux: ".PadRight(16) + gsp.Result.Gamestate.WarbucksPool.ToString("n0").PadLeft(20) + "</pre>");

            // Get combat unit numbers for each player.
            sb.AppendLine("<h1><a id=\"PlayerCombatUnits\"></a>PLAYER COMBAT UNITS</h1>");
            sb.AppendLine("<p></p>");
            foreach (var p in players)
            {
                string name   = p.Key;
                Player player = p.Value;
                sb.AppendLine("<h1>" + name + "</h1>");
                // TOTAL units
                sb.AppendLine(GetTotalCombatUnits(player, name, pad, gsp));
                // AVAILABLE units
                sb.AppendLine(GetPlayerCombatUnits(player));
            } // End getting combat units for each player.

            // Get resources for each player.
            IslesOfWar.ResourceNames resourceNames = new ResourceNames();
            sb.AppendLine("<p></p>");
            sb.AppendLine("<h1><a id=\"PlayerResources\"></a>PLAYER RESOURCES</h1>");
            sb.AppendLine("<p></p>");
            foreach (var p in players)
            {
                string name   = p.Key;
                Player player = p.Value;
                sb.AppendLine("<h2>" + name + "</h2>");

                sb.AppendLine(GetPlayerResources(player, resourceNames));
            }

            // What islands have what resources or defenses? Let's ignore resources for now as it's a bunch of arrays - just do defenses.
            Dictionary <string, Island> islands = gsp.Result.Gamestate.Islands;

            sb.AppendLine("<p></p>");
            sb.AppendLine("<h1><a id=\"IslandsAndTheirDefenses\"></a>ISLANDS AND THEIR DEFENSES</h1>");
            sb.AppendLine("<p></p>");
            sb.AppendLine(GetIslandNumbering());
            sb.AppendLine("<p></p>");

            Dictionary <string, int> playerIslandCount = new Dictionary <string, int>();
            int islandCount = 0;

            foreach (var isle in islands)
            {
                string hexName = isle.Key;
                Island island  = isle.Value;
                sb.AppendLine("<pre>" + island.Owner + " owns " + hexName + "</pre>");
                // Add in if there's a player attacking it
                sb.AppendLine(GetAttackingPlayer(hexName, players));

                if (island.SquadCounts != null && island.SquadPlans != null)
                {
                    // Add in defenses for each island
                    int squadPlanCount  = island.SquadPlans.Count;
                    int squadCountCount = island.SquadCounts.Count;

                    sb.AppendLine(GetDefenderSquadInfo(island, squadCountCount, unitNames, pad));
                }
                else
                {
                    // Add in for islands with no squads
                    sb.AppendLine("<pre>");
                    sb.AppendLine(GetDefenderNoSquadInfo(island));
                    sb.AppendLine("</pre>");
                }

                // There are 12 hexes per island. Each one can have a squad.
                if (playerIslandCount.Keys.Contains <string>(island.Owner))
                {
                    playerIslandCount[island.Owner]++;
                    islandCount++;
                }
                else
                {
                    playerIslandCount.Add(island.Owner, 1);
                    islandCount++;
                }
            }

            // Islands targeted for attack.
            sb.AppendLine("<p></p>");
            sb.AppendLine("<h1><a id=\"IslandsTargetedForAttack\"></a>ISLANDS TARGETED FOR ATTACK</h1>");
            sb.AppendLine(GetIslandsTargetedForAttack(players, islands));

            // How many islands each player has
            sb.AppendLine("<p></p>");
            sb.AppendLine("<h1><a id=\"HowManyIslands\"></a>HOW MANY ISLANDS DOES EACH PLAYER OWN?</h1>");
            sb.AppendLine("<h2>" + islandCount.ToString() + " ISLANDS TOTAL</h2>");
            sb.AppendLine("<p></p><pre>");

            // Sort by player name
            var l   = playerIslandCount.OrderBy(key => key.Key);
            var dic = l.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value);

            playerIslandCount = dic;

            foreach (var v in playerIslandCount)
            {
                sb.AppendLine("" + v.Key + " owns " + v.Value.ToString() + " islands");
            }
            sb.AppendLine("</pre>");
            sb.AppendLine(GetFooter());

            string newtext = sb.ToString();

            wbBrowser.DocumentText = newtext; // "<pre>" + newtext + "</pre>";
        }