Beispiel #1
0
function showaverages($results)
{
    global $forces, $options;
    //set colors and units in the next if-then for rulesets on PHP side...uses the CSS
    $className = '';
    if (isset($_REQUEST['ruleset'])) {
        if ($_REQUEST['ruleset'] == 'Classic') {
            $className = 'cla';
        }
        if ($_REQUEST['ruleset'] == 'Revised') {
            $className = 'rev';
        }
        if ($_REQUEST['ruleset'] == 'AA50') {
            $className = 'aa50';
        }
    }
    $output = '';
    $sides = array('att' => 'Attacker', 'def' => 'Defender');
    $statkeys = array('count', 'cost', 'punch');
    $output .= '<h2>Average outcome of ' . number_format($_REQUEST['reps']) . ' battles</h2><b>Attacker: </b>' . showforce($forces['att']) . '<b> v. Defender: </b>' . showforce($forces['def']) . ' ' . scenariolink('web');
    $output .= '<p>Average battle duration: <b>' . $results['duration'] . '</b> rounds of combat</p>';
    $breaks = array('att' => array(), 'def' => array());
    if (!has_sea($forces['def']) && can_bombard($forces['att'])) {
        //Amphibious assault
        unset($forces['att']['Des']);
        unset($forces['att']['Cru']);
        unset($forces['att']['Bat']);
        $output .= "<b>Note:</b> Bombarding units are included in the statistics, although not reported in count/cost/punch/units.<br />";
    }
    $benchmarks = calibrate($forces);
    $graphstyles = ' display: block; float: left; font-size: 60%; text-align: center; padding: 0; margin: 0; height:15px; border: 1px solid grey; ';
    $output .= '<table border=0><tr><th>&nbsp;</th>
	<th>avg. # units left<!--<div style="font-size: 60%">scale: ' . $benchmarks['count'] . '</div>--></th>
	<th>IPC value <!--<div style="font-size: 60%">scale: ' . $benchmarks['cost'] . '</div>--></th>
	<th>Punch <!--<div style="font-size: 60%">scale: ' . $benchmarks['punch'] . '</div>--></th>
	</tr>';
    $gross = array();
    #debugarray($results);
    $stats = array();
    foreach ($sides as $side => $name) {
        // show both sides
        arsort($results[$side]);
        $totals = array('count' => 0, 'cost' => 0, 'punch' => 0, 'wins' => 0);
        #debugarray ($results[$side]);
        $percentpoint = 0;
        foreach ($results[$side] as $index => $data) {
            $wins = round($data['total'] / $_REQUEST['reps'] * 100, 2);
            $lastpercent = $percentpoint;
            $percentpoint += $wins;
            $units = $data['stats']['count'];
            $lossdata = assess($data['lost'], substr(strtolower($name), 0, -2));
            #debug ("$wins, total $percentpoint, $index");
            #debug($percentpoint);
            //the 1st SD includes 68% of the results centered about the mean or median
            $stdev1 = $percentpoint >= 84 && $lastpercent < 84 || $percentpoint >= 16 && $lastpercent < 16;
            //the 2nd SD includes 94% of the results centered about the mean or median
            $stdev2 = $percentpoint >= 97 && $lastpercent < 97 || $percentpoint >= 3 && $lastpercent < 3;
            $median = $percentpoint >= 50 && $lastpercent < 50;
            if ($wins > 0) {
                $breaks[$side][$index] = probgraph($wins, showforce($data['force']), $units, showforce($data['lost']), $lossdata['cost'], $median, $stdev1, $stdev2);
            }
            foreach ($statkeys as $key) {
                $totals[$key] += $data['stats'][$key] * $wins;
            }
            $totals['wins'] += $wins;
        }
        #debugarray ($breaks[$side]);
        $astats = array();
        foreach ($statkeys as $key) {
            if ($totals['wins'] !== 0) {
                $astats[$key] = round($totals[$key] / 100, 1);
                #			if ($side=='att'){$astats['punch']=$astats['opunch'];}else{$astats['punch']=$astats['dpunch'];}
            } else {
                $astats[$key] = 0;
            }
        }
        $stats[$side] = $astats;
        //save for later
        $mode = strtolower(substr($sides[$side], 0, -2));
        //gets 'attack' or 'defend' from "Attacker" or "Defender"
        $astartstats = assess($forces[$side], $mode);
        #if ($side=='att'){$astartstats['punch']=$astartstats['opunch'];}else{$astartstats['punch']=$astartstats['dpunch'];}
        $aloststats = array();
        foreach ($statkeys as $key) {
            $aloststats[$key] = $astartstats[$key] - $astats[$key];
        }
        if ($totals['wins'] > 100) {
            $totals['wins'] = 100;
        }
        $gross[$side] = round($totals['wins'], 1);
        $survive = '<th class="' . $side . $className . '">' . $name . ':</th>';
        if ($totals['wins'] < 99) {
            $losses = 100 - $totals['wins'];
            $stdev2 = $losses >= 3;
            //2nd standard deviation contains about 95% of the results. thus, 50-(95/2) = 3% when rounded up
            $stdev1 = $losses >= 16;
            //1st standard deviation contains about 68% of the results. thus, 50-(68/2) = 16%
            $median = $losses >= 50;
            $width = width($losses, 50);
            $lossdata = assess($forces[$side], substr(strtolower($name), 0, -2));
            $breaks[$side][0] = probgraph($losses, 'no units.', 0, showforce($forces[$side]), $lossdata['cost'], $median, $stdev1, $stdev2);
            #'<!--0-->
            #<span style="background: grey; width: '.$width.'px; display: block; float: left; border: 1px solid black; margin: 3px 3px 0 0; font-size: 60%;">&nbsp;</span>
            #0 units: '.$losses.'%: left with <b>nothing</b>.';
        }
        foreach ($benchmarks as $stat => $bmark) {
            $afw = width($astats[$stat], $bmark);
            $alw = width($aloststats[$stat], $bmark);
            $alossw = width($astartstats[$stat], $bmark);
            $survive .= '<td class="graph">';
            if ($astats[$stat] > 0) {
                $survive .= '<span class="' . $side . $className . '"style="width:' . $afw . 'px; ' . $graphstyles . '">' . $astats[$stat] . '</span>';
            }
            #<span class="noshow">Left:</span> ';
            if ($aloststats[$stat] > 0) {
                $survive .= '<span class="lost" style="' . $graphstyles . ' border: none !important; padding: 1px 0px !important; width:' . ($alw - 1) . 'px;">' . $aloststats[$stat] . '</span>';
            }
            $survive .= '</td>';
        }
        #if ($totals['wins'] > 0)
        #		$ics=$astartstats['count']*$astartstats['punch'];
        #		$survive.= "<td>count: ".$astartstats['count']." punch: ".$astartstats['punch']. "ICS: $ics</td>";
        $output .= '<tr>' . $survive . '</tr>';
    }
    $output .= '
	<tr><td>&nbsp;</td><td><span class="att' . $className . '" style="display: block; float: left; border: 1px solid black; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%;">#</span> Surviving Attackers <br />
	 </td>
	<td valign="top"><span class="def' . $className . '" style="display: block; float: left; border: 1px solid black; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%;">#</span> Surviving Defenders</td><td><span class="lost" style="display: block; float: left; border: 1px solid  grey; padding: 0 3px; margin: 3px 3px 0 0; font-size: 60%; ">#</span>Casualties</td></tr>
	<tr><td colspan="4"><hr /></td></tr>
	<tr style="font-weight: bold;">
	<th>Overall %*:</th><td class="att' . $className . '" style="font-weight: bold; padding: 0px 4px;">A. survives: ' . $gross['att'] . '%</td>
	<td class="def' . $className . '" style="font-weight: bold; padding: 0px 4px;">D. survives: ' . $gross['def'] . '%</td>
	<td class="lost" style="font-weight: bold; padding: 0px 4px;">No one survives: ' . round($results['draw'] / $_REQUEST['reps'] * 100, 1) . '%</td></tr>
	</table>
	<em>* percentages may not total 100 due to rounding. The average results from above are<span style="background: yellow;"> highlighted </span>in charts below, while the median result (equal odds of getting a worse or better result) is written in <span style="color: red">red</span>. If shown, the 1st and 2nd standard deviations about the mean are represented in <span style="color: blue">blue</span> and <span style="color: dodgerblue">light blue</span>.</em>
	<table>';
    #debugarray ($results);
    foreach ($sides as $side => $name) {
        $output .= '<tr><th colspan="4">' . $name . ' results:<hr /></td></tr>
		<tr><th style="text-align: right;">Probability</th><th style="text-align: left;">%</th><th style="text-align: right;">#</th><th style="text-align: left">units</th><th>/ losses</th></tr>
		';
        #		<tr><td>';
        #		foreach ($breaks[$side] as $line) $output .= "<tr>$line</tr>";
        #$output .= '</ul></td><td><h3>By remaining count</h3><ul>';
        natcasesort($breaks[$side]);
        #debug ('Stats:');
        #debugarray ($stats[$side]);
        #debug ('Avg. Count for '.$side.': '.$stats[$side]['count']);
        $breaks[$side] = array_reverse($breaks[$side]);
        #debugarray ($r);
        #debug (count($breaks[$side]));
        foreach ($breaks[$side] as $index => $line) {
            $color = 'background: #eef;';
            $avgcount = round($stats[$side]['count']);
            $count = 0;
            if ($index !== 0) {
                $count = $results[$side][$index]['stats']['count'];
            }
            if ($count > $avgcount - 1.0 and $count < $avgcount + 1.0) {
                $color = 'background: yellow;';
            }
            if ($count !== 0 && $color == '') {
                $countratio = $avgcount / $count;
                if ($countratio >= 0.9 and $countratio <= 1.1) {
                    $color = 'background: yellow;';
                }
            }
            $output .= "<tr style=\"font-size: 60%; {$color}\">{$line}</tr>\n\t\t\t";
        }
        $output .= '<tr><td colspan=4><hr /></td></tr>';
    }
    $output .= '</table>';
    global $rounds;
    #if ($rounds == '')
    $output .= showipcdiffs($results['ipcdiff']);
    #debug ($rounds);
    $output .= scenariolink('mail');
    return $output;
}
Beispiel #2
0
function getformdata($posted)
{
    global $options, $canhit, $rounds, $sides, $ool, $baseool, $mustland, $round, $abortratio, $unitspecs;
    $customOOL = array();
    $valid = true;
    $options['tech'] = (isset($_REQUEST['toggletechs']) && $_REQUEST['toggletechs'] == "Enable techs" or isset($_REQUEST['techs']) && (!isset($_REQUEST['toggletechs']) or $_REQUEST['toggletechs'] !== 'Disable techs'));
    #debugarray ($options);
    if ($options['tech']) {
        $units = allunits();
    } else {
        $units = notechunits();
    }
    /* DO NOT NEED IF NOT STORING GAMES ANYMORE?
    	#validate gameid:
    	#$gameid=$_REQUEST['gameid'];
    	
    	if ($gameid!=='') {
    		#$gameid=intval($gameid);
    		#if (strval($gameid)!==$_REQUEST['gameid']) {
    		#	echo 'No non-numeric characters permitted in Game ID #<p>';
    		#	$gameid='';
    		#} else
    		$options['gameidok']=false;
    		if (!file_exists('makegame/games/'.$gameid.'.php')) {
    			echo "No record exists for game ID # $gameid.<p>";
    			$gameid='';
    		} else if ($_REQUEST['territory']=='' or $_REQUEST['turnid']=='') {
    			echo "<div style=\"color: red\"><b>Error:</b> You must specify a territory and a Turn ID for the game record.</div>";
    			$_REQUEST['pbem']='';
    		} else { #Game ID is valid and file exists
    			include ('makegame/games/'.$gameid.'.php');
    			if ($_REQUEST['password']==$data['password']) {
    				$_REQUEST['ruleset']=$data['ruleset'];
    				$_REQUEST['luck']=$data['luck'];
    				if (isset ($_REQUEST['pbem']) && strlen($_REQUEST['pbem']) > 6 ) { # User has entered a game id but also entered e-mail addresses 
    					$_REQUEST['pbem']=str_replace (array($data['player1'],$data['player2']), '', $_REQUEST['pbem']).' '.$data['player1'].' '.$data['player2'];
    					#Previous line strips out the e-mail addresses from the pbem field that are already saved in game ID.
    				} else {				
    					$_REQUEST['pbem']=$data['player1'].' '.$data['player2']; # This line preloads the saved e-mail addresses.
    				}
    				$_REQUEST['reps']=1;
    				$_REQUEST['gameid']=$gameid;
    				$turnid=$_REQUEST['turnid'];
    				$options['gameidok']=true;
    			} else { 
    				echo "The password provided did not match.<p>";
    				$_REQUEST['gameid']='';
    			}
    		}
    	}
    	*/
    /* OLD OPTIONS FOR OTHER RULESETS...SHOULD BE PUT IN THE UPDATEUNITS FUNCTION IF WE WANT BACK IN
    	if (isset($_REQUEST['ruleset'])) {
    		if 	($_REQUEST['ruleset'] == 'LHTR' ) {
    			#$unitspecs['HBom']['attack']=5.33;
    			$unitspecs['SSub']['defend']=3;
    			$unitspecs['HBom']['attackdice']=1;
    			if ($_REQUEST['luck']!=='pure') $unitspecs['HBom']['attack']=5.33;
    			#debug ($_REQUEST['luck']);
    		}
    		if ($_REQUEST['ruleset'] == 'Europe' ) {
    			$unitspecs['Fig']['cost']=12;
    			$unitspecs['Arm']['defend']=2;
    			$unitspecs['Car']['cost']=18;
    		}
    	}*/
    if (isset($_REQUEST['round'])) {
        $round = intval($_REQUEST['round']);
    }
    foreach ($sides as $side => $name) {
        $string = trim($_REQUEST['ool_' . $side]);
        $string = str_replace(' ', '-', $string);
        $replacechars = array('  ', ' ', ',', '.', '<', '>', '?', '/', '\\', '!');
        $string = str_replace($replacechars, '', $string);
        $array = explode('-', $string);
        //find if any elements are missing or changed names
        foreach ($baseool as $type) {
            if (!in_array($type, $array)) {
                $valid = false;
            }
        }
        //find if number of elements is the the same
        if (count($array) !== count($baseool)) {
            $valid = false;
        }
        $customOOL[$side] = $array;
    }
    if ($valid) {
        $ool = $customOOL;
        $warned = false;
        foreach ($sides as $side => $name) {
            //prevent battleships being killed first if Classic ruleset used and Bat's listed first in OOL
            if ($_REQUEST['ruleset'] == 'Classic' && $ool[$side][0] == 'Bat') {
                #debug ("Saving $side's battleships!");
                if (!$warned) {
                    echo "<b>Notice:</b> OOL adjusted to prevent battleships being killed first.";
                }
                $warned = true;
            }
            $string = implode('-', $ool[$side]);
            #setcookie ('ool_'.$side, $string, +31536000);
        }
    } else {
        echo "<p><strong>Error</strong> - elements in the custom Order of Loss should be separated only by dashes.\n\t\t\tThe elements themselves should not be edited, deleted or added to.</p>";
    }
    $mustland = 0;
    if (isset($_REQUEST['mustland']) && $_REQUEST['mustland'] !== '' && !isset($_REQUEST['Clear'])) {
        $_REQUEST['mustland'] = intval($_REQUEST['mustland']);
        $mustland = $_REQUEST['mustland'];
    }
    $abortratio = intval($_REQUEST['abortratio']);
    if ($abortratio > 1000 or $abortratio < 1) {
        $abortratio = 0;
    }
    $options['legal'] = false;
    $_REQUEST['reps'] = intval($_REQUEST['reps']);
    $options['AA'] = (isset($_REQUEST['AA']) or isset($_REQUEST['AAr']));
    $_REQUEST['pbem'] = str_replace(array(',', ';'), ' ', $_REQUEST['pbem']);
    $_REQUEST['pbem'] = explode(' ', $_REQUEST['pbem']);
    foreach ($_REQUEST['pbem'] as $key => $val) {
        if (!valid_email($val)) {
            unset($_REQUEST['pbem'][$key]);
        }
    }
    if ($_REQUEST['reps'] > 1 && ($_REQUEST['luck'] == 'none' or $_REQUEST['battle'] == 'Evaluate units')) {
        echo '<p><strong>Note:</strong> in No Luck mode or Evaluate Units mode, every battle turns out the same
			(so not much point running it 1000x). Battle has only been run once.</p>';
        $_REQUEST['reps'] = 1;
    }
    foreach ($posted as $key => $value) {
        if ((!isset($value) or $value == '') && $key !== 'rounds') {
            $posted[$key] = 0;
        } else {
            $posted[$key] = intval($value);
        }
    }
    $forces = array('att' => array(), 'def' => array());
    foreach ($units as $type) {
        if ($posted['a' . $type] > 0) {
            $forces['att'][$type] = $posted['a' . $type];
        }
        if ($posted['d' . $type] > 0) {
            $forces['def'][$type] = $posted['d' . $type];
        }
    }
    //check that both sides have units:
    $options['nounits'] = count($forces['att']) == 0 && count($forces['def']) == 0;
    if (!$options['nounits']) {
        //seabattle boolean conditions. check if seabattle or not.
        $options['seabattle'] = (has_sea($forces['def']) or has_sea($forces['att'])) && !has_land($forces['att']) && !has_land($forces['def']) && !isset($forces['def']['Bom']) && !isset($forces['def']['HBom']);
        //landbattle boolean must also be set to true if a shore bombardment is possible
        $options['landbattle'] = (has_land($forces['def']) or has_land($forces['att']) or has_air($forces['def']) or has_air($forces['att']));
        if (isset($forces['att']['Bat'])) {
            $options['landbattle'] = $options['landbattle'] && has_land($forces['att']);
        }
        //if AA50 or AA1942, add Des as invalid attacker and Cru as valid attacker into landbattle option
        if ($_REQUEST['ruleset'] == 'AA50' || $_REQUEST['ruleset'] == 'AA1942') {
            if (isset($forces['att']['Cru'])) {
                $options['landbattle'] = $options['landbattle'] && has_land($forces['att']);
            }
            $options['landbattle'] = $options['landbattle'] && !isset($forces['att']['Des']);
        }
        //if Revised, add Des as valid attacker into landbattle option -- No need to address Cru since it's not on Revised form
        if ($_REQUEST['ruleset'] == 'Revised') {
            if (isset($forces['att']['Des'])) {
                $options['landbattle'] = $options['landbattle'] && has_land($forces['att']);
            }
        }
        //no need to address Cru and Des in Classic either since they are not on the Classic form now
        //finish list of requirements for landbattle to not include any of the following
        $options['landbattle'] = $options['landbattle'] && !has_sea($forces['def']) && !isset($forces['att']['Tra']) && !isset($forces['att']['Sub']) && !isset($forces['att']['SSub']) && !isset($forces['att']['Car']) && !isset($forces['att']['dBat']);
        // check if attacker has combined standard and upgraded units of the same class:
        $illegalattackcomb = (isset($forces['att']['Art']) && isset($forces['att']['AArt']) or isset($forces['att']['Bom']) && isset($forces['att']['HBom']) or isset($forces['att']['Fig']) && isset($forces['att']['JFig']) or isset($forces['att']['Sub']) && isset($forces['att']['SSub']));
        $options['legal'] = $options['landbattle'] + $options['seabattle'] !== 2 && $options['landbattle'] + $options['seabattle'] !== 0 && $illegalattackcomb == false;
    }
    return $forces;
}
Beispiel #3
0
     $def[$t]['force'] = $outcome['def']['force'];
     $astats = assess($att[$t]['force'], 'attack');
     $dstats = assess($def[$t]['force'], 'defend');
     $abort = ($astats['count'] - round(($dstats['punch'] + 3) / 6) < $saveunits && $saveunits > 0 or $dstats['count'] - round(($astats['punch'] + 3) / 6) < $strafeunits && $strafeunits > 0 or $dstats['punch'] > 0 and $astats['punch'] / $dstats['punch'] * 100 < $abortratio);
     if (!$stalemate) {
         if (!has_sea($def[$t]['force']) && (!nonsubs($att[$t]['force']) or isset($_REQUEST['asubschicken'])) && has_sub($att[$t]['force'])) {
             $submerged['att'] = array();
             if (isset($att[$t]['force']['Sub'])) {
                 $submerged['att']['Sub'] = $att[$t]['force']['Sub'];
             }
             if (isset($att[$t]['force']['SSub'])) {
                 $submerged['att']['SSub'] = $att[$t]['force']['SSub'];
             }
             $att[$t]['force'] = submerge($att[$t]['force']);
         } else {
             if (!has_sea($att[$t]['force']) && (!nonsubs($def[$t]['force']) or isset($_REQUEST['dsubschicken'])) && has_sub($def[$t]['force'])) {
                 $submerged['def'] = array();
                 if (isset($def[$t]['force']['Sub'])) {
                     $submerged['def']['Sub'] = $def[$t]['force']['Sub'];
                 }
                 if (isset($def[$t]['force']['SSub'])) {
                     $submerged['def']['SSub'] = $def[$t]['force']['SSub'];
                 }
                 $def[$t]['force'] = submerge($def[$t]['force']);
             }
         }
     }
 }
 // Battle has been resolved
 //Note that bombarding Battleships, Cruisers, and Destroyers are also tracked in $submerged.
 if (isset($submerged['att']['Sub']) or isset($submerged['att']['SSub'])) {