session_id($_GET["sessionId"]);
}
session_start();
session_write_close();
$userId = null;
if (isset($_GET['userId'])) {
    $userId = $_GET['userId'];
}
if (!UtilFunctions::checkUserSession($userId)) {
    $result->result = "401 : auth error";
    header("HTTP/1.1 401 Unauthorized");
    echo json_encode($result);
    exit(1);
}
if (!empty($userId)) {
    $roomGroups = GameRoomGroups::findByExample(DBUtils::getConnection(), GameRoomGroups::create());
    if (empty($roomGroups)) {
        $roomGroups = array();
    } else {
        $tmp = array();
        foreach ($roomGroups as $room) {
            if (!empty($room) && $room->roomGroupId != "default") {
                array_push($tmp, $room);
            }
        }
        $roomGroups = $tmp;
        unset($tmp);
    }
    $result->success = true;
    $result->result = new stdClass();
    $result->result->roomGroups = $roomGroups;
 /**
  * get single GameRoomGroups instance from a DOMElement
  *
  * @param DOMElement $node
  * @return GameRoomGroups
  */
 public static function fromDOMElement(DOMElement $node)
 {
     $o = new GameRoomGroups();
     $o->assignByHash(self::domNodeToHash($node, self::$FIELD_NAMES, self::$DEFAULT_VALUES, self::$FIELD_TYPES));
     $o->notifyPristine();
     return $o;
 }
 public static function updateCoin(GameUsers $user, GameUsers $opponent, $roomGroupId, $action, $gameId = null, $double = 0, $normal = true, $type = null, $time = null)
 {
     //Add Log
     $result = new FunctionResult();
     $result->success = false;
     if (!empty($user) && !empty($action)) {
         $roomGroup = GameRoomGroups::getRoomGroupByGroupId($roomGroupId);
         if (!empty($roomGroup)) {
             $coin = $user->getCoins();
             $maxCoin = $roomGroup->getMatchCoin() * 128;
             $matchCoins = $roomGroup->getMatchCoin();
             if (!$normal) {
                 $matchCoins = $matchCoins * 2;
             }
             if ($double > 1) {
                 $matchCoins = $matchCoins * pow(2, $double);
             }
             $calCcoin = $matchCoins;
             $error = false;
             if ($action == GameUtils::$GAME_RESULT_ACTION_WIN) {
                 if ($matchCoins > $maxCoin) {
                     $matchCoins = $maxCoin;
                 }
             } else {
                 if ($action == GameUtils::$GAME_RESULT_ACTION_LOST) {
                     $matchCoins = -1 * $matchCoins;
                     if ($coin + $matchCoins < 0) {
                         $matchCoins = -1 * $coin;
                     }
                 } else {
                     $error = true;
                 }
             }
             if (!$error) {
                 $gameResult = $action . "_" . $normal . "_" . $double . "_" . $type;
                 $oppId = null;
                 if (!empty($opponent)) {
                     $oppId = $opponent->getUserId();
                 }
                 if (empty($time)) {
                     $time = time();
                 }
                 $add = 1;
                 if ($action == GameUtils::$GAME_RESULT_ACTION_LOST) {
                     $add = 0;
                 }
                 Queue::addUserCoinLog($user->userId, $calCcoin, $matchCoins, $time, GameUserXpLog::$CONSTANT_LOG_TYPE_GAME, $add, $gameId, $gameResult, $oppId);
                 Queue::addUserLeaderBoard($user->userId, $matchCoins, $time);
                 $user->setCoins($coin + $matchCoins);
                 $result->success = true;
             } else {
                 $result->success = false;
                 $result->result = "Action unknown";
             }
         } else {
             $result->success = false;
             $result->result = "Room Group is unknown";
         }
     }
     return $result;
 }