Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/autofight/Interfaces/BattleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface BattleLogger
const TYPE_DEATH = 3;
const TYPE_MOVE = 4;
const TYPE_INSANE = 5;
const TYPE_HEAL = 6;
const TYPE_NOTHEAL = 7;

/**
* Logs a misc message
Expand Down
12 changes: 11 additions & 1 deletion Classes/autofight/Loggers/LoggerCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class LoggerCli implements BattleLogger
BattleLogger::TYPE_DEATH => 'Death',
BattleLogger::TYPE_MOVE => 'Move',
BattleLogger::TYPE_INSANE => 'Insanity',
BattleLogger::TYPE_HEAL => 'Healing',
BattleLogger::TYPE_NOTHEAL => 'Missed healing'
);

/** @var array The colors are color code expressions for the terminal */
Expand All @@ -31,7 +33,9 @@ class LoggerCli implements BattleLogger
BattleLogger::TYPE_MISS => '0;31m',
BattleLogger::TYPE_DEATH => '0;33m',
BattleLogger::TYPE_MOVE => '0;37m',
BattleLogger::TYPE_INSANE => '0;36m'
BattleLogger::TYPE_INSANE => '0;36m',
BattleLogger::TYPE_HEAL => '0;35m',
BattleLogger::TYPE_NOTHEAL => '0;34m'
);

/**
Expand All @@ -56,6 +60,12 @@ function logResult(BattleResult $oResult)
case (BattleLogger::TYPE_DEATH) :
$sMessage .= 'causes '.$oResult->amount.' damage and '.$oResult->message.' '.ucfirst($oResult->defender).'!!';
break;
case (BattleLogger::TYPE_HEAL) :
$sMessage .= 'receives '.$oResult->amount.' health '.$oResult->message.' '.ucfirst($oResult->defender).'!';
break;
case (BattleLogger::TYPE_NOTHEAL) :
$sMessage .= 'only gets '.$oResult->amount.' health '.$oResult->message.' '.ucfirst($oResult->defender).'!';
break;
default:
break;
}
Expand Down
18 changes: 16 additions & 2 deletions Classes/autofight/Loggers/LoggerWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class LoggerWeb implements BattleLogger
BattleLogger::TYPE_MISS => 'Miss',
BattleLogger::TYPE_DEATH => 'Death',
BattleLogger::TYPE_MOVE => 'Move',
BattleLogger::TYPE_INSANE => 'Insanity'
BattleLogger::TYPE_INSANE => 'Insanity',
BattleLogger::TYPE_HEAL => 'Healing',
BattleLogger::TYPE_NOTHEAL => 'Missed healing'
);

/** @var array */
Expand All @@ -30,7 +32,9 @@ class LoggerWeb implements BattleLogger
BattleLogger::TYPE_MISS => 'color: red',
BattleLogger::TYPE_DEATH => 'color: black; text-decoration: underline',
BattleLogger::TYPE_MOVE => 'color: grey',
BattleLogger::TYPE_INSANE => 'color: orange; font-weight:bold; text-decoration: underline'
BattleLogger::TYPE_INSANE => 'color: orange; font-weight:bold; text-decoration: underline',
BattleLogger::TYPE_HEAL => 'color: darkorchid',
BattleLogger::TYPE_NOTHEAL => 'color: LightSlateGray'
);

/**
Expand Down Expand Up @@ -65,6 +69,16 @@ function logResult(BattleResult $oResult)
case (BattleLogger::TYPE_DEATH) :
$sMessage .= 'causes '.$oResult->amount.' damage and '.$oResult->message.' '.ucfirst($oResult->defender).'!!';
break;
default:
break;
case (BattleLogger::TYPE_HEAL) :
$sMessage .= $oResult->message.'. '.ucfirst($oResult->defender);
$sMessage .= ' receives '.$oResult->amount.' health.';
break;
case (BattleLogger::TYPE_NOTHEAL) :
$sMessage .= $oResult->message.'. '.ucfirst($oResult->defender);
$sMessage .= ' only gets '.$oResult->amount.' health.';
break;
default:
break;
}
Expand Down
176 changes: 176 additions & 0 deletions Classes/autofight/Priest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace autofight;

use autofight\Abstracts\Unit as aUnit;
use autofight\Interfaces\BattleLogger;
use autofight\Interfaces\Unit as iUnit;

class Priest extends aUnit
{
/** @var array */
protected $aMessages = array(
1 => array(
'started crying',
'took a break',
),
20 => array(
'misses badly',
'doesn\'t even touch',
),
40 => array(
'misses by a yardstick',
'heals a little',
),
50 => array(
'makes it',
'seems to remember what to do',
),
60 => array(
'touches',
'waves a hand at',
),
80 => array(
'blesses',
'sends his grace',
),
99 => array(
'hugs',
'holds hand',
),
100 => array(
'sends God',
'kisses',
),
);

/** @var int */
protected $iHealth = 200;

/** @var int */
protected $iMaxHealth = 200;

/** @var int */
protected $iAccuracy = 30;

/** @var int */
protected $iRadius = 0;

/** @var string */
protected $sType = 'priest';

/** @var int */
protected $iDamage = 10;

/** @var int */
protected static $rarity = 30;

/**
* Rarity is the chance of getting this unit in a random draw of units.
* A bigger number means more chance to appear.
*
* @return int
*/
public static function getRarity()
{
return self::$rarity;
}

/**
* @param Army $oAttackedArmy
*
* @return array
*/
public function act(Army $oAttackedArmy)
{
$oAttackedArmy = $this->getArmy();
$oUnit = $oAttackedArmy->getRandomAliveUnit($this);

if ($oUnit) {
return $this->shoot($oUnit);
}

return array();
}

/**
* Shoots at a given unit. Hit or miss depends on accuracy and other
* factors. Can shoot at self and commit suicide with a 100% success rate.
*
* @param \autofight\Interfaces\Unit $oUnit
*
* @return mixed
*/
public function shoot(iUnit $oUnit)
{
$aResults = array();
$oResult = new BattleResult();
$oResult->attacker = $this;
$oResult->defender = $oUnit;

$aPostMerge = array();

// Calculate hit or miss.
$iHitScore = rand(1, 100);
$bHit = $iHitScore >= $this->iAccuracy;

$oResult->type = ($bHit) ? BattleLogger::TYPE_HEAL : BattleLogger::TYPE_NOTHEAL;
$oResult->message = $this->determineMessage($iHitScore);

$fPercentageOfAccuracy = $iHitScore / $this->iAccuracy * 100;
if (!$bHit) {
$iAmount = 0;
// MISS
if ($fPercentageOfAccuracy > 50 && $fPercentageOfAccuracy < 60) {
/*
If the hit score was between 50% and 60% of accuracy
there's a chance the adjacent trooper was hit.
*/
$aAdjacent = $this->getArmy()->getAdjacentUnits($this, 2);
if (!empty($aAdjacent)) {
$oUnitToShootAt = $this->getRandomElement($aAdjacent);
if ($oUnitToShootAt) {
$aResults[] = $this.' aims at '.$oUnit.' but his power strays towards '.$oUnitToShootAt.'!';
$aPostMerge = $this->shoot($oUnitToShootAt);
}
}
} elseif ($iHitScore == 1) {
// CRITICAL MISS
switch (rand(0, 1)) {
case 0:
// Reduce accuracy by 10
$this->iAccuracy = ($this->iAccuracy < 11) ? 1 : ($this->iAccuracy - 10);
$sAddedMessage = $this.' was punished by God for not using his powers right!';
break;
case 1:
// Reduce health by 10
$this->iHealth = ($this->iHealth < 11) ? 1 : ($this->iHealth - 10);
$sAddedMessage = $this.' was punished by God for not using his powers right!';
break;
default:
break;
}
}
} else {
// HIT
if ($iHitScore == 100) {
// CRITICAL HIT
$iAmount = $this->iDamage * 5;
$aResults[] = $this.' is playing Jesus!!';
} else {
$iAmount = $this->iDamage * $iHitScore / 100;
}
}

$oUnit->increaseHealth($iAmount);
$oResult->amount = $iAmount;

$aResults[] = $oResult;
if (isset($sAddedMessage)) {
$aResults[] = $sAddedMessage;
}
$aResults = array_merge($aResults, $aPostMerge);

return $aResults;
}
}
18 changes: 1 addition & 17 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@
<meta name="viewport" content="width=device-width, initial-scale=1">

<style type="text/css">

#main {
max-width: 1024px;
margin: auto;
}

.container {
height: auto;
overflow: hidden;
}

#content {
float: none; /* not needed, just for clarification */
/* the next props are meant to keep this block independent from the other floated one */
Expand All @@ -30,13 +27,11 @@
border-radius: 5px;
border: 1px solid silver;
}

#right {
width: 340px;
float: right;
margin-left: 20px;
}

footer {
position: fixed;
bottom: 0;
Expand All @@ -51,7 +46,6 @@
font-size: 10pt;
}
footer a {color: black;}

</style>

</head>
Expand Down Expand Up @@ -102,15 +96,12 @@
<?php endif; ?>

<?php

/** Autoloader helps us avoid requires and includes */
require_once 'autoload.php';
/** Utility methods help us with some common operations */
require_once 'utility_methods.php';

/** Use helps us avoid long class names */
use autofight\Army;

/** Check if we got the required params */
/** @var int $iArmy1 */
$iArmy1 = (PHP_SAPI == 'cli')
Expand All @@ -120,7 +111,6 @@
$iArmy2 = (PHP_SAPI == 'cli')
? (isset($argv[2]) ? $argv[2] : 0)
: ((isset($_GET['army2'])) ? (int)$_GET['army2'] : 0);

if (!$iArmy1 || !$iArmy2) {
$sMsg = 'Two parameters are expected - army1 and army2.
Cannot continue without both. ';
Expand All @@ -136,21 +126,18 @@
}
echo $sMsg;
} else {

/**
* Register available unit types
*/
Army::addUnitType(new \autofight\Infantry());
Army::addUnitType(new \autofight\Tank());

Army::addUnitType(new \autofight\Priest());
/**
* Build armies
*/
$oArmy1 = new Army($iArmy1);
$oArmy2 = new Army($iArmy2);

$oWar = new \autofight\War();

/**
* Register appropriate logger, depending on context
*/
Expand All @@ -159,7 +146,6 @@
? new \autofight\Loggers\LoggerCli()
: new \autofight\Loggers\LoggerWeb()
);

/**
* Start the war
*/
Expand Down Expand Up @@ -193,10 +179,8 @@
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');

ga('create', 'UA-43657205-1', 'bitfalls.com');
ga('send', 'pageview');

</script>
</body>
</html>
Expand Down