Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
diceroll command
#1
I was looking around in some old code yesterday, following Morde's casino event (thanks for that!) and realized we'd previously talked about the utility of a dice rolling command since that can add a lot more to dice games than a dice and cup. 

As it so happens I still had some similar code laying around. It might need to be adapted for using statements here and tweaked slightly to work.

Out of the box it supports up to 10 100 sided dice.

Sample usage is diceroll 1d6 to roll one six sided dice.

Quote:using System;
using Server.Items;
using Server.Mobiles;
using Server.Misc;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Server.Network;
using Server.Prompts;
using Server.Engines.XmlSpawner2;

namespace Server.Scripts.Commands
{
    public class DiceRoll
    {
        public static void Initialize()
        {
            Server.Commands.Register( "DiceRoll", AccessLevel.Player, new CommandEventHandler( DiceRoll_OnCommand ) );
        }

        [Usage( "DiceRoll" )]
        [Description( "Allows you to roll up to 10 dice of up to 100 sides." )]
        private static void DiceRoll_OnCommand( CommandEventArgs e )
        {
            if( e.Mobile == null || !( e.Mobile is PlayerMobile ) || e.Mobile.Deleted )
                return;

            if( e.Arguments.Length > 0 && e.Arguments[0].ToLower().Contains( "d" ) )
            {
                string[] args = e.Arguments[0].Split( new char[] { 'd' } );

                if( args.Length > 1 )
                {
                    int numberOfDice = 0;
                    int numberOfSides = 0;

                    if( int.TryParse( args[0], out numberOfDice ) && int.TryParse( args[1], out numberOfSides ) &&
                        numberOfSides > 0 && numberOfDice > 0 && numberOfDice < 11 && numberOfSides < 101 )
                    {
                        List<int> results = new List<int>();
                        int total = 0;
                        StringBuilder sb = new StringBuilder();

                        for( int i = 0; i < numberOfDice; i++ )
                        {
                            int rolled = Utility.RandomMinMax( 1, numberOfSides );
                            results.Add( rolled );
                            total += rolled;

                            if( sb.Length > 0 )
                                sb.Append( ", " );

                            sb.Append( rolled.ToString() );
                        }

                        sb.Append( "." );

                        string plural = numberOfDice > 1 ? "s" : "";

                        foreach( NetState ns in e.Mobile.GetClientsInRange( 12 ) )
                            ns.Mobile.SendMessage( e.Mobile.Name + " rolled " + numberOfDice.ToString() + "d" + numberOfSides + plural +
                                ". Result" + plural + ": " + sb.ToString() + " Total: " + total.ToString() + "." );

                        return;
                    }
                }
            }

            e.Mobile.SendMessage( "This command requires one argument for the number of dice to roll and the amount of sides on the dice." );
            e.Mobile.SendMessage( "For example, to roll 1 6 sided die, use: \".DiceRoll 1d6\"." );
        }
    }
}


Good friends, good books, and a sleepy conscience: This is the ideal life.

Mark Twain
Reply
#2
I love this.
Reply
Topic Options
Forum Jump:




Users browsing this thread: 1 Guest(s)