Top Banner
Triskelion and Crapshoot Miami Ruby Brigade January 17, 2010 Monday, January 17, 2011
78
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Triskelion and Crapshoot

Triskelionand

CrapshootMiami Ruby Brigade

January 17, 2010

Monday, January 17, 2011

Page 2: Triskelion and Crapshoot

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

Page 3: Triskelion and Crapshoot

Pen and Paper Games

PensPaperDiceBooks

TalkingSnacks

Monday, January 17, 2011

Page 4: Triskelion and Crapshoot

Pen and Paper Games

PensPaperDiceBooks

TalkingSnacks

Monday, January 17, 2011

Page 5: Triskelion and Crapshoot

Monday, January 17, 2011

Page 6: Triskelion and Crapshoot

Monday, January 17, 2011

Page 7: Triskelion and Crapshoot

Timesta

mp

Monday, January 17, 2011

Page 8: Triskelion and Crapshoot

Timesta

mp

Name

Monday, January 17, 2011

Page 9: Triskelion and Crapshoot

Timesta

mp

Name

Tripcode

Monday, January 17, 2011

Page 10: Triskelion and Crapshoot

Timesta

mp

Name

Tripcode

Content

Monday, January 17, 2011

Page 11: Triskelion and Crapshoot

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

Page 12: Triskelion and Crapshoot

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

Page 13: Triskelion and Crapshoot

Chat

auto_link chat.content, :html=>{:target=>'_blank'}

Monday, January 17, 2011

Page 14: Triskelion and Crapshoot

Chat

auto_link chat.content, :html=>{:target=>'_blank'}

http://www.flickr.com/photos/lostvegas/2214183472/

Monday, January 17, 2011

Page 15: Triskelion and Crapshoot

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

Page 16: Triskelion and Crapshoot

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

Page 17: Triskelion and Crapshoot

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

Page 18: Triskelion and Crapshoot

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

Page 19: Triskelion and Crapshoot

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

Page 21: Triskelion and Crapshoot

Dice Language

4d6v 3d10^ 2d6 300+ – -

Monday, January 17, 2011

Page 22: Triskelion and Crapshoot

Dice Language

4d6v 3d10^ 2d6 300+ – -Expression

Monday, January 17, 2011

Page 23: Triskelion and Crapshoot

Dice Language

4d6v 3d10^ 2d6 300+ – -

Monday, January 17, 2011

Page 24: Triskelion and Crapshoot

Dice Language

2d6 300-

Monday, January 17, 2011

Page 25: Triskelion and Crapshoot

Dice Language

2d6 300-BinaryExpression

Monday, January 17, 2011

Page 26: Triskelion and Crapshoot

Dice Language

2d6 300-

Monday, January 17, 2011

Page 27: Triskelion and Crapshoot

Dice Language

2d6

300

-

Monday, January 17, 2011

Page 28: Triskelion and Crapshoot

Dice Language

2d6

300

-

Series

Monday, January 17, 2011

Page 29: Triskelion and Crapshoot

Dice Language

2d6

300

-

Series

Arithmetic

Monday, January 17, 2011

Page 30: Triskelion and Crapshoot

Dice Language

2d6

300

-

Series

Arithmetic

Constant

Monday, January 17, 2011

Page 31: Triskelion and Crapshoot

Dice Language

2d6

300

-

Monday, January 17, 2011

Page 32: Triskelion and Crapshoot

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

Page 33: Triskelion and Crapshoot

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

Page 34: Triskelion and Crapshoot

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

Page 35: Triskelion and Crapshoot

Ragel

Monday, January 17, 2011

Page 36: Triskelion and Crapshoot

Ragelscan.rl: 62 lines

Monday, January 17, 2011

Page 37: Triskelion and Crapshoot

Ragelscan.rl: 62 lines

scan.rb: 330 lines

Monday, January 17, 2011

Page 38: Triskelion and Crapshoot

Ragelscan.rl: 62 lines

scan.rb: 330 lines

scan.pdf: priceless

Monday, January 17, 2011

Page 39: Triskelion and Crapshoot

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

Page 40: Triskelion and Crapshoot

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

Page 41: Triskelion and Crapshoot

Parsed Dice

4d6vcount: 4sides: 6drop: ‘v’

3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Monday, January 17, 2011

Page 42: Triskelion and Crapshoot

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

Page 43: Triskelion and Crapshoot

Infix & Postfix

Monday, January 17, 2011

Page 44: Triskelion and Crapshoot

Infix & Postfix

Infix1 + 2 + 3 + 4

Used for centuries

Hard for computers

Monday, January 17, 2011

Page 45: Triskelion and Crapshoot

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

Page 46: Triskelion and Crapshoot

Parsed Dice

4d6vcount: 4sides: 6drop: ‘v’

3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Monday, January 17, 2011

Page 47: Triskelion and Crapshoot

Postfixed Dice

4d6vcount: 4sides: 6drop: ‘v’

3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Monday, January 17, 2011

Page 48: Triskelion and Crapshoot

Postfix Evaluation

Monday, January 17, 2011

Page 49: Triskelion and Crapshoot

Postfix Evaluation

•Series

Monday, January 17, 2011

Page 50: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

Monday, January 17, 2011

Page 51: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

Monday, January 17, 2011

Page 52: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

•Push result to stack

Monday, January 17, 2011

Page 53: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

•Push result to stack

•Constant

Monday, January 17, 2011

Page 54: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

•Push result to stack

•Constant

•Push value to stack

Monday, January 17, 2011

Page 55: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

•Push result to stack

•Constant

•Push value to stack

•Arithmetic

Monday, January 17, 2011

Page 56: Triskelion and Crapshoot

Postfix Evaluation

•Series

•Roll dice

•Do drop

•Push result to stack

•Constant

•Push value to stack

•Arithmetic

•Pop twice

Monday, January 17, 2011

Page 57: Triskelion and Crapshoot

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

Page 58: Triskelion and Crapshoot

Postfix Evaluation

4d6vcount: 4sides: 6drop: ‘v’

3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Stack

Monday, January 17, 2011

Page 59: Triskelion and Crapshoot

Postfix Evaluation

15 3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Stack

Monday, January 17, 2011

Page 60: Triskelion and Crapshoot

Postfix Evaluation

15

3d10^count: 3sides: 10drop: ‘^’

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Stack

Monday, January 17, 2011

Page 61: Triskelion and Crapshoot

Postfix Evaluation

15

5 2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Stack

Monday, January 17, 2011

Page 62: Triskelion and Crapshoot

Postfix Evaluation

15 5

2d6count: 2sides: 6drop: nil

300value: 300

+ - -

Stack

Monday, January 17, 2011

Page 63: Triskelion and Crapshoot

Postfix Evaluation

15 5

2d6count: 2sides: 6drop: nil

300value: 300

+

- -

Stack

Monday, January 17, 2011

Page 64: Triskelion and Crapshoot

Postfix Evaluation

20

2d6count: 2sides: 6drop: nil

300value: 300

- -

Stack

Monday, January 17, 2011

Page 65: Triskelion and Crapshoot

Postfix Evaluation

20

9 300value: 300

- -

Stack

Monday, January 17, 2011

Page 66: Triskelion and Crapshoot

Postfix Evaluation

20 9

300value: 300

- -

Stack

Monday, January 17, 2011

Page 67: Triskelion and Crapshoot

Postfix Evaluation

20 9

300value: 300

-

-

Stack

Monday, January 17, 2011

Page 68: Triskelion and Crapshoot

Postfix Evaluation

11

300value: 300

-

Stack

Monday, January 17, 2011

Page 69: Triskelion and Crapshoot

Postfix Evaluation

11

300 -

Stack

Monday, January 17, 2011

Page 70: Triskelion and Crapshoot

Postfix Evaluation

11 300

-

Stack

Monday, January 17, 2011

Page 71: Triskelion and Crapshoot

Postfix Evaluation

11 300-Stack

Monday, January 17, 2011

Page 72: Triskelion and Crapshoot

Postfix Evaluation

-289Stack

Monday, January 17, 2011

Page 73: Triskelion and Crapshoot

Result

-289

Monday, January 17, 2011

Page 74: Triskelion and Crapshoot

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

Page 75: Triskelion and Crapshoot

Randomness

Computers are deterministic

Gather and store entropy where possible

Use secure hashes to disburse entropy

Monday, January 17, 2011

Page 76: Triskelion and Crapshoot

Randomness

Just use OpenSSL

Monday, January 17, 2011

Page 77: Triskelion and Crapshoot

SecureRandom

Ruby 1.9 and ActiveSupport

random_number

random_bytes

hex

Monday, January 17, 2011

Page 78: Triskelion and Crapshoot

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