Advertisement:

Author Topic: Pointers for writing a dice rolling mod  (Read 534 times)

Offline JoshuaD

  • Semi-Newbie
  • *
  • Posts: 22
Pointers for writing a dice rolling mod
« on: December 23, 2019, 01:34:28 AM »
I have a strong background in programming. I've written themes and plugins for wordpress and I have written a few standalone programs.  That being said, I am not familiar with SMF yet, so I'm not sure about the best way to get started on this mod I'd like to write.

I would like to write a dice roller mod that has the following features, if possible:

  • Uses the [roll] tag
  • Saves the roll result on post somewhere (custom table in database?) so if the user edits or removes the roll  after the roll results, i can show a "roll has been edited" text.
  • Allows the user to edit the non-roll portions of the post without showing the error flag.


I'm familiar with this dice roller mod but it is very easy to cheat and doesn't allow you to edit the post at all (even to make a simple spelling correction), and it also consumes the blockquote tag, which isn't ideal.  I also don't like that it modifies the post text at the time of submission, substituting a blockquote tag in where the roll tag was.

I'll have no problem writing any necessary javascript and php, including the tag parser.  The main thing I don't know how to do is plug into SMF in the right way. Any pointers, directions, or documentation that could be helpful would be much appreciated.

Also, if this is practically not possible to do within SMF that would be useful to know as well.  Thanks!


Offline JoshuaD

  • Semi-Newbie
  • *
  • Posts: 22
Re: Pointers for writing a dice rolling mod
« Reply #2 on: January 14, 2020, 02:37:11 AM »
Two questions:

1. Is it possible to call a function where the text RESULT_GOES_HERE is?

Sources/Subs.php, line 1029:
Code: [Select]
array(
   'tag' => 'jroll',
   'type' => 'unparsed_equals',
   'before' => '<a class="dice-tool-tip" href="#"><img src="/forum/Themes/josh-theme/images/dice.png">',
   'after' => " (RESULT_GOES_HERE)<span>$1</span></a>",
   'quoted' => 'optional',
),

2. Is there a variable for the content between the tags?  I've found $1, but I'm looking for [tag]A variable with this text in it[/tag].

« Last Edit: January 14, 2020, 03:22:39 AM by JoshuaD »

Offline Looking

  • SMF Hero
  • ******
  • Posts: 2,417
  • SMF Customization
    • jeanborde on Facebook
    • SMF Custom Themes & Custom Coding
Re: Pointers for writing a dice rolling mod
« Reply #3 on: January 14, 2020, 07:06:41 AM »
JoshuaD, have you seen this: Dice Roller?

Offline JoshuaD

  • Semi-Newbie
  • *
  • Posts: 22
Re: Pointers for writing a dice rolling mod
« Reply #4 on: January 14, 2020, 12:47:09 PM »
Thank you, yes.  For my purposes, it has serious issues.

It's easy to cheat, it doesn't allow the post to be edited at all without throwing an error, it consumes the blockquote tag, and it modifies the post text submitted by the user rather than rendering a result without changing the submitted text.

The method I am trying to implement would work something like this:

1. At the time of post submission, parse the post data for roll tags and generate a roll based on that tag.

2. Put the resultant tag and roll result into the database, associated with the post.

3. At the time of viewing a post, fetch the result from the database and display it (like in my previous post, replacing the RESULT_GOES_HERE with a database fetch).

4. At the time of an edit submission, compare all dice bbc codes in the post to the data in the database. If they are identical, no problem. If they are not identical, set a flag on the post that the dice rolls may have been tampered with and show a flag on the post when displayed.

This is a completely different method than that roller uses. 

For now, if I can figure out how to make that edit in Subs, as referenced in my last post, I think I have a part of the problem solved, and I suspect I'll be able to solve the rest of it when I start playing with the post-submission code.

Offline JoshuaD

  • Semi-Newbie
  • *
  • Posts: 22
Re: Pointers for writing a dice rolling mod
« Reply #5 on: January 14, 2020, 06:05:32 PM »
I have written proof-of-concept code in Subs-Post.php in the function createPost() that:

1. Parses any present roll bbc tags.
2. Generates a random number according to the roll.
3. Stores the result and necessary information in a new table in the database, keyed by $msgOptions['id'].

The next step for me is to fetch the data from the database and insert it in the appropriate place at the time the post is displayed. 

The difficulty I'm facing is that I don't seem to have access to $msgOptions in Posts.php on line ~1026, where $codes are declared. 

Is $msgOptions available at this point, or at any other point in the page-render pipeline before emitting the page html?

Offline JoshuaD

  • Semi-Newbie
  • *
  • Posts: 22
Re: Pointers for writing a dice rolling mod
« Reply #6 on: January 14, 2020, 06:22:25 PM »
Oh sweet, the thing I need is $cache_id right there in parse_bbc's arguments.