function convertPlacementToScenario() { $placements = json_decode(file(FNPL)[0], true); $scenario = Scenario::buildScenarioByPlacements($placements); $scenario['vms'] = json_decode(file(FNVM)[0], true); $scenario['pms'] = json_decode(file(FNPM)[0], true); file_put_contents(FNSC, json_encode($scenario)); return $scenario; }
/** * @param $user User */ public static function createNewScenario($user) { global $db, $PARTY_IDS, $PARTY_NAMES; //Figure out what job the user has if ($user->job_id == JOB_IDS::REPORTER) { $effected_party_id = rand(1, count($PARTY_IDS)); $is_users_party = $effected_party_id == $user->party_id; $target_name = Scenario::generateRandomName(); //Is the news good or bad? And how bad? $severity_scale = rand(1, 100); // 50 is neutral, 1 is terrible, 100 very good $random_event = null; $options = array(); if ($severity_scale < 10) { //terrible event $random_event = $db->query_one("SELECT scenario_description, scenario_title FROM scenario_descriptions WHERE job_id = {$user->job_id} AND severity_scale = " . SEVERITY_SCALE::TERRIBLE . " ORDER BY RAND() LIMIT 1"); } else { if ($severity_scale <= 50) { //mildly bad event $random_event = $db->query_one("SELECT scenario_description, scenario_title FROM scenario_descriptions WHERE job_id = {$user->job_id} AND severity_scale = " . SEVERITY_SCALE::MILDLY_BAD . " ORDER BY RAND() LIMIT 1"); } else { if ($severity_scale <= 90) { //mildly good event $random_event = $db->query_one("SELECT scenario_description, scenario_title FROM scenario_descriptions WHERE job_id = {$user->job_id} AND severity_scale = " . SEVERITY_SCALE::MILDLY_GOOD . " ORDER BY RAND() LIMIT 1"); } else { //amazing event. $random_event = $db->query_one("SELECT scenario_description, scenario_title FROM scenario_descriptions WHERE job_id = {$user->job_id} AND severity_scale = " . SEVERITY_SCALE::AMAZING . " ORDER BY RAND() LIMIT 1"); } } } $options[0] = array("text" => "Stretch the negatives"); $options[1] = array("text" => "Report just the facts"); $options[2] = array("text" => "Downplay/minimize with facts"); $options[3] = array("text" => "Stretch the positive angles"); //add the NAME and PARTY into the event. // event: "[name] ([party]) was speeding and got out of a ticket because they are a politician. $scenario_description = str_replace(array("[name]", "[party]"), array($target_name, $PARTY_IDS[$effected_party_id]), $random_event['scenario_description']); $scenario_title = str_replace(array("[name]", "[party]"), array($target_name, $PARTY_IDS[$effected_party_id]), $random_event['scenario_title']); //save to database $scenario_id = $db->query("INSERT INTO scenarios\n (user_id, job_id,\n description, name,\n scenario_status, scenario_date, severity,\n target_name, effected_party_id)\n VALUES\n ({$user->user_id}, {$user->job_id}, x'" . bin2hex($scenario_description) . "', x'" . bin2hex($scenario_title) . "', 0, DATE(NOW()), {$severity_scale}, x'" . bin2hex($target_name) . "', {$effected_party_id})"); //insert the options $option_inserts = array(); foreach ($options as $option) { $empty_array = serialize(array()); $option_inserts[] = "({$scenario_id}, x'" . bin2hex($option['text']) . "',x'" . bin2hex($empty_array) . "')"; } $db->query("INSERT INTO scenario_options (scenario_id, scenario_option_description, scenario_option_result) VALUES " . implode(",", $option_inserts)); } return new Scenario($scenario_id); }
/** * Cron controller */ public function index() { if (isset($_POST['authors'])) { print_r($_POST); $scenario = new Scenario($this->input->post('scenario')); $authors_ids = $this->input->post('authors'); $authors = new Author(); $authors->where_in('id', $authors_ids)->get(); foreach ($authors as $author) { echo 'adding ' . $author . '<br>'; $scenario->save_authors($author); } } elseif (isset($_POST['author'])) { print_r($_POST); $author = new Author(); $author->name = $_POST['author']; $author->save(); echo 'Created author:' . $_POST['author']; } elseif (isset($_POST['storelink'])) { print_r($_POST); $scenario = new Scenario($_POST['storelink_scenario']); $scenario->link = $_POST['storelink']; $scenario->save(); echo 'Added storelink to: ' . $_POST['storelink_scenario']; } $data['scenarios'] = new Scenario(); $data['scenarios']->where_related('authors', 'id IS NULL', null)->order_by('name', 'asc')->get(); $data['scenarios'] = $data['scenarios']->all_to_single_array('name'); $data['storelink_scenarios'] = new Scenario(); $data['storelink_scenarios']->where('link IS NULL', null)->order_by('name', 'asc')->get(); $data['storelink_scenarios'] = $data['storelink_scenarios']->all_to_single_array('name'); $data['authors'] = new Author(); $data['authors']->order_by('name', 'asc')->get(); $data['authors'] = $data['authors']->all_to_single_array('name'); $this->load->helper('form'); $this->load->view('admin', $data); }
/** * @depends testbuildScenarioByPlacements */ public function testJsonToScenario() { $placements = array(array('A:2', 'A:3'), array('B:1', 'B:2'), array('C:2', 'C:3'), array('D:1', 'D:2', 'D:3')); $scenario1 = Scenario::buildScenarioByPlacements($placements); $json = Scenario::toDataTableJSON($scenario1); $scenario2 = Scenario::getScenarioFromJSON($json); $this->assertEquals($scenario1['nvms'], $scenario2['nvms'], 'VM # !match'); $this->assertEquals($scenario1['npms'], $scenario2['npms'], 'PM # !match'); foreach ($scenario1['rvm'] as $key => $value) { $this->assertEquals($value, $scenario2['rvm'][$key], "# of placements of {$key} !match"); } foreach ($scenario1['rpm'] as $key => $value) { $this->assertEquals($value, $scenario2['rpm'][$key], "# of placements of {$key} !match"); } }
public function postUpdate($tournament) { if ($tournament->user != Auth::user()->id) { return Redirect::to('tournaments'); } $tournament->name = Input::get('name'); $maps = array_keys(Input::get('maps', [])); $players = array_keys(Input::get('players.ids', [])); $names = array_keys(Input::get('players.names', [])); if (is_null($tournament->name)) { return Redirect::back(); } $total = count($names) + count($players); if (ceil($total / 2) != count($maps)) { return Redirect::back(); } $tournament->save(); $tournament->maps()->detach(); foreach ($maps as $map) { $tournament->maps()->attach($map); } $tournament->players()->detach(); foreach ($players as $player) { $tournament->players()->attach($player); } $scenarii = new Collection(); $tournament->maps->each(function ($map) use(&$scenarii, $tournament) { if ($scenarii->count() < 2) { $scenarii = Scenario::where('season', Carbon::now()->year)->get()->shuffle(); } $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); $map->scenarii($tournament)->attach($scenarii->pop()->id, ['tournament' => $tournament->id]); }); foreach ($names as $name) { $player = new Player(); $player->name = $name; $player->user = Auth::user()->id; $player->save(); $tournament->players()->attach($player->id); } if ($total % 2 != 0) { $tournament->players()->attach(User::fantom()->id); } return Redirect::to('tournaments/' . $tournament->id); }
static function getCombinatorialSliceQuantities($scenario, $maxVM) { require_once 'libs/Combinatorics.php'; $combinatorics = new Math_Combinatorics(); $sliceSize = $maxVM * 2 + 1; $input = array_keys($scenario['placements']); $combinations = $combinatorics->combinations($input, $sliceSize); $quatities = array(); foreach ($combinations as $combination) { $slice = array(); foreach ($combination as $index) { $slice[] = $scenario['placements'][$index]; } $sliceScenario = Scenario::buildScenarioByPlacements($slice); $quatities[] = Approximation::calcularComRegrasMaxVMOutIn($sliceScenario, $maxVM); } return $quatities; }
public function generate($type = 0) { $scenario_options = array('played_most', 'played_least', 'season', 'evergreen'); $person_options = array('player_complete_pfs', 'player_complete_core', 'gm_complete_pfs', 'gm_complete_core'); if (in_array($type, $scenario_options)) { // This is a scenario based statistic $scenarios = new Scenario(); $scenarios->where('archived IS NULL', NULL); if ($type == 'evergreen') { $scenarios->where('evergreen', '1'); } $scenarios->get(); $options = array(); foreach ($scenarios as $scenario) { $options[$scenario->id] = $scenario->players->count(); } // Sort if ($type == 'played_least') { asort($options); } else { arsort($options); } // Select only top 5 $options = array_slice($options, 0, 10, true); // Delete old data $this->where('type', $type)->get()->delete_all(); $number = 1; foreach ($options as $key => $option) { $this->clear(); $this->type = $type; $this->number = $number; $this->scenario_id = $key; $this->comment = $option; $this->created_on = date("Y-m-d H:i:s"); $this->save(); $number++; } } else { // This is a person based statistic $people = new Person(); $people->get(); $number = 1; $options = array(); foreach ($people as $person) { if ($type == 'player_complete_pfs') { $scenarios = $person->scenarios->where_join_field('players', 'pfs IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get(); $options[$person->id] = $scenarios->result_count(); } elseif ($type == 'gm_complete_pfs') { $scenarios = $person->scenarios->where_join_field('players', 'pfs_gm IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get(); $options[$person->id] = $scenarios->result_count(); } elseif ($type == 'player_complete_core') { $scenarios = $person->scenarios->where_join_field('players', 'core IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get(); $options[$person->id] = $scenarios->result_count(); } elseif ($type == 'gm_complete_core') { $scenarios = $person->scenarios->where_join_field('players', 'core_gm IS NOT NULL', NULL, FALSE)->where('archived IS NULL', NULL)->get(); $options[$person->id] = $scenarios->result_count(); } } // Sort arsort($options); // Select only top 5 $options = array_slice($options, 0, 10, true); // Delete old data $this->where('type', $type)->get()->delete_all(); // Get total amount of scenarios $scenarios = new Scenario(); $total = $scenarios->where('archived IS NULL', NULL)->count(); foreach ($options as $key => $option) { $this->clear(); $this->type = $type; $this->number = $number; $this->person_id = $key; $this->comment = $option . '/' . $total; $this->save(); $number++; } } return TRUE; }
<?php require_once '../vendor/twig/twig/lib/Twig/Autoloader.php'; Twig_Autoloader::register(); $dir = "*/request.php"; define("SCENARIO_CACHE_URL", "https://raw.githubusercontent.com/balanced/balanced-docs/master/scenario.cache"); getScenarioCache(); foreach (glob($dir) as $file) { $scenario_name = dirname($file); $scenario_func = new Scenario($scenario_name); $rendered = $scenario_func->render(); if ($rendered) { $request = $scenario_func->write_executable($rendered); $scenario_func->write_mako(); } else { @unlink($scenario_name . "/executable.php"); echo "Error rendering {$scenario_name}\n"; } } function getScenarioCache() { if (file_exists("../scenario.cache")) { unlink("../scenario.cache"); } $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, SCENARIO_CACHE_URL); curl_setopt($ch, CURLOPT_SSLVERSION, 3); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch);
if ($round == null) { App::abort(404); } else { return $round; } }); Route::bind('map', function ($key, $route) { $round = Map::find($key); if ($round == null) { App::abort(404); } else { return $round; } }); Route::bind('scenario', function ($key, $route) { $round = Scenario::find($key); if ($round == null) { App::abort(404); } else { return $round; } }); Route::bind('report', function ($key, $route) { $report = Report::find($key); if ($report == null) { App::abort(404); } else { return $report; } }); Route::group(array('before' => 'auth'), function () {
public function testGetInstance() { $instance = Scenario::getInstance(); $this->assertInstanceOf('Scenario_Core', $instance); }
static function getScenarioFromJSON($json) { $table = json_decode($json, true); unset($table['cols'][0]); foreach ($table['cols'] as $k => $v) { $pms[] = $v['label']; } $placements = array(); foreach ($table['rows'] as $r => $row) { $tmp = array_shift($row['c']); $vm = $tmp['v']; foreach ($row['c'] as $k => $c) { if ($c['v'] == 1) { $pm = $pms[$k]; $placements[$r][] = "{$vm}:{$pm}"; } } } return Scenario::buildScenarioByPlacements($placements); }
<?php /** * Created by PhpStorm. * User: nickr_000 * Date: 10/6/2015 * Time: 10:10 PM */ require "../includes.php"; $scenario = null; //Check the database for an existing uncompleted scenario for this user $scenario_check = $db->query_one("SELECT scenario_id FROM scenarios WHERE user_id = {$user->user_id} AND status = " . SCENARIO_STATUS::AVAILABLE); if (isset($scenario_check['scenario_id'])) { $scenario = new Scenario($scenario_check['scenario_id']); } else { //if no uncompleted scenarios - see if the user has already completed a scenario for the day $scenario_check = $db->query_one("SELECT scenario_id FROM scenarios WHERE user_id = {$user->user_id} AND status = " . SCENARIO_STATUS::COMPLETED . " AND scenario_date = date(today())"); if (isset($scenario_check['scenario_id'])) { die("<h2>No new scenarios for today</h2>"); } } //if completely new user - or they simply didn't get a scenario - create a scenario for them if ($scenario == null) { $scenario = Scenario::createNewScenario($user); } //process and display the scenario
ini_set('xdebug.max_nesting_level', 1000); ini_set('max_execution_time', 0); $max = isset($_GET['max']) ? $_GET['max'] : 3; $nvm = isset($_GET['nvm']) ? $_GET['nvm'] : 7; $npm = isset($_GET['npm']) ? $_GET['npm'] : 3; $apr = isset($_GET['apr']) ? $_GET['apr'] : 0.75; //$_GET['file'] = 'Scenarios_APR(0.75)_VMs(10,12,14,16,18,20)_PMs(4).json'; if (isset($_GET['state'])) { $scenario = Scenario::getScenarioFromJSON($_GET['state']); } elseif (isset($_GET['file']) && file_exists('others/' . $_GET['file'])) { $scenario = array_pop(json_decode(file('others/' . $_GET['file'])[0], true)); } else { $scenarios = Scenario::geraScenarios($apr, array($nvm), array($npm)); $scenario = array_pop($scenarios); } $json = Scenario::toDataTableJSON($scenario); //$memcache = memcache_connect('localhost', 11211); //$accordion = isset($_GET['cache']) ? false : memcache_get($memcache, 'accordion'); //$accordion = false; //if ($accordion == false) { //echo "<script>alert('Not Cached');</script>"; $accordion = new Accordion(); $sem = Approximation::calcularSemRegras($scenario); $com = Approximation::calcularComRegras($scenario); $last = Approximation::calcularComRegrasMaxVMSub($scenario, $max); $tree = Approximation::treeSearchApproach($scenario, $max); $test1 = 0; //Approximation::calcularComRegrasMaxVMOutIn($scenario, $max); $test_c_a = 0; //Approximation::calculateAvgCombSplitterApproach($scenario, $max); $test_c_s = 0;
function playerprogress_get() { if (!$this->get('pfsnumber') || !$this->get('type')) { $this->response(NULL, 400); } $scenario = new Scenario(); $scenario->select('season, type')->distinct()->get(); $types = array(); foreach ($scenario as $s) { $types[] = $s->season; } $types[] = 'mod'; $types[] = 'ap'; $response = array(); $playerprogress = new Person(); $scen = new Scenario(); $playerprogress->where('pfsnumber', $this->get('pfsnumber'))->get(); foreach ($scenario as $s) { if ($s->type == 'mod') { // Modules $response['mod'] = array('season' => 'Modules', 'total' => 0, 'completed' => 0); $response['mod']['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('type', 'mod')->where('archived IS NULL', NULL)->get()->result_count(); $response['mod']['total'] = $scen->where('type', 'mod')->where('archived IS NULL', NULL)->count(); } elseif ($s->type == 'ap') { // Adventure paths $response['ap'] = array('season' => 'APs', 'total' => 0, 'completed' => 0); $response['ap']['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('type', 'ap')->where('archived IS NULL', NULL)->get()->result_count(); $response['ap']['total'] = $scen->where('type', 'ap')->where('archived IS NULL', NULL)->count(); } else { // Seasons $response[$s->season] = array('season' => $s->season, 'total' => 0, 'completed' => 0, 'contenttype' => $s->type); $response[$s->season]['completed'] = $playerprogress->scenarios->where_join_field('players', $this->get('type') . ' IS NOT NULL', NULL)->where('season', $s->season)->where('archived IS NULL', NULL)->get()->result_count(); $response[$s->season]['total'] = $scen->where('season', $s->season)->where('archived IS NULL', NULL)->count(); } } $this->response($response, 200); }
<?php require_once "../src/model/Scenario.php"; //Seta APR $apr = 0.75; //Seta intervalos de VMs $nvms = array(10, 12, 14, 16, 18, 20); //Seta intervalos de PMs $npms = array(4); //Gerar Scenarios $scenarios = Scenario::geraScenarios($apr, $nvms, $npms); //Converver para JSON $json = json_encode($scenarios); //Criar o nome do Arquivo $filename = sprintf("Scenarios_APR(%s)_VMs(%s)_PMs(%s).json", $apr, implode(',', $nvms), implode(',', $npms)); //Salvar no arquivo file_put_contents($filename, $json); echo "{$filename}";
/** * @depends testgetInsidersCombinations */ public function testtreeSearchApproach() { $placements = array(array('A:1', 'A:2'), array('B:2', 'B:3'), array('C:1', 'C:3'), array('D:1', 'D:2'), array('E:2', 'E:3'), array('F:2', 'F:3'), array('G:1', 'G:3'), array('H:1', 'H:3')); $scenario = Scenario::buildScenarioByPlacements($placements); $maxVM = 3; $resp = Approximation::treeSearchApproach($scenario, $maxVM); $this->assertEquals(80, $resp, "# of approximation !match."); }
<?php // Sample PDO adapter setup // make sure we've got the lib folder in our path (set to the directory where your Scenario folder is) set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/lib')); // load up Scenario (and therefore scenario_core) require_once 'Scenario.php'; // load up the adapter we want to use require_once 'Scenario/Data/Adapter/Pdo.php'; // set up the adapter $dsn = 'mysql:host=localhost;dbname=scenario'; $adapter = new Scenario_Data_Adapter_Pdo($dsn, 'username', 'password'); // configure the core Scenario::getInstance()->config(array('adapter' => $adapter));
?> Reaction</small></h2> </div> <div class="center"> <?php $formBuilder->generateResetButton($scriptName); ?> </div> <div class="center"> <?php $formBuilder->generateOptionsForm($scriptName, "Calculate", $options, $reactionID); ?> </div> <?php } else { $scenario = new Scenario($reactionID, $chain, $datetime); switch ($_SESSION['params']['t']) { case "d": $timeframeStr = "Daily"; break; case "w": $timeframeStr = "Weekly"; break; case "m": $timeframeStr = "Monthly"; break; } ?> <div class="page-header"> <h1 class="center"><?php echo $scenario->getReactionName();
public function listing() { $scenarii = Scenario::orderBy("season", "DESC")->orderBy("reference", "ASC")->get(); $this->display('scenarii.table', ['scenarii' => $scenarii]); }