defold / extension-fbinstant

Facebook Instant Games extension for the Defold game engine
https://defold.com/extension-fbinstant/
MIT License
39 stars 20 forks source link

Dummy API #6

Open subsoap opened 6 years ago

subsoap commented 6 years ago

There's a version of the API which returns dummy results, not require connection to Facebook for testing, may be useful to support somehow

britzl commented 6 years ago

Do you have some more information about this?

subsoap commented 6 years ago

There is a "mock" api included in the example https://origincache.facebook.com/developers/resources/?id=tic-tac-toe.zip fbinstant.6.0.mock.js.txt

It returns dummy values to all function calls. Could detect if the domain is localhost, and then load the dummy api instead. That way building Instant Game apps with Defold would be easier doing Project -> Build HTML in the editor.

britzl commented 6 years ago

Ah, I see. Ok, so that mocks the fbinstant js api. That might be useful.

I actually also provide a mock version of the fbinstant. namespace for rapid testing on desktop, without the need to build/bundle to HTML5. It's a pure-Lua implementation of the fbinstant. functions with static responses (no backend or anything like that involved). I'm not sure if I want to document it since I'm not sure of its usefulness yet. The mock version is in fbinstant.utils.mock.lua. require() it in a main/loader script before calling any fbinstant functions and give it some dummy data about the player(s) and context:

-- mock fbinstant.* to test your game without the need to build/bundle for HTML5
require "fbinstant.utils.mock"
-- this will be true on non HTML5 builds
if fbinstant.mock then
    -- this is the logged in player
    fbinstant.PLAYER = {
        name = "Player 1",
        id = "100000000001fake",
        photo = "http://i.pravatar.cc/200?u=123",
        locale = "en_US",
    }

    -- these are the players in the current context (fbinstant.get_players())
    fbinstant.PLAYERS = {
        fbinstant.PLAYER,
        {
            name = "Player 2",
            id = "100000000002fake",
            photo = "http://i.pravatar.cc/200?u=456",
            locale = "en_US",
        }
    }

    -- the current context
    fbinstant.CONTEXT = {
        id = "123456fake",
        type = fbinstant.CONTEXT_THREAD,
        size = 2,
    }
end
subsoap commented 6 years ago

Desktop builds are faster than HTML5 so it would be useful too. The problem I see is needing to update so many things as the API changes.

I'm going to go all in on making some big Facebook Instant Games (and port some in progress projects to it) so anything to help productivity will be useful and I bet others will appreciate it too!

Another thing which may be useful would be a chrome extension or js include which can detect new builds somehow and autoreload the web page... I've used something like that with other tools before. Another way to do it would be to make the build script open a fresh page but have a for example Chrome extension auto close other tabs of the specified name...

britzl commented 6 years ago

I can't imagine that Facebook would make many API changes now that the API is public. Sure, they may deprecate functions and add new ones, but the ones that are documented now are not likely to change.

It would be quite useful if Facebook provided this mock API somewhere more accessible, instead of hidden away inside a zip. Or if they provided an API definition in a parsable format so that something like my mock.lua file could be autogenerated much in the same way as Amazon provides API definitions for the AWS SDK.