Beispiel #1
0
def user_items_batch(session, grant, user, items):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if len(items) == 0:
        raise ValueError("List of item instance GUIDs cannot be empty")
    for guid in items:
        if not verify_guid(guid):
            raise ValueError("Invalid item instance GUID given")

    response = session.api_post(endpoints.user_item_batch,
                                user,
                                grant=grant,
                                batch=items)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        if reply[
                "Message"] == "Error retrieving batch user game items. If any item doesn't exist the batch will fail.":
            # No such achievement
            raise InvalidBatch(response)

        # No such user
        return None

    # Return response
    return reply["Result"]
Beispiel #2
0
def user_achievements_batch(session,
                            grant,
                            user,
                            achievements,
                            countrycode=None):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if len(achievements) == 0:
        raise ValueError("List of achievement GUIDs cannot be empty")
    for guid in achievements:
        if not verify_guid(guid):
            raise ValueError("Invalid achievement GUID given")

    response = session.api_post(endpoints.achievement_user_query,
                                user,
                                grant=grant,
                                batch=achievements,
                                countrycode=countrycode)
    reply = response.json()

    if reply["Status"] == requests.codes.not_fund:
        if reply["Message"] == "Error retrieving batch items.":
            # No such achievement
            raise InvalidBatch(response)

        # No such user
        return None

    # Return response
    return reply["Result"]
Beispiel #3
0
def user_achievements_unlock(session, grant, user, achievement):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if not verify_guid(user):
        raise ValueError("Invalid achievement GUID given")

    response = session.api_post(endpoints.achievement_user_client,
                                user,
                                achievement,
                                grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.forbidden:
        if reply[
                "Message"] == "Requesting access grant's user GUID must match user GUID parameter":
            # User does not match access grant user
            raise WrongUser(response)

        if reply["Message"] == "Achievement is already redeemed":
            # Achievement already unlocked
            return False

        # Can't be unlocked from client
        raise InvalidRequest(response)

    if reply["Status"] == requests.codes.not_found:
        # No such achievement
        return None

    # Success
    return True
Beispiel #4
0
def game_offers_redeem(session,
                       grant,
                       user,
                       offer,
                       currency,
                       transaction,
                       parent=None):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if not verify_guid(offer):
        raise ValueError("Invalid offer GUID given")
    if currency not in ("HP", "MP"):
        raise ValueError("Invalid currency given")
    if not verify_guid(transaction):
        raise ValueError("Invalid transaction GUID given")

    data = {"Currency": currency, "TransactionId": transaction}

    if parent is not None:
        if not verify_guid(parent):
            raise ValueError("Invalid parent item GUID given")
        data["ExistingParentInstanceGuid"] = parent

    try:
        response = session.api_post(endpoints.offer_redeemer,
                                    user,
                                    offer,
                                    grant=grant,
                                    data=data)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)

    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        if reply[
                "Message"] == "No valid transaction for user {0} with id {1}".format(
                    user, transaction):
            # Invalid transaction
            raise InvalidRequest(response)

        if reply["Message"] == "No items found":
            # No such parent item
            raise InvalidRequest(response)

        # No such offer
        return None

    if reply["Status"] == requests.codes.forbidden:
        # Offer is disabled
        raise InvalidRequest(response)

    if reply["Status"] == requests.codes.precondition_failed:
        # Not enough currency
        raise InsufficientFunds(response)

    # Return response
    return reply["Result"]
Beispiel #5
0
def user_items_broker(session, grant, user, instance, data):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if not verify_guid(instance):
        raise ValueError("Invalid item instance GUID given")

    try:
        response = session.api_put(endpoints.user_item_broker,
                                   user,
                                   instance,
                                   grant=grant,
                                   data=data)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)

    reply = response.json()

    if reply["Status"] == requests.codes.forbidden:
        # Action not allowed
        raise NotAllowed(response)

    if reply["Status"] == requests.codes.not_found:
        # No such item
        return None

    if reply["Status"] == requests.codes.ok:
        # Success
        return True

    # Catch all failure
    return False
Beispiel #6
0
def stat_overflow_transfer_to(session, grant, user, instance, overflow,
                              amount):
    # Verify guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if not verify_guid(instance):
        raise ValueError("Invalid mech instance GUID given")
    if not verify_guid(overflow):
        raise ValueError("Invalid overflow GUID given")
    if amount < 1:
        raise ValueError("Must transfer at least 1 point")

    data = {"Amount": amount, "OverflowId": overflow}

    try:
        response = session.api_put(endpoints.statoverflow_transfer,
                                   user,
                                   instance,
                                   grant=grant,
                                   data=data)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)
    except InvalidRequest as e:
        if InvalidStatTransfer.detect(e.response):
            # Invalid stat transfer operation
            raise InvalidStatTransfer(e.response)

        raise

    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        if reply["Message"] == "{0} not found".format(overflow):
            # No such overflow
            raise InvalidRequest(response)

        # No such item
        return None

    if reply["Status"] == requests.codes.precondition_failed:
        # Not enough currency
        raise InsufficientFunds(response)

    if reply["Status"] == requests.codes.ok:
        # Success
        return True

    # Catch all failure
    return False
Beispiel #7
0
def user_game_settings_delete(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    try:
        response = session.api_delete(endpoints.user_settings_single,
                                      guid,
                                      grant=grant)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)

    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No game settings exists
        return None

    if reply["Status"] == requests.codes.ok:
        # Success
        return True

    # Catch all failure
    return False
Beispiel #8
0
def revoke_auth(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    data = {"AccessGrant": grant}

    try:
        response = session.api_put(endpoints.user_accessgrant,
                                   guid,
                                   grant=grant,
                                   data=data,
                                   check=False)
    except NotAuthorized as e:
        if e.error == NotAuthorized.Error.revoked:
            # Already revoked
            return True

        raise

    if response.json()["Status"] == requests.codes.ok:
        # Success
        return True

    # Catch all failure
    return False
Beispiel #9
0
def matchmaking_advertisement(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid advertisement GUID given")

    response = session.api_get(endpoints.advertisement_single,
                               guid,
                               grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such advertisement
        return None

    # Fix incomplete request marked as ready
    if reply["Result"]["ReadyToDeliver"] and \
       (reply["Result"]["AssignedServerIp"] in (None, "") or reply["Result"]["AssignedServerPort"] == 0):
        reply["Result"]["ReadyToDeliver"] = False

    # Fix newline appended to the server IP
    if reply["Result"]["AssignedServerIp"] is not None:
        reply["Result"]["AssignedServerIp"] = reply["Result"][
            "AssignedServerIp"].strip("\n")

    # Check for requested/assigned server mismatch
    if reply["Result"]["ReadyToDeliver"] and \
       reply["Result"]["RequestedServerGuid"] != BLANK_GUID and \
       reply["Result"]["AssignedServerGuid"] != reply["Result"]["RequestedServerGuid"]:
        raise InvalidResponse(
            response,
            "Requested server GUID does not matched assigned server GUID")

    # Return response
    return reply["Result"]
Beispiel #10
0
def voice_channel(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid channel GUID given")

    response = session.api_get(endpoints.voice_channel, guid, grant=grant)

    # Return response
    return response.json()["Result"]
Beispiel #11
0
def generate_advertisement_matchmaking(gameversion,
                                       region,
                                       owner,
                                       users,
                                       gametype=None,
                                       party=None):
    # Check the parameters given
    if not isinstance(gameversion, str) or gameversion == "":
        raise ValueError("Game Version cannot be blank")
    if not isinstance(region, str) or region == "":
        raise ValueError("Region cannot be blank")
    if not verify_guid(owner):
        raise ValueError("Invalid owner GUID given")
    if len(users) == 0:
        raise ValueError("List of user GUIDs cannot be empty")
    for guid in users:
        if not verify_guid(guid):
            raise ValueError("Invalid user GUID given")

    # Build base advertisement
    advertisement = {
        "GameVersion": gameversion,
        "OwnerGuid": owner,
        "Region": region,
        "Users": users
    }

    if gametype is not None:
        # Check and add gametype
        if not isinstance(gametype, str) or gametype == "":
            raise ValueError("Game Type cannot be blank")
        advertisement["GameType"] = gametype

    if party is not None:
        # Check and add party
        if not verify_guid(party):
            raise ValueError("Invalid party GUID given")
        advertisement["PartyGuid"] = party

    # Return advertisement
    return advertisement
Beispiel #12
0
def user_eula_read(session, grant, guid):
    # Verify guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    try:
        response = session.api_get(endpoints.user_eula, guid, grant=grant)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)

    # Return response
    return response.json()["Result"]
Beispiel #13
0
def game_offers_single(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid offer GUID given")

    response = session.api_get(endpoints.offer_single, guid, grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such offer
        return None

    # Return response
    return reply["Result"]
Beispiel #14
0
def currency_meteor(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    response = session.api_get(endpoints.currency_meteor, guid, grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such user
        return None

    # Return response
    return reply["Result"]
Beispiel #15
0
def user_items_stats_single(session, grant, user, instance):
    # Validate the guids given
    if not verify_guid(user):
        raise ValueError("Invalid user GUID given")
    if not verify_guid(instance):
        raise ValueError("Invalid item instance GUID given")

    response = session.api_get(endpoints.user_item_stat_single,
                               user,
                               instance,
                               grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        if reply["Message"] == "No items found":
            # No such item
            return None

        # No such user
        return False

    # Return response
    return reply["Result"]
Beispiel #16
0
def stat_overflow_single(session, grant, guid):
    # Verify guid given
    if not verify_guid(guid):
        raise ValueError("Invalid overflow GUID given")

    response = session.api_get(endpoints.statoverflow_single,
                               guid,
                               grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such overflow
        return None

    # Return response
    return reply["Result"]
Beispiel #17
0
def game_items_batch(session, grant, guids):
    # Validate the guids given
    if len(guids) == 0:
        raise ValueError("List of item GUIDs cannot be empty")
    for guid in guids:
        if not verify_guid(guid):
            raise ValueError("Invalid item GUID given")

    response = session.api_post(endpoints.item_batch, grant=grant, batch=guids)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such item
        raise InvalidBatch(response)

    # Return response
    return reply["Result"]
Beispiel #18
0
def achievement_rewards_single(session, grant, guid, countrycode=None):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid achievement reward GUID given")

    response = session.api_get(endpoints.achievement_reward_single,
                               guid,
                               grant=grant,
                               countrycode=countrycode)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such reward
        return None

    # Return response
    return reply["Result"]
Beispiel #19
0
def bundle_batch(session, grant, guids):
    # Validate the guids given
    if len(guids) == 0:
        raise ValueError("List of bundle GUIDs cannot be empty")
    for guid in guids:
        if not verify_guid(guid):
            raise ValueError("Invalid bundle GUID given")

    data = []
    # Perform a chunked request
    for chunk in chunks(guids, BATCH_LIMIT):
        # Retrieve a chunk and add the response to the data set
        data.extend(
            session.api_get(endpoints.bundle, grant=grant,
                            batch=chunk).json()["Result"])

    # Return data set
    return data
Beispiel #20
0
def presence_domain(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    try:
        response = session.api_get(endpoints.presence_domain,
                                   guid,
                                   grant=grant)
    except NotAuthorized as e:
        if e.message == "Access grant with matching user GUID required":
            # User does not match access grant user
            raise WrongUser(e.response)

        raise

    # Return response
    return response.json()["Result"]
Beispiel #21
0
def user_transaction(session, grant, guid):
    # Verify guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    response = session.api_post(endpoints.transaction, guid, grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        # No such user
        return None

    if reply["Status"] == requests.codes.created:
        # Return response
        return reply["Result"]

    # Catch all failure
    return False
Beispiel #22
0
def user_stats_batch(session, grant, guids):
    # Validate the guids given
    if len(guids) == 0:
        raise ValueError("List of user GUIDs cannot be empty")
    for guid in guids:
        if not verify_guid(guid):
            raise ValueError("Invalid user GUID given")

    data = []
    # Perform a chunked request
    # BUG: API breaks at anything more than 100 users at a time
    for chunk in chunks(guids, 100):
        # Retrieve a chunk and add the response to the data set
        data.extend(
            session.api_get(endpoints.user_stat_batch,
                            grant=grant,
                            batch=chunk).json()["Result"])

    # Return data set
    return data
Beispiel #23
0
def achievement_rewards_batch(session, grant, guids, countrycode=None):
    # Validate the guids given
    if len(guids) == 0:
        raise ValueError("List of achievement reward GUIDs cannot be empty")
    for guid in guids:
        if not verify_guid(guid):
            raise ValueError("Invalid achievement reward GUID given")

    data = []
    # Perform a chunked request
    for chunk in chunks(guids, BATCH_LIMIT):
        # Retrieve a chunk and add the response to the data set
        data.extend(
            session.api_get(endpoints.achievement_reward_batch,
                            grant=grant,
                            batch=chunk,
                            countrycode=countrycode).json()["Result"])

    # Return data set
    return data
Beispiel #24
0
def user_game_settings_create(session, grant, guid, data):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    try:
        response = session.api_post(endpoints.user_settings_single,
                                    guid,
                                    grant=grant,
                                    data=data)
    except NotAllowed as e:
        # User does not match access grant user
        raise WrongUser(e.response)

    if response.json()["Status"] == requests.codes.created:
        # Success
        return True

    # Catch all failure
    return False
Beispiel #25
0
def user_achievements_list(session, grant, guid, countrycode=None):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid user GUID given")

    response = session.api_get(endpoints.achievement_user,
                               guid,
                               grant=grant,
                               countrycode=countrycode)
    reply = response.json()

    if reply["Status"] == requests.codes.not_found:
        if reply["Message"] == "User not found":
            # No such user
            return None

        # No achievements for the user
        return []

    # Return response
    return reply["Result"]
Beispiel #26
0
def matchmaking_advertisement_delete(session, grant, guid):
    # Validate the guid given
    if not verify_guid(guid):
        raise ValueError("Invalid advertisement GUID given")

    response = session.api_delete(endpoints.advertisement_single,
                                  guid,
                                  grant=grant)
    reply = response.json()

    if reply["Status"] == requests.codes.forbidden:
        # Owner does not match access grant user
        raise WrongUser(response)

    if reply["Status"] == requests.codes.not_found:
        # No such advertisement
        return None

    if reply["Status"] == requests.codes.ok:
        # Success
        return True

    # Catch all failure
    return False