作者 主题: 嗯啊,我把MiniDiceBot修好了……  (阅读 2593 次)

副标题: 原来是没去掉颜色控制符……

离线 GZYZ

  • Adventurer
  • *
  • 帖子数: 79
  • 苹果币: 0
    • http://
嗯啊,我把MiniDiceBot修好了……
« 于: 2005-10-25, 周二 20:43:47 »
估计这辈子用不着了,所以干脆开源……
因为不会贴文件,于是把关键性的源代码贴在这里了……
希望后人能在此基础上写出更好的……
在下放弃一切版权

还有,传说中牛X的开发包和开发说明在这里下载
用这个可以做出一个完整的IRC客户端,甚至是不亚于FantaRPG之流的工具哦~
http://www.jibble.org/pircbot.php

引用

package mybot;

import org.jibble.pircbot.*;
import trpg.dices.*;

public class MiniBot extends PircBot
{
   protected boolean  enableSayHello = false;
   protected DiceRoller   roller = new DiceRoller ();
   protected String  lastErrorMessage = "No error";

   public MiniBot ()
   {
  setName ("MiniDiceBot");
  setVerbose (true);
  setLogin ("MiniDiceBot");
  setMessageDelay (1l);
   }

   public void setNickName (String nick)
   {
  setName (nick);
   }

   // recieving a channel info, try to join that channel
   protected void onChannelInfo (String channel, int userCount, String topic)
   {
  log ("发现频道: " + channel);
  if (isInThisChannel (channel)) return;
  joinChannel (channel);
   }

   // report successful join
   protected void onJoin (String channel, String sender, String login, String hostname)
   {
  // the bot has performed a successful join
  if (sender.equals (getNick()))
  {
     log ("已加入频道:" + channel);

  }
  // say hello to new comer
  else
     if (enableSayHello)
    sendNotice (sender, "PircBot welcomes you, " + sender);
   }

   // whether i am in this channel
   public boolean isInThisChannel (String channel)
   {
  String[] channels = getChannels ();
  for (int i = 0; i < channels.length; i++)
  {
     if (channels .equals (channel))
    return true;
  }
  return false;
   }

   // overriden to make neater output
   public void log (String log)
   {
  System.out.println (log);
   }

   // try to join channels on connection
   protected void onConnect ()
   {
  listChannels ();
  // may not work on servers that prohabit channel creation... XD
  joinChannel ("#MiniDiceBot");
  log ("/list 命令执行");
   }

   // reply on a roll request
   protected synchronized void onMessage(String channel, String sender, String login, String hostname, String message)
   {
  parseMessage (channel, sender, message);
   }

   protected synchronized void onPrivateMessage(String sender, String login, String hostname, String message)
   {
  parseMessage (sender, sender, message);
   }

   private void parseMessage (String channel, String sender, String message)
   {
  message = Colors.removeFormattingAndColors (message);// 这里很关键……然则……
  if (message.startsWith (".r ") || message.startsWith (".h "))
  {
     String description;
     String command;
     int commandPosition = message.indexOf (" ", 2);
     if (commandPosition == -1) return;
     int descriptionPosition = message.indexOf (" ", commandPosition + 1);
     if (descriptionPosition == -1)
     {
    command = message.substring (commandPosition);
    description = "";
     }
     else
     {
    command = message.substring (commandPosition, commandPosition + descriptionPosition - 1);
    description = message.substring (descriptionPosition);
     }
     try
     {
    if (message.startsWith (".r "))
       sendMessage (channel, sender + "进行" + description + "判定,结果是" + roller.getRoll (command));
    else
       sendMessage (sender, sender + "进行" + description + "判定,结果是" + roller.getRoll (command));
     }
     catch (Exception e)
     {
    e.printStackTrace ();
    lastErrorMessage = e.getMessage ();
    sendMessage (channel, sender + "这个废柴输入有误!");
     }
  }
  else if (message.startsWith (".version"))
  {
     sendMessage (channel, "MiniDiceRollerBot by GZYZ version 0.002");
  }
  else if (message.startsWith (".info"))
  {
     sendMessage (channel, "MiniDiceBot is an IRC bot designed to perform dice rolls and spamming. Codes are based on PircBot [www.jibble.org/pircbot]");
  }
  else if (message.startsWith (".help"))
  {
     sendMessage (channel, "命令格式: .r 1d20+8 [原因] 注意:只能有一个修正值,原因和1可以省略 .h密投");
  }
  else if (message.startsWith (".debug"))
  {
     sendMessage (channel, "The last exception message is: " + lastErrorMessage);
  }
   }
}

引用
// 下面是trpg/dices/DiceRoller.java
package trpg.dices;

import java.util.Random;
import java.lang.String;

/**
 * Class DiceRoller
 * Author GZYZ 2005
 *
 * parse a specialfied string of dice-roll command and give a corresponding result
 * example "1d20+5" will have a result of "1d20+5=7+5=12"
 * notice all rolls are restricted to > 0 as 1d2-2 = 1
 */

public class DiceRoller
{
   private Random randomizer = new Random ();

   // empty constructor
   public DiceRoller () {}

   /**
  * method getRoll
  * parse a roll command and make a correponding roll
  * return the result in full detailed description
  * watch out for exceptions, as the client that create the command is mostly
  * human (whose behavior can not be expected)
  */
   public String getRoll (String rollCommand) throws InvalidRollException
   {
  // 1d20+5     1d20+5           1d20+5
  // ^dice num   ^the 'd' thing    ^the dicetype
  // 1d20+5                      1d20+5
  //     ^ the '+' or '-'             ^modifier
  Roll roll = parseRoll (rollCommand);
  return (rollCommand + '=' + Integer.toString (roll.result) + (roll.modifier < 0 ? "" : "+") + Integer.toString (roll.modifier) + '=' + Integer.toString (roll.total));
   }

   /**
  * method getRollInt
  * works as same as parseRoll, but returns integer instead
  * who knows when these little integers will come in handy?
  */
   public int getRollInt (String rollCommand) throws InvalidRollException
   {
  Roll roll = parseRoll (rollCommand);
  return roll.total;
   }

   // the thing that does the work... returns the roll (with result and modifier)
   private Roll parseRoll (String rollCommand) throws InvalidRollException
   {
  Roll roll;
  try
  {
     String command = rollCommand.toLowerCase ().trim ();
     int theDPosition = command.indexOf ("d");
     // you never know why guys would do this...
     if (theDPosition == -1)
    throw new IllegalArgumentException ("No 'd' is found");
     // get dice num
     String beforeDstr = command.substring (0, theDPosition);
     int diceNum;
     if (beforeDstr.equals ("")) diceNum = 1; // designed for the lasy guys...like me
     else
     {
    // trying to parse an int, exceptions are caught
    diceNum = Integer.parseInt (beforeDstr);
    if (diceNum < 1) throw new IllegalArgumentException ("erronous number of dices");
     }
     // get dice type/faces and modifier
     int plusPosition = command.indexOf ("+", theDPosition);
     int minusPosition = command.indexOf ("-", theDPosition);
     boolean positive;
     int modifierPosition;
     int modifier;
     int diceFace;
     // two modifiers...
     if (plusPosition != -1 && minusPosition != -1)
    throw new IllegalArgumentException ("you can't have two or more modifiers in one roll");
     // no modifier for this roll
     else if (plusPosition == -1 && minusPosition == -1)
     {
    modifier = 0;
    positive = true;
    diceFace = Integer.parseInt (command.substring (theDPosition + 1));
     }
     else // general situation
     {
    if (plusPosition > minusPosition)
    {
       modifierPosition = plusPosition;
       positive = true;
    }
    else
    {
       modifierPosition = minusPosition;
       positive = false;
    }
    diceFace = Integer.parseInt (command.substring (theDPosition + 1, modifierPosition));
    int temp = Integer.parseInt (command.substring (modifierPosition + 1));
    modifier = positive ? temp : -temp;
     }
     // let's roll!
     int result = 0;
     for (int i = 0; i < diceNum; i++)
    result += 1 + randomizer.nextInt (diceFace);
     int total = result + modifier < 1 ? 1 : result + modifier;
     // return result
     roll = new Roll ();
     roll.diceNum = diceNum;
     roll.diceFace = diceFace;
     roll.result = result;
     roll.modifier = modifier;
     roll.total = total;
  }
  catch (Exception e)
  {
     throw new InvalidRollException ("[" + e.getMessage () + "] via " + e.getClass ().getName ());
  }
  return roll;
   }
   // something to wrap some ints here
   class Roll
   {
  int diceNum; int diceFace;
  int result; int modifier; int total;
   }
}

[/i][/font]

离线 ircdm

  • 版主
  • *
  • 帖子数: 658
  • 苹果币: 0
    • http://
嗯啊,我把MiniDiceBot修好了……
« 回帖 #1 于: 2005-10-27, 周四 18:08:19 »
原代码不要贴在这样的小地方,贴在主区看的人比较多^^
what are you going?
i'm going you!