Skip to content

Commit

Permalink
Merge pull request fluffy-servers#9 from Kesefon/table
Browse files Browse the repository at this point in the history
Use table for rich presence data
  • Loading branch information
rafraser authored Jan 2, 2021
2 parents 975373b + 77f4d0e commit 9297cc4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 111 deletions.
135 changes: 36 additions & 99 deletions cpp/GRPCTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,96 +139,43 @@ LUA_FUNCTION(DiscordRespond) {
return 0;
}

LUA_FUNCTION(UpdateDiscordStatus_Basic) {

// Read the arguments from the lua function
const char *dState = LUA->GetString(1);
const char *dDetails = LUA->GetString(2);
const char *dImage = LUA->GetString(3);
const char *dSubtitle = LUA->GetString(4);

// Prepare rich presence
DiscordRichPresence discordP{};

// Update the rich presence
discordP.state = dState;
discordP.details = dDetails;
discordP.largeImageKey = dImage;
discordP.largeImageText = dSubtitle;
Discord_UpdatePresence(&discordP);

// Return 0 arguments to lua
return 0;
}

LUA_FUNCTION(UpdateDiscordStatus_Players) {

// Read the arguments from the lua function
const char *dState = LUA->GetString(1);
const char *dDetails = LUA->GetString(2);
const char *dImage = LUA->GetString(3);
const char *dSubtitle = LUA->GetString(4);
const int dPlayers = LUA->GetNumber(5);
const int dMaxPlayers = LUA->GetNumber(6);

// Prepare rich presence
LUA_FUNCTION(UpdateDiscordStatus) {
DiscordRichPresence discordP;
memset(&discordP, 0, sizeof(discordP));
LUA->GetField(1, "state");
discordP.state = LUA->GetString();
LUA->GetField(1, "details");
discordP.details = LUA->GetString();
LUA->GetField(1, "startTimestamp");
discordP.startTimestamp = LUA->GetNumber();
LUA->GetField(1, "endTimestamp");
discordP.endTimestamp = LUA->GetNumber();
LUA->GetField(1, "largeImageKey");
discordP.largeImageKey = LUA->GetString();
LUA->GetField(1, "largeImageText");
discordP.largeImageText = LUA->GetString();
LUA->GetField(1, "smallImageKey");
discordP.smallImageKey = LUA->GetString();
LUA->GetField(1, "smallImageText");
discordP.smallImageText = LUA->GetString();
LUA->GetField(1, "partyId");
discordP.partyId = LUA->GetString();
LUA->GetField(1, "partySize");
discordP.partySize = LUA->GetNumber();
LUA->GetField(1, "partyMax");
discordP.partyMax = LUA->GetNumber();
LUA->GetField(1, "partyPrivacy");
discordP.partyPrivacy = LUA->GetNumber();
LUA->GetField(1, "matchSecret");
discordP.matchSecret = LUA->GetString();
LUA->GetField(1, "joinSecret");
discordP.joinSecret = LUA->GetString();
LUA->GetField(1, "spectateSecret");
discordP.spectateSecret = LUA->GetString();
LUA->GetField(1, "instance");
discordP.instance = LUA->GetNumber();

// Update the rich presence
discordP.state = dState;
discordP.details = dDetails;
discordP.largeImageKey = dImage;
discordP.largeImageText = dSubtitle;
discordP.partyId = "hahalolparty";
discordP.partySize = dPlayers;
discordP.partyMax = dMaxPlayers;
Discord_UpdatePresence(&discordP);

// Return 0 arguments to lua
return 0;
}

LUA_FUNCTION(UpdateDiscordStatus_Elapsed) {

// Read the arguments from the lua function
const char *dState = LUA->GetString(1);
const char *dDetails = LUA->GetString(2);
const char *dImage = LUA->GetString(3);
const char *dSubtitle = LUA->GetString(4);
const int dPlayers = LUA->GetNumber(5);
const int dMaxPlayers = LUA->GetNumber(6);
const int64_t dTime = LUA->GetNumber(7);
const char *dPartyId = LUA->GetString(8);
const char *dJoinSecret = LUA->GetString(9);
const char *dSpectateSecret = LUA->GetString(10);

// Prepare rich presence
DiscordRichPresence discordP;
memset(&discordP, 0, sizeof(discordP));

// Update the rich presence
discordP.state = dState;
discordP.details = dDetails;
discordP.largeImageKey = dImage;
discordP.largeImageText = dSubtitle;
if (dPartyId != NULL) {
discordP.partyId = dPartyId;
} else {
discordP.partyId = "hahalolparty";
}
discordP.partySize = dPlayers;
discordP.partyMax = dMaxPlayers;
discordP.startTimestamp = dTime;
if (dJoinSecret != NULL) {
discordP.joinSecret = dJoinSecret;
}
if (dSpectateSecret != NULL) {
discordP.spectateSecret = dSpectateSecret;
}
Discord_UpdatePresence(&discordP);

// Return 0 arguments to lua
return 0;
}

Expand All @@ -250,18 +197,8 @@ GMOD_MODULE_OPEN() {
LUA->Pop();

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->PushCFunction(UpdateDiscordStatus_Basic);
LUA->SetField(-2, "DiscordRPCBasic");
LUA->Pop();

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->PushCFunction(UpdateDiscordStatus_Players);
LUA->SetField(-2, "DiscordRPCPlayers");
LUA->Pop();

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->PushCFunction(UpdateDiscordStatus_Elapsed);
LUA->SetField(-2, "DiscordRPCTime");
LUA->PushCFunction(UpdateDiscordStatus);
LUA->SetField(-2, "DiscordUpdateRPC");
LUA->Pop();

return 0;
Expand All @@ -270,4 +207,4 @@ GMOD_MODULE_OPEN() {
GMOD_MODULE_CLOSE() {
Discord_Shutdown();
return 0;
}
}
27 changes: 15 additions & 12 deletions lua/cl_discord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,46 @@ local map_list = {
gm_flatgrass = true,
gm_construct = true
}
local map_default = "default"
local image_fallback = "default"
local discord_id = "626155559779041331"
local refresh_time = 60

local discord_start = discord_start or -1

function DiscordUpdate()
-- Determine what type of game is being played
local playing_message = ""
local rpc_data = {}
if game.SinglePlayer() then
playing_message = "Singleplayer"
rpc_data["state"] = "Singleplayer"
else
local ip = game.GetIPAddress()
if ip == "loopback" then
if GetConVar("p2p_enabled"):GetBool() then
playing_message = "Peer 2 Peer"
rpc_data["state"] = "Peer 2 Peer"
else
playing_message = "Local Server"
rpc_data["state"] = "Local Server"
end
else
playing_message = string.Replace(ip, ":27015", "")
rpc_data["state"] = string.Replace(ip, ":27015", "")
end
end

-- Determine the max number of players
local maxplayers = game.MaxPlayers()
if game.SinglePlayer() then maxplayers = 0 end
rpc_data["partySize"] = player.GetCount()
rpc_data["partyMax"] = game.MaxPlayers()
if game.SinglePlayer() then rpc_data["partyMax"] = 0 end

-- Handle map stuff
-- See the config
local map = game.GetMap()
rpc_data["largeImageKey"] = game.GetMap()
rpc_data["largeImageText"] = game.GetMap()
if map_restrict and not map_list[map] then
map = map_default
rpc_data["largeImageKey"] = image_fallback
end
local gamemode = GAMEMODE.Name
rpc_data["details"] = GAMEMODE.Name
rpc_data["startTimestamp"] = discord_start

DiscordRPCTime(playing_message, gamemode, map, game.GetMap(), player.GetCount(), maxplayers, discord_start)
DiscordUpdateRPC(rpc_data)
end

hook.Add("Initialize", "UpdateDiscordStatus", function()
Expand Down

0 comments on commit 9297cc4

Please sign in to comment.