emesene forum
March 13, 2010, 10:26:22 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: REPORT ANY ISSUE HERE - PLEASE CHECK IF THE PROBLEM HAS ALREADY BEEN REPORTED FIRST -- THANKS
 
  Home   Forum   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [NEW AMAZING PLUGIN] Dices  (Read 2602 times)
louizatakk
Hero Member
*****

l33tness: 0
Offline Offline

Posts: 428



View Profile Email
« on: March 12, 2008, 09:08:51 AM »

Hello, I've made a little plugin, for those who sometimes play role playing game (like Dungeon and Dragons), it's for my personal use at first, but I think it could be useful to some others (ie : I really don't except it to be in emesene, but use it if you want it)

The command is /dice xdy, and it sends x dices with y faces.

Please report any bug or comment, or feedback, or WHAT YOU WANT Smiley

For those you want to read it :
Code:
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.1'
import random
from Plugin import Plugin

class MainClass(Plugin):

    def __init__(self, controller, msn):
        Plugin.__init__(self, controller, msn)

        self.description = _('Show random numbers in the conversation. "/help dice" for usage.')
        self.authors = {'Le Coz Florent (aka louiz)': 'louizatakk AT gmail DOT com'}
        self.website = 'None'
        self.displayName = _('Dices')
        self.name = 'Dices'
        self.Slash = controller.Slash
        self.enabled = False

    def start(self):
        self.Slash.register('dice', self.rolls_dice, _('Dice:\nformat : xdy\nRolls x dices, each dice has y faces'))
        self.enabled = True
       
    def rolls_dice(self, slash_action):
        '''rolls dices : it sends random numbers, defined by the xdy format'''
        data = slash_action.getParams()
        params = data.split('d')
        if len(params) != 2:
            return
        try:
            x = int(params[0])
            nb = int(params[1])
        except ValueError:
            return
        data += " :\n"
        if (nb <= 0):
            return
        for i in range(x):
            data += str(random.randint(1, nb)) + '  '
        slash_action.outputText(data, True)
       
    def stop(self):
        self.Slash.unregister('dice')
        self.enabled = False

    def check(self):
        return (True, 'Ok')

and those who want to download it and use it, it's attached too.

Smiley
« Last Edit: March 14, 2008, 10:28:23 PM by louizatakk » Logged
bug
Newbie
*

l33tness: 0
Offline Offline

Posts: 2


Bug.In.Your.System


View Profile WWW
« Reply #1 on: May 23, 2008, 02:42:34 AM »

Heh. I coded the same plugin today.
Didn't know you coded it 11 days ago.

Though mine got an advantage over yours. Mine will parse 7d as 7d6, d8 as 1d8 and will give 1d6 for whatever random input the user might have.

Code:
"""
A pack of functions. For the heck of learning.
"""

import random

def power(num):
    """
    Input: num of type N [Full positive integer]
    Output: Power of num.
    """
    if (num > 1):
        return num * power(num - 1)
    else:
        return 1

def fib(num):
    """
    Input: num of n [Full positive integer] or 0
    Output: Fibonatchi of num.
    """
    if (num < 2):
        return 1
    else:
        return fib(num-1) + fib(num-2)

def random_password(num = 8):
    """
    Generates a random password.
    num = how many chars the password should contain. This number belongs
        to the N grup. Full and positive.
    Default is 8
    """
    if not isinstance(num, int):
        num = 8
    if (num < 1):
        num = 8
    i = 1
    password = ""
    password += random.choice( \
        'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    while (i < num):
        password += random.choice( \
        'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
        i += 1
    return password

def roll(dice = "3d6"):
    """
    Roll a dice.
    @Param dice = 1d6, d3, 2d2. Amount `d` dice type.
        No amount = 1 time [d6].
        Default value = 1d6.
    """

    # Parse what the heck the user wants
    dice = dice.split('d')
    dice.append(' ')
    try:
        amount = int(dice[0])
    except ValueError:
        amount = 1
    try:
        dtype = int(dice[1])
    except ValueError:
        dtype = 6

    # Roll the dice!
    total = 0
    i = 0
    while (i < amount):
        total += random.randint(1, dtype)
        i += 1
    return total

def main():
    """ Main program """
    print "Power of 4: ", power(4)
    i = 0
    for i in range(10):
        print "Fib", i, "=", fib(i)
    print random_password(20)
    print "You roll 1d6: %d" % roll()
    print "You roll 2d: %d" % roll("2d")
    print "You roll d10: %d" % roll("d10")
    print "You roll 5d10: %d" % roll("5d10")

if __name__ == "__main__":
    main()

Logged
louizatakk
Hero Member
*****

l33tness: 0
Offline Offline

Posts: 428



View Profile Email
« Reply #2 on: May 24, 2008, 08:26:07 AM »

Ok, but mine is directly integrated with emesene Wink

Code:
You roll 5d10: 24

OK, but I see only ONE number, not 5 ;(
« Last Edit: May 24, 2008, 08:29:04 AM by louizatakk » Logged
bug
Newbie
*

l33tness: 0
Offline Offline

Posts: 2


Bug.In.Your.System


View Profile WWW
« Reply #3 on: May 29, 2008, 12:05:59 PM »

Yeah. It's a total.
I guess I could have made it show the results and the sum, but thats my preference.

Maybe we should unite 'em or something [And add an option to decide when-ever to show total or the dices results]. What do you say?
Logged
louizatakk
Hero Member
*****

l33tness: 0
Offline Offline

Posts: 428



View Profile Email
« Reply #4 on: May 31, 2008, 04:55:46 PM »

Why not, we could make a very complete dice plugin, which cans also make the coffee  Wink

Yeah, i'll try too add some feature, if possible, even if it may not be useful, still interesting to do.
Don't hesitate to do it yourself Wink
Logged
Pages: [1]
  Print  
 
Jump to:  

TinyPortal v.1.0.6 beta 2 © Bloc
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!