|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Specialized; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Controls; |
| 10 | +using System.Windows.Data; |
| 11 | +using System.Windows.Documents; |
| 12 | +using System.Windows.Input; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Windows.Media.Imaging; |
| 15 | +using System.Windows.Shapes; |
| 16 | + |
| 17 | +namespace ValheimServerWarden |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Interaction logic for DiscordWebhookEventNamesWindow.xaml |
| 21 | + /// </summary> |
| 22 | + public partial class DiscordWebhookEventNamesWindow : Window |
| 23 | + { |
| 24 | + private ValheimServer Server { get; set; } |
| 25 | + private Dictionary<string, string> DefaultNames {get; set;} |
| 26 | + private Dictionary<string, string> CustomNames { get; set; } |
| 27 | + private List<TextBox> nameTextBoxes; |
| 28 | + private List<string> removeKeys; |
| 29 | + public DiscordWebhookEventNamesWindow(ValheimServer server) |
| 30 | + { |
| 31 | + InitializeComponent(); |
| 32 | + Server = server; |
| 33 | + nameTextBoxes = new(); |
| 34 | + removeKeys = new(); |
| 35 | + DefaultNames = ValheimServer.DiscordWebhookDefaultAttackNames; |
| 36 | + CustomNames = Server.DiscordServerEventNames; |
| 37 | + var combinedNames = new SortedDictionary<string,string>(); |
| 38 | + foreach (string key in CustomNames.Keys) |
| 39 | + { |
| 40 | + combinedNames.Add(key, CustomNames[key]); |
| 41 | + } |
| 42 | + foreach (string key in DefaultNames.Keys) |
| 43 | + { |
| 44 | + if (!combinedNames.ContainsKey(key)) |
| 45 | + { |
| 46 | + combinedNames.Add(key, DefaultNames[key]); |
| 47 | + } |
| 48 | + } |
| 49 | + foreach (string key in combinedNames.Keys) |
| 50 | + { |
| 51 | + addEventRow(key, combinedNames[key]); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void addEventRow(string key, string name) |
| 56 | + { |
| 57 | + if (key == null) key = ""; |
| 58 | + if (Name == null) Name = ""; |
| 59 | + var txtName = new TextBox(); |
| 60 | + txtName.Margin = new Thickness(5); |
| 61 | + txtName.Text = name; |
| 62 | + txtName.SetValue(Grid.RowProperty, gridNames.RowDefinitions.Count); |
| 63 | + txtName.SetValue(Grid.ColumnProperty, 1); |
| 64 | + nameTextBoxes.Add(txtName); |
| 65 | + |
| 66 | + var txtKey = new TextBox(); |
| 67 | + txtKey.Margin = new Thickness(5); |
| 68 | + txtKey.Text = key; |
| 69 | + txtKey.Tag = key; |
| 70 | + txtKey.IsReadOnly = DefaultNames.ContainsKey(key); |
| 71 | + txtKey.SetValue(Grid.RowProperty, gridNames.RowDefinitions.Count); |
| 72 | + txtKey.SetValue(Grid.ColumnProperty, 0); |
| 73 | + txtName.Tag = txtKey; |
| 74 | + |
| 75 | + gridNames.Children.Add(txtKey); |
| 76 | + gridNames.Children.Add(txtName); |
| 77 | + if (!DefaultNames.ContainsKey(key)) |
| 78 | + { |
| 79 | + var btnRemove = new Button(); |
| 80 | + var image = new Image(); |
| 81 | + image.Source = (BitmapImage)FindResource("Remove"); |
| 82 | + btnRemove.Content = image; |
| 83 | + btnRemove.ToolTip = "Remove custom event name"; |
| 84 | + btnRemove.Margin = new Thickness(5); |
| 85 | + btnRemove.Tag = txtName; |
| 86 | + btnRemove.SetValue(Grid.RowProperty, gridNames.RowDefinitions.Count); |
| 87 | + btnRemove.SetValue(Grid.ColumnProperty, 2); |
| 88 | + btnRemove.Click += (sender, args) => |
| 89 | + { |
| 90 | + var button = (Button)sender; |
| 91 | + var txtName = (TextBox)button.Tag; |
| 92 | + var txtKey = (TextBox)txtName.Tag; |
| 93 | + nameTextBoxes.Remove(txtName); |
| 94 | + gridNames.Children.Remove(txtKey); |
| 95 | + gridNames.Children.Remove(txtName); |
| 96 | + gridNames.Children.Remove(button); |
| 97 | + if (Server.DiscordServerEventNames.ContainsKey(txtKey.Tag.ToString())) |
| 98 | + { |
| 99 | + if (!removeKeys.Contains(txtKey.Tag.ToString())) |
| 100 | + { |
| 101 | + removeKeys.Add(txtKey.Tag.ToString()); |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + gridNames.Children.Add(btnRemove); |
| 106 | + } |
| 107 | + |
| 108 | + var rowDef = new RowDefinition(); |
| 109 | + rowDef.Height = GridLength.Auto; |
| 110 | + gridNames.RowDefinitions.Add(rowDef); |
| 111 | + } |
| 112 | + private void addEventRow() |
| 113 | + { |
| 114 | + addEventRow(null, null); |
| 115 | + } |
| 116 | + |
| 117 | + private void btnCancel_Click(object sender, RoutedEventArgs e) |
| 118 | + { |
| 119 | + Close(); |
| 120 | + } |
| 121 | + |
| 122 | + private void btnSave_Click(object sender, RoutedEventArgs e) |
| 123 | + { |
| 124 | + foreach (var removekey in removeKeys) |
| 125 | + { |
| 126 | + if (Server.DiscordServerEventNames.ContainsKey(removekey)) |
| 127 | + { |
| 128 | + Server.DiscordServerEventNames.Remove(removekey); |
| 129 | + } |
| 130 | + } |
| 131 | + foreach (var txtName in nameTextBoxes) |
| 132 | + { |
| 133 | + var key = ((TextBox)txtName.Tag).Text; |
| 134 | + var oldkey = ((TextBox)txtName.Tag).Tag.ToString(); |
| 135 | + var name = txtName.Text; |
| 136 | + if (!ValheimServer.DiscordWebhookDefaultAttackNames.ContainsKey(key) || (ValheimServer.DiscordWebhookDefaultAttackNames.ContainsKey(key) && ValheimServer.DiscordWebhookDefaultAttackNames[key] != name)) |
| 137 | + { |
| 138 | + if (key == "") continue; |
| 139 | + if (Server.DiscordServerEventNames.ContainsKey(key)) |
| 140 | + { |
| 141 | + Server.DiscordServerEventNames[key] = name; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + Server.DiscordServerEventNames.Add(key, name); |
| 146 | + } |
| 147 | + if (key != oldkey && Server.DiscordServerEventNames.ContainsKey(oldkey)) |
| 148 | + { |
| 149 | + Server.DiscordServerEventNames.Remove(oldkey); |
| 150 | + } |
| 151 | + } |
| 152 | + else if (ValheimServer.DiscordWebhookDefaultAttackNames.ContainsKey(key) && Server.DiscordServerEventNames.ContainsKey(key)) |
| 153 | + { |
| 154 | + Server.DiscordServerEventNames.Remove(key); |
| 155 | + } |
| 156 | + } |
| 157 | + Close(); |
| 158 | + } |
| 159 | + private void btnAddEvent_Click(object sender, RoutedEventArgs e) |
| 160 | + { |
| 161 | + addEventRow(); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments