triskelion and crapshoot
TRANSCRIPT
Triskelionand
CrapshootMiami Ruby Brigade
January 17, 2010
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011
Pen and Paper Games
PensPaperDiceBooks
TalkingSnacks
Monday, January 17, 2011
Pen and Paper Games
PensPaperDiceBooks
TalkingSnacks
Monday, January 17, 2011
Monday, January 17, 2011
Monday, January 17, 2011
Timesta
mp
Monday, January 17, 2011
Timesta
mp
Name
Monday, January 17, 2011
Timesta
mp
Name
Tripcode
Monday, January 17, 2011
Timesta
mp
Name
Tripcode
Content
Monday, January 17, 2011
Colored NameEasier to read than text-only nickname
Shift the first few bytes of hash into LSBs of color
def chat_color digest = Digest::SHA1.hexdigest(self.tripcode + self.name) r = (digest[0..1].to_i(16) >> 1) + 127 g = (digest[2..3].to_i(16) >> 1) + 127 b = (digest[4..5].to_i(16) >> 1) + 127 "##{r.to_s 16}#{g.to_s 16}#{b.to_s 16}" end
Monday, January 17, 2011
Tripcode
Identity without Password or Registration
Used on 4chan
def tripcode_digest [Digest::SHA1.digest(self.tripcode)]. pack('m'). tr('+/','-_')[0..7] end
Monday, January 17, 2011
Chat
auto_link chat.content, :html=>{:target=>'_blank'}
Monday, January 17, 2011
Chat
auto_link chat.content, :html=>{:target=>'_blank'}
http://www.flickr.com/photos/lostvegas/2214183472/
Monday, January 17, 2011
Roll
<span class="code"> <%=h roll.code %></span><span class="result"> <%=h roll.result %></span><span class="description"> <%=h roll.description %></span>
4d6v + 40 =57 ((3+5+6+6-3)+40)
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011
Dice Code
•2d4
•Two four-sided dice
•1d100 + 8
•One hundred-sided die plus eight
•4d6v
•Four six-sided dice, minus the lowest
Monday, January 17, 2011
Dice Language
A programming language is […] designed to express computations, […] to express algorithms precisely, or as a mode of human communication.
http://en.wikipedia.org/wiki/Programming_language
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011
Regular LanguageIn computer science, a regular language is a formal language […] that satisfies the following equivalent properties:
•it can be accepted by a deterministic finite state machine
•it can be generated by a regular grammarhttp://en.wikipedia.org/wiki/Regular_language
Monday, January 17, 2011
Dice Language
4d6v 3d10^ 2d6 300+ – -
Monday, January 17, 2011
Dice Language
4d6v 3d10^ 2d6 300+ – -Expression
Monday, January 17, 2011
Dice Language
4d6v 3d10^ 2d6 300+ – -
Monday, January 17, 2011
Dice Language
2d6 300-
Monday, January 17, 2011
Dice Language
2d6 300-BinaryExpression
Monday, January 17, 2011
Dice Language
2d6 300-
Monday, January 17, 2011
Dice Language
2d6
300
-
Monday, January 17, 2011
Dice Language
2d6
300
-
Series
Monday, January 17, 2011
Dice Language
2d6
300
-
Series
Arithmetic
Monday, January 17, 2011
Dice Language
2d6
300
-
Series
Arithmetic
Constant
Monday, January 17, 2011
Dice Language
2d6
300
-
Monday, January 17, 2011
Dice Language
Number = digit+;
Constant = Number;
Drop = ('^' | 'v');Series = Number 'd' Number Drop?;
Arithmetic = ('+' | '-');
UnaryExpression = Series | Constant;BinaryExpression = UnaryExpression (space* Arithmetic space* UnaryExpression)+;Expression = UnaryExpression | BinaryExpression;
100%
CERTIFIED
REGULAR
LANGUAGE
Monday, January 17, 2011
Ragel
Ragel compiles executable finite state machines from regular languages. Ragel targets C, C++, Objective-C, D, Java and Ruby.
http://www.complang.org/ragel/
Monday, January 17, 2011
scan.rl%%{
machine scanner;
action _number { @mark_num = p } action number { @num_stack.push atos(data[@mark_num..p-1]) }
action constant { @tokens << Tokens::Constant.new(@num_stack.pop) } action series { drop = @drop_current @drop_current = nil sides = @num_stack.pop count = @num_stack.pop @tokens << Tokens::Series.new(count, sides, drop) } action arithmetic { @tokens << Tokens::Arithmetic.new(data[p-1].chr) }
action drop { @drop_current = data[p-1].chr }
Number = digit+ >_number %number;
Constant = Number %constant;
Drop = ('^' | 'v') %drop; Series = Number 'd' Number Drop? %series;
Arithmetic = ('+' | '-') %arithmetic;
UnaryExpression = Series | Constant; BinaryExpression = UnaryExpression (space* Arithmetic space* UnaryExpression)+; Expression = UnaryExpression | BinaryExpression;
main := Expression;}%%
Act
ions
Lang
uage
Monday, January 17, 2011
Ragel
Monday, January 17, 2011
Ragelscan.rl: 62 lines
Monday, January 17, 2011
Ragelscan.rl: 62 lines
scan.rb: 330 lines
Monday, January 17, 2011
Ragelscan.rl: 62 lines
scan.rb: 330 lines
scan.pdf: priceless
Monday, January 17, 2011
scan.rl
action constant { @tokens << Tokens::Constant.new(@num_stack.pop) } action series { drop = @drop_current @drop_current = nil sides = @num_stack.pop count = @num_stack.pop @tokens << Tokens::Series.new(count, sides, drop) } action arithmetic { @tokens << Tokens::Arithmetic.new(data[p-1].chr) }
Transform constant, series, and arithmetic elements into Token objects
Monday, January 17, 2011
Tokens
Token#eval Turn token into result during evaluation
Token#independentDetermine if token needs the next token during postfixing
Token#inspect<Crapshoot::Tokens::Series dice=4d6 drop=nothing>
Monday, January 17, 2011
Parsed Dice
4d6vcount: 4sides: 6drop: ‘v’
3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011
Infix & Postfix
Monday, January 17, 2011
Infix & Postfix
Infix1 + 2 + 3 + 4
Used for centuries
Hard for computers
Monday, January 17, 2011
Infix & Postfix
Infix1 + 2 + 3 + 4
Used for centuries
Hard for computers
Postfix1 2 + 3 + 4 +
Easy for computers
Convertible from infix
Monday, January 17, 2011
Parsed Dice
4d6vcount: 4sides: 6drop: ‘v’
3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Monday, January 17, 2011
Postfixed Dice
4d6vcount: 4sides: 6drop: ‘v’
3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Monday, January 17, 2011
Postfix Evaluation
Monday, January 17, 2011
Postfix Evaluation
•Series
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
•Constant
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
•Constant
•Push value to stack
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
•Constant
•Push value to stack
•Arithmetic
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
•Constant
•Push value to stack
•Arithmetic
•Pop twice
Monday, January 17, 2011
Postfix Evaluation
•Series
•Roll dice
•Do drop
•Push result to stack
•Constant
•Push value to stack
•Arithmetic
•Pop twice
•Push result
Monday, January 17, 2011
Postfix Evaluation
4d6vcount: 4sides: 6drop: ‘v’
3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Stack
Monday, January 17, 2011
Postfix Evaluation
15 3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Stack
Monday, January 17, 2011
Postfix Evaluation
15
3d10^count: 3sides: 10drop: ‘^’
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Stack
Monday, January 17, 2011
Postfix Evaluation
15
5 2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Stack
Monday, January 17, 2011
Postfix Evaluation
15 5
2d6count: 2sides: 6drop: nil
300value: 300
+ - -
Stack
Monday, January 17, 2011
Postfix Evaluation
15 5
2d6count: 2sides: 6drop: nil
300value: 300
+
- -
Stack
Monday, January 17, 2011
Postfix Evaluation
20
2d6count: 2sides: 6drop: nil
300value: 300
- -
Stack
Monday, January 17, 2011
Postfix Evaluation
20
9 300value: 300
- -
Stack
Monday, January 17, 2011
Postfix Evaluation
20 9
300value: 300
- -
Stack
Monday, January 17, 2011
Postfix Evaluation
20 9
300value: 300
-
-
Stack
Monday, January 17, 2011
Postfix Evaluation
11
300value: 300
-
Stack
Monday, January 17, 2011
Postfix Evaluation
11
300 -
Stack
Monday, January 17, 2011
Postfix Evaluation
11 300
-
Stack
Monday, January 17, 2011
Postfix Evaluation
11 300-Stack
Monday, January 17, 2011
Postfix Evaluation
-289Stack
Monday, January 17, 2011
Result
-289
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011
Randomness
Computers are deterministic
Gather and store entropy where possible
Use secure hashes to disburse entropy
Monday, January 17, 2011
Randomness
Just use OpenSSL
Monday, January 17, 2011
SecureRandom
Ruby 1.9 and ActiveSupport
random_number
random_bytes
hex
Monday, January 17, 2011
Triskelion and Crapshoot
•Triskelion - Rails application
•UI Elements
•Crapshoot - Dice-rolling library
•Parsing with Ragel
•Infix and Postfix Notation
•Random numbers
Monday, January 17, 2011