public function getUnitInit(Request $req, CouchService $cs, AdminService $ad, $game, $arg = false)
 {
     $user = Auth::user()['name'];
     $wargame = urldecode($req->session()->get("wargame"));
     $chat = Input::get('chat', TRUE);
     $cs->setDb('games');
     $doc = $cs->get(urldecode($wargame));
     if ($user != $doc->createUser) {
         return redirect("wargame/play");
     }
     $opts = [];
     foreach ($_GET as $k => $v) {
         $opts[] = $k;
     }
     //        $this->load->model('users/users_model');
     $battle = Battle::battleFromName($game, $arg, $opts);
     $opts = [];
     foreach ($_GET as $k => $v) {
         $opts[] = $k;
     }
     //        $battle = $this->battle->getBattle($game, null, $arg, $opts);
     $cs->setDb('games');
     if (method_exists($battle, 'terrainInit')) {
         try {
             $terrainName = "terrain-{$game}.{$arg}";
             $terrainDoc = $cs->get($terrainName);
         } catch (\GuzzleHttp\Exception\BadResponseException $e) {
         }
         if (empty($terrainDoc)) {
             try {
                 $terrainName = "terrain-{$game}";
                 $terrainDoc = $cs->get($terrainName);
             } catch (\GuzzleHttp\Exception\BadResponseException $e) {
                 var_dump($e->getMessage());
             }
         }
         $battle->terrainName = $terrainName;
         $battle->terrainInit($terrainDoc);
     }
     if (method_exists($battle, 'init')) {
         $battle->init();
     }
     $doc->wargame = $battle->save();
     $click = $doc->_rev;
     $matches = array();
     preg_match("/^([0-9]+)-/", $click, $matches);
     $click = $matches[1];
     $doc->wargame->gameRules->phaseClicks[] = $click + 1;
     /* should probably get rid of this old code for genTerrain */
     if (isset($doc->wargame->genTerrain)) {
         try {
             $ter = $cs->get($doc->wargame->terrainName);
         } catch (\GuzzleHttp\Exception\BadResponseException $e) {
         }
         if (empty($ter)) {
             $data = array("_id" => $doc->wargame->terrainName, "docType" => "terrain", "terrain" => $doc->wargame->terrain);
             $cs->post($data);
         } else {
             $data = array("_id" => $doc->wargame->terrainName, "docType" => "terrain", "terrain" => $doc->wargame->terrain);
             /* totally throw the old one away */
             $cs->delete($doc->wargame->terrainName, $ter->_rev);
             $cs->post($data);
         }
         unset($doc->wargame->terrain);
         $doc->wargame->genTerrain = false;
     }
     $className = $doc->className = get_class($battle);
     $doc->chats = array();
     $doc->gameName = $game;
     $doc = $cs->put($doc->_id, $doc);
     event(new \App\Events\Analytics\RecordGameEvent(['docId' => $doc->id, 'type' => 'game-created', 'className' => $className, 'scenario' => $battle->scenario, 'arg' => $battle->arg, 'time' => time()]));
     return redirect("wargame/play-as/{$game}");
 }
 public function deleteMaps(CouchService $client, $id)
 {
     try {
         $doc = $client->get($id);
     } catch (\GuzzleHttp\Exception\BadResponseException $e) {
         return json_encode(new \stdClass());
     }
     $client->delete($doc->_id, $doc->_rev);
     return json_encode(new \stdClass());
 }
 function getDeleteGame(CouchService $cs, $gameName)
 {
     $user = Auth::user()['name'];
     $cs->setDb('games');
     if ($gameName) {
         try {
             $doc = $cs->get($gameName);
             if ($doc->createUser == $user || Auth::user()['is_admin']) {
                 if ($doc && $doc->_id && $doc->_rev) {
                     $cs->delete($doc->_id, $doc->_rev);
                 }
             }
         } catch (Exception $e) {
         }
     }
     return redirect('/admin/allgames');
 }