Skip to content

Commit

Permalink
Better Channel Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
valkyrienyanko committed Dec 28, 2021
1 parent fcfcc1b commit f91a2bc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Scripts/IO/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Godot.Collections.Dictionary<string, string> GetStorage()
}
catch (Exception e)
{
Godot.GD.Print($"GetJsonWebToken Error: {e.Message}");
Godot.GD.PrintErr($"GetJsonWebToken Error: {e.Message}");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Handle(PacketReader packetReader)

if (data.ResponseChannelCreateOpcode == ResponseChannelCreateOpcode.ChannelExistsAlready)
{
GD.Print($"Server says channel '{data.ChannelId}' exists already");
GD.Print($"Server says channel '{data.ChannelId}' exists already, going to existing channel");
UIChannels.GoToChannel(data.ChannelId);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Netcode/Packets/Handle/HandlePacketPlayerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static void AddPlayersToUserList()

if (UIUsers.Users.ContainsKey(p.Key))
{
GD.Print($"WARNING: User ID {p.Key} exists in user list already! (Ignoring)");
GD.PrintErr($"WARNING: User ID {p.Key} exists in user list already! (Ignoring)");
GD.Print("UIGame.Players");
foreach (var a in UIGame.Players)
GD.Print($"{a.Key}: {a.Value}");
Expand Down
48 changes: 35 additions & 13 deletions Scripts/UI/UIChannels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ private static void SetupChannel(uint channelId, UIChannel channel)
{
// Create the channel tab button
var btn = new Button();
btn.Text = channel.ChannelName;

if (channel.Users.ContainsKey(channel.CreatorId))
btn.Text = channel.Users[channel.CreatorId]; // e.g. User defined channels
else
btn.Text = channel.ChannelName; // e.g. Global / Game

btn.Connect("pressed", Instance, nameof(_on_Channel_Tab_Btn_pressed), new Godot.Collections.Array{ channelId });

// Add the button to the channel tab list in the scene
Instance.AddChild(btn);

GD.PrintErr("Adding channel with ID " + channelId);

// Keep track of the channel
Channels.Add(channelId, new UIChannel {
ChannelName = channel.ChannelName,
Expand All @@ -68,31 +71,50 @@ public static void RemoveAllChannels()
foreach (var channel in Channels.Values)
channel.Button.QueueFree();

GD.PrintErr("Cleared all channels");
Channels.Clear();
}

public static void CreateChannel(RPacketCreateChannel data)
{
if (!Channels.ContainsKey(data.ChannelId))
if (Channels.ContainsKey(data.ChannelId))
{
GD.Print("WARNING: A new channel with the same key tried to be added but was ignored");
GD.PrintErr("WARNING: A new channel with the same key tried to be added but was ignored");
return;
}

SetupChannel(data.ChannelId, new UIChannel {
CreatorId = data.CreatorId,
ChannelName = data.Users[data.CreatorId],
Users = data.Users
});
GoToChannel(data.ChannelId);
if (data.CreatorId == UIGame.ClientPlayerId)
{
// If this client is the creator of the channel

// Find the other user the creator opened this channel with
uint otherUserId = 0;
foreach (var user in data.Users)
if (user.Key != data.CreatorId)
otherUserId = user.Key;

SetupChannel(data.ChannelId, new UIChannel {
CreatorId = data.CreatorId,
ChannelName = data.Users[otherUserId],
Users = data.Users
});
GoToChannel(data.ChannelId);
}
else
{
// If this client is the other user the channel is being opened to
SetupChannel(data.ChannelId, new UIChannel {
CreatorId = data.CreatorId,
ChannelName = data.Users[data.CreatorId],
Users = data.Users
});
}
}

public static void GoToChannel(uint channelId)
{
if (!Channels.ContainsKey(channelId))
{
GD.Print($"WARNING: Channel with channel ID '{channelId}' does not exist (ignoring)");
GD.PrintErr($"WARNING: Channel with channel ID '{channelId}' does not exist (ignoring)");
GD.Print("A list of all the channels this client can see are listed below");
foreach (var value in Channels.Values)
GD.Print(value.ChannelName);
Expand Down
2 changes: 1 addition & 1 deletion Scripts/UI/UILogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private async static void Login()
break;
}

GD.Print(webResponse.Message);
GD.Print("Web Response: " + webResponse.Message);

var res = JsonConvert.DeserializeObject<WebPostLoginResponse>(webResponse.Message);

Expand Down

0 comments on commit f91a2bc

Please sign in to comment.