[Library] Storage API

edited June 2015 in Tools

Storage API

Storage API interface to store data for Dota 2 Custom Game players.

How to setup

  • Clone or download the storageapi folder into your addon's vscript folder
  • Get API key from http://dota2.tools/api/v1/storage/createApiKey?game=<Name of your custom game>&author=<Your name> or make your own server receiver and edit STORAGEAPI_API_URL in storage.lua
  • Check info on server responses at the bottom of this readme if you decide to build your own reciever

Usage

require('storageapi/json')
require('storageapi/storage')

Storage:SetApiKey(api_key)

Storage:Put( steam_id, data, function( resultTable, successBool )
    if successBool then
        print("Successfully put data in storage")
    end
end)

Storage:Get( steam_id, function( resultTable, successBool )
    if successBool then
        DeepPrintTable(resultTable)
    end
end)

Testing

GET: STORAGEAPI_API_URL?api_key=<api_key>&steam_id=<steam_id>

PUT (POST): STORAGEAPI_API_URL?api_key=<api_key>&steam_id=<steam_id>&data=<json>

Server

GET with query string api_key=<api_key>&steam_id=<steam_id> should return

{
    "data": {
        "api_key": string,
        "steam_id": string,
        "data": {
            json_object
        }
    }
}

POST with query string api_key=<api_key>&steam_id=<steam_id>&data=<json> should return

{
  "data": {
    "success": true
  }
}

Errors should return

{
  "errors": {
    "error_code": int,
    "message": string
  }
}

Comments

  • edited June 2015 Posts: 188

    Thank you!

    For those confused here is simple functions to save gold and items and then recall them


    require('storageapi/json') require('storageapi/storage') Storage:SetApiKey("api key") Convars:RegisterCommand('data_save', function() if Convars:GetDOTACommandClient() then local player = Convars:GetDOTACommandClient() local playerID = player:GetPlayerID() local gold = PlayerResource:GetGold(playerID) local steam_id = PlayerResource:GetSteamAccountID(playerID) local data = { gold } local hero = player:GetAssignedHero() for i = 0,11 do local item = hero:GetItemInSlot(i) if item then table.insert(data, item:GetAbilityName()) local n = item:GetCurrentCharges() or 0 table.insert(data, n) end end Storage:Put( steam_id, data, function( resultTable, successBool ) if successBool then print("Successfully put data in storage") end end) end end, 'Data save!', 0) Convars:RegisterCommand('data_get', function() if Convars:GetDOTACommandClient() then local player = Convars:GetDOTACommandClient() local playerID = player:GetPlayerID() local gold = PlayerResource:GetGold(playerID) local steam_id = PlayerResource:GetSteamAccountID(playerID) Storage:Get( steam_id, function( resultTable, successBool ) if successBool then DeepPrintTable(resultTable) PlayerResource:SetGold(playerID,resultTable[1],false) local hero = player:GetAssignedHero() for i = 0,11 do local item = hero:GetItemInSlot(i) if item then hero:RemoveItem(item) end end local a = 2 local b = 3 for i = 0,11 do if resultTable[a] and resultTable[b] then hero:AddItemByName(resultTable[a]) hero:GetItemInSlot(i):SetCurrentCharges(resultTable[b]) end a = a + 2 b = b + 2 end end end) end end, 'Data Get', 0)
  • Thanks for making this. I see it being useful for me. :smile:

  • edited January 2016 Posts: 74

    Is there a list of the error codes?

    A player's file has error code 5, with the message "resource not found with steam_id 'his id'"

    And I'm not sure what's causing the save to fail

    It's getting the data fine, but for some reason successbool is returning nil

    Edit: The problem has been resolved thanks to Elinea