Skip to content

Commit

Permalink
Fix players duplicating, also don't draw pants on dead people.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarbonNeuron committed Jan 12, 2021
1 parent b399fcb commit f3c0c49
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
6 changes: 5 additions & 1 deletion AUCapture-WPF/Controls/PlayerControl.xaml
Expand Up @@ -10,6 +10,7 @@
<UserControl.Resources>
<converters1:PlayerToImage x:Key="PlayerToImage" />
<converters1:PantsConverter x:Key="PantsConverter" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Grid>

Expand All @@ -29,7 +30,10 @@
</Image>
<Image Grid.Row="1" Margin="0 0 0 10" Stretch="Uniform">
<Image.Source>
<Binding ElementName="PlayerControlWindow" Path="PlayerPantsID" Converter="{StaticResource PantsConverter}"/>
<MultiBinding Converter="{StaticResource PantsConverter}">
<Binding ElementName="PlayerControlWindow" Path="PlayerPantsID"></Binding>
<Binding ElementName="PlayerControlWindow" Path="AliveStatus"></Binding>
</MultiBinding>
</Image.Source>
</Image>
<Viewbox Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center"
Expand Down
19 changes: 13 additions & 6 deletions AUCapture-WPF/Converters/PantsConverter.cs
Expand Up @@ -10,16 +10,23 @@

namespace AUCapture_WPF.Converters
{
class PantsConverter : IValueConverter
class PantsConverter : IMultiValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var pantID = value as uint? ?? 0;
return new BitmapImage(new Uri($"pack://application:,,,/Resources/Pants/{pantID}.png"));
if (values.Count() == 2)
{
var pantID = values[0] as uint? ?? 0;
var alive = values[1] as bool? ?? false;
return new BitmapImage(new Uri($"pack://application:,,,/Resources/Pants/{(alive? pantID:0) }.png"));
}

return new BitmapImage(new Uri($"pack://application:,,,/Resources/Players/aured.png"));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return value?.Equals(true.ToString()) ?? false;
throw new NotImplementedException();
}
}
}
53 changes: 39 additions & 14 deletions AUCapture-WPF/MainWindow.xaml.cs
Expand Up @@ -9,6 +9,7 @@
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
Expand Down Expand Up @@ -116,6 +117,7 @@ public MainWindow()
GameMemReader.getInstance().GameOver += OnGameOver;
App.socket.OnConnected += SocketOnOnConnected;
App.socket.OnDisconnected += SocketOnOnDisconnected;
context.Players.CollectionChanged += PlayersOnCollectionChanged;
IPCAdapter.getInstance().OnToken += (sender, token) =>
{
this.BeginInvoke((w) =>
Expand Down Expand Up @@ -181,15 +183,24 @@ public MainWindow()
//ApplyDarkMode();
}

private void PlayersOnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
AmongUsCapture.Settings.conInterface.WriteModuleTextColored("Players", Color.Aqua, JsonConvert.SerializeObject(e, Formatting.None,new StringEnumConverter()));
}

private void OnPlayerCosmeticChanged(object? sender, PlayerCosmeticChangedEventArgs e)
{
var player = context.Players.First(x => x.Name == e.Name);
Console.WriteLine("Cosmetic change");
Dispatcher.Invoke((Action) (() =>
if (context.Players.Any(x => x.Name == e.Name))
{
player.HatID = e.HatId;
player.PantsID = e.SkinId;
}));
var player = context.Players.First(x => x.Name == e.Name);
Console.WriteLine("Cosmetic change");
Dispatcher.Invoke((Action) (() =>
{
player.HatID = e.HatId;
player.PantsID = e.SkinId;
}));
}

}

private void SocketOnOnDisconnected(object? sender, EventArgs e)
Expand Down Expand Up @@ -354,7 +365,10 @@ private void OnGameOver(object? sender, GameOverEventArgs e)
{
Dispatcher.Invoke((Action) (() =>
{
context.Players.Clear(); //Clear players because game is over
foreach (var player in context.Players)
{
player.Alive = true;
}
}));
}

Expand All @@ -370,9 +384,9 @@ private void UserForm_PlayerChanged(object sender, PlayerChangedEventArgs e)
}
else
{
if (e.Action != PlayerAction.Joined && context.Players.Any(x => x.Name == e.Name))
if (e.Action != PlayerAction.Joined && context.Players.Any(x => x.Name == e.Name || x.Color==e.Color))
{
var player = context.Players.First(x => x.Name == e.Name);
var player = context.Players.First(x => x.Name == e.Name || x.Color == e.Color);
Dispatcher.Invoke((Action) (() =>
{
switch (e.Action)
Expand All @@ -395,14 +409,19 @@ private void UserForm_PlayerChanged(object sender, PlayerChangedEventArgs e)
}
else
{
Dispatcher.Invoke((Action) (() =>
if (e.Action == PlayerAction.Joined)
{
context.Players.Add(new Player(e.Name, e.Color, !e.IsDead, 0, 0));
}));
Dispatcher.Invoke((Action) (() =>
{
context.Players.Add(new Player(e.Name, e.Color, !e.IsDead, 0, 0));
}));
}

}
}

//Program.conInterface.WriteModuleTextColored("GameMemReader", Color.Green, e.Name + ": " + e.Action);
AmongUsCapture.Settings.conInterface.WriteModuleTextColored("GameMemReader", Color.Green, e.Name + ": " + e.Action);
}

private void OnChatMessageAdded(object sender, ChatMessageEventArgs e)
Expand Down Expand Up @@ -565,7 +584,13 @@ private void GameStateChangedHandler(object sender, GameStateChangedEventArgs e)
Dispatcher.Invoke((Action) (() =>
{
context.GameState = e.NewState;
context.Players.Clear(); //Clear players because game is over
}));
}
else if (e.NewState == GameState.LOBBY)
{
Dispatcher.Invoke((Action) (() =>
{
context.GameState = e.NewState;
}));
}
//Program.conInterface.WriteModuleTextColored("GameMemReader", Color.Green, "State changed to " + e.NewState);
Expand Down

0 comments on commit f3c0c49

Please sign in to comment.