Advertisement
Guest User

8chan dragonraid

a guest
Oct 21st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.34 KB | None | 0 0
  1. <?php
  2. /**
  3. * DragonRaid - A prototype script to transform any /b/ thread into a dragon slaying match.
  4. *
  5. * @author Jonathan Tavares <the.entomb@gmail.com>
  6. * @license GNU General Public License, version 3
  7. * @link https://github.com/entomb/dragonchan GitHub Source
  8. * @filesource
  9. *
  10. * Modified by Stygian for use on 8chan
  11. */
  12.  
  13.  
  14. /**
  15. * Chan Boss Raid main class
  16. */
  17. Class DragonRaid{
  18. var $_version = "8";
  19.  
  20. var $THREAD_ID;
  21. var $THREAD;
  22. var $LOG;
  23. var $DPS;
  24.  
  25. var $OP;
  26. var $OPost;
  27.  
  28. var $BossHP;
  29. var $BossHP_MAX;
  30.  
  31. var $WINNER = array();
  32. var $deadPlayers = array();
  33. var $revivedStack = array();
  34. var $avengedStack = array();
  35. var $bardBuffs = array();
  36. var $bardBonusValue = 0;
  37.  
  38. //internal caches
  39. var $_cached_post_authors = array();
  40. var $_set_nicknames = array();
  41.  
  42. protected static $available_characters = array('a','b','c','d','e','f','0','1','2','3','4','5','6','7','8','9');
  43.  
  44.  
  45.  
  46. /**
  47. * CONFIGS
  48. */
  49. var $min_roll = 11;
  50. var $min_roll_enraged = 22;
  51. var $max_revive_times = 6;
  52. var $max_avenge_times = 6;
  53. var $bard_buff_duration = 3;
  54. var $boss_hp_factor = 300;
  55. var $boss_heal_factor = 30;
  56. var $boss_enrage_percent = 0.2;
  57. var $critical_hit_ratio = 2;
  58. var $burst_hit_ratio = 4;
  59.  
  60. var $available_elements = array(
  61. // key, Friendly Name
  62. 'electric',
  63. 'fire',
  64. 'water',
  65. 'earth',
  66. 'ice',
  67. );
  68.  
  69. var $element_weakness = array(
  70. // water is weak to electric
  71. 'electric' => 'earth',
  72. 'fire' => 'water',
  73. 'water' => 'electric',
  74. 'earth' => 'ice',
  75. 'ice' => 'fire'
  76. );
  77.  
  78. var $warlock_minions = array(
  79. 'electric' => 'Eletric Golem',
  80. 'water' => 'Water Golem',
  81. 'fire' => 'Fire Golem',
  82. 'earth' => 'Earth Golem',
  83. 'ice' => 'Frost Golem',
  84. );
  85.  
  86. // Less than 22 = death while enraged
  87. var $_miss_hits = array('23','51','71');
  88.  
  89. /**
  90. * Init functions
  91. * @param array $_parsed_thread Parsed thread from the json API
  92. */
  93. function __construct($_parsed_thread){
  94.  
  95.  
  96. $this->THREAD = $_parsed_thread;
  97. $this->OPost = $this->THREAD->posts[0];
  98. $this->OP = $this->OPost->id;
  99. $this->THREAD_ID = $this->OPost->no;
  100.  
  101.  
  102. /* Uncomment to force dev variables
  103. $this->OPost->com = "name@LichKing
  104. difficulty@hard
  105. element@ice
  106. health@19900";
  107. */
  108.  
  109. //boss status
  110. $this->BossIMG = "http://8chan.co/dragonraid/thumb/".$this->OPost->tim.".png";
  111. $this->setBossDifficulty('easy');
  112. $this->BossElement = self::getBossElement($this->OPost->no);
  113. $this->BossName = "RandomBeast";
  114.  
  115. /**
  116. * OP COMMANDS
  117. */
  118.  
  119. // OP's commands should be case sensitive, like the boss name
  120. $this->OPost->commands = self::parseCommandValues($this->OPost, true);
  121.  
  122. //set OP options [difficulty@]
  123. if($_difficulty = self::checkForCommand('difficulty@',$this->OPost)){
  124. $this->setBossDifficulty($_difficulty);
  125. }
  126.  
  127. //set OP options [name@]
  128. if($_name = self::checkForCommand('name@',$this->OPost)){
  129. $this->BossName = $_name;
  130. }
  131.  
  132. //set OP options [element@]
  133. if($_element = self::checkForCommand('element@',$this->OPost)){
  134. if(in_array($_element,$this->available_elements)){
  135. $this->BossElement = $_element;
  136. }
  137. }
  138.  
  139. //set OP options [health@]
  140. if($_health = self::checkForCommand('health@',$this->OPost)){
  141. $this->setBossHealth((int)$_health);
  142. }
  143.  
  144. }
  145.  
  146.  
  147. /**
  148. * main function cycle
  149. * Does all combat checks and assigns
  150. * Skips invalid posts
  151. * @return void
  152. */
  153. function play(){
  154.  
  155. foreach($this->THREAD->posts as $post){
  156. //save the post author for later
  157. $this->_cached_post_authors[$post->no] = $post->id;
  158.  
  159. //ignore OP first post
  160. if($post->no==$this->THREAD_ID){
  161. continue;
  162. }
  163.  
  164. //boss is already dead!
  165. if($this->BossHP<=0){
  166. continue;
  167. }
  168.  
  169. // Dev variables
  170. /*
  171. $post->com = "summon@fire";
  172. */
  173.  
  174.  
  175. //get the current player class
  176. $post->class = self::getPlayerClass($post->id);
  177.  
  178. //parse post commands
  179. $post->commands = self::parseCommandValues($post);
  180.  
  181. //check and set nickname from command
  182. if($nickname = self::checkForCommand('nickname@',$post)){
  183. $this->setNickname($post->id,$nickname);
  184. }
  185.  
  186. //ignore fallen players with the exeption of deadknights
  187. if($this->isDeadPlayer($post->id) && !in_array($post->class,array('DK','DVK'))){
  188. continue;
  189. }
  190.  
  191. //gets the current bard buff value
  192. $this->bardBonusValue = $this->calculateBardBonus();
  193.  
  194. //add link to this roll
  195. $post->link= "http://8chan.co/dragonraid/res/".$this->THREAD_ID."#p".$post->no;
  196.  
  197. //GET THE CURRENT ROLL
  198. $post->roll = self::roll($post->no,2);
  199.  
  200. //mandatory data (that might not be on the API item)
  201. $post->com = isset($post->com) ? $post->com : "";
  202. $post->tim = isset($post->tim) ? $post->tim : "";
  203.  
  204. // Default an element
  205. $post->chosen_element = "normal";
  206.  
  207. //mass resurection and damage
  208. if($post->roll>99){
  209. $this->damage($post,false);
  210. $this->massResurection($post);
  211. if($this->bossIsDead()){
  212. $this->WINNER = $post;
  213. $action = 'winrar';
  214. $post->action = $action;
  215. $this->log($action,$post);
  216. }
  217. continue;
  218. }
  219.  
  220. //mass resurection but no damage
  221. if($post->roll==69){
  222. $this->massResurection($post);
  223. continue;
  224. }
  225.  
  226. //enrage the boss (only once)
  227. if($this->bossIsEnraged() && $this->min_roll!=$this->min_roll_enraged){
  228. $this->min_roll = $this->min_roll_enraged;
  229. $action = 'enrage';
  230. $post->action = $action;
  231. $this->log($action,$post);
  232. }
  233.  
  234. //death roll!
  235. if($post->roll<$this->min_roll && !$this->isDeadPlayer($post->id)){
  236. $action = "kill";
  237. $post->action = $action;
  238. $this->killPlayer($post);
  239. continue;
  240. }
  241.  
  242. //miss hit!
  243. if(in_array(self::roll($post->time),$this->_miss_hits)){
  244. $action = 'miss';
  245. $post->action = $action;
  246. $this->log($action,$post);
  247. continue;
  248. }
  249.  
  250.  
  251. //calculate regular hit
  252. $this->damage($post);
  253.  
  254. //add bard buff!
  255. if($post->class=='B' && isset($post->filename)){
  256. $this->addBardBuff($post);
  257. }
  258.  
  259. //special hit with target
  260. if($post->roll%2==0){
  261.  
  262. //avenges and revives
  263. $_targets = $this->getTargetPosts($post->com);
  264. foreach($_targets as $_target_post_id => $_target_id){
  265.  
  266. //only dead target's post
  267. if(self::roll($_target_post_id)>=$this->min_roll) continue;
  268.  
  269. //only if poster is alive (DVK)
  270. if($this->isDeadPlayer($post->id)) continue;
  271.  
  272. //avenger!
  273. if(in_array($post->class,array('K','P','DVK'))){
  274. //knight, paladin and dragonborn
  275. if($this->isDeadPlayer($_target_id) && $this->canAvenge($_target_id)){
  276. $this->damage($post,true,false);
  277. $this->avengePlayer($_target_id);
  278. $post->_target = $_target_id;
  279. $action = 'avenge';
  280. $post->action = $action;
  281. $this->log($action,$post);
  282. }
  283. }
  284.  
  285. //Reviver!
  286. if(in_array($post->class,array('H','P','DVK'))){
  287. //Healer, paladin and dragonborn
  288. if($this->isDeadPlayer($_target_id) && $this->canRevive($_target_id)){
  289. $post->_target = $_target_id;
  290. $this->revivePlayer($_target_id);
  291. $action = 'revive';
  292. $post->action = $action;
  293. $this->log($action,$post);
  294. }
  295. }
  296. }
  297. }
  298.  
  299. if($this->bossIsDead()){
  300. $action = 'winrar';
  301. $this->WINNER = $post;
  302. $post->action = $action;
  303. $this->log($action,$post);
  304. }
  305.  
  306.  
  307. }
  308.  
  309.  
  310. }
  311.  
  312.  
  313. /**
  314. * Checks if a player is dead
  315. * @param string $_id Player ID
  316. * @return boolean
  317. */
  318. function isDeadPlayer($_id){
  319. return in_array($_id, $this->deadPlayers);
  320. }
  321.  
  322. /**
  323. * Set boss difficulty
  324. * @param string $difficulty preg_matched difficulty
  325. * @return null
  326. */
  327. function setBossDifficulty($difficulty){
  328. switch ($difficulty) {
  329. case 'noob':
  330. $boss_min_hp = self::roll($this->OPost->no)*$this->boss_hp_factor;
  331. $this->BossHP_MAX = ($boss_min_hp < 6000 ? 6000 : $boss_min_hp);
  332. $this->BossHP_MAX = ($boss_min_hp > 16000 ? 16000 : $boss_min_hp);
  333. $this->BossHP = $this->BossHP_MAX;
  334. $this->boss_heal_factor = 30;
  335. break;
  336. default:
  337. case 'easy':
  338. $boss_min_hp = self::roll($this->OPost->no)*$this->boss_hp_factor;
  339. $this->BossHP_MAX = ($boss_min_hp < 16000 ? 16000 : $boss_min_hp);
  340. $this->BossHP = $this->BossHP_MAX;
  341. $this->boss_heal_factor = 33;
  342. break;
  343. case 'medium':
  344. $boss_min_hp = self::roll($this->OPost->no)*$this->boss_hp_factor*1.5;
  345. $this->BossHP_MAX = ($boss_min_hp < 22000 ? 22000 : $boss_min_hp);
  346. $this->BossHP = $this->BossHP_MAX;
  347. $this->boss_heal_factor = 35;
  348. break;
  349. case 'hard':
  350. $boss_min_hp = self::roll($this->OPost->no)*$this->boss_hp_factor*2;
  351. $this->BossHP_MAX = ($boss_min_hp < 27000 ? 27000 : $boss_min_hp);
  352. $this->BossHP = $this->BossHP_MAX;
  353. $this->boss_heal_factor = 38;
  354. break;
  355. case 'nigger':
  356. $this->BossHP_MAX = 66666;
  357. $this->BossHP = $this->BossHP_MAX;
  358. $this->boss_heal_factor = 20;
  359. break;
  360. }
  361.  
  362. }
  363.  
  364. /**
  365. * Overrides boss's health
  366. * @param string $health preg_matched health
  367. */
  368. function setBossHealth($health){
  369. $this->BossHP_MAX = ($health > 27000 ? 27000 : $health);
  370. $this->BossHP = $this->BossHP_MAX;
  371. }
  372.  
  373. function getBossElement($id) {
  374.  
  375. $last_digit = substr($id, -1);
  376. $element = "normal";
  377.  
  378. /*
  379. 'electric' => 'Electric',
  380. 'fire' => 'Fire',
  381. 'water' => 'Water',
  382. 'earth' => 'Earth',
  383. 'ice' => 'Ice'
  384.  
  385. */
  386.  
  387. switch ($last_digit) {
  388.  
  389. case 0:
  390. $element = $this->available_elements[0];
  391. break;
  392. case 1:
  393. $element = $this->available_elements[0];
  394. break;
  395. case 2:
  396. $element = $this->available_elements[1];
  397. break;
  398. case 3:
  399. $element = $this->available_elements[1];
  400. break;
  401. case 4:
  402. $element = $this->available_elements[2];
  403. break;
  404. case 5:
  405. $element = $this->available_elements[2];
  406. break;
  407. case 6:
  408. $element = $this->available_elements[3];
  409. break;
  410. case 7:
  411. $element = $this->available_elements[3];
  412. break;
  413. case 8:
  414. $element = $this->available_elements[4];
  415. break;
  416. case 9:
  417. $element = $this->available_elements[4];
  418. break;
  419. }
  420.  
  421. return $element;
  422. }
  423.  
  424.  
  425. /**
  426. * Calculates and returns the current bard bonus
  427. * @return int Bard damage bonus
  428. */
  429. function calculateBardBonus(){
  430. $bonus = 0;
  431. foreach($this->bardBuffs as $k => $buff){
  432. if(0>$this->bardBuffs[$k]['duration']--){
  433. $this->bardBuffs[$k]['duration']=0;
  434. }
  435. if($this->bardBuffs[$k]['duration']>0){
  436. $bonus+=$this->bardBuffs[$k]['value'];
  437. }
  438. }
  439.  
  440. return $bonus;
  441. }
  442.  
  443.  
  444. /**
  445. * Adds a bard buff to the stack
  446. * @param object $post the full post object
  447. */
  448. function addBardBuff($post){
  449. $post->bonus = ceil($post->roll/3);
  450. $this->bardBuffs[] = array(
  451. 'duration' => $this->bard_buff_duration+1,
  452. 'value' => $post->bonus,
  453. 'buffer' => $post,
  454. );
  455. $action = 'buff';
  456. $post->action = $action;
  457. $this->log($action,$post);
  458. }
  459.  
  460.  
  461. /**
  462. * Applies damage to the boss
  463. * @param object $post the full post object
  464. * @param boolean $canCritical if this attack can critical
  465. * @param boolean $reportDamage if this damage should be reported
  466. * @return void
  467. */
  468. function damage($post,$canCritical=true,$reportDamage=true){
  469.  
  470. $chosen_element = $post->chosen_element;
  471.  
  472. //define damage
  473. if( ($post->class=='K') && $canCritical && self::isCriticalHit($post->roll)){
  474. $post->damage = $post->roll*$this->critical_hit_ratio;
  475. }else{
  476. $post->damage = $post->roll;
  477. }
  478.  
  479. if($post->roll<=99){
  480. $post->bonus = $this->bardBonusValue;
  481.  
  482. //warlock pet damage
  483. if($post->class=="W" && isset($post->filename)){
  484.  
  485. $_pet_damage = self::roll($post->tim);
  486.  
  487. //warlock burst (only if not 00)
  488. if($_pet_damage<99 && self::lastDigitMatch($post->no,$post->tim)){
  489. $_pet_damage = $_pet_damage*$this->burst_hit_ratio;
  490. }
  491.  
  492. // Give W's a possible elemental damage for their pets
  493. // Maybe break out into a summoner class?
  494.  
  495. if($_summon = self::checkForCommand('summon@',$post)){
  496. $_summon_element = $_summon;
  497. }
  498.  
  499. foreach($this->available_elements as $element) {
  500. if(!empty($_summon_element) && $_summon_element == $element) {
  501. $chosen_element = $element;
  502. $post->chosen_element = $chosen_element;
  503. break;
  504. }
  505. }
  506.  
  507. // Give the pet bonus damage if it's the boss's weakness
  508. if($this->element_weakness[$this->BossElement] == $chosen_element) {
  509. $_pet_damage = ceil($_pet_damage * 1.5);
  510. }
  511. elseif($this->BossElement == $chosen_element) {
  512. // If the element is the same as the boss, make him resistant
  513. $_pet_damage = ceil($_pet_damage * .5);
  514. }
  515.  
  516. $post->_pet_damage = $_pet_damage;
  517. $post->bonus+=$_pet_damage;
  518. }
  519.  
  520. //death knight death bonus
  521. if($post->class=="DK" || $post->class=="DVK"){
  522. if($this->isDeadPlayer($post->id)){
  523. $post->bonus+= $post->damage;
  524. }elseif($post->class=="DK"){
  525. //only DK have life penalty
  526. $post->bonus-= floor($post->damage/3);
  527. }
  528. }
  529.  
  530. if($post->class=="R"){
  531. switch (self::lastDigit($post->roll)) {
  532. case 0: $post->damage*=1; break;
  533. case 1: $post->damage*=1; break;
  534. case 2: $post->damage*=2; break;
  535. case 3: $post->damage*=3; break;
  536. case 4: $post->damage*=4; break;
  537. case 5: $post->damage*=5; break;
  538. case 6: $post->damage*=4; break;
  539. case 7: $post->damage*=3; break;
  540. case 8: $post->damage*=1; break;
  541. case 9: $post->damage*=0; break;
  542. }
  543. }
  544.  
  545. }else{
  546. $post->bonus = 0;
  547. }
  548.  
  549. //OVER NINETHOUSAND!
  550. if($post->damage===9000){
  551. $post->damage++;
  552. }
  553.  
  554. //take the damage
  555. $this->BossHP-= ($post->damage+$post->bonus);
  556.  
  557. //log the hit
  558. if($reportDamage){
  559. $action = 'damage';
  560. $post->action = $action;
  561. $this->log($action,$post);
  562. }
  563. }
  564.  
  565.  
  566. /**
  567. * Does a mass resurection
  568. * @param object $post the full post object
  569. * @return void
  570. */
  571. function massResurection($post){
  572. //clean dead players!
  573. foreach($this->deadPlayers as $_target_id){
  574. $post->_target = $_target_id;
  575. $action = 'revive';
  576. $post->action = $action;
  577. $this->log($action,$post);
  578. }
  579.  
  580. $this->deadPlayers = array();
  581.  
  582. //log the hit
  583. $action = 'massrevive';
  584. $post->action = $action;
  585. $this->log($action,$post);
  586. }
  587.  
  588.  
  589. /**
  590. * Kills a user and heals the boss
  591. * @param object $post the full post object
  592. * @return void
  593. */
  594. function killPlayer($post){
  595. //add player to the dead player poll
  596. $this->deadPlayers[] = $post->id;
  597.  
  598. if(!$this->bossIsEnraged()){
  599. //heal the boss
  600. $_heal = ($post->roll*$this->boss_heal_factor);
  601. $this->BossHP+=$_heal;
  602. $post->damage=-$_heal;
  603. //limit the heal
  604. if($this->BossHP>$this->BossHP_MAX){
  605. $this->BossHP = $this->BossHP_MAX;
  606. }
  607. }
  608.  
  609. //log the death
  610. $action = 'death';
  611. $post->action = $action;
  612. $this->log($action,$post);
  613. }
  614.  
  615.  
  616. /**
  617. * Avenges a user
  618. * @param string $avenge_target Dead user id
  619. * @return void
  620. */
  621. function avengePlayer($avenge_target){
  622. if(!isset($this->avengedStack[$avenge_target])){
  623. $this->avengedStack[$avenge_target] = 0;
  624. }
  625. $this->avengedStack[$avenge_target]++;
  626. }
  627.  
  628.  
  629. /**
  630. * Revives a user
  631. * @param string $revive_target the dead user ID
  632. * @return bool
  633. */
  634. function revivePlayer($revive_target){
  635. foreach($this->deadPlayers as $key => $_id){
  636. if($_id == $revive_target){
  637. if(!isset($this->revivedStack[$revive_target])){
  638. $this->revivedStack[$revive_target] = 0;
  639. }
  640. $this->revivedStack[$revive_target]++;
  641. $this->deadPlayers[$key] = null;
  642. unset($this->deadPlayers[$key]);
  643. }
  644. }
  645. }
  646.  
  647.  
  648. /**
  649. * Checks if a target can be revived
  650. * @param string $avenge_target the dead user ID
  651. * @return bool
  652. */
  653. function canRevive($revive_target){
  654. if(isset($this->revivedStack[$revive_target])){
  655. return (bool)($this->revivedStack[$revive_target]<$this->max_revive_times);
  656. }else{
  657. $this->revivedStack[$revive_target]=0;
  658. return true;
  659. }
  660. }
  661.  
  662.  
  663. /**
  664. * Checks if a target can be avenged
  665. * @param string $avenge_target the dead user ID
  666. * @return bool
  667. */
  668. function canAvenge($avenge_target){
  669. if(isset($this->avengedStack[$avenge_target])){
  670. return (bool)($this->avengedStack[$avenge_target]<$this->max_avenge_times);
  671. }else{
  672. $this->avengedStack[$avenge_target]=0;
  673. return true;
  674. }
  675. }
  676.  
  677.  
  678. /**
  679. * sets a nickname for a user
  680. * @param string $user_id the user ID to map
  681. * @param string $nickname the desired nickname
  682. */
  683. function setNickname($user_id,$nickname){
  684.  
  685. // Stop fucking making names a dickyear long
  686. if(strlen($nickname) > 14) {
  687. $nickname = substr($nickname, 0, 14);
  688. }
  689.  
  690. if($nickname=="heaven"){
  691. return false; //fuck off
  692. }
  693. if(in_array($nickname,$this->_set_nicknames)){
  694. //was already used
  695. return false;
  696. }
  697. //set the nick
  698. $this->_set_nicknames[$user_id] = $nickname;
  699. return true;
  700. }
  701.  
  702.  
  703. /*
  704. * gets a previously set nickname
  705. * @param string $user_id the user to search for
  706. * @return string the nickname to use
  707. */
  708. function getNickname($user_id){
  709. if(isset($this->_set_nicknames[$user_id])){
  710. return $this->_set_nicknames[$user_id]." <small>($user_id)</small>";
  711. //return "<b title='$user_id'>".$this->_set_nicknames[$user_id]."</b>";
  712. }else{
  713. return "<small>($user_id)</small>";
  714. }
  715. }
  716.  
  717.  
  718. /**
  719. * Logs an action
  720. * @param string $action the type of action ('damage','revive','massrevive',etc)
  721. * @param object $post the full post object
  722. * @return void
  723. */
  724. function log($action,$post){
  725. $this->LOG[] = array(
  726. 'link' => $post->link,
  727. 'post' => $post->no,
  728. 'id' => $post->id,
  729. 'sprite' => self::getPlayerSprite($post),
  730. 'weapon' => self::getPlayerWeapon($post),
  731. 'chosen_element' => $post->chosen_element,
  732. '_pet_damage' => isset($post->_pet_damage) ? $post->_pet_damage : false,
  733. 'roll' => $post->roll,
  734. 'class' => $post->class,
  735. 'action' => $action,
  736. 'target' => isset($post->_target) ? $post->_target : 0,
  737. 'damage' => isset($post->damage) ? $post->damage : 0,
  738. 'bonus' => isset($post->bonus) ? $post->bonus : 0,
  739. );
  740. }
  741.  
  742.  
  743. /**
  744. * Calculates and returns the top 10 damage dealers
  745. * @return array
  746. */
  747. function getTopDamage($max=10){
  748. $TOP = array();
  749. foreach($this->LOG as $_hit){
  750. if($_hit['action']=='damage' || $_hit['action']=='avenge'){
  751. if(!isset($TOP[$_hit['id']])){
  752. $TOP[$_hit['id']] = 0;
  753. }
  754. $TOP[$_hit['id']]+= (int)$_hit['damage']+$_hit['bonus'];
  755. }
  756. }
  757.  
  758. arsort($TOP);
  759. $TOP = array_slice($TOP,0,$max,true);
  760. return $TOP;
  761. }
  762.  
  763.  
  764. /**
  765. * Calculates and returns the top revivers list
  766. * @return array
  767. */
  768. function getTopRevive($max=10){
  769. $TOP = array();
  770. foreach($this->LOG as $_hit){
  771. if($_hit['action']=='revive'){
  772. if(!isset($TOP[$_hit['id']])){
  773. $TOP[$_hit['id']] = 0;
  774. }
  775. $TOP[$_hit['id']]++;
  776. }
  777. }
  778.  
  779. arsort($TOP);
  780. $TOP = array_slice($TOP,0,$max,true);
  781. return $TOP;
  782. }
  783.  
  784.  
  785. /**
  786. * Calculates and returs the top avengers list
  787. * @return array
  788. */
  789. function getTopAvenge($max=10){
  790. $TOP = array();
  791. foreach($this->LOG as $_hit){
  792. if($_hit['action']=='avenge'){
  793. if(!isset($TOP[$_hit['id']])){
  794. $TOP[$_hit['id']] = 0;
  795. }
  796. $TOP[$_hit['id']]++;
  797. }
  798. }
  799.  
  800. arsort($TOP);
  801. $TOP = array_slice($TOP,0,$max,true);
  802. return $TOP;
  803. }
  804.  
  805. /**
  806. * Calculates and returs the top avengers list
  807. * @return array
  808. */
  809. function getTopDeaths($max=10){
  810. $TOP = array();
  811. foreach($this->LOG as $_hit){
  812. if($_hit['action']=='death'){
  813. if(!isset($TOP[$_hit['id']])){
  814. $TOP[$_hit['id']] = 0;
  815. }
  816. $TOP[$_hit['id']]++;
  817. }
  818. }
  819.  
  820. arsort($TOP);
  821. $TOP = array_slice($TOP,0,$max,true);
  822. return $TOP;
  823. }
  824.  
  825.  
  826. /**
  827. * Calculates and returs the top avengers list
  828. * @return array
  829. */
  830. function getTopBuffs($max=10){
  831. $TOP = array();
  832. foreach($this->LOG as $_hit){
  833. if($_hit['action']=='buff'){
  834. if(!isset($TOP[$_hit['id']])){
  835. $TOP[$_hit['id']] = 0;
  836. }
  837. $TOP[$_hit['id']]+=($_hit['bonus']*$this->bard_buff_duration);
  838. }
  839. }
  840.  
  841. arsort($TOP);
  842. $TOP = array_slice($TOP,0,$max,true);
  843. return $TOP;
  844. }
  845.  
  846.  
  847. /**
  848. * Calls the main fight template
  849. * @return void
  850. */
  851. function display(){
  852. $topDamage = $this->getTopDamage();
  853. $topRevive = $this->getTopRevive();
  854. $topAvenge = $this->getTopAvenge();
  855. $topDeaths = $this->getTopDeaths();
  856. $topBuffs = $this->getTopBuffs();
  857.  
  858. $BATTLE = &$this->LOG;
  859. $BATTLE = array_reverse($BATTLE);
  860. //template goes here
  861. include("template/fight.tpl");
  862. }
  863.  
  864. /**
  865. * Calls the status iframe template
  866. * @return void
  867. */
  868. function displayStatus(){
  869. $topDamage = $this->getTopDamage(3);
  870.  
  871. $BATTLE = &$this->LOG;
  872. $BATTLE = array_reverse($BATTLE);
  873. //template goes here
  874. include("template/status.tpl");
  875. }
  876. /**
  877. * Calls the status iframe template
  878. * @return void
  879. */
  880. function displayStatusAjax(){
  881. $topDamage = $this->getTopDamage(3);
  882.  
  883. $BATTLE = &$this->LOG;
  884. $BATTLE = array_reverse($BATTLE);
  885.  
  886. //template goes here
  887. include("template/status_core.tpl");
  888. }
  889.  
  890.  
  891. /**
  892. * Prints a complete json string with all game informaion.
  893. * @return [type] [description]
  894. */
  895. function jsonAPI(){
  896. $this->topDamage = $this->getTopDamage();
  897. $this->topRevive = $this->getTopRevive();
  898. $this->topAvenge = $this->getTopAvenge();
  899. $this->topDeaths = $this->getTopDeaths();
  900. $this->topBuffs = $this->getTopBuffs();
  901.  
  902. $this->LOG = array_reverse($this->LOG);
  903.  
  904. header('Cache-Control: no-cache, must-revalidate');
  905. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  906. header('Content-type: application/json');
  907.  
  908. echo json_encode($this);
  909. }
  910.  
  911.  
  912. /**
  913. * Checks if the boss is dead
  914. * @return bool
  915. */
  916. function bossIsDead(){
  917. if($this->BossHP<0){
  918. $this->BossHP = 0;
  919. }
  920. return (bool)($this->BossHP<=0);
  921. }
  922.  
  923.  
  924. /**
  925. * Checks if the boss is enrages depending on self::boss_enrage_percent
  926. * @return bool
  927. */
  928. function bossIsEnraged(){
  929. return (bool)($this->BossHP<=$this->BossHP_MAX*$this->boss_enrage_percent);
  930. }
  931.  
  932.  
  933.  
  934.  
  935. //**********************************************************
  936. // STATIC CALLS
  937. //**********************************************************
  938.  
  939. /**
  940. * get the roll number. if posts ends in 00 it will expand 1 digit until we end up with >0 number (ex 2000)
  941. * @param string $post_number Post number
  942. * @param integer $num roll size, default: 2 (last 2 digits)
  943. * @return int Roll number
  944. */
  945.  
  946.  
  947. // THIS PARSES POST NUMBER FOR ROLL -Stygian
  948. // static function roll($post_number,$num=2){
  949. // $r = (int)substr($post_number, strlen($post_number)-$num,strlen($post_number));
  950. // while($r==0){
  951. // //dubs dubs dubs
  952. // $r = (int)substr($post_number, strlen($post_number)-$num++,strlen($post_number));
  953. // }
  954. // return $r;
  955. // }
  956.  
  957. static function roll($post_number,$num=2){
  958. //$r = (int)substr($post_number, strlen($post_number)-$num,strlen($post_number));
  959. $r = rand(0, 99);
  960. while($r==0){
  961. //dubs dubs dubs
  962. $r = 100;
  963. }
  964. return $r;
  965. }
  966.  
  967. /**
  968. * Checks if the last digit of 2 numbers match
  969. * @param int|string $num1 First Number
  970. * @param int|string $num2 Second Number
  971. * @return bool
  972. */
  973. static function lastDigitMatch($num1,$num2){
  974. return (bool)(self::lastDigit($num1) == self::lastDigit($num2));
  975. }
  976. /**
  977. * Checks if the last digit of a numbers
  978. * @param int|string $num1 Number
  979. * @return bool
  980. */
  981. static function lastDigit($num1){
  982. return (int)(substr((string)$num1,-1, 1));
  983. }
  984.  
  985. /**
  986. * Gets the class of a player based on his ID
  987. * @param string $post_id Player ID
  988. * @return string ['H','B','P','K']
  989. */
  990. static function getPlayerClass($post_id){
  991.  
  992. //dragonborn Super Rare (Starts and ends with 0)
  993. if(in_array($post_id[0],array('0')) && in_array($post_id[5],array('0'))){
  994. return "DVK";
  995. }
  996.  
  997. //paladin (If starts with 0)
  998. if(in_array($post_id[0],array('0'))){
  999. return "P";
  1000. }
  1001.  
  1002. //death knight (if ends with 0)
  1003. if(in_array($post_id[7],array('0'))){
  1004. return "DK";
  1005. }
  1006.  
  1007. //healer (If starts with 1, 2, 3)
  1008. if(in_array($post_id[0],array('1','2','3'))){
  1009. return "H";
  1010. }
  1011.  
  1012. //bard (If starts with 4, 5, 6)
  1013. if(in_array($post_id[0],array('4','5','6'))){
  1014. return "B";
  1015. }
  1016.  
  1017. //warlock (If starts with 7, 8, 9)
  1018. if(in_array($post_id[0],array('7','8','9'))){
  1019. return "W";
  1020. }
  1021.  
  1022. //ranger (If starts with a, b)
  1023. if(in_array($post_id[0],array('a','b'))){
  1024. return "R";
  1025. }
  1026.  
  1027. //knight (If starts with c, d, e, f)
  1028. return "K";
  1029. }
  1030.  
  1031.  
  1032. /*
  1033. * Gets the sprite of a player based on his ID
  1034. * @param string $post_id Player ID
  1035. * @return string "male_knight_1.png"
  1036. */
  1037.  
  1038. static function getPlayerSprite($post){
  1039. // ** There's a better way to do this, but let's slap something neat together for now.
  1040.  
  1041. // Let's set some variables we can expect later
  1042. $sprite = "";
  1043. $segment = "1";
  1044. $class = $post->class;
  1045. $post_id = $post->id;
  1046.  
  1047. // 64 variations
  1048. $range = self::$available_characters;
  1049.  
  1050.  
  1051. // Knight
  1052. if($class == "K"){
  1053. $segment_range = array_chunk($range, 2);
  1054. }
  1055.  
  1056. // Healer
  1057. if($class == "H"){
  1058. $segment_range = array_chunk($range, 2);
  1059. }
  1060.  
  1061. // Warlock
  1062. if($class == "W"){
  1063. $segment_range = array_chunk($range, 2);
  1064. }
  1065.  
  1066. // Bard
  1067. if($class == "B"){
  1068. $segment_range = array_chunk($range, 3);
  1069. }
  1070.  
  1071. // Paladin
  1072. if($class == "P"){
  1073. $segment_range = array_chunk($range, 2);
  1074. }
  1075.  
  1076. // Death knight
  1077. if($class == "DK"){
  1078. $segment_range = array_chunk($range, 32);
  1079. }
  1080.  
  1081. // Dragonborn
  1082. if($class == "DVK"){
  1083. $segment_range = array_chunk($range, 5);
  1084. }
  1085.  
  1086. // Ranger
  1087. if($class == "R"){
  1088. $segment_range = array_chunk($range, 5);
  1089. }
  1090.  
  1091. $segment = array_tree_search_key($segment_range, $post_id[1]);
  1092.  
  1093. $sprite .= $class . "/" . $class . "_" . $segment . ".png";
  1094.  
  1095. return $sprite;
  1096. }
  1097.  
  1098.  
  1099. static function getPlayerWeapon($post){
  1100. // ** There's a better way to do this, but let's slap something neat together for now.
  1101.  
  1102. // Let's set some variables we can expect later
  1103. $sprite = "";
  1104. $segment = "1";
  1105. $class = $post->class;
  1106. $post_id = $post->id;
  1107.  
  1108. // 64 variations
  1109. $range = self::$available_characters;
  1110.  
  1111. // Knight
  1112. if($class == "K"){
  1113. $segment_range = array_chunk($range, 1);
  1114. }
  1115.  
  1116. // Healer
  1117. if($class == "H"){
  1118. $segment_range = array_chunk($range, 2);
  1119. }
  1120.  
  1121. // Warlock
  1122. if($class == "W"){
  1123. $segment_range = array_chunk($range, 3);
  1124. }
  1125.  
  1126. // Bard
  1127. if($class == "B"){
  1128. $segment_range = array_chunk($range, 5);
  1129. }
  1130.  
  1131. // Paladin
  1132. if($class == "P"){
  1133. $segment_range = array_chunk($range, 3);
  1134. }
  1135.  
  1136. // Death knight
  1137. if($class == "DK"){
  1138. $segment_range = array_chunk($range, 3);
  1139. }
  1140.  
  1141. // Dragonborn
  1142. if($class == "DVK"){
  1143. $segment_range = array_chunk($range, 5);
  1144. }
  1145.  
  1146. // Ranger
  1147. if($class == "R"){
  1148. $segment_range = array_chunk($range, 5);
  1149. }
  1150.  
  1151. // Last character of ID, instead of the first
  1152. $segment = array_tree_search_key($segment_range, $post_id[1]);
  1153.  
  1154. $sprite .= $class . "/" . $class . "_" . $segment . ".png";
  1155.  
  1156. return $sprite;
  1157. }
  1158.  
  1159. /**
  1160. * Gets the roll critical status.
  1161. * will return true for numbers ending in 5 or 0 (defined by $this->critical_hit_mod)
  1162. * @param int $num Roll digits from self::roll()
  1163. * @return bool
  1164. */
  1165. static function isCriticalHit($num){
  1166. if($num%5== 0){
  1167. return true;
  1168. }else{
  1169. return false;
  1170. }
  1171. }
  1172.  
  1173. /**
  1174. * Parses a post to check for command calls
  1175. * @param object $post the full post object
  1176. * @return array full command list
  1177. */
  1178. static function parseCommandValues($post, $case_sensitive = false){
  1179. if(!isset($post->com)){
  1180. return array();
  1181. }
  1182.  
  1183. if(empty($post->com)){
  1184. return array();
  1185. }
  1186.  
  1187. $_text = $post->com;
  1188. if($case_sensitive == false) {
  1189. $_text = strtolower($post->com);
  1190. }
  1191. $_text = strip_tags($_text,"<br>");
  1192. $_text = str_replace("<br>", " ", $_text);
  1193. $_text = str_replace("<br/>", " ", $_text);
  1194.  
  1195. $_commands = array();
  1196. $_command_regex = '/(\w+@)([a-zA-Z0-9\S]{3,15})/i';
  1197. if($count_commands = (int)preg_match_all($_command_regex, $_text, $match)){
  1198. for($k=0;$k<$count_commands;$k++){
  1199. $_commands[$match[1][$k]] = $match[2][$k];
  1200. }
  1201. }
  1202.  
  1203. return $_commands;
  1204.  
  1205. }
  1206.  
  1207. /**
  1208. * Checks if a post is calling a set command and returns the command value
  1209. * @param string $command 'comand@' to check
  1210. * @param object $post the full post object
  1211. * @return false|string returns the command value or false on not-found
  1212. */
  1213. static function checkForCommand($command,$post){
  1214.  
  1215. if(isset($post->commands[$command])){
  1216. return $post->commands[$command];
  1217. }else{
  1218. return false;
  1219. }
  1220.  
  1221. }
  1222.  
  1223. /**
  1224. * Gets the targeted posts any text
  1225. * @param string $text post text
  1226. * @return array post numbers
  1227. */
  1228. function getTargetPosts($text){
  1229. $text = html_entity_decode($text);
  1230. $preg = preg_match_all('/>>(\d+){9}/i', $text,$raw);
  1231. $match = array();
  1232. if(isset($raw[0])){
  1233. foreach ($raw[0] as $key => $value) {
  1234. $match[$key] = str_replace(">", '', $value);
  1235. }
  1236. }
  1237.  
  1238. $match = array_unique($match);
  1239. $players = array();
  1240. foreach($match as $_post_id){
  1241. $players[$_post_id]= $this->getPostAuthor($_post_id);
  1242. }
  1243.  
  1244. $players = array_unique($players);
  1245. return $players;
  1246. }
  1247.  
  1248. /**
  1249. * returns the author of a post
  1250. * authors are var cached for speed
  1251. * @param int $post_number Post number to search
  1252. * @return string post author unique ID
  1253. */
  1254. function getPostAuthor($post_number){
  1255. if(isset($this->_cached_post_authors[$post_number])){
  1256. return $this->_cached_post_authors[$post_number];
  1257. }
  1258.  
  1259. foreach($this->THREAD->posts as $post){
  1260. if($post->no==$post_number){
  1261. $this->_cached_post_authors[$post_number] = $post->id;
  1262. return $post->id;
  1263. }
  1264. }
  1265.  
  1266. //not found
  1267. return false;
  1268. }
  1269.  
  1270. /**
  1271. * Gets the full API object from replies of a certain post.
  1272. * @param string $post_id the post to target
  1273. * @return array
  1274. */
  1275. function getPostReplies($post_id){
  1276. $replies = array();
  1277. foreach($this->THREAD->posts as $post){
  1278. if(!isset($post->com)){
  1279. continue;
  1280. }
  1281.  
  1282. $targets = $this->getTargetPosts($post->com);
  1283. $targets = array_keys($targets);
  1284.  
  1285. $post->class = self::getPlayerClass($post->id);
  1286. $post->weapon = self::getPlayerWeapon($post);
  1287. $post->sprite = self::getPlayerSprite($post);
  1288.  
  1289. //clean the text, more or less
  1290. $post->text = html_entity_decode(strip_tags($post->com));
  1291. $post->text = preg_replace('/>>(\d+){9}/i','',$post->text);
  1292.  
  1293. if(in_array($post_id,$targets)){
  1294. $replies[] = $post;
  1295. }
  1296. }
  1297. return $replies;
  1298. }
  1299.  
  1300. }
  1301.  
  1302.  
  1303. /* Some generic helper functions */
  1304. function array_tree_search_key($a, $subkey) {
  1305. foreach($a as $k=>$v) {
  1306. if(in_array($subkey, $v)) {
  1307. return $k;
  1308. }
  1309. }
  1310. return 0;
  1311. }
  1312.  
  1313.  
  1314. function sprite($filename){
  1315. $src=str_replace("//", "/", "images/sprites/rpg/".$filename);
  1316. echo "<img src='$src' align='absmiddle'/> ";
  1317. }
  1318.  
  1319. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement