/bet

/bet

Postby ismellike » 04 Jul 2013, 03:30

This command bets on who will win.
It was a suggestion by Remmington I think.
Here is the code.
Change the cost of the bets by changing the -> static int cost <- in the code

Code: Select all
using System;
using System.Collections.Generic;
using System.IO;
using i = MCDzienny.InfectionSystem;

namespace MCDzienny
{
    public class CmdBet : Command
    {
        #region Toolbox
        string path = "extra/bet.bool";
        static int cost = 1; //change this to how much a bet should cost.
        List<Player> bhuman = new List<Player>();
        List<Player> bzombie = new List<Player>();
        int dist = 0;
        public override string name { get { return "bet"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        #endregion
        #region Player Input
        public override void Init()
        {
            if (!File.Exists(path))
                File.WriteAllText(path, "true");
            i.InfectionSystem.AnnounceWinners += new EventHandler<i.AnnounceWinnersEventArgs>(InfectionSystem_AnnounceWinners);
        }
        void InfectionSystem_AnnounceWinners(object sender, i.AnnounceWinnersEventArgs e)
        {
            if (e.NotInfected.Count != 0)
            {
                dist /= bhuman.Count;
                bhuman.ForEach(delegate(Player p)
                {
                    Player.SendMessage(p, "You know how to bet! %c+%e " + dist.ToString());
                    p.money += 1;
                });
            }
            else
            {
                dist /= bzombie.Count;
                bzombie.ForEach(delegate(Player p)
                {
                    Player.SendMessage(p, "You know how to bet! %c+%e " + dist.ToString());
                    p.money += dist;
                });
            }
            bhuman.Clear();
            bzombie.Clear();
            dist = 0;
        }
        public override void Use(Player p, string message)
        {
            if (enabled())
            {
                if (message == "") { Help(p); return; }
                string[] split = message.Trim().Split(' ');
                if (split[0] != "odds" && (p.money - cost) < 0)
                {
                    Player.SendMessage(p, "You do not have enough money to bet.");
                    return;
                }
                switch (split[0])
                {
                    case "alive":
                    case "humans":
                        bhuman.Add(p);
                        Player.GlobalMessage(p.PublicName + " has just bet on humans.");
                        SayStatistics(null);
                        dist += cost;
                        p.money -= cost;
                        return;
                    case "infected":
                    case "zombies":
                        bzombie.Add(p);
                        Player.GlobalMessage(p.PublicName + " has just bet on zombies.");
                        SayStatistics(null);
                        dist += cost;
                        p.money -= cost;
                        return;
                    case "odds":
                        SayStatistics(p);
                        return;
                    case "toggle":
                        if (p.group.Permission >= LevelPermission.Admin)
                        {
                            bool bo = !bool.Parse(path);
                            File.WriteAllText(path, bo.ToString());
                            Player.SendMessage(p, "Betting has been set to " + bo.ToString());
                        }
                        return;
                    default:
                        Help(p);
                        return;
                }
            }
            else
            {
                Player.SendMessage(p, "Betting is not currently enabled.");
            }
        }
        void SayStatistics(Player p)
        {
            int percentage;
            int ex;
            string who;
            if (bzombie.Count > bhuman.Count)
            {
                if (bzombie.Count == 0)
                    ex = 1;
                else
                    ex = bzombie.Count;
                percentage = bhuman.Count / ex * 100;
                who = "zombies";
            }
            else
            {
                if (bhuman.Count == 0)
                    ex = 1;
                else
                    ex = bhuman.Count;
                percentage = bzombie.Count / ex * 100;
                who = "humans";
            }
            if (p == null)
                Player.GlobalMessage("The odds are on " + who + " by " + percentage.ToString() + "%.");
            else
                Player.SendMessage(p, "The odds are on " + who + " by " + percentage.ToString() + "%.");
        }
        bool enabled()
        {
            try
            {
                return bool.Parse(path);
            }
            catch
            {
                return false;
            }
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/bet humans -- bets that humans will win");
            Player.SendMessage(p, "/bet zombies -- bets that zombies will win");
            Player.SendMessage(p, "/bet odds -- tells you who people think will win");
            if (p.group.Permission >= LevelPermission.Admin)
                Player.SendMessage(p, "/bet toggle -- toggles betting");
        }
        #endregion
    }
}
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: /bet

Postby Remmington1999 » 04 Jul 2013, 15:22

Alright, thanks! Can't wait to test it out.
Remmington1999
 
Posts: 5
Joined: 28 Dec 2012, 07:00
Location: North, Idaho

Re: /bet

Postby tinyCreeper » 05 Jul 2013, 12:24

Won't let me enable it. No matter what I do all i can seem to get is "Betting is not currently enabled."
tinyCreeper
 
Posts: 48
Joined: 05 Jul 2013, 12:21

Re: /bet

Postby tinyCreeper » 05 Jul 2013, 12:35

also, I got this error the first round I tried to use it and the round wouldn't end, it just sat at 0:00.

----7/5/2013 6:18:40 AM ----
Type: DivideByZeroException
Source: Cmdbet
Message: Attempted to divide by zero.
Target: InfectionSystem_AnnounceWinners
Trace: at MCDzienny.CmdBet.InfectionSystem_AnnounceWinners(Object sender, AnnounceWinnersEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at MCDzienny.InfectionSystem.InfectionSystem.OnAnnounceWinners(Object sender, AnnounceWinnersEventArgs e)
at MCDzienny.InfectionSystem.InfectionSystem.AnnounceAndAwardWinners()
at MCDzienny.InfectionSystem.InfectionSystem.EndInfectionRound()
at MCDzienny.InfectionSystem.InfectionUtils.RoundTimeManager(Object sender, ElapsedEventArgs e)

-------------------------
tinyCreeper
 
Posts: 48
Joined: 05 Jul 2013, 12:21

Re: /bet

Postby tinyCreeper » 05 Jul 2013, 12:46

I don't mean to spam, but I also found another bug that im pretty sure is related.
At the end of the round, it will announce winners of round, but won't bring up a map vote. I had to then restart server, reloaded command and tried again, and it did it again. So i restarted server again, without reloading /bet, and it didn't do it. Hope this helps.
tinyCreeper
 
Posts: 48
Joined: 05 Jul 2013, 12:21

Re: /bet

Postby ismellike » 05 Jul 2013, 14:05

Ok thanks, I can't fix it right now, so I will lock the thread for now.

I will fix the bugs when I do get home.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 5 guests