Skip to content

Commit

Permalink
fix exceptions when messages happen on unexpected groups
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkyAI committed Jan 25, 2021
1 parent e339c66 commit 558d00a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
68 changes: 50 additions & 18 deletions src/MatterbridgeMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ private void ActionCommandHandler(IServerPlayer player, int groupid, CmdArgs arg
}

WebsocketHandler.SendUserMessage(
player: player,
text: message,
player: player,
text: message,
gateway: gateway,
@event: ApiMessage.EventUserAction
);
Expand Down Expand Up @@ -258,15 +258,17 @@ private void BridgeCommandHandler(IServerPlayer player, int groupid, CmdArgs arg
case "version":
{
player.SendMessage(
groupid,
groupid,
$"{Mod.Info.Name} version {Mod.Info.Version}",
EnumChatType.Notification
);
break;
}
default:
{
player.SendMessage(groupid, $"unknown subcommand {arg0}\n available: join|leave|list|listall|version", EnumChatType.Notification);
player.SendMessage(groupid,
$"unknown subcommand {arg0}\n available: join|leave|list|listall|version",
EnumChatType.Notification);
break;
}
}
Expand Down Expand Up @@ -331,6 +333,7 @@ private void Event_PlayerDisconnect(IServerPlayer byPlayer)
{
totalPlaytime = TimeSpan.Zero;
}

data.CustomPlayerData[PLAYERDATA_TOTALPLAYTIMEKEY] = (timePlayed + totalPlaytime).ToString();

data.CustomPlayerData[PLAYERDATA_TOTALPLAYTIMEKEY] = timePlayed.ToString();
Expand All @@ -357,6 +360,7 @@ private void Event_PlayerNowPlaying(IServerPlayer byPlayer)
{
ConnectTimeDict.Remove(byPlayer.PlayerUID);
}

ConnectTimeDict.Add(byPlayer.PlayerUID, DateTime.UtcNow);

// this forces autojoin groups
Expand Down Expand Up @@ -399,33 +403,53 @@ private void OnTempStormTick(float t1)

var data = _temporalSystem.StormData;

string? messageText = null;

if (_lastData?.stormDayNotify > 1 && data.stormDayNotify == 1 && Config.SendStormEarlyNotification)
{
WebsocketHandler.SendSystemMessage(
text: Config.TEXT_StormEarlyWarning.Replace("{strength}",
data.nextStormStrength.ToString().ToLower()),
gateway: Config.generalGateway
messageText = Config.TEXT_StormEarlyWarning.Replace(
"{strength}", data.nextStormStrength.ToString().ToLower()
);
}

if (_lastData?.stormDayNotify == 1 && data.stormDayNotify == 0)
{
WebsocketHandler.SendSystemMessage(
text: Config.TEXT_StormBegin.Replace("{strength}", data.nextStormStrength.ToString().ToLower()),
gateway: Config.generalGateway
messageText = Config.TEXT_StormBegin.Replace(
"{strength}", data.nextStormStrength.ToString().ToLower()
);
}

//double activeDaysLeft = data.stormActiveTotalDays - api.World.Calendar.TotalDays;
if (_lastData?.stormDayNotify == 0 && data.stormDayNotify != 0)
{
WebsocketHandler.SendSystemMessage(
text: Config.TEXT_StormEnd.Replace("{strength}", data.nextStormStrength.ToString().ToLower()),
gateway: Config.generalGateway
messageText = Config.TEXT_StormEnd.Replace(
"{strength}", data.nextStormStrength.ToString().ToLower()
);
}

_lastData = JsonConvert.DeserializeObject<TemporalStormRunTimeData>(JsonConvert.SerializeObject(data));

if (messageText != null)
{
WebsocketHandler.SendSystemMessage(
text: messageText,
gateway: Config.generalGateway
);
foreach (var mappingEntry in Config.ChannelMapping)
{
if (mappingEntry.skipStormWarning) continue;
var group = Api.Groups.GetOrCreate(Api, mappingEntry.groupName);
Api.SendMessageToGroup(
@group.Uid,
messageText,
EnumChatType.OthersMessage
);
WebsocketHandler.SendSystemMessage(
text: messageText,
gateway: mappingEntry.gateway
);
}
}
}

private void Event_PlayerChat(IServerPlayer byPlayer, int channelId, ref string message, ref string data,
Expand All @@ -443,7 +467,15 @@ private void Event_PlayerChat(IServerPlayer byPlayer, int channelId, ref string
Mod.Logger.Debug($"group: {group.Uid} {group.Name}");

// look up gateway for group name
gateway = Config.ChannelMapping.First(entry => entry.groupName == group.Name).gateway;
var firstItem = Config.ChannelMapping.FirstOrDefault(entry => entry.groupName == group.Name);
if (firstItem != null)
{
gateway = firstItem?.gateway;
}
else
{
return;
}
}

Mod.Logger.Debug("chat: {0}", message);
Expand All @@ -462,20 +494,20 @@ private void Event_PlayerChat(IServerPlayer byPlayer, int channelId, ref string
gateway: gateway
);
}

[HarmonyPatch(typeof(ServerSystemEntitySimulation), "GetDeathMessage")]
class GetDeathMessagePatch
{
static void Postfix(
ConnectedClient client,
ConnectedClient client,
DamageSource src,
ref string __result
)
{
if (Config.SendPlayerDeathEvents)
{
string deathMessage = __result.Replace($"Player {client.PlayerName} ", "");

WebsocketHandler.SendUserMessage(
playerName: client.PlayerName,
playerUid: client.SentPlayerUid,
Expand Down
4 changes: 3 additions & 1 deletion src/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ internal class ChannelMappingEntry
public readonly string groupName;
public readonly string gateway;
public readonly bool isPrivate;
public readonly bool skipStormWarning;

public ChannelMappingEntry(string groupName, string gateway, bool isPrivate)
public ChannelMappingEntry(string groupName, string gateway, bool isPrivate, bool skipStormWarning)
{
this.groupName = groupName;
this.gateway = gateway;
this.isPrivate = isPrivate;
this.skipStormWarning = skipStormWarning;
}
}
}

0 comments on commit 558d00a

Please sign in to comment.