justinburdett / playtest

A simple ruby script system that lets you quickly prototype physical card games
8 stars 1 forks source link

Corrects HTML rendering, expands RSpec coverage, refactors code slightly. #14

Closed armahillo closed 10 years ago

armahillo commented 10 years ago

This commit significantly expands the RSpec coverage. The Deck and Template models now have some rudimentary specs that cover basic functionality. I've refactored the << method in Template to be a little more streamlined, and partitioned the rendering/HTML stuff out of the << logic. It feels a little cleaner, at least.

Perhaps the most important aspect is that it now displays much better -- the 3x3 display is back up and running, using tables. I've got the render_html method re-rendering new tables every 9 cards, which helps the outlines for the cards display on each page. (this might address the "Registration marks" issue?)

justinburdett commented 10 years ago

I'm running into some annoying dependency problems because I had originally installed an old version of wkhtmltopdf (~0.8.3) - working on resolving them, but this looks good in principle :)

armahillo commented 10 years ago

The current version used by the Gemfile (from Rubygems) is v0.1.2 (http://rubygems.org/gems/wkhtmltopdf) -- based on what I see in the Github repo for their newer version, Rubygems is WAY behind. Do you think we should source it from Github instead? This appears to be the "official" repo https://github.com/antialize/wkhtmltopdf

We can change the Gemfile to do:

gem 'wkhtmltopdf', :git => "https://github.com/antialize/wkhtmltopdf"

And that should grab the HEAD version of it (which appears to be 0.12.0) if you do a bundle install again. I'll go ahead and do that; it will update this pull request. :)

armahillo commented 10 years ago

Actually, it appears that's just the binaries, not the Ruby gem... Where did you source your 0.8.3 gem from?

justinburdett commented 10 years ago

When I was first writing the script, wkhtmltopdf had a problem installing to Mavericks via homebrew. I was able to find a way to install it locally after a lot of digging, but it appears what I installed was several version out of date. I'm able to install using brew, but I'm running into some strange permissions problems now. Ever see something like this?

/Library/Ruby/Gems/2.0.0/gems/pdfkit-0.5.4/lib/pdfkit/pdfkit.rb:65:in `popen': Permission denied - /usr/local/Cellar/wkhtmltopdf/0.11.0_rc1 (Errno::EACCES)

armahillo commented 10 years ago

Hm... I'm no stranger to the Errno::EACCES exception (though usually it's an asset pipeline issue for me, in Rails). Do you have feelings one way or another about switching to a different gem library? The script uses pretty basic features of the lib and is not overly invested in it.

just checking over @ Ruby Toolbox... https://www.ruby-toolbox.com/categories/pdf_generation

Prawn seems to be all the rage right now, according to that. I've used Prawn at work for PDF generation and it's pretty decent. I don't know specifically if it would be a drop-in replacement for HTML->PDF rendering; at work I'm using it for manipulating an existing PDF (adding a watermark, mainly). If you source from the Github instance and not the Rubygems instance, it's pretty current and is being actively maintained. (https://github.com/prawnpdf/prawn)

Also -- thinking ahead... do you have thoughts about ultimately hooking into the Dev API on TheGameCrafter and having it submit the data to a project there? Alternately: We could have it render the individual cards to image files that fit the appropriate templates. I remember someone from TGC did that with Perl and it worked pretty well for them... That could be a "down the road" feature, just thinking ahead. :)

Let me know what you want to do about the PDF lib -- I think it wouldn't be too hard to switch libaries and since the PDF functionality is all encapsulated in the Template class, the only real changes we'd have to do is in the Template::to_pdf and in the Gemfile.

justinburdett commented 10 years ago

I'm not committed to using any particular PDF library. I chose PDFKit initially since it seemed the easiest to go from HTML+CSS->PDF but I did do a cursory look over at Prawn and a handful of others. Prawn seems like we would need to more strictly generate a PDF template, but does give us more control, which may actually be more important.

Re: TheGameCrafter - how'd you know? :) I'd like to make it at least an option someday, but I haven't explored their API a ton to know what that might look like.

armahillo commented 10 years ago

OK I will try out Prawn integration and see if I can get facsimile functionality with it and push it up to mine -- I'll tack it onto this PR.

Re: TGC Here's their docs https://www.thegamecrafter.com/developer/ If nothing else, if the script could generate the images that were suitable for TGC uploading, that would probably be a huge boon -- hooking into TGC's API would be bonus :)

justinburdett commented 10 years ago

You rock. Thanks! :+1:

armahillo commented 10 years ago

OK I've successfully implemented the Prawn library. It's pretty slick. Definitely a lot more complex (I spent the past few days poring over the doc references to figure out how to render things). But it works! It prints out a grid of 3x3 cards, shows the card title in bold at the top of the card, with the card body underneath it in normal weight text.

Looks pretty slick.

If/when we ever want to branch out different layouts, we should derive them from the Template class -- ie. PokerTemplate, HalfPokerTemplate, whatever... the calculations are all made dynamically, so we would just modify "render_poker_cards_to_pdf":

  ...
  pdf.define_grid(columns: COLUMNS, rows: ROWS, gutter: 0)
  ...
  pdf.grid((cards_drawn % (ROWS*COLUMNS)) / ROWS, cards_drawn % COLUMNS).bounding_box do |cell|
  ...

And I think that should do it...

The way the text renders ON the card could use some tweaking, certainly. Kinda hard to know exactly how to render the text when it's pretty flexible; but for basic prototyping purposes, this should work, I think?

justinburdett commented 10 years ago

This is awesome! Great job. Thank you.