From e6a0c07dc79939171e0f676fc82a495ff283cc2d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 12 Jan 2015 00:24:55 -0500 Subject: [PATCH] Initial setup - the test command is for command line use and shouldn't really be in production --- README | 9 +++++++++ db.rb | 2 ++ die.rb | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ dieroller.html | 8 ++++++++ display.rb | 36 ++++++++++++++++++++++++++++++++++ display.rb.bk | 6 ++++++ roller.rb | 8 ++++++++ test.rb | 3 +++ 8 files changed, 125 insertions(+) create mode 100644 README create mode 100644 db.rb create mode 100644 die.rb create mode 100644 dieroller.html create mode 100755 display.rb create mode 100755 display.rb.bk create mode 100755 roller.rb create mode 100755 test.rb diff --git a/README b/README new file mode 100644 index 0000000..4265e43 --- /dev/null +++ b/README @@ -0,0 +1,9 @@ +Basically, it's a slightly lower tech version of InvisibleCastle.com + +You use the dieroller.html to roll some dice, and then, use display.rb to view the resutls. Longterm goal is to emulate most of the functionality of Invisible castle. + +Currently uses the format: +NdM for a roll of N dice with M sides. +If you add a ! to the end of the roll line, dice will reroll if they roll the largest number + +If you add a > sign followed by a number, it will assume that you want to roll successes, and will only roll dice and count the number that come up higher than the number after the > sign diff --git a/db.rb b/db.rb new file mode 100644 index 0000000..c18ba82 --- /dev/null +++ b/db.rb @@ -0,0 +1,2 @@ +require "mysql" +$my = Mysql.connect('localhost', 'dice', 'Yqv8JPzsYew34YY7', 'dieroller') diff --git a/die.rb b/die.rb new file mode 100644 index 0000000..8363a40 --- /dev/null +++ b/die.rb @@ -0,0 +1,53 @@ +def roll(rollstring) + if rollstring[rollstring.length-1] == "!" + @explode = true + rollstring = rollstring.chop + else + @explode = false + end + @token = rollstring.split("d") + @total = 0 + @retval = "" + + if @token[1].include? ">" + temp = @token[1].split(">") + @token[1] = temp[0] + diff = temp[1] + successes(@token[0].to_i, @token[1].to_i, diff.to_i) + @retval = @retval.to_s + " Successes: " + @total.to_s + else + @token[0].to_i.times do + adddie(@token[1].to_i) + end + @retval = @retval.to_s + " Total: " + @total.to_s + end + return @retval +end + +def onedie(min,max) + return(rand(max)+min) +end + +def adddie(size) + singleroll = onedie(1,size) + @retval = @retval + " " + singleroll.to_s + if @explode + if singleroll == size + adddie(size) + end + end + @total = @total.to_i + singleroll.to_i +end + +def successes(num, size, diff) + num.times do + singleroll = onedie(1,size) + @retval = @retval + " " + singleroll.to_s + if @explode + if singleroll == size + successes(1, size, diff) + end + end + @total = @total+1 if singleroll.to_i >= diff + end +end diff --git a/dieroller.html b/dieroller.html new file mode 100644 index 0000000..297254a --- /dev/null +++ b/dieroller.html @@ -0,0 +1,8 @@ + +
+

Die Roll

+

User

+

Game

+ +
+ diff --git a/display.rb b/display.rb new file mode 100755 index 0000000..0904617 --- /dev/null +++ b/display.rb @@ -0,0 +1,36 @@ +#!/usr/bin/ruby +require "cgi" +require "./db" +cgi = CGI.new +clause = "" +emptyclause = 0 +if cgi.has_key?("user") + unless cgi.params["user"][0]=="" + clause = " WHERE user='" + cgi.params["user"][0] +"'" + emptyclause = 1 + end +end +if cgi.has_key?("game") + unless cgi.params["game"][0]=="" + if emptyclause == 1 + clause = clause + " AND " + else + clause = " WHERE " + end + clause = clause + "game ='" + cgi.params['game'][0] + "'" + end +end + +print '' +print '
' +print ' User' +print ' Game' +print ' ' +print '
' +print '' + + +result = $my.query("SELECT * from rolls" + clause) +result.num_rows.times do + puts result.fetch_row.join("\s") + "
" +end diff --git a/display.rb.bk b/display.rb.bk new file mode 100755 index 0000000..4caf09e --- /dev/null +++ b/display.rb.bk @@ -0,0 +1,6 @@ +#!/usr/bin/ruby +require "./db" +result = $my.query("SELECT * from rolls") +result.num_rows.times do + puts result.fetch_row.join("\s") + "
" +end diff --git a/roller.rb b/roller.rb new file mode 100755 index 0000000..2e740dc --- /dev/null +++ b/roller.rb @@ -0,0 +1,8 @@ +#!/usr/bin/ruby +require "./db" +require "./die" +require "cgi" +cgi = CGI.new +result = roll(cgi.params["rollstring"][0].to_s) +$my.query("INSERT INTO rolls set result='" + cgi.params["rollstring"][0] + "', roll='" + result.to_s + "', user='" + cgi.params["user"][0] + "', game='" + cgi.params["game"][0] + "'") +puts result diff --git a/test.rb b/test.rb new file mode 100755 index 0000000..475bea1 --- /dev/null +++ b/test.rb @@ -0,0 +1,3 @@ +#!/usr/bin/ruby +require "./die.rb" +print roll (ARGV[0])