Skip to content

Commit

Permalink
Matches support in gamelink sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-wang committed Nov 30, 2024
1 parent e17b2d5 commit 5ba6588
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 3 deletions.
47 changes: 46 additions & 1 deletion include/config_footer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,49 @@ namespace nlohmann
}
};
}
#endif

template<>
struct std::hash<gamelink::string>
{
inline std::size_t operator()(const gamelink::string& str) const
{
return std::hash<
std::string_view
>()(std::string_view(str.c_str(), str.size()));
}
};
#endif

namespace nlohmann
{
template<typename T>
struct adl_serializer<std::unordered_map<gamelink::string, T>>
{
static inline void to_json(json& js, const std::unordered_map<gamelink::string, T>& s)
{
for (auto it = s.begin(); it != s.end(); ++it)
{
js[json::string_t(it->first.c_str())] = it->second;
}
}

static inline void from_json(const json& j, std::unordered_map<gamelink::string, T>& s)
{
if (!j.is_object())
{
return;
}

s.clear();
for (auto it = j.begin(); it != j.end(); ++it)
{
gamelink::string key(it.key().c_str());

T value;
it.value().get_to(value);

s.insert(std::make_pair(key, std::move(value)));
}
}
};
}
59 changes: 59 additions & 0 deletions include/gamelink.h
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,63 @@ namespace gamelink
Event<schema::MatchmakingUpdate>& OnMatchmakingQueueInvite();
#pragma endregion

#pragma region Matches
// Creates a match, which is a collection of users that can have some operations performed on them
// as an entire collection, instead of channel by channel.
// Match manipulation functions are only supported by a server Gamelink connection. Calling these
// from a client will result in an authorization faiure.

// Creates a match with the given ID
RequestId CreateMatch(const string& id);

// Keeps a match alive
RequestId KeepMatchAlive(const string& id);

// Adds channels to a match
RequestId AddChannelsToMatch(const string& id, const std::vector<string>& channels);

// Removes channels from a match.
RequestId RemoveChannelsFromMatch(const string& id, const std::vector<string>& channels);

// Runs a poll in a match. Very similar to RunPoll, except for the callback type, as it receives
// information for all matches.

RequestId RunMatchPoll(
const string& matchId,
const string& pollId,
const string& prompt,
const PollConfiguration& config,
const string* optionsBegin,
const string* optionsEnd,
std::function<void(const schema::MatchPollUpdate&)> onUpdateCallback,
std::function<void(const schema::MatchPollUpdate&)> onFinishCallback
);

RequestId RunMatchPoll(
const string& matchId,
const string& pollId,
const string& prompt,
const PollConfiguration& config,
const std::vector<string>& opts,
std::function<void(const schema::MatchPollUpdate&)> onUpdateCallback,
std::function<void(const schema::MatchPollUpdate&)> onFinishCallback
);

// Stops a poll in a match
RequestId StopMatchPoll(const string& matchId, const string& pollId);

/// Sends a broadcast to all viewers on all channels in the match
template<typename T>
RequestId SendMatchBroadcast(const string& matchId, const string& topic, const T& value)
{
schema::BroadcastMatchRequest<T> payload(matchId, topic, value);
return queuePayload(payload);
}

/// Sends a broadcast to all viewers on all channels in the match
RequestId SendMatchBroadcast(const string& matchId, const string& topic, const nlohmann::json& message);
#pragma endregion

/// Sends a request to set your games metadata. You're expected to fill out a gamelink::GameMetadata struct with your games metadata
/// and provide it.
/// @return RequestId of the generated request
Expand Down Expand Up @@ -1683,6 +1740,8 @@ namespace gamelink
Event<schema::GetDropsResponse> _onGetDrops;

Event<schema::MatchmakingUpdate> _onMatchmakingUpdate;

Event<schema::MatchPollUpdate> _onMatchPollUpdate;
};

// Implementation of Event::Remove is here because of completeness requirements of SDK.
Expand Down
267 changes: 267 additions & 0 deletions schema/matches.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
#pragma once
#ifndef MUXY_GAMELINK_SCHEMA_MATCHES_H
#define MUXY_GAMELINK_SCHEMA_MATCHES_H
#include "schema/envelope.h"
#include "schema/subscription.h"
#include "schema/poll.h"

namespace gamelink
{
namespace schema
{
struct CreateMatchRequestBody
{
gamelink::string matchId;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_1(CreateMatchRequestBody,
"id", matchId
);
};

struct MUXY_GAMELINK_API CreateMatchRequest : SendEnvelope<CreateMatchRequestBody>
{
explicit inline CreateMatchRequest(const string& id)
{
this->action = string("create");
this->params.target = string("match");

this->data.matchId = id;
}
};

struct KeepMatchAliveBody
{
gamelink::string matchId;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_1(KeepMatchAliveBody,
"id", matchId
);
};

struct MUXY_GAMELINK_API KeepMatchAliveRequest : SendEnvelope<KeepMatchAliveBody>
{
explicit inline KeepMatchAliveRequest(const string& id)
{
this->action = string("keepalive");
this->params.target = string("match");

this->data.matchId = id;
}
};

struct AddOrRemoveMatchChannelsRequestBody
{
gamelink::string matchId;
std::vector<gamelink::string> channels;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_2(AddOrRemoveMatchChannelsRequestBody,
"id", matchId,
"channel_ids", channels
);
};

struct MUXY_GAMELINK_API AddMatchChannelsRequest : SendEnvelope<AddOrRemoveMatchChannelsRequestBody>
{
explicit inline AddMatchChannelsRequest(const string& id, const std::vector<string>& channels)
{
this->action = string("add_channels");
this->params.target = string("match");

this->data.matchId = id;
this->data.channels = channels;
}
};

struct MUXY_GAMELINK_API RemoveMatchChannelsRequest : SendEnvelope<AddOrRemoveMatchChannelsRequestBody>
{
explicit inline RemoveMatchChannelsRequest(const string& id, const std::vector<string>& channels)
{
this->action = string("remove_channels");
this->params.target = string("match");

this->data.matchId = id;
this->data.channels = channels;
}
};

struct CreateMatchPollRequestBody
{
gamelink::string matchId;
CreatePollWithConfigurationRequestBody poll;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_2(CreateMatchPollRequestBody,
"id", matchId,
"poll", poll
);
};

struct MUXY_GAMELINK_API CreateMatchPollRequest : SendEnvelope<CreateMatchPollRequestBody>
{
explicit inline CreateMatchPollRequest(const string& match, const CreatePollWithConfigurationRequestBody& poll)
{
this->action = string("create");
this->params.target = string("match_poll");

this->data.matchId = match;
this->data.poll = poll;
}
};


struct DeleteMatchPollRequestBody
{
gamelink::string matchId;
gamelink::string pollId;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_2(DeleteMatchPollRequestBody,
"id", matchId,
"poll_id", pollId
);
};

struct MUXY_GAMELINK_API DeleteMatchPollRequest : SendEnvelope<DeleteMatchPollRequestBody>
{
explicit inline DeleteMatchPollRequest(const string& match, const string& poll)
{
this->action = string("delete");
this->params.target = string("match_poll");

this->data.matchId = match;
this->data.pollId = poll;
}
};

template<typename Config>
struct ReconfigureMatchPollRequestBody
{
gamelink::string matchId;
gamelink::string pollId;
Config config;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_3(ReconfigureMatchPollRequestBody,
"id", matchId,
"poll_id", pollId,
"config", config
);
};

struct MUXY_GAMELINK_API ExpireMatchPollRequest : SendEnvelope<ReconfigureMatchPollRequestBody<ExpireConfig>>
{
explicit inline ExpireMatchPollRequest(const string& id, const string& pollId)
{
this->action = string("reconfigure");
this->params.target = string("match_poll");

this->data.matchId = id;
this->data.pollId = pollId;
this->data.config.endsAt = -1;
}
};

struct MUXY_GAMELINK_API SetMatchPollDisableRequest : SendEnvelope<ReconfigureMatchPollRequestBody<DisableConfig>>
{
explicit inline SetMatchPollDisableRequest(const string& id, const string& pollId, bool status)
{
this->action = string("reconfigure");
this->params.target = string("match_poll");

this->data.matchId = id;
this->data.pollId = pollId;
this->data.config.disabled = status;
}
};

struct MUXY_GAMELINK_API SubscribeMatchPollRequest : SendEnvelope<SubscribeTopicRequestBody>
{
explicit inline SubscribeMatchPollRequest(const string& matchId)
{
this->action = string("subscribe");
this->params.target = string("match_poll");
this->data.topic_id = string(matchId);
}
};

struct MUXY_GAMELINK_API UnsubscribeMatchPollRequest : SendEnvelope<UnsubscribeTopicRequestBody>
{
explicit inline UnsubscribeMatchPollRequest(const string& matchId)
{
this->action = string("unsubscribe");
this->params.target = string("match_poll");
this->data.topic_id = string(matchId);
}
};

struct MatchPollUpdateInformation
{
string matchId;
string pollId;
string status;

std::unordered_map<gamelink::string, PollUpdateBody> results;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_4(MatchPollUpdateInformation,
"match_id", matchId,
"poll_id", pollId,
"status", status,
"results", results
);
};

struct MUXY_GAMELINK_API MatchPollUpdate : ReceiveEnvelope<MatchPollUpdateInformation>
{};


struct MatchPollUpdateInformationInternal
{
string matchId;
string pollId;
string status;

nlohmann::json results;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_4(MatchPollUpdateInformationInternal,
"match_id", matchId,
"poll_id", pollId,
"status", status,
"results", results
);
};

struct MUXY_GAMELINK_API MatchPollUpdateInternal : ReceiveEnvelope<MatchPollUpdateInformationInternal>
{};

template<typename T>
struct BroadcastMatchRequestBody
{
string matchId;
string topic;

T data;

MUXY_GAMELINK_SERIALIZE_INTRUSIVE_3(BroadcastMatchRequestBody,
"match_id", matchId,
"topic", topic,
"data", data
);
};

template<typename T>
struct BroadcastMatchRequest : SendEnvelope<BroadcastMatchRequestBody<T>>
{
BroadcastMatchRequest(const string& matchId, const string& topic, const T& data)
{
this->action = string("broadcast");
this->params.target = string("match");

this->data.matchId = matchId;
this->data.topic = topic;
this->data.data = data;
}
};

struct MUXY_GAMELINK_API BroadcastMatchResponse : ReceiveEnvelope<OKResponseBody>
{};
}
}

#endif
Loading

0 comments on commit 5ba6588

Please sign in to comment.