Example #1
0
function formula_parseToSQL($formula)
{
    global $FORMULA_SYMBOLS, $params;
    $farmmalus = max($params->SESSION->player->fame - FREE_FARM_POINTS, 0);
    $formula = str_replace("[E25.ACT]", $farmmalus, $formula);
    // abstract functions are sql functions -> no translation needed
    // parse symbols
    for ($i = 0; $i < strlen($formula); $i++) {
        // opening brace
        if ($formula[$i] == '[') {
            $symbol = $formula[++$i];
            $index = 0;
            while ($formula[++$i] != '.') {
                $index = $index * 10 + ($formula[$i] + 0);
            }
            $field = substr($formula, ++$i, 3);
            // 'ACT]' or 'MAX]'
            $i += 3;
            if (strncasecmp($field, "ACT", 3) == 0) {
                $sql .= $FORMULA_SYMBOLS[$symbol][$index]->dbFieldName;
            } else {
                if (strncasecmp($field, "MAX", 3) == 0) {
                    $sql .= formula_parseToSQL($FORMULA_SYMBOLS[$symbol][$index]->maxLevel);
                }
            }
        } else {
            $sql .= $formula[$i];
        }
    }
    $sql = str_replace(array('min(', 'max(', 'sgn('), array('LEAST(', 'GREATEST(', 'SIGN('), $sql);
    return $sql;
}
Example #2
0
/**
 *
 */
function externals_performOrder($externalID, $caveID, $cave, $db)
{
    global $defenseSystemTypeList, $unitTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList;
    $external = $defenseSystemTypeList[$externalID];
    $maxLevel = round(eval('return ' . formula_parseToPHP("{$external->maxLevel};", '$cave')));
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}'", "{$external->dbFieldName} < {$maxLevel}");
    // get all the resource costs
    foreach ($external->resourceProductionCost as $key => $value) {
        if ($value) {
            $formula = formula_parseToSQL($value);
            $dbField = $resourceTypeList[$key]->dbFieldName;
            $set[] = "{$dbField} = {$dbField} - ({$formula})";
            $setBack[] = "{$dbField} = {$dbField} + ({$formula})";
            $where[] = "{$dbField} >= ({$formula})";
        }
    }
    // get all the unit costs
    foreach ($external->unitProductionCost as $key => $value) {
        if ($value) {
            $formula = formula_parseToSQL($value);
            $dbField = $unitTypeList[$key]->dbFieldName;
            $set[] = "{$dbField} = {$dbField} - ({$formula})";
            $setBack[] = "{$dbField} = {$dbField} + ({$formula})";
            $where[] = "{$dbField} >= ({$formula})";
        }
    }
    // get all the building costs
    foreach ($external->buildingProductionCost as $key => $value) {
        if ($value) {
            $formula = formula_parseToSQL($value);
            $dbField = $buildingTypeList[$key]->dbFieldName;
            $set[] = "{$dbField} = {$dbField} - ({$formula})";
            $setBack[] = "{$dbField} = {$dbField} + ({$formula})";
            $where[] = "{$dbField} >= ({$formula})";
        }
    }
    // get all the external costs
    foreach ($external->externalProductionCost as $key => $value) {
        if ($value) {
            $formula = formula_parseToSQL($value);
            $dbField = $defenseSystemTypeList[$key]->dbFieldName;
            $set[] = "{$dbField} = {$dbField} - ({$formula})";
            $setBack[] = "{$dbField} = {$dbField} + ({$formula})";
            $where[] = "{$dbField} >= ({$formula})";
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependencies
    foreach ($external->buildingDepList as $key => $value) {
        if ($value) {
            $where[] = "{$buildingTypeList[$key]->dbFieldName} >= {$value}";
        }
    }
    foreach ($external->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            $where[] = "{$buildingTypeList[$key]->dbFieldName} <= {$value}";
        }
    }
    foreach ($external->defenseSystemDepList as $key => $value) {
        if ($value) {
            $where[] = "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}";
        }
    }
    foreach ($external->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            $where[] = "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}";
        }
    }
    foreach ($external->resourceDepList as $key => $value) {
        if ($value) {
            $where[] = "{$resourceTypeList[$key]->dbFieldName} >= {$value}";
        }
    }
    foreach ($external->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            $where[] = "{$resourceTypeList[$key]->dbFieldName} <= {$value}";
        }
    }
    foreach ($external->scienceDepList as $key => $value) {
        if ($value) {
            $where[] = "{$scienceTypeList[$key]->dbFieldName} >= {$value}";
        }
    }
    foreach ($external->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            $where[] = "{$scienceTypeList[$key]->dbFieldName} <= {$value}";
        }
    }
    foreach ($external->unitDepList as $key => $value) {
        if ($value) {
            $where[] = "{$unitTypeList[$key]->dbFieldName} >= {$value}";
        }
    }
    foreach ($external->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            $where[] = "{$unitTypeList[$key]->dbFieldName} <= {$value}";
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || !$db->affected_rows() == 1) {
        return _('Der Auftrag konnte nicht erteilt werden. Es fehlen die notwendigen Voraussetzungen.');
    }
    // calculate the production time;
    $prodTime = 0;
    if ($time_formula = $external->productionTimeFunction) {
        $time_eval_formula = formula_parseToPHP($time_formula, '$cave');
        $time_eval_formula = "\$prodTime={$time_eval_formula};";
        eval($time_eval_formula);
    }
    $prodTime *= DEFENSESYSTEM_TIME_BASE_FACTOR;
    $now = time();
    $query = sprintf("INSERT INTO Event_defenseSystem (caveID, defenseSystemID, " . "`start`, `end`) VALUES (%d, %d, '%s', '%s')", $caveID, $externalID, time_toDatetime($now), time_toDatetime($now + $prodTime));
    if (!$db->query($query)) {
        $db->query($setBack);
        return _('Der Auftrag konnte nicht erteilt werden. Es fehlen die notwendigen Voraussetzungen.');
    }
    return _('Der Auftrag wurde erteilt.');
}
Example #3
0
function wonder_processOrder($playerID, $wonderID, $caveID, $coordX, $coordY, $caveData, $db)
{
    global $defenseSystemTypeList, $unitTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList, $wonderTypeList;
    // ADDED by chris--- for farmschutz: protection_end in sql
    // check the target cave
    $query = "SELECT playerID, caveID, protection_end FROM Cave " . "WHERE xCoord = '{$coordX}' " . "AND yCoord = '{$coordY}'";
    if (!($result = $db->query($query)) || !($targetData = $result->nextRow())) {
        return -3;
    }
    // ADDED by chris--- for farmschutz
    if (date("YmdHis", time()) < $targetData[protection_end]) {
        return -4;
    }
    // ----------------------------------
    $targetID = $targetData[caveID];
    // check, if cave allowed
    if ($wonderTypeList[$wonderID]->target == "same") {
        $allowed = $caveID == $targetID;
    } else {
        if ($wonderTypeList[$wonderID]->target == "own") {
            $allowed = $playerID == $targetData[playerID];
        } else {
            if ($wonderTypeList[$wonderID]->target == "other") {
                $allowed = $playerID != $targetData[playerID];
            } else {
                // $wonderTypeList[$wonderID]->target == "all"
                $allowed = 1;
            }
        }
    }
    if (!$allowed) {
        return -2;
    }
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}' ");
    // get all the resource costs
    foreach ($wonderTypeList[$wonderID]->resourceProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $resourceTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - ({$formula})");
            array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            array_push($where, "{$dbField} >= ({$formula})");
        }
    }
    // get all the unit costs
    foreach ($wonderTypeList[$wonderID]->unitProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $unitTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependecies
    foreach ($wonderTypeList[$wonderID]->buildingDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$wonderTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->defenseSystemDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->resourceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->scienceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->unitDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || $db->affected_rows() != 1) {
        return 0;
    }
    // calculate the chance and evaluate into $chance
    if ($chance_formula = $wonderTypeList[$wonderID]->chance) {
        $chance_eval_formula = formula_parseToPHP($chance_formula, '$caveData');
        $chance_eval_formula = "\$chance={$chance_eval_formula};";
        eval($chance_eval_formula);
    }
    // does the wonder fail?
    //  if ((double)rand() / (double)getRandMax() > $chance) {
    srand((double) microtime() * 1000000);
    $wond1 = (double) rand();
    $wond2 = (double) getRandMax();
    //echo $wond1." / ".$wond2." = ".$wond1/$wond2." -- ". $chance."<br>";
    if ($wond1 / $wond2 > $chance) {
        return 2;
        // wonder did fail
    }
    // schedule the wonder's impacts
    // create a random factor between -0.3 and +0.3
    $delayRandFactor = rand(0, getrandmax()) / getrandmax() * 0.6 - 0.3;
    // now calculate the delayDelta depending on the first imact's delay
    $delayDelta = $wonderTypeList[$wonderID]->impactList[0][delay] * $delayRandFactor;
    foreach ($wonderTypeList[$wonderID]->impactList as $impactID => $impact) {
        $delay = (int) (($delayDelta + $impact[delay]) * WONDER_TIME_BASE_FACTOR);
        $query = "INSERT INTO Event_wonder " . "(casterID, sourceID, targetID, wonderID, impactID, event_start, " . "event_end) " . "VALUES ('{$playerID}', '{$caveID}', '{$targetID}', '{$wonderID}', " . "'{$impactID}', NOW()+0, (NOW() + INTERVAL {$delay} SECOND)+0)";
        if (!$db->query($query)) {
            $db->query($setBack);
            return -1;
        }
    }
    // create messages
    $sourceMessage = "Sie haben auf die Siedlung in {$coordX}/{$coordY} einen Zauber " . $wonderTypeList[$wonderID]->name . " erwirkt.";
    $targetMessage = "Der Besitzer der Siedlung in {$caveData['xCoord']}/{$caveData['yCoord']} " . "hat auf Ihre Siedlung in {$coordX}/{$coordY} einen Zauber gewirkt.";
    messages_sendSystemMessage($playerID, 9, "Zauber erwirkt auf {$coordX}/{$coordY}", $sourceMessage, $db);
    messages_sendSystemMessage($targetData[playerID], 9, "Zauber!", $targetMessage, $db);
    return 1;
}
Example #4
0
function processProductionCostSetBack($item, $caveID, $cave, $quantity = 1, $fromTribeStorage = false)
{
    global $db;
    $setBack = array();
    // get all the resource costs
    if (isset($item->resourceProductionCost)) {
        foreach ($item->resourceProductionCost as $key => $value) {
            if ($value) {
                $formula = formula_parseToSQL($value);
                $formula = "{$quantity} * ({$formula})";
                $dbField = $GLOBALS['resourceTypeList'][$key]->dbFieldName;
                array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            }
        }
    }
    // get all the unit costs
    if (isset($item->unitProductionCost)) {
        foreach ($item->unitProductionCost as $key => $value) {
            if ($value) {
                $formula = formula_parseToSQL($value);
                $formula = "{$quantity} * ({$formula})";
                $dbField = $GLOBALS['unitTypeList'][$key]->dbFieldName;
                $setBack[] = "{$dbField} = {$dbField} + ({$formula})";
            }
        }
    }
    // get all the building costs
    if (isset($item->buildingProductionCost)) {
        foreach ($item->buildingProductionCost as $key => $value) {
            if ($value) {
                $formula = formula_parseToSQL($value);
                $formula = "{$quantity} * ({$formula})";
                $dbField = $GLOBALS['buildingTypeList'][$key]->dbFieldName;
                array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            }
        }
    }
    // get all the defense costs
    if (isset($item->defenseProductionCost)) {
        foreach ($item->defenseProductionCost as $key => $value) {
            if ($value) {
                $formula = formula_parseToSQL($value);
                $formula = "{$quantity} * ({$formula})";
                $dbField = $GLOBALS['defenseSystemTypeList'][$key]->dbFieldName;
                array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            }
        }
    }
    // generate SQL
    if ($fromTribeStorage) {
        $table = TRIBE_TABLE;
        $where = "tag = '" . $_SESSION['player']->tribe . "'";
    } else {
        $table = CAVE_TABLE;
        $where = "caveID = " . $caveID;
    }
    if (sizeof($setBack)) {
        $setBack = implode(", ", $setBack);
        $sql = "UPDATE " . $table . " SET {$setBack} WHERE {$where}";
        if (!$db->exec($sql)) {
            return false;
        }
    }
    return true;
}
Example #5
0
function unit_processOrder($unitID, $quantity, $caveID, $db, $details)
{
    global $defenseSystemTypeList, $unitTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList;
    if ($quantity > MAX_SIMULTAN_BUILDED_UNITS || $quantity <= 0) {
        return 4;
    }
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}'");
    // get all the resource costs
    foreach ($unitTypeList[$unitID]->resourceProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $formula *= $quantity;
            $dbField = $resourceTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the unit costs
    foreach ($unitTypeList[$unitID]->unitProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $formula *= $quantity;
            $dbField = $unitTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the building costs
    foreach ($unitTypeList[$unitID]->buildingProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $formula *= $quantity;
            $dbField = $buildingTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the external costs
    foreach ($unitTypeList[$unitID]->externalProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $formula *= $quantity;
            $dbField = $defenseSystemTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependecies
    foreach ($unitTypeList[$unitID]->buildingDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->defenseSystemDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->resourceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->scienceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->unitDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($unitTypeList[$unitID]->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || !$db->affected_rows() == 1) {
        return 2;
    }
    $prodTime = 0;
    // calculate the production time;
    if ($time_formula = $unitTypeList[$unitID]->productionTimeFunction) {
        $time_eval_formula = formula_parseToPHP($time_formula, '$details');
        eval('$prodTime=' . $time_eval_formula . ';');
    }
    $prodTime *= BUILDING_TIME_BASE_FACTOR * $quantity;
    $now = time();
    $query = sprintf("INSERT INTO Event_unit (caveID, unitID, quantity, " . "`start`, `end`) VALUES (%d, %d, %d, '%s', '%s')", $caveID, $unitID, $quantity, time_toDatetime($now), time_toDatetime($now + $prodTime));
    if (!$db->query($query)) {
        $db->query($setBack);
        return 2;
    }
    return 3;
}
Example #6
0
function improvement_processOrder($buildingID, $caveID, $caveData, $db)
{
    global $defenseSystemTypeList, $unitTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList;
    $building = $buildingTypeList[$buildingID];
    $maxLevel = round(eval('return ' . formula_parseToPHP("{$building->maxLevel};", '$caveData')));
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}'", "{$building->dbFieldName} < {$maxLevel}");
    // get all the resource costs
    foreach ($building->resourceProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $resourceTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - ({$formula})");
            array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            array_push($where, "{$dbField} >= ({$formula})");
        }
    }
    // get all the unit costs
    foreach ($building->unitProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $unitTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the building costs
    foreach ($building->buildingProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $buildingTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the external costs
    foreach ($building->externalProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $defenseSystemTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependecies
    foreach ($building->buildingDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($building->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($building->defenseSystemDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($building->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($building->resourceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($building->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($building->scienceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($building->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($building->unitDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($building->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || !$db->affected_rows() == 1) {
        return 2;
    }
    $prodTime = 0;
    // calculate the production time;
    if ($time_formula = $building->productionTimeFunction) {
        $time_eval_formula = formula_parseToPHP($time_formula, '$caveData');
        $time_eval_formula = "\$prodTime={$time_eval_formula};";
        eval($time_eval_formula);
    }
    $prodTime *= BUILDING_TIME_BASE_FACTOR;
    $now = time();
    $query = sprintf("INSERT INTO Event_expansion (caveID, expansionID, " . "`start`, `end`) VALUES (%d, %d, '%s', '%s')", $caveID, $buildingID, time_toDatetime($now), time_toDatetime($now + $prodTime));
    if (!$db->query($query)) {
        $db->query($setBack);
        return 2;
    }
    return 3;
}
Example #7
0
function science_processOrder($scienceID, $caveID, $playerID, $caveData, $db)
{
    global $defenseSystemTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList, $unitTypeList, $config;
    $science = $scienceTypeList[$scienceID];
    $maxLevel = round(eval('return ' . formula_parseToPHP("{$science->maxLevel};", '$caveData')));
    // check, that this science isn't researched in an other cave at the
    // same time
    $sql = "SELECT event_scienceID FROM Event_science " . "WHERE playerID='{$playerID}' AND scienceID = '{$scienceID}'";
    $r = $db->query($sql);
    if (!$r) {
        page_dberror();
    }
    if (!$r->isEmpty()) {
        return 4;
    }
    // check for scienceMaxDeps in Event_Handler
    $dep_count = 0;
    foreach ($science->maxScienceDepList as $key => $value) {
        if ($value != -1 && $caveData[$scienceTypeList[$key]->dbFieldName] > $value - 1) {
            if ($dep_count) {
                $deps .= ",";
            }
            $deps .= $key;
            $dep_count++;
        }
    }
    if ($dep_count) {
        $query = "SELECT event_scienceID FROM Event_science " . "WHERE playerID = '{$playerID}' AND scienceID IN ({$deps})";
        if (!($r = $db->query($query))) {
            page_dberror();
        }
        if (!$r->isEmpty()) {
            return 5;
        }
    }
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}'", "{$science->dbFieldName} < {$maxLevel}");
    // get all the resource costs
    foreach ($science->resourceProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $resourceTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - ({$formula})");
            array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            array_push($where, "{$dbField} >= ({$formula})");
        }
    }
    // get all the unit costs
    foreach ($science->unitProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $unitTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the building costs
    foreach ($science->buildingProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $buildingTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // get all the external costs
    foreach ($science->externalProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $defenseSystemTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependecies
    foreach ($science->buildingDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($science->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($science->defenseSystemDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($science->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($science->resourceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($science->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($science->scienceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($science->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($science->unitDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($science->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || !$db->affected_rows() == 1) {
        return 2;
    }
    $prodTime = 0;
    // calculate the production time;
    if ($time_formula = $science->productionTimeFunction) {
        $time_eval_formula = formula_parseToPHP($time_formula, '$caveData');
        $time_eval_formula = "\$prodTime={$time_eval_formula};";
        eval($time_eval_formula);
    }
    $prodTime *= SCIENCE_TIME_BASE_FACTOR;
    $now = time();
    $query = sprintf("INSERT INTO Event_science (caveID, playerID, scienceID, " . "`start`, `end`) VALUES (%d, %d, %d, '%s', '%s')", $caveID, $playerID, $scienceID, time_toDatetime($now), time_toDatetime($now + $prodTime));
    if (!$db->query($query)) {
        $db->query($setBack);
        return 2;
    }
    return 3;
}
Example #8
0
function wonder_processOrder($playerID, $wonderID, $caveID, $coordX, $coordY, $caveData, $db)
{
    global $defenseSystemTypeList, $unitTypeList, $buildingTypeList, $scienceTypeList, $resourceTypeList, $wonderTypeList, $WONDERRESISTANCE;
    if ($wonderTypeList[$wonderID]->target == "same") {
        $targetID = $caveID;
        $query = "SELECT * FROM Cave " . "WHERE caveID = '{$targetID}' ";
        if (!($result = $db->query($query)) || !($targetData = $result->nextRow())) {
            return -3;
        }
        $coordX = $targetData['xCoord'];
        $coordY = $targetData['yCoord'];
    } else {
        // check the target cave
        $query = "SELECT * FROM Cave " . "WHERE xCoord = '{$coordX}' " . "AND yCoord = '{$coordY}'";
        if (!($result = $db->query($query)) || !($targetData = $result->nextRow())) {
            return -3;
        }
        $targetID = $targetData['caveID'];
    }
    // check, if cave allowed
    if ($wonderTypeList[$wonderID]->target == "own") {
        $allowed = $playerID == $targetData['playerID'];
    } else {
        if ($wonderTypeList[$wonderID]->target == "other") {
            $allowed = $playerID != $targetData['playerID'];
        } else {
            // $wonderTypeList[$wonderID]->target == "all"  or == "same"
            $allowed = 1;
        }
    }
    if (!$allowed) {
        return -2;
    }
    $set = array();
    $setBack = array();
    $where = array("WHERE caveID = '{$caveID}' ");
    // get all the resource costs
    foreach ($wonderTypeList[$wonderID]->resourceProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $resourceTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - ({$formula})");
            array_push($setBack, "{$dbField} = {$dbField} + ({$formula})");
            array_push($where, "{$dbField} >= ({$formula})");
        }
    }
    // get all the unit costs
    foreach ($wonderTypeList[$wonderID]->unitProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $unitTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->buildingProductionCost as $key => $value) {
        if ($value != "" && $value != "0") {
            $formula = formula_parseToSQL($value);
            $dbField = $buildingTypeList[$key]->dbFieldName;
            array_push($set, "{$dbField} = {$dbField} - {$formula}");
            array_push($setBack, "{$dbField} = {$dbField} + {$formula}");
            array_push($where, "{$dbField} >= {$formula}");
        }
    }
    // generate SQL
    if (sizeof($set)) {
        $set = implode(", ", $set);
        $set = "UPDATE Cave SET {$set} ";
        $setBack = implode(", ", $setBack);
        $setBack = "UPDATE Cave SET {$setBack} WHERE caveID = '{$caveID}'";
    }
    // generate dependecies
    foreach ($wonderTypeList[$wonderID]->buildingDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$buildingTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxBuildingDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$wonderTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->defenseSystemDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxDefenseSystemDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$defenseSystemTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->resourceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxResourceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$resourceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->scienceDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxScienceDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$scienceTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->unitDepList as $key => $value) {
        if ($value != "" && $value != "0") {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} >= {$value}");
        }
    }
    foreach ($wonderTypeList[$wonderID]->maxUnitDepList as $key => $value) {
        if ($value != -1) {
            array_push($where, "{$unitTypeList[$key]->dbFieldName} <= {$value}");
        }
    }
    $where = implode(" AND ", $where);
    if (!$db->query($set . $where) || $db->affected_rows() != 1) {
        return 0;
    }
    // calculate the chance and evaluate into $chance
    if ($chance_formula = $wonderTypeList[$wonderID]->chance) {
        $chance_eval_formula = formula_parseToPHP($chance_formula, '$caveData');
        $chance_eval_formula = "\$chance={$chance_eval_formula};";
        eval($chance_eval_formula);
    }
    // if this wonder is offensive
    // calculate the wonder resistance and evaluate into $resistance
    // TODO: Wertebereich der Resistenz ist derzeit 0 - 1, also je höher desto resistenter
    if ($wonderTypeList[$wonderID]->offensiveness == "offensive") {
        $resistance_eval_formula = formula_parseToPHP($WONDERRESISTANCE, '$targetData');
        $resistance_eval_formula = "\$resistance={$resistance_eval_formula};";
        eval($resistance_eval_formula);
    } else {
        $resistance = 0.0;
    }
    // does the wonder fail?
    if ((double) rand() / (double) getRandMax() > $chance - $resistance) {
        return 2;
        // wonder did fail
    }
    // schedule the wonder's impacts
    // create a random factor between -0.3 and +0.3
    $delayRandFactor = rand(0, getrandmax()) / getrandmax() * 0.6 - 0.3;
    // now calculate the delayDelta depending on the first impact's delay
    $delayDelta = $wonderTypeList[$wonderID]->impactList[0]['delay'] * $delayRandFactor;
    foreach ($wonderTypeList[$wonderID]->impactList as $impactID => $impact) {
        $delay = (int) (($delayDelta + $impact['delay']) * WONDER_TIME_BASE_FACTOR);
        $now = time();
        $query = sprintf("INSERT INTO Event_wonder (casterID, sourceID, targetID, " . "wonderID, impactID, `start`, `end`) " . "VALUES (%d, %d, %d, %d, %d, '%s', '%s')", $playerID, $caveID, $targetID, $wonderID, $impactID, time_toDatetime($now), time_toDatetime($now + $delay));
        if (!$db->query($query)) {
            $db->query($setBack);
            return -1;
        }
    }
    // create messages
    $sourceMessage = "Sie haben auf die H&ouml;hle in {$coordX}/{$coordY} ein Wunder " . $wonderTypeList[$wonderID]->name . " erwirkt.";
    $targetMessage = "Der Besitzer der H&ouml;hle in {$caveData['xCoord']}/{$caveData['yCoord']} " . "hat auf Ihre H&ouml;hle in {$coordX}/{$coordY} ein Wunder gewirkt.";
    messages_sendSystemMessage($playerID, 9, "Wunder erwirkt auf {$coordX}/{$coordY}", $sourceMessage, $db);
    messages_sendSystemMessage($targetData['playerID'], 9, "Wunder!", $targetMessage, $db);
    return 1;
}
Example #9
0
function formula_parseToSelectSQL($formula)
{
    global $FORMULA_SYMBOLS;
    $sql = "";
    $farmmalus = max($_SESSION['player']->fame - FREE_FARM_POINTS, 0);
    $formula = str_replace("[E25.ACT]", $farmmalus, $formula);
    // abstract functions are sql functions -> no translation needed
    // parse symbols
    for ($i = 0; $i < strlen($formula); $i++) {
        // opening brace
        if ($formula[$i] == '[') {
            $symbol = $formula[++$i];
            $index = 0;
            while ($formula[++$i] != '.') {
                $index = $index * 10 + ($formula[$i] + 0);
            }
            $field = substr($formula, ++$i, 3);
            // 'ACT]' or 'MAX]'
            $i += 3;
            if (strncasecmp($field, "ACT", 3) == 0) {
                $sqlFields[$FORMULA_SYMBOLS[$symbol][$index]->dbFieldName] = true;
            } else {
                if (strncasecmp($field, "MAX", 3) == 0) {
                    formula_parseToSQL($FORMULA_SYMBOLS[$symbol][$index]->maxLevel);
                }
            }
        }
    }
    return array_keys($sqlFields);
}