diceSinglePattern := '^(\d+)?d(\d+)?$'. diceSingleRegex := AGRegex regexWithPattern:diceSinglePattern. diceDoublePattern := '^(?:(\d+)\s+)?(\d+)$'. diceDoubleRegex := AGRegex regexWithPattern:diceDoublePattern. diceInputPattern := '^!(\d+)?d(\d+)?\s*$'. diceInputRegex := AGRegex regexWithPattern:diceInputPattern. processUserCommand := [ :command :arguments :connection :view | (command caseInsensitiveCompare:'roll') = 0 ifTrue:[ |str numDice numPips didMatch match| str := arguments string. str := str stringByTrimmingCharactersInSet:NSCharacterSet whitespaceCharacterSet. numDice := 1. numPips := 6. didMatch := false. str = '' ifTrue:[ didMatch := true. ] ifFalse:[ match := diceSingleRegex findInString:str. match == nil ifFalse:[ ((match groupAtIndex:@{1,2}) @== nil) @not \ #| ifTrue:[ |tempDice tempPips| tempDice := match groupAtIndex:1. tempPips := match groupAtIndex:2. tempDice == nil ifFalse:[ numDice := tempDice ]. tempPips == nil ifFalse:[ numPips := tempPips ]. didMatch := true. ]. ] ifTrue:[ match := diceDoubleRegex findInString:str. match == nil ifFalse:[ |tempDice| tempDice := match groupAtIndex:1. tempDice == nil ifFalse:[ numDice := tempDice ]. numPips := match groupAtIndex:2. didMatch := true. ]. ]. ]. didMatch ifTrue:[ rollDice value:numDice intValue value:numPips intValue value:view value:nil. true. ]. ]. ]. processIncomingMessage := [ :message :view | |match str| message sender isLocalUser ifFalse:[ str := message bodyAsPlainText. match := diceInputRegex findInString:str. match == nil ifFalse:[ |numDice numPips| numDice := match groupAtIndex:1. numPips := match groupAtIndex:2. numDice == nil ifTrue:[ numDice := 1 ]. numPips == nil ifTrue:[ numPips := 6 ]. rollDice value:numDice intValue value:numPips intValue value:view value:message senderName. true. ]. ]. ]. rollDice := [ :dice :pips :view :user | |msg rolls| rolls := {}. 1 to:(dice min:20) do:[ :i | rolls addObject:pips random + 1. ]. msg := 'rolls ' ++ (makeEnglishList value:rolls) ++ '.'. user == nil ifFalse:[ msg := user ++ ' ' ++ msg. ]. msg := JVMutableChatMessage alloc initWithText:msg sender:view connection localUser. msg setAction:(user == nil). view sendMessage:msg. view performSelector:#echoSentMessageToDisplay: withObject:msg afterDelay:0.0. ]. makeEnglishList := [ :nums | |buffer| nums count > 0 ifTrue:[ buffer := NSMutableString stringWithString:'a '. buffer appendString:(nums at:0) stringValue. nums count > 1 ifTrue:[ nums count = 2 ifTrue:[ #appendFormat: value:buffer value:' and a %@' value:(nums at:1). ] ifFalse:[ 1 to:nums count - 2 do:[ :i | #appendFormat: value:buffer value:', a %@' value:(nums at:i) stringValue. ]. #appendFormat: value:buffer value:', and a %@' value:(nums at:nums count - 1). ]. ]. ]. buffer. ].