public function run()
 {
     $conf = \Config::get('couch');
     var_dump($conf);
     $user = $conf['username'];
     $pass = $conf['password'];
     $host = $conf['hostname'];
     $port = $conf['port'];
     $login = $user ? "{$user}:{$pass}@" : "";
     $guz = new \App\Services\CouchService(['base_uri' => "http://{$login}{$host}:{$port}/", 'timeout' => 1800.0]);
     $guz->setDb('analytics');
     $this->couchsag = $guz;
     $this->initDoc();
 }
 function cloneFile(CouchService $client, $stuff)
 {
     $client->setDb('rest');
     $doc = $client->get($stuff);
     if ($doc->docType == "hexMapData") {
         unset($doc->_id);
         unset($doc->_rev);
         $hexStr = $doc->map->hexStr;
         $doc->map->hexStr = "";
         $ret = $client->post($doc);
         $mapId = $ret->id;
         $mapRev = $ret->rev;
         if ($ret->ok === true) {
             echo "good ";
             if ($hexStr) {
                 echo "better '";
                 $hexDoc = $client->get($hexStr);
                 unset($hexDoc->_id);
                 unset($hexDoc->_rev);
                 $hexDoc->hexStr->map = $mapId;
                 $hexRet = $client->post($hexDoc);
                 if ($hexRet->ok) {
                     echo "Best";
                     $doc->_id = $mapId;
                     $doc->_rev = $mapRev;
                     $doc->map->hexStr = $hexRet->id;
                     $client->put($doc->_id, $doc);
                     echo "BFF forever ";
                 }
             }
         }
     }
 }
 public function terrainInit(CouchService $cs, WargameService $ws, $game = "MartianCivilWar", $arg = false, $terrainDocId = false)
 {
     $user = Auth::user()['name'];
     $battle = Battle::battleFromName($game, $arg);
     if (method_exists($battle, 'terrainGen')) {
         $cs->setDb("rest");
         $terrainDoc = $cs->get($terrainDocId);
         $mapId = $terrainDoc->hexStr->map;
         $mapDoc = $cs->get($mapId);
         $battle->terrainGen($mapDoc, $terrainDoc);
     } else {
         echo "No TerrainGen ";
         return;
     }
     $mapUrl = $battle->terrain->mapUrl;
     $mapWidth = $battle->terrain->mapWidth;
     if ($mapWidth && $mapWidth !== "auto") {
         $mapWidth = preg_replace("/[^\\d]*(\\d*)[^\\d]*/", "\$1", $mapWidth);
         $battle->terrain->mapUrl = $ws->resizeImage($mapUrl, $mapWidth, "images");
         if (!empty($mapDoc->map->trueRows)) {
             $ws->rotateImage($battle->terrain->mapUrl, "images");
         }
     }
     $battle->terrain->smallMapUrl = $ws->resizeImage($mapUrl);
     //        $this->rotateImage($mapUrl);
     $battle->terrainName = false;
     $wargameDoc = $battle->save();
     $terrainName = "terrain-{$game}";
     $ws->saveTerrainDoc(urldecode($terrainName . "." . $arg), $battle);
     if (!empty($mapDoc->map->isDefault)) {
         $ws->saveTerrainDoc(urldecode($terrainName), $battle);
     }
     $ret = new \stdClass();
     $ret->ok = true;
     return response()->json($ret);
 }
 public function fetchLobbyChanges(CouchService $cs, $user, $last_seq = 0, $chatsIndex = 0)
 {
     do {
         $retry = false;
         try {
             if ($last_seq) {
                 $seq = $cs->get("_changes?since={$last_seq}&feed=longpoll&filter=newFilter/lobbyChanges&name={$user}");
             } else {
                 $seq = $cs->get("_changes");
             }
         } catch (Exception $e) {
             $retry = true;
         }
     } while ($retry || $seq->last_seq <= $last_seq);
     return $seq;
 }
 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');
 }