/** * Test assigning value to variable if it is null * @covers \Copycat\System\Logic::onNullAssign() */ public function testOnNullAssign() { $test = null; Logic::onNullAssign($test, 'foo'); $this->assertTrue($test === 'foo'); Logic::onNullAssign($test, 'bar'); $this->assertFalse($test === 'bar'); }
public static function __and__($number) { $list = array(); for ($i = 0; $i < $number; $i++) { array_push($list, self::getCondition()); } return self::addCondition(Logic::__and__($list)); }
/** * @param array $patterns * @return \Closure */ protected static function __match(array $patterns) { return function (...$args) use($patterns) { // [a] -> Bool $patternApplies = function ($pattern) use($args) { /** @noinspection PhpParamsInspection */ return Logic::all(Arrays::zipWith(Lambda::apply(), Arrays::map(self::make(), Arrays::init($pattern)), $args)); }; try { /** @noinspection PhpParamsInspection */ $getMatchedImplementation = Lambda::compose(Arrays::last(), Arrays::first($patternApplies), Arrays::filter(function ($pattern) use($args) { return count($pattern) - 1 === count($args); })); return call_user_func_array($getMatchedImplementation($patterns), $args); } catch (\Exception $e) { throw new IncompletePatternMatchException('Incomplete pattern match expression.'); } }; }
public function init($params) { parent::init($params); $dbs = $params['mysql']; $this->db = new MysqlDb($dbs['host'], $dbs['user'], $dbs['password'], $dbs['dbname']); }
<?php /** * Created by JetBrains PhpStorm. * User: Effus * Date: 01.12.13 * Time: 22:47 * To change this template use File | Settings | File Templates. */ include_once 'exceptions.php'; include_once 'lib.php'; include_once 'storage.php'; include_once 'coins.php'; include_once 'funds.php'; include_once 'pairs.php'; include_once 'helpers.php'; include_once 'logic.php'; if (count($argv) < 2) { log_msg('Usage: php command.php <config-file.json>', true); } try { $logic = new Logic(); $startParams = array('baseCoin' => new Coin('btc'), 'min_fund_amount' => 0.012, 'expire_fund' => 60 * 10, 'expire_pairs' => 60, 'expire_pairs_life' => 60 * 2, 'diffs_sell' => array('btc_rur' => 15, 'btc_usd' => 1, 'btc_eur' => 2, 'ltc_btc' => 0.002), 'diffs_buy' => array('btc_rur' => 60, 'btc_usd' => 2, 'btc_eur' => 2, 'ltc_btc' => 0.002), 'capture_count' => array('sell' => 2, 'buy' => 6)); $logic->init($startParams); $logic->run(); } catch (Exception $e) { log_msg('Connection failed: ' . $e->getMessage(), true); }
} } } class Logic { private $sanitized_info = array(); function process($info) { $this->sanitize_input($info); $this->calc_sum(); } private function sanitize_input($info) { foreach ($info as $key => $piece) { $sanitized_pieces = explode(' ', $piece); foreach ($sanitized_pieces as $sanitized_piece) { $this->sanitized_info[$key][] = $sanitized_piece; } } } private function calc_sum() { foreach ($this->sanitized_info as $info) { $sum = $info[0] + $info[1]; print $sum . "\n"; } } } $perception = new Perception(); $logic = new Logic(); $logic->process($perception->info);
<?php require 'Logic.php'; require 'Json.php'; $json = new Json(); foreach ($json->getHeaders() as $value) { header($value); } $logic = new Logic($json); $logic->loginUser('*****@*****.**', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'); //Continue Game logic - dies if not logged in $logic->getState();
public function run() { while (true) { log_msg('run >> wait for ' . $this->pause . ' seconds...'); try { $lastUpdate = ''; $updateTime = $this->db->getLastUpdateTime(); if ($updateTime['dt'] != $lastUpdate) { $lastUpdate = $updateTime['dt']; } else { continue; } sleep($this->pause); log_msg('run >> loading data'); $ms = microtime(true); $DBconf = $this->db->loadActiveConfiguration(); if (!isset($DBconf['baseCoin'])) { throw new BtceMysqlException('db conf not loaded'); } /** * 'ANL','BT','WP','SP','ST','WB','BB' */ if (!isset($trendPair)) { $trendPair = ''; } switch ($DBconf['stage']) { case 'ANL': // analyze trends log_msg('ANL: started'); $res = $this->db->getMaxFund(); $ms = round(microtime(true) - $ms, 5); log_msg('ANL >> funds loaded at ' . $ms . ' sec'); $fund = $res['code']; $fundAmount = $res['amount']; if ($fundAmount > 0) { if ($DBconf['baseCoin'] != $fund) { $this->db->setBaseCoin($fund); log_msg('ANL >> change baseCoin to ' . $fund . ' with fund amount: ' . $fundAmount); } } else { log_msg('ANL >> expecting funds...'); $this->pause = 20; continue; } // look at most tranded $prices = $this->db->loadPrices('trend', $fund); $maxtrend = 0; for ($i = 0; $i < count($prices); $i++) { if (abs($prices[$i]['trend']) > $maxtrend && abs($prices[$i]['trend']) > 3) { $orders = $this->db->getLastOrder($trendPair); if (count($orders) > 0) { $maxtrend = $prices[$i]['trend']; $trendPair = $prices[$i]['pair']; } } } if (!$trendPair) { log_msg('ANL >> no trands with our pairs, look anything...'); for ($i = 0; $i < count($prices); $i++) { if (abs($prices[$i]['trend']) > $maxtrend && abs($prices[$i]['trend']) > 3) { $orders = $this->db->getLastOrder($trendPair); $maxtrend = $prices[$i]['trend']; $trendPair = $prices[$i]['pair']; } } } if ($maxtrend && $trendPair) { log_msg('ANL >> get pair : ' . $trendPair . ' / trend: ' . $maxtrend); $coinA = substr($trendPair, 0, 3); $coinB = substr($trendPair, 4, 3); if ($maxtrend > 0) { log_msg('ANL >> positive trend'); if ($fund == $coinB) { // XXX/FUND, up, need to buy XXX log_msg('ANL >> we need BUY(BT)'); $this->db->setStrategy('BUY'); $this->db->setStage('BT'); } else { if ($fund == $coinA) { // FUND/XXX, up, wait peak log_msg('ANL >> we need wait peak(WP)'); $this->db->setStrategy('BUY'); $this->db->setStage('WP'); } } } else { log_msg('ANL >> negative trend'); if ($fund == $coinB) { // XXX/FUND, down, wait log_msg('ANL >> we need wait bottom(WB)'); $this->db->setStrategy('SELL'); $this->db->setStage('WB'); } else { if ($fund == $coinA) { // FUND/XXX, down, sell for XXX log_msg('ANL >> we need SELL(BB)'); $this->db->setStrategy('SELL'); $this->db->setStage('ST'); } } } $this->pause = 0; } else { log_msg('ANL >> no interesting trends'); $this->db->setStrategy('WAIT'); $this->pause = 30; } break; /* ------ BUY strategy ------- */ /* ------ BUY strategy ------- */ case 'BT': // Buy at trend log_msg('BT: started'); if (!$trendPair) { // pair not defined $this->db->setStrategy('WAIT'); $this->db->setStage('ANL'); log_msg('BT: undefined pair, return to ANL stage'); $this->pause = 30; continue; } $prices = $this->db->loadPrices('trend', $trendPair); $this->storage = Loader::storage(); $api = Loader::api($this->storage->data->key, $this->storage->data->secret); // check for minimal amount for this fund if (!$fundAmount || $fundAmount < $DBconf['minFund'] || !$prices[0]['buy']) { throw new BtceLogicException('BT.fundAmount[' . $DBconf['baseCoin'] . '] is low than minFund:[' . $DBconf['minFund'] . ']'); } $fundAmount = $fundAmount - $DBconf['minFund']; $fee = $fundAmount * 0.0002; $fundAmount -= $fee; $fundAmount = round($fundAmount, 8); $buyAmount = round($fundAmount / $prices[0]['buy'], 8); // expected result $calcResult = Logic::getOrderResult($buyAmount, $prices[0]['buy'], 0.02); log_msg('BT: try to make order. Pair: ' . $prices[0]['pair'] . ', BUY ' . $buyAmount . ' with price: ' . $prices[0]['buy']); // make real order try { $apiResult = $api->makeOrder($buyAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_BUY, $prices[0]['buy']); } catch (BTCeAPIException $e) { throw new BTCeAPIException('BT: API exception message:' . $e->getMessage()); } if (isset($apiResult['return']) && isset($apiResult['return']['order_id'])) { $orderComplete = $this->expectOrder($apiResult['return']['order_id'], 600); // expect order complete for 10 minutes if ($orderComplete) { // success $this->db->registerOrder($apiResult['return']['order_id'], $prices[0]['pair'], 'buy', $buyAmount, $prices[0]['buy'], $calcResult); $bcCode = substr($prices[0]['pair'], 0, 3); // set stage for old fund $this->db->setStage('ANL'); $this->db->setBaseCoin($bcCode); // set stage for new fund $this->db->setStage('WP'); log_msg('BT: new base coin: ' . $bcCode . ', next stage is WP'); continue; } else { // fail log_msg('BT: order is cancelled. Return to ANL stage in current funds'); $this->pause = 30; } } else { throw new BTCeAPIException('Bad API result: ' . print_r($apiResult, true)); } break; case 'WP': // Wait for peak log_msg('WP: check...'); $lastOrderRow = $this->db->getLastOrder($trendPair); $pair = $lastOrderRow['pair']; if (!$pair) { log_msg('WP: havent orders with trend pair: ' . $trendPair); $lastOrderRow = $this->db->getLastOrder(); $pair = $lastOrderRow['pair']; } log_msg('WP: look at pair: ' . $pair); $border = $lastOrderRow['price'] + $lastOrderRow['price'] * 0.02 * 2; $prices = $this->db->loadPrices('trend', $pair); if ($prices[0]['pair'] == $pair) { if ($prices[0]['trend'] < 0) { if ($prices[0]['sell'] > $border) { // trend rotated, price good $this->db->setStage('SP'); $this->pause = 0; log_msg('WP >> ' . $pair . ' yes, time to sell'); } else { log_msg('WP >> ' . $pair . ' sell price low than border: ' . $prices[0]['sell'] . ' / ' . $border); $this->pause = 30; } } else { log_msg('WP >> trend is grow up'); $this->pause = 30; } } else { throw new BtceLogicException('WP: Unknown pair ' . $pair . ', trend pair: ' . $trendPair); } break; case 'SP': // Sell at peak if (!$trendPair) { $lastOrderRow = $this->db->getLastOrder(); $pair = $lastOrderRow['pair']; } else { $pair = $trendPair; } $prices = $this->db->loadPrices('trend', $pair); $this->storage = Loader::storage(); $api = Loader::api($this->storage->data->key, $this->storage->data->secret); if ($prices[0]['pair'] != $pair) { $this->pause = 10; continue; } $res = $this->db->getFund($DBconf['baseCoin']); $fundAmount = $res['amount']; if (!$fundAmount || $fundAmount < $DBconf['minFund'] || !$prices[0]['sell']) { throw new BtceLogicException('SP.fundAmount[' . $DBconf['baseCoin'] . '] is low than minFund:[' . $DBconf['minFund'] . ']'); } // expected result $calcResult = Logic::getOrderResult($fundAmount, $prices[0]['sell'], 0.02); log_msg('SP: try to make order. Pair: ' . $prices[0]['pair'] . ', SELL ' . $fundAmount . ' with price: ' . $prices[0]['sell']); // make real order try { $apiResult = $api->makeOrder($fundAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_SELL, $prices[0]['sell']); } catch (BTCeAPIException $e) { if (strpos($e->getMessage(), 'It is not enough')) { $fundAmount -= 1.0E-6; $apiResult = $api->makeOrder($fundAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_SELL, $prices[0]['sell']); } } if (isset($apiResult['return']) && isset($apiResult['return']['order_id'])) { $orderComplete = $this->expectOrder($apiResult['return']['order_id'], 600); // expect order complete for 10 minutes if ($orderComplete) { // success $this->db->registerOrder($apiResult['return']['order_id'], $prices[0]['pair'], 'sell', $fundAmount, $prices[0]['sell'], $calcResult); $bcCode = substr($prices[0]['pair'], 4, 3); // set stage for old fund $this->db->setStage('ANL'); $this->db->setBaseCoin($bcCode); // set stage for new fund $this->db->setStage('ANL'); log_msg('SP: new base coin: ' . $bcCode . ', next stage is ANL'); continue; } else { // fail log_msg('SP: order is cancelled. Return to ANL stage in current funds'); $this->pause = 30; } } else { throw new BTCeAPIException('Bad API result: ' . print_r($apiResult, true)); } break; /** ------------ SELL strategy ------------ */ /** ------------ SELL strategy ------------ */ case 'ST': // Sell at trend log_msg('ST: started'); if (!$trendPair) { // pair not defined $this->db->setStrategy('WAIT'); $this->db->setStage('ANL'); log_msg('ST: undefined pair, return to ANL stage'); $this->pause = 30; continue; } $prices = $this->db->loadPrices('trend', $trendPair); $this->storage = Loader::storage(); $api = Loader::api($this->storage->data->key, $this->storage->data->secret); // check for minimal amount for this fund if (!$fundAmount || $fundAmount < $DBconf['minFund'] || !$prices[0]['sell']) { throw new BtceLogicException('ST.fundAmount[' . $DBconf['baseCoin'] . '] is low than minFund:[' . $DBconf['minFund'] . ']'); } $fundAmount = $fundAmount - $DBconf['minFund']; // expected result $calcResult = Logic::getOrderResult($fundAmount, $prices[0]['sell'], 0.02); log_msg('ST: try to make order. Pair: ' . $prices[0]['pair'] . ', SELL ' . $fundAmount . ' with price: ' . $prices[0]['sell']); // make real order try { $apiResult = $api->makeOrder($fundAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_SELL, $prices[0]['sell']); } catch (BTCeAPIException $e) { if (strpos($e->getMessage(), 'It is not enough')) { $fundAmount -= 1.0E-6; $apiResult = $api->makeOrder($fundAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_SELL, $prices[0]['sell']); } } if (isset($apiResult['return']) && isset($apiResult['return']['order_id'])) { $orderComplete = $this->expectOrder($apiResult['return']['order_id'], 600); // expect order complete for 10 minutes if ($orderComplete) { // success $this->db->registerOrder($apiResult['return']['order_id'], $prices[0]['pair'], 'sell', $fundAmount, $prices[0]['sell'], $calcResult); $bcCode = substr($prices[0]['pair'], 4, 3); // set stage for old fund $this->db->setStage('ANL'); $this->db->setBaseCoin($bcCode); // set stage for new fund $this->db->setStage('WB'); log_msg('ST: new base coin: ' . $bcCode . ', next stage is WP'); continue; } else { // fail log_msg('BT: order is cancelled. Return to ANL stage in current funds'); $this->pause = 30; } } else { throw new BTCeAPIException('Bad API result: ' . print_r($apiResult, true)); } break; case 'WB': // Wait for bottom log_msg('WB: check...'); $lastOrderRow = $this->db->getLastOrder($trendPair); $pair = $lastOrderRow['pair']; if (!$pair) { log_msg('WB: havent orders with trend pair: ' . $trendPair); $lastOrderRow = $this->db->getLastOrder(); $pair = $lastOrderRow['pair']; } log_msg('WB: look at pair: ' . $pair); $border = $lastOrderRow['price'] + $lastOrderRow['price'] * 0.02 * 2; $prices = $this->db->loadPrices('trend', $pair); if ($prices[0]['pair'] == $pair) { if ($prices[0]['trend'] > 0) { if ($prices[0]['buy'] < $border) { // trend rotated, price good $this->db->setStage('BB'); $this->pause = 0; log_msg('WB >> ' . $pair . ' yes, time to buy'); } else { log_msg('WB >> ' . $pair . ' buy price more than border: ' . $prices[0]['buy'] . ' / ' . $border); $this->pause = 30; } } else { log_msg('WB >> trend is go down... ' . $prices[0]['trend'] . ' / ' . $prices[0]['buy']); $this->pause = 30; } } else { throw new BtceLogicException('WB: Unknown pair ' . $pair . ', trend pair: ' . $trendPair); } break; case 'BB': // Buy at bottom if (!$trendPair) { $lastOrderRow = $this->db->getLastOrder(); $pair = $lastOrderRow['pair']; } else { $pair = $trendPair; } $prices = $this->db->loadPrices('trend', $pair); $this->storage = Loader::storage(); $api = Loader::api($this->storage->data->key, $this->storage->data->secret); if ($prices[0]['pair'] != $pair) { $this->pause = 10; continue; } $res = $this->db->getFund($DBconf['baseCoin']); $fundAmount = $res['amount']; if (!$fundAmount || $fundAmount < $DBconf['minFund'] || !$prices[0]['buy']) { throw new BtceLogicException('BB.fundAmount[' . $DBconf['baseCoin'] . '] is low than minFund:[' . $DBconf['minFund'] . ']'); } $fundAmount = $fundAmount - $DBconf['minFund']; $fee = $fundAmount * 0.0002; $fundAmount -= $fee; $fundAmount = round($fundAmount, 8); $buyAmount = round($fundAmount / $prices[0]['buy'], 8); // expected result $calcResult = Logic::getOrderResult($buyAmount, $prices[0]['buy'], 0.02); log_msg('BB: try to make order. Pair: ' . $prices[0]['pair'] . ', BUY ' . $buyAmount . ' with price: ' . $prices[0]['buy']); // make real order try { $apiResult = $api->makeOrder($buyAmount, $prices[0]['pair'], BTCeAPI::DIRECTION_BUY, $prices[0]['buy']); } catch (BTCeAPIException $e) { throw new BTCeAPIException('BB: API exception message:' . $e->getMessage()); } if (isset($apiResult['return']) && isset($apiResult['return']['order_id'])) { $orderComplete = $this->expectOrder($apiResult['return']['order_id'], 600); // expect order complete for 10 minutes if ($orderComplete) { // success $this->db->registerOrder($apiResult['return']['order_id'], $prices[0]['pair'], 'buy', $buyAmount, $prices[0]['buy'], $calcResult); $bcCode = substr($prices[0]['pair'], 0, 3); // set stage for old fund $this->db->setStage('ANL'); $this->db->setBaseCoin($bcCode); // set stage for new fund $this->db->setStage('WP'); log_msg('BB: new base coin: ' . $bcCode . ', next stage is WP'); continue; } else { // fail log_msg('BB: order is cancelled. Return to ANL stage in current funds'); $this->pause = 30; } } break; } } catch (Exception $e) { log_msg('run >> exception: (' . $e->getCode() . ') ' . $e->getMessage()); $this->db->setStage('ANL'); $this->pause = 30; } } }
<?php require_once __DIR__ . '/Logic.class.php'; $logic = new Logic(); echo "asc array(10, 8, 3, 4, 6, 5, 2, 1, 9, 7)\n"; print_r($logic->asc(array(10, 8, 3, 4, 6, 5, 2, 1, 9, 7))); echo "desc array(10, 8, 3, 4, 6, 5, 2, 1, 9, 7)\n"; print_r($logic->desc(array(10, 8, 3, 4, 6, 5, 2, 1, 9, 7)));
public function Calculate($operations, $numbers) { //TODO: write function that calculate // Multiplied first foreach ($operations as $key => $value) { if ($value == "*") { echo ","; $c = $key + 1; $numbers[$key] = Logic::multi($numbers[$key], $numbers[$c]); // remove number & array reorder unset($numbers[$c]); $numbers = array_values($numbers); // remove operator and reorder unset($operations[$key]); $operations = array_values($operations); break; } } // than calculate the other while (isset($numbers[1])) { foreach ($operations as $key => $value) { switch ($value) { case "+": $c = $key + 1; $numbers[$key] = Logic::add($numbers[$key], $numbers[$c]); // remove number & array reorder unset($numbers[$c]); $numbers = array_values($numbers); // remove operator and reorder unset($operations[$key]); $operations = array_values($operations); break; case "-": $c = $key + 1; $numbers[$key] = Logic::sub($numbers[$key], $numbers[$c]); // remove number & array reorder unset($numbers[$c]); $numbers = array_values($numbers); // remove operator and reorder unset($operations[$key]); $operations = array_values($operations); break; case "/": $c = $key + 1; $numbers[$key] = Logic::div($numbers[$key], $numbers[$c]); // remove number & array reorder unset($numbers[$c]); $numbers = array_values($numbers); // remove operator and reorder unset($operations[$key]); $operations = array_values($operations); break; } } } return $numbers[0]; }
<?php include_once 'includes/common.php'; include_once 'includes/logic.php'; include_once 'graphviz/GraphViz.php'; Logic::load(); $graph = new Image_GraphViz(); $i = 0; foreach (Logic::$contacts as $contact) { if (strlen($contact->nombre) > 0) { $nodeName = camelCase($contact->nombre); $nodeLabel = $contact->nombre; if (strlen($contact->empresa) > 0) { $nodeLabel .= "\n" . $contact->empresa; } } elseif (strlen($contact->empresa) > 0) { $nodeName = camelCase($contact->empresa); $nodeLabel = $contact->empresa; } else { $nodeName = "node{$i}"; $nodeLabel = '(nada)'; } $graph->addNode($nodeName, array('label' => $nodeLabel, 'shape' => 'octagon', 'fontsize' => '8')); if (sizeof($contact->relations) > 0) { foreach ($contact->relations as $relation) { if ($relation->meetInPerson) { $colorRelation = 'black'; } else { $colorRelation = 'grey'; } if (strlen($relation->nombre) > 0) {
} $p_cmd = $arr_query['cmd']; $p_sid = $arr_query['sid']; $p_pwd = $arr_query['pwd']; $p_key = $arr_query['key']; $p_val = $arr_query['val']; //if ($p_cmd != 'play'){ // print 'cmd => '.$p_cmd.'<br>'; // print 'sid => '.$p_sid.'<br>'; // print 'key => '.$p_key.'<br>'; // print 'val => '.$p_val.'<br>'; //} $db = new IdoDB(); if ($p_cmd == 'play') { $ret = array('empty', 'err.html'); $lg = new Logic(); if ($p_key == 'regist') { $ret = $lg->autoRun(); } else { if ($p_key == 'second') { $ret = $lg->playSecond(); } else { if ($p_key == 'signup') { $ret = $lg->playSignup(); } else { if ($p_key == 'login') { $ret = $lg->login($p_sid, $p_pwd); } else { if ($p_key == 'login_only') { $ret = $lg->login_only($p_sid, $p_pwd); } else {
for($c = $key+1; $numbers[$c] != " "; $c++){ unset($numbers[$c]); } } if($value == " "){ unset($numbers[$key]); } } foreach($operations as $key=>$value){ if($value == "("){ $operations[$key] = null; for($c = $key+1; $operations[$c] != ")"; $c++){ unset($operations[$c]); } } if($value == ")"){ unset($operations[$key]); } } } }*/ $result = Logic::getInstance()->Calculate($operations, $numbers); //debug outputs echo " = ", $result, "<br>"; var_dump($operations, $numbers); echo "<br>"; var_dump($extractedBracketsNums, $extractedBracketsOper);
<?php include_once 'includes/common.php'; include_once 'includes/logic.php'; header('Content-Type: text/plain'); Logic::load(); echo Logic::toString();