Passed
Push — master ( 606b80...d10f8a )
by Jakub
02:32
created

InitiativeFormulaParser::calculateInitiative()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 10
cts 10
cp 1
cc 2
eloc 11
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
/**
7
 * InitiativeFormulaParser
8
 *
9
 * @author Jakub Konečný
10
 */
11 1
final class InitiativeFormulaParser implements IInitiativeFormulaParser {
12 1
  use \Nette\SmartObject;
13
  
14
  public function calculateInitiative(string $formula, Character $character): int {
15 1
    $result = 0;
16
    $stats = [
17 1
      "INT" => $character->intelligence, "DEX" => $character->dexterity, "STR" => $character->strength, "CON" => $character->constitution,
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 138 characters
Loading history...
18 1
      "CHAR" => $character->charisma,
19
    ];
20 1
    $formula = str_replace(array_keys($stats), array_values($stats), $formula);
21 1
    preg_match_all("/^([1-9]+)d([1-9]+)/", $formula, $dices);
22 1
    for($i = 1; $i <= (int) $dices[1][0]; $i++) {
23 1
      $result += rand(1, (int) $dices[2][0]);
24
    }
25 1
    preg_match_all("/\+([0-9]+)\/([0-9]+)/", $formula, $ammendum);
26 1
    $result += (int) $ammendum[1][0] / (int) $ammendum[2][0];
27 1
    return (int) $result;
28
  }  
29
}
30
?>