-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
153 lines (135 loc) · 4.23 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
include "Character.php";
include "Monster.php";
include "Roller.php";
include "Console.php";
if(empty($_GET['character'])){
$_GET['character'] = "";
}
if (empty($_GET['monster'])){
$_GET['monster'] = "";
}
echo "<div><form><label for='character'>Character</label><select name='character'>";
echo "<option value=''></option>";
foreach ( glob("*.character") as $file ) {
echo "<option value='$file'";
if($file == $_GET['character']){
echo " selected";
}
echo ">$file</option>";
}
echo "</select>";
echo "<label for='monster'>Monster</label><select name='monster'>";
echo "<option value=''></option>";
foreach ( glob("*.monster") as $file ) {
echo "<option value='$file'";
if($file == $_GET['monster']){
echo " selected";
}
echo ">$file</option>";
}
echo "<input type='submit' value='Roll'/>";
echo "</form></div>";
if(empty($_GET['character']) || empty($_GET['monster'])){
exit();
}
$_GET['rounds'] = 1000;
//read character file
$character = new Character(json_decode(file_get_contents($_GET['character']), true));
//read monster file
$monster = new Monster(json_decode(file_get_contents($_GET['monster']), true));
$current = NULL;
$wincount = array();
//for $n rounds
$character_wins = 0;
$character_wincount = [];
$monster_wins = 0;
$monster_wincount = [];
$character_hits = 0;
$character_rolls = 0;
$monster_hits = 0;
$monster_rolls = 0;
for($n=0; $n < $_GET['rounds']; $n++){
//roll for initiative
if ($character->initiative() > $monster->react){
$current = $character;
}else{
$current = $monster;
}
$rounds = 0;
while( $character->current_hp > 0 && $monster->current_hp > 0){
if(is_a($current, 'Character')){
//echo "<h3>Character Attacks</h3>";
// roll character attack vs monster Deflect and echo result
$result = $character->attack($monster);
//Console::CharacterAttack($result);
//set next combatant
$current = $monster;
$character_rolls ++;
if($result['hit']){
$character_hits ++;
}
}else{
//echo "<h3>Character Attacks</h3>";
//roll character Deflect vs monster attack
$result = $character->defend($monster);
//Console::MonsterAttack($result);
//set next combatant
$current = $character;
$monster_rolls ++;
if($result['hit']){
$monster_hits ++;
}
}
$rounds++;
//Console::Status($character, $monster);
}
//echo winner
if($character->current_hp > 0){
//echo "<p>$character->name Wins after $rounds rounds</p>";
$character_wins ++;
if(isset($character_wincount[$rounds])){
$character_wincount[$rounds]++;
}else{
$character_wincount[$rounds] = 1;
}
}else{
//echo "<p>$monster->name Wins after $rounds rounds</p>";
$monster_wins ++;
if(isset($monster_wincount[$rounds])){
$monster_wincount[$rounds]++;
}else{
$monster_wincount[$rounds] = 1;
}
}
$character->reset();
$monster->reset();
//tally winner
}
ksort($character_wincount);
ksort($monster_wincount);
echo "<h3>". $character->name. " wins $character_wins vs. ".$monster->name." wins $monster_wins</h3>";
echo "<h4>". $character->name. " hits ". round($character_hits/$character_rolls, 4)*100 ."% vs. ".$monster->name." hits " . round($monster_hits/$monster_rolls,4) *100 ."%</h4>";
echo "<table>\n";
echo " <tr>\n";
echo " <th>".$character->name. " wins $character_wins.</th>\n";
echo " <th>".$monster->name. " wins $monster_wins.</th>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td>\n";
echo " <ul>";
foreach($character_wincount as $rounds=>$count){
echo " <li>$rounds rounds - $count times.</li>\n";
}
echo " </ul>\n";
echo " </td>\n";
echo " <td>\n";
echo " <ul>";
foreach($monster_wincount as $rounds=>$count){
echo " <li>$rounds rounds - $count times.</li>\n";
}
echo " </ul>\n";
echo " </td>\n";
echo " </tr>\n";
echo " </tr>\n";
echo "</table>\n";