Advertisement
Guest User

MOBA challenger

a guest
Dec 12th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. <?php
  2.  
  3. $input = readline();
  4.  
  5. $players =[];
  6. while ($input !== "Season end") {
  7.  
  8. if (strstr($input, '->')) {
  9. list($player, $position, $skill) = explode(" -> ", $input);
  10. if (!key_exists($player, $players)) {
  11. $players[$player][$position] = $position;
  12. $players[$player][$skill] = intval($skill);
  13. } else {
  14. if (!key_exists($position, $players[$player])) {
  15. $players[$player][$position] = $position;
  16. if (key_exists($skill, $players[$player])) {
  17. $players[$player]['$skill'.$skill] = intval($skill);
  18. } else {
  19. $players[$player][$skill] = intval($skill);
  20. }
  21. } else {
  22. if ($players[$player][$skill][0] === '$') {
  23. if ($players[$player]['$skill'.$skill] < $skill) {
  24. $players[$player][$skill] = intval($skill);
  25. }
  26. } else {
  27. if ($players[$player][$skill] < $skill) {
  28. $players[$player][$skill] = intval($skill);
  29. }
  30. }
  31. }
  32. }
  33. } else if (false !== strpos($input, 'vs')) {
  34. list($leftPlayer, $rightPlayer) = explode(" vs ", $input);
  35. if (key_exists($leftPlayer, $players) && key_exists($rightPlayer, $players)) {
  36. $matches = array_intersect($players[$leftPlayer], $players[$rightPlayer]);
  37. if (count($matches) !== 0) {
  38. $totalLeftSum = 0 ;
  39. foreach ($players[$leftPlayer] as $key => $value) {
  40. if (is_numeric($key)) {
  41. $totalLeftSum += $key;
  42. }
  43. }
  44. $totalRightSum = 0;
  45. foreach ($players[$rightPlayer] as $key => $value) {
  46. if (is_numeric($key)) {
  47. $totalRightSum += $key;
  48. }
  49. }
  50.  
  51. if ($totalRightSum > $totalLeftSum) {
  52. unset($players[$leftPlayer]);
  53. } else if ($totalLeftSum > $totalRightSum) {
  54. unset($players[$rightPlayer]);
  55. } else if($totalRightSum === $totalLeftSum) {
  56. $input = readline();
  57. continue;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. $input = readline();
  64. }
  65.  
  66. $playersPrint = [];
  67. $statsPrint = [];
  68. foreach ($players as $name => $values) {
  69. $totalSum = 0;
  70. foreach ($values as $key => $value) {
  71. if (is_numeric($key)) {
  72. $totalSum += $key;
  73. } else if ($key[0] === '$') {
  74. $totalSum += $value;
  75. }
  76. }
  77. $playersPrint[$name] = $totalSum;
  78. }
  79.  
  80. foreach ($players as $currName => $val) {
  81. $currStat = implode(" ", $players[$currName]);
  82. $tokens = explode(" ", $currStat);
  83. if (count($tokens) === 2) {
  84. $statsPrint[$currName][$tokens[0]] = intval($tokens[1]);
  85. } else {
  86. for ($i = 0; $i < count($tokens);$i++){
  87. if ($i % 2 === 1) {
  88. $statsPrint[$currName][$tokens[$i - 1]] = intval($tokens[$i]);
  89. }
  90. }
  91. }
  92.  
  93. }
  94.  
  95. uksort($playersPrint, function($key1, $key2) use ($playersPrint){
  96. $val1 = $playersPrint[$key1];
  97. $val2 = $playersPrint[$key2];
  98.  
  99. if ($val1 === $val2) {
  100. return $key1 <=> $key2;
  101. }
  102. return $val2 <=> $val1;
  103. });
  104.  
  105. foreach ($playersPrint as $name => $val) {
  106. echo "$name: $val skill" . PHP_EOL;
  107. foreach ($statsPrint[$name] as $pos => $stat) {
  108. $currArr = $statsPrint[$name];
  109. uksort($currArr, function($key1, $key2) use($currArr) {
  110. $val1 = $currArr[$key1];
  111. $val2 = $currArr[$key2];
  112.  
  113. if ($val1 === $val2) {
  114. return $key1 <=> $key2;
  115. }
  116. return $val2 <=> $val1;
  117. });
  118. }
  119. foreach ($currArr as $poss => $statt) {
  120. echo "- $poss <::> $statt" . PHP_EOL;
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement