Skip to content

Commit

Permalink
get_connected_players (#37)
Browse files Browse the repository at this point in the history
* Added support for connected player
  • Loading branch information
britzl authored Mar 28, 2018
1 parent ba7e414 commit e30d6f4
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ The ```player``` table contains the following key-value pairs:
* ```locale``` (string) - Player locale


### fbinstant.get_connected_players()
Get an list of players that are connected to the current player.

**PARAMETERS**
* ```callback``` (function) - Function to call with the list of connected players

The ```callback``` function is expected to accept the following values:

* ```self``` (userdata) - Script self reference
* ```players``` (string) - JSON encoded array of connected player information

Each entry in the ```players``` array represents a player with these properties:

* ```id``` (string) - Id of the connected player
* ```photo``` (string) - The player's public profile photo
* ```name``` (string) - The player's full name


### fbinstant.get_player_data(keys, callback)
Retrieve data from the designated cloud storage of the current player.

Expand Down
24 changes: 21 additions & 3 deletions apitester/apitester.script
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if fbinstant.mock then
fbinstant.PLAYER = {
name = "Player 1",
id = "100000000001fake",
photo = "http://i.pravatar.cc/200?u=123",
photo = "http://i.pravatar.cc/200?u=1",
locale = "en_US",
}

Expand All @@ -19,11 +19,29 @@ if fbinstant.mock then
{
name = "Player 2",
id = "100000000002fake",
photo = "http://i.pravatar.cc/200?u=456",
photo = "http://i.pravatar.cc/200?u=2",
locale = "en_US",
}
}


fbinstant.CONNECTED_PLAYERS = {
{
name = "Rick Dangerous",
id = "100000000003fake",
photo = "http://i.pravatar.cc/200?u=3",
},
{
name = "Lara Croft",
id = "100000000004fake",
photo = "http://i.pravatar.cc/200?u=4",
},
{
name = "Gordon Freeman",
id = "100000000005fake",
photo = "http://i.pravatar.cc/200?u=5",
},
}

-- the current context
fbinstant.CONTEXT = {
id = "123456fake",
Expand Down
62 changes: 62 additions & 0 deletions apitester/player/player.gui
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,68 @@ nodes {
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 320.0
y: 1000.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 0.3
y: 0.3
z: 1.0E-6
w: 1.0
}
size {
x: 600.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "..."
font: "dirtylarry"
id: "connected_players"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_NW
outline {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: true
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 1.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
layers {
name: "below"
}
Expand Down
5 changes: 5 additions & 0 deletions apitester/player/player.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function init(self)
print("init")
msg.post(".", "acquire_input_focus")
msg.post("#", "update_player")
msg.post("#", "update_connected_players")
end

function on_message(self, message_id, message, sender)
Expand All @@ -20,6 +21,10 @@ function on_message(self, message_id, message, sender)
text = text .. "PHOTO: " .. (player.photo or "nil") .. "\n"
text = text .. "LOCALE: " .. (player.locale or "nil") .. "\n"
gui.set_text(gui.get_node("player"), text)
elseif message_id == hash("update_connected_players") then
fbinstant.get_connected_players(function(self, players)
gui.set_text(gui.get_node("connected_players"), prettify(json.decode(players)))
end)
end
end

Expand Down
3 changes: 3 additions & 0 deletions fbinstant/include/fbinstant.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef void (*OnInterstitialAdLoadedCallback)(const int success);
typedef void (*OnInterstitialAdShownCallback)(const int success);
typedef void (*OnRewardedVideoLoadedCallback)(const int success);
typedef void (*OnRewardedVideoShownCallback)(const int success);
typedef void (*OnConnectedPlayersCallback)(const char* players);


extern "C" {
Expand Down Expand Up @@ -55,6 +56,8 @@ extern "C" {
char* FBInstant_PlatformGetPlayerPhoto();
char* FBInstant_PlatformGetPlayerLocale();

char* FBInstant_PlatformGetConnectedPlayersAsync(OnConnectedPlayersCallback callback);

char* FBInstant_PlatformGetPlatform();
char* FBInstant_PlatformGetSupportedAPIs();
char* FBInstant_PlatformGetSDKVersion();
Expand Down
24 changes: 23 additions & 1 deletion fbinstant/lib/js-web/library_fbinstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ var FBInstantLibrary = {
},


// =====================================
// GetConnectedPlayersAsync
// =====================================
FBInstant_PlatformGetConnectedPlayersAsync: function(callback) {
FBInstant.player.getConnectedPlayersAsync().then(function(connected_players) {
var players = [];
for(var i=0; i<connected_players.length; i++) {
var connected_player = connected_players[i];
players.push({
id: connected_player.getID(),
name: connected_player.getName(),
photo: connected_player.getPhoto(),
});
}
Utils.dynCall(callback, [JSON.stringify(players)]);
}).catch(function(err) {
console.log("FBInstant_PlatformGetConnectedPlayersAsync - error", err);
Runtime.dynCall("vi", callback, [0]);
});
},


// =====================================
// SetSessionData
// =====================================
Expand Down Expand Up @@ -395,7 +417,7 @@ var FBInstantLibrary = {
FBInstant_PlatformGetPlatform: function() {
var platform = FBInstant.getPlatform();
if (platform) {
return Utils.storeString("platform", platform);
return Utils.manageString("platform", platform);
}
else {
return null;
Expand Down
35 changes: 34 additions & 1 deletion fbinstant/src/fbinstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,39 @@ static int FBInstant_IncrementPlayerStatsAsync(lua_State* L) {
return 0;
}


lua_Listener getConnectedPlayersAsyncListener;

static void FBInstant_OnConnectedPlayers(const char* players) {
lua_State* L = getConnectedPlayersAsyncListener.m_L;
int top = lua_gettop(L);

lua_pushlistener(L, getConnectedPlayersAsyncListener);
if (players != NULL) {
lua_pushstring(L, players);
}
else {
lua_pushnil(L);
}
int ret = lua_pcall(L, 2, 0, 0);
if (ret != 0) {
lua_pop(L, 1);
}

assert(top == lua_gettop(L));
}

static int FBInstant_GetConnectedPlayersAsync(lua_State* L) {
int top = lua_gettop(L);

luaL_checklistener(L, 1, getConnectedPlayersAsyncListener);
FBInstant_PlatformGetConnectedPlayersAsync((OnConnectedPlayersCallback)FBInstant_OnConnectedPlayers);

assert(top == lua_gettop(L));
return 0;
}


// ===============================================
// START GAME
// ===============================================
Expand Down Expand Up @@ -1092,7 +1125,7 @@ static const luaL_reg Module_methods[] = {
{"get_player_stats", FBInstant_GetPlayerStatsAsync},
{"set_player_stats", FBInstant_SetPlayerStatsAsync},
{"increment_player_stats", FBInstant_IncrementPlayerStatsAsync},
// TODO: {"get_connected_players", FBInstant_GetConnectedPlayersAsync},
{"get_connected_players", FBInstant_GetConnectedPlayersAsync},

// context functions
{"choose_context", FBInstant_ChooseContextAsync},
Expand Down
6 changes: 6 additions & 0 deletions fbinstant/utils/mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ local player_stats = {}

fbinstant.PLAYER = {}
fbinstant.PLAYERS = {}
fbinstant.CONNECTED_PLAYERS = {}
fbinstant.CONTEXT = nil

local function get_self()
Expand Down Expand Up @@ -166,6 +167,11 @@ function fbinstant.get_player()
return fbinstant.PLAYER
end

function fbinstant.get_connected_players(cb)
print("get_connected_players")
cb(get_self(), rxijson.encode(fbinstant.CONNECTED_PLAYERS))
end

function fbinstant.get_player_data(keys, cb)
print("get_player_data", keys)
local result = {}
Expand Down
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title = XOXO
dependencies = https://github.com/britzl/gooey/archive/5.2.0.zip,https://github.com/britzl/monarch/archive/1.0.zip

[bootstrap]
main_collection = /tictactoe/tictactoe.collectionc
main_collection = /apitester/apitester.collectionc

[input]
game_binding = /input/game.input_bindingc
Expand Down

0 comments on commit e30d6f4

Please sign in to comment.