Fixed Discord packet system for >2 players

This commit is contained in:
MysterD 2021-06-15 00:15:03 -07:00
parent 10e8af83ba
commit 15f8b12770
7 changed files with 26 additions and 14 deletions

View file

@ -132,13 +132,6 @@ static bool ns_discord_initialize(enum NetworkType networkType) {
set_instance_env_variable();
#endif
#ifdef UNSTABLE_BRANCH
if (networkType != NT_NONE) {
// refuse to host on discord for unstable branch
exit(1);
}
#endif
if (!gDiscordInitialized) {
// set up discord params
struct DiscordCreateParams params;
@ -195,6 +188,7 @@ static void ns_discord_shutdown(void) {
struct NetworkSystem gNetworkSystemDiscord = {
.initialize = ns_discord_initialize,
.get_id = ns_discord_get_id,
.save_id = ns_discord_save_id,
.clear_id = ns_discord_clear_id,
.update = ns_discord_update,

View file

@ -38,10 +38,14 @@ void discord_network_flush(void) {
app.lobbies->flush_network(app.lobbies);
}
void ns_discord_save_id(u8 localId) {
s64 ns_discord_get_id(u8 localId) {
return gNetworkUserIds[localId];
}
void ns_discord_save_id(u8 localId, s64 networkId) {
assert(localId > 0);
assert(localId < MAX_PLAYERS);
gNetworkUserIds[localId] = gNetworkUserIds[0];
gNetworkUserIds[localId] = (networkId == 0) ? gNetworkUserIds[0] : networkId;
LOG_INFO("saved user id %d == %lld", localId, gNetworkUserIds[localId]);
}

View file

@ -8,7 +8,8 @@ u8 discord_user_id_to_local_index(int64_t userId);
int ns_discord_network_send(u8 localIndex, u8* data, u16 dataLength);
void discord_network_on_message(UNUSED void* eventData, int64_t lobbyId, int64_t userId, uint8_t channelId, uint8_t* data, uint32_t dataLength);
void discord_network_flush(void);
void ns_discord_save_id(u8 localId);
s64 ns_discord_get_id(u8 localId);
void ns_discord_save_id(u8 localId, s64 networkId);
void ns_discord_clear_id(u8 localId);
void discord_network_init(int64_t lobbyId);
void discord_network_shutdown(void);

View file

@ -33,7 +33,8 @@ enum NetworkSystemType {
struct NetworkSystem {
bool (*initialize)(enum NetworkType);
void (*save_id)(u8 localIndex);
s64 (*get_id)(u8 localIndex);
void (*save_id)(u8 localIndex, s64 networkId);
void (*clear_id)(u8 localIndex);
void (*update)(void);
int (*send)(u8 localIndex, u8* data, u16 dataLength);

View file

@ -130,7 +130,7 @@ u8 network_player_connected(enum NetworkPlayerType type, u8 globalIndex) {
if (np->globalIndex != globalIndex) { continue; }
np->localIndex = i;
np->lastReceived = clock();
gNetworkSystem->save_id(i);
if (gNetworkType == NT_SERVER || type == NPT_SERVER) { gNetworkSystem->save_id(i, 0); }
LOG_ERROR("player connected, reusing local %d, global %d, duplicate event?", i, globalIndex);
return i;
}
@ -152,7 +152,7 @@ u8 network_player_connected(enum NetworkPlayerType type, u8 globalIndex) {
np->globalIndex = (gNetworkType == NT_SERVER) ? i : globalIndex;
np->type = type;
np->lastReceived = clock();
gNetworkSystem->save_id(i);
if (gNetworkType == NT_SERVER || type == NPT_SERVER) { gNetworkSystem->save_id(i, 0); }
for (int j = 0; j < MAX_SYNC_OBJECTS; j++) { gSyncObjects[j].rxEventId[i] = 0; }
if (type == NPT_SERVER) { gNetworkPlayerServer = np; }
else { chat_add_message_ext("player connected", CMT_SYSTEM, get_player_color(np->globalIndex, 0)); }

View file

@ -19,6 +19,7 @@ static void network_send_to_network_players(u8 sendToLocalIndex) {
u8 npType = gNetworkPlayers[i].type;
if (npType == NPT_LOCAL) { npType = NPT_SERVER; }
else if (i == sendToLocalIndex) { npType = NPT_LOCAL; }
s64 networkId = gNetworkSystem->get_id(i);
packet_write(&p, &npType, sizeof(u8));
packet_write(&p, &gNetworkPlayers[i].globalIndex, sizeof(u8));
packet_write(&p, &gNetworkPlayers[i].currLevelAreaSeqId, sizeof(u16));
@ -26,6 +27,7 @@ static void network_send_to_network_players(u8 sendToLocalIndex) {
packet_write(&p, &gNetworkPlayers[i].currActNum, sizeof(s16));
packet_write(&p, &gNetworkPlayers[i].currLevelNum, sizeof(s16));
packet_write(&p, &gNetworkPlayers[i].currAreaIndex, sizeof(s16));
packet_write(&p, &networkId, sizeof(s64));
LOG_INFO("send network player [%d == %d]", gNetworkPlayers[i].globalIndex, npType);
}
@ -54,6 +56,7 @@ void network_receive_network_players(struct Packet* p) {
u8 npType, globalIndex;
u16 levelAreaSeqId;
s16 courseNum, actNum, levelNum, areaIndex;
s64 networkId;
packet_read(p, &npType, sizeof(u8));
packet_read(p, &globalIndex, sizeof(u8));
packet_read(p, &levelAreaSeqId, sizeof(u16));
@ -61,6 +64,7 @@ void network_receive_network_players(struct Packet* p) {
packet_read(p, &actNum, sizeof(s16));
packet_read(p, &levelNum, sizeof(s16));
packet_read(p, &areaIndex, sizeof(s16));
packet_read(p, &networkId, sizeof(s64));
u8 localIndex = network_player_connected(npType, globalIndex);
LOG_INFO("received network player [%d == %d] (%d)", globalIndex, npType, localIndex);
@ -72,6 +76,9 @@ void network_receive_network_players(struct Packet* p) {
np->currLevelNum = levelNum;
np->currAreaIndex = areaIndex;
LOG_INFO("received network player location (%d, %d, %d, %d)", courseNum, actNum, levelNum, areaIndex);
if (gNetworkType == NT_CLIENT && globalIndex != 0 && localIndex != 0) {
gNetworkSystem->save_id(localIndex, networkId);
}
}
}
}

View file

@ -96,7 +96,11 @@ static bool ns_socket_initialize(enum NetworkType networkType) {
return true;
}
static void ns_socket_save_id(u8 localId) {
static s64 ns_socket_get_id(u8 localId) {
return 0;
}
static void ns_socket_save_id(u8 localId, s64 networkId) {
assert(localId > 0);
assert(localId < MAX_PLAYERS);
addr[localId] = addr[0];
@ -139,6 +143,7 @@ static void ns_socket_shutdown(void) {
struct NetworkSystem gNetworkSystemSocket = {
.initialize = ns_socket_initialize,
.get_id = ns_socket_get_id,
.save_id = ns_socket_save_id,
.clear_id = ns_socket_clear_id,
.update = ns_socket_update,