Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.39 KB | None | 0 0
  1. <?php
  2.  
  3. /*MyBB Dice Roller - Permenant Rolls, rolls created using this plugin cannot be edited.*/
  4.  
  5. if(!defined("IN_MYBB")) { die("Unauthorized"); }
  6.  
  7. $diceroll_salt = "";
  8. $diceroll_verificationvisible = true;
  9. $diceroll_enable = false;
  10.  
  11. function diceroll_info()
  12. {
  13. return array(
  14. "name" => "TJs Dice Roll Plugin",
  15. "Description" => "A plugin that creates a permenant dice-roll to attach to posts (Rolls CANNOT be edited/deleted)",
  16. "Website" => "http://101stdivision.net",
  17. "author" => "NINTHTJ",
  18. "authorsite" => "http://101stdivision.net",
  19. "version" => "1.1",
  20. "guid" => "",
  21. "compatibility" => "18*"
  22. );
  23. }
  24.  
  25. function diceroll_activate()
  26. {
  27. global $db;
  28. $diceroll_group = array(
  29. "gid" => "",
  30. "name" => "TJsDiceRollPlugin",
  31. "title" => "Dice Roller",
  32. "description" => "Dice Roll Settings",
  33. "disporder" => "1",
  34. "isdefault" => "0"
  35. );
  36.  
  37. $db -> insert_query("settinggroups",$diceroll_group);
  38. $gid = $db -> insert_id();
  39.  
  40. $diceroll_setting = array(
  41. 'sid' => 'NULL',
  42. "name" => "diceroll_enabled",
  43. "title" => "Dice roll system enabled?",
  44. "description" => "If you set this option to yes, dice rolls will be available on your board",
  45. "optionscode" => "yesno",
  46. "value" => "1",
  47. "disporder" => 1,
  48. "gid" => intval($gid)
  49. );
  50.  
  51. $db -> insert_query("settings",$diceroll_setting);
  52.  
  53. $diceroll_setting = array(
  54. 'sid' => 'NULL',
  55. "name" => "diceroll_salt",
  56. "title" => "Dice role salt (for verification)",
  57. "description" => "Set this value to be a string only your admins know. They can use it to verify the authenticity of roles.",
  58. "optionscode" => "text",
  59. "value" => "OJISndiujn91udndu9nasdu",
  60. "disporder" => 3,
  61. "gid" => intval($gid)
  62. );
  63.  
  64. $db -> insert_query("settings",$diceroll_setting);
  65.  
  66. $diceroll_setting = array(
  67. 'sid' => 'NULL',
  68. "name" => "diceroll_verificationvisible",
  69. "title" => "Is the verification code visible?",
  70. "description" => "If set to YES, the verification code will be visible on posts",
  71. "optionscode" => "yesno",
  72. "value" => "1",
  73. "disporder" => 2,
  74. "gid" => intval($gid)
  75. );
  76.  
  77. $db -> insert_query("settings",$diceroll_setting);
  78.  
  79. rebuild_settings();
  80.  
  81. $db -> add_column("posts","diceroll","text");
  82. }
  83.  
  84. function diceroll_deactivate()
  85. {
  86. global $db;
  87. $db -> query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_enabled')");
  88. $db -> query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_salt')");
  89. $db -> query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_verificationvisible')");
  90. $db -> query("DELETE FROM " . TABLE_PREFIX . "settinggroups WHERE name = 'TJsDiceRollPlugin'");
  91.  
  92. $db -> drop_column("posts","diceroll");
  93.  
  94. rebuild_settings();
  95. }
  96.  
  97. $plugins -> add_hook("global_start","diceroll_init");
  98. function diceroll_init()
  99. {
  100. global $db;
  101. global $diceroll_salt;
  102. global $diceroll_verificationvisible;
  103. global $diceroll_enable;
  104. global $plugins;
  105. $result = $db -> query("SELECT * FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_salt')");
  106. $diceroll_salt = $db -> fetch_field($result,"value");
  107. $result = $db -> query("SELECT * FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_verificationvisible')");
  108. $diceroll_verificationvisible = $db -> fetch_field($result,"value");
  109. $result = $db -> query("SELECT * FROM " . TABLE_PREFIX . "settings WHERE name IN ('diceroll_enabled')");
  110. $diceroll_enable = $db -> fetch_field($result,"value");
  111.  
  112. if($diceroll_enable)
  113. {
  114. $plugins->add_hook("datahandler_post_insert_post", "diceroll_roll");
  115. $plugins->add_hook("datahandler_post_insert_thread_post", "diceroll_roll");
  116. $plugins -> add_hook("postbit","diceroll_appendrolls");
  117. }
  118. }
  119.  
  120. /**************************************************************************/
  121.  
  122. function diceroll_roll(&$post)
  123. {
  124. global $db;
  125. $msg = $post->post_insert_data['message'];
  126. $rollArray = array();
  127. $matches = array();
  128. preg_match_all("!\[roll([\.|\!]?) ([0-9a-z ]+) ([0-9]{1,2})k([0-9]{1,2})\]!i",$msg,$matches,PREG_SET_ORDER);
  129. // \[roll([\.|\!]?) ([0-9a-z ]+) ([0-9]{1,2})k([0-9]{1,2})([^\]]*)\]
  130. //? would \[roll([^\]]*)\] and then regex $match[1] be faster?
  131.  
  132. if($matches)
  133. {
  134. foreach($matches as $match)
  135. {
  136.  
  137. $rollType = 'Skilled';
  138.  
  139. if( $match[1] == '.')
  140. {
  141. $rollType = 'Unskilled';
  142. }
  143. else if( $match[1] == '!')
  144. {
  145. $rollType = 'Emphasis';
  146. }
  147.  
  148. $rollName = $match[2];
  149. $rolledDice = $match[3];
  150. $keptDice = $match[4];
  151. $tags = $match[5];
  152.  
  153. $bonus = 0;
  154. $explodeOn = 10;
  155.  
  156. if( $rollName && $rolledDice > 0 && $keptDice > 0 )
  157. {
  158. while( $rolledDice > 10 )
  159. {
  160. if( $keptDice <= 10 )
  161. {
  162. $rolledDice -= $rolledDice % 2;
  163.  
  164. $olledDice -= 2;
  165. $keptDice ++;
  166. }
  167. else
  168. {
  169. $rolledDice--;
  170. $bonus += 2;
  171. }
  172. }
  173.  
  174. if( $keptDice >= $rolledDice )
  175. {
  176. if( $rolledDice == 10 )
  177. {
  178. $bonus += 2 * ( $keptDice - 10);
  179. }
  180.  
  181. $keptDice = $rolledDice;
  182. }
  183.  
  184. //$rollArray[$rollName]['Tags'] = $bonus . ' ' . $rollType . ' ' . $tags;
  185.  
  186. for($roll = 0; $roll < $rolledDice; $roll ++)
  187. {
  188. $rollArray[$rollName][$keptDice][$roll] = 0;
  189.  
  190. $num = rand(1,10);
  191.  
  192. if( $rollType == 'Emphasis' && $num == 1 )
  193. {
  194. $num = rand(1,10);
  195. }
  196.  
  197. while( $rollType != 'Unskilled' && $num >= $explodeOn )
  198. {
  199. $rollArray[$rollName][$keptDice][$roll] += $num;
  200. $num = rand(1,10);
  201. }
  202.  
  203. $rollArray[$rollName][$keptDice][$roll] += $num;
  204. }
  205.  
  206. sort($rollArray[$rollName][$keptDice]);
  207. }
  208.  
  209. $replace = '['.$rollName.']';
  210. $post['message'] -> post_insert_data['message'] = str_replace($match[0],$replace,$post -> post_insert_data['message']);
  211. }
  212.  
  213. $saveString = serialize($rollArray);
  214. $post->post_insert_data['diceroll'] = $db -> escape_string($saveString);
  215. }
  216. }
  217.  
  218. function diceroll_appendrolls(&$post)
  219. {
  220. global $diceroll_salt;
  221. global $diceroll_verificationvisible;
  222.  
  223. if($post['diceroll'])
  224. {
  225. $rollArray = @unserialize($post['diceroll']);
  226.  
  227. if(is_array($rollArray))
  228. {
  229.  
  230. if(count($rollArray) > 0)
  231. {
  232. $post['message'] .= '<br /><br /><span style="color:purple;"><i>Dice rolls attached to this post:</i>';
  233.  
  234. foreach($rollArray as $rollName => $rolledPool)
  235. {
  236. //+$tags = $rollArray[$rollName]['Tags'];
  237. $rollType = 'Skilled'; //match[2] in tags
  238. $bonus = 0; //match[1] in tags
  239. $calledRaises = 0; //regex 'cr#'
  240. $freeRaises = 0; //regex 'fr#'
  241.  
  242. ///
  243. $voidSpent = 0; //regex 'v#'
  244. $explodeOn = 10; //regex 'E#'
  245. ///
  246.  
  247. foreach( $rolledPool as $keptDice => $dice )
  248. {
  249. $post['message'] .= '<br /><i>' . $rollName . ' (';
  250.  
  251. if( $rollType == 'Unskilled' )
  252. {
  253. $post['message'] .= 'Unskilled ';
  254. }
  255.  
  256. $post['message'] .= count($dice) . 'K' . $keptDice;
  257.  
  258. if( $bonus != 0 )
  259. {
  260. $post['message'] .= ' ';
  261.  
  262. if( $bonus > 0 )
  263. {
  264. $post['message'] .= '+';
  265. }
  266.  
  267. $post['message'] .= $bonus;
  268. }
  269.  
  270. if( $rollType == 'Emphasis' )
  271. {
  272. $post['message'] .= ' with Emphasis';
  273. }
  274.  
  275. if( $calledRaises > 0 )
  276. {
  277. $post['message'] .= ' ' . $calledRaises . ' called Raises';
  278. }
  279.  
  280. $post['message'] .= '): ';
  281.  
  282. $appendRolls = '';
  283. $highestKept = 0;
  284. $count = 0;
  285.  
  286. foreach( $dice as $diceNum => $rollResult )
  287. {
  288. if( $count > 0 )
  289. {
  290. $appendRolls .= ', ';
  291. }
  292.  
  293. if( $count >= count($dice) - $keptDice )
  294. {
  295. $highestKept += $rollResult;
  296. }
  297.  
  298. $appendRolls .= $rollResult;
  299. $count ++;
  300. }
  301.  
  302. $highestKept += $bonus;
  303.  
  304. if( $calledRaises > 0 )
  305. {
  306. $highestKept -= $calledRaises * 5;
  307. }
  308.  
  309. if( $count > 1 )
  310. {
  311. $appendRolls .= ' = ' . $highestKept;
  312. }
  313. else
  314. {
  315. $appendRolls = $highestKept;
  316. }
  317.  
  318. if( $freeRaises > 0 )
  319. {
  320. $appendRolls .= ' with ' . $freeRaises . ' free Raises';
  321. }
  322.  
  323. $post['message'] .= $appendRolls;
  324. }
  325.  
  326. $post['message'] .= '</i>';
  327. }
  328.  
  329. if($diceroll_verificationvisible)
  330. {
  331. $post['message'] .= '<br /><br />Verification Code: ' . sha1($diceroll_salt . $post['pid']) . '<br />';
  332. }
  333.  
  334. $post['message'] .= '</span>';
  335. }
  336. }
  337. }
  338. }
  339. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement