Skip to content

Commit

Permalink
version 1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrizziDerKek committed Aug 23, 2024
1 parent 9aed67f commit 828ba8d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 74 deletions.
21 changes: 21 additions & 0 deletions BSR_Client/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

#pragma warning disable IDE0079
#pragma warning disable IDE0044

namespace BSR_Client
Expand Down Expand Up @@ -73,6 +76,24 @@ public partial class MainWindow
private bool PacketHandled = false;
private bool UsedTrashBin = false;

private class ElementLib
{
public Rectangle[] Bullets;
public ProgressBar[] HealthBars;
public Button[] Items;
public Button[] Players;

public ElementLib(MainWindow w)
{
Bullets = new Rectangle[] { w.Bullet1, w.Bullet2, w.Bullet3, w.Bullet4, w.Bullet5, w.Bullet6, w.Bullet7, w.Bullet8 };
HealthBars = new ProgressBar[] { w.Health1, w.Health2, w.Health3, w.Health4, w.Health5 };
Items = new Button[] { w.Item1, w.Item2, w.Item3, w.Item4, w.Item5, w.Item6, w.Item7, w.Item8 };
Players = new Button[] { w.Player1, w.Player2, w.Player3, w.Player4, w.Player5 };
}
}

private ElementLib Elements = null;

private class SoundLib
{
public MediaPlayer Title = new MediaPlayer() { Volume = 0.1 };
Expand Down
99 changes: 37 additions & 62 deletions BSR_Client/Logic.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BSR_Client
{
Expand All @@ -26,16 +24,14 @@ public void ShowBullets(EBullet[] bullets)
nblank++;
}
Announce(nlive + " lives, " + nblank + " blanks");
Rectangle[] displays = new Rectangle[] { Bullet1, Bullet2, Bullet3, Bullet4, Bullet5, Bullet6, Bullet7, Bullet8 };
for (int i = 0; i < bullets.Length; i++)
displays[i].Fill = bullets[i] == EBullet.Live ? Brushes.Red : Brushes.Gray;
Elements.Bullets[i].Fill = bullets[i] == EBullet.Live ? Brushes.Red : Brushes.Gray;
}

public void ResetBullets()
{
Rectangle[] displays = new Rectangle[] { Bullet1, Bullet2, Bullet3, Bullet4, Bullet5, Bullet6, Bullet7, Bullet8 };
for (int i = 0; i < displays.Length; i++)
displays[i].Fill = Brushes.Transparent;
for (int i = 0; i < Elements.Bullets.Length; i++)
Elements.Bullets[i].Fill = Brushes.Transparent;
}

public void Dead()
Expand Down Expand Up @@ -74,14 +70,12 @@ public void RemovePlayer(string player)
{
Players.Remove(player);
PlayerTurns.Remove(player);
ProgressBar[] displays = new ProgressBar[] { Health1, Health2, Health3, Health4, Health5 };
Button[] players = new Button[] { Player1, Player2, Player3, Player4, Player5 };
int it = 0;
foreach (Button b in players)
foreach (Button b in Elements.Players)
{
if (IsPlayerSlot(b, player))
{
displays[it].Value = 0;
Elements.HealthBars[it].Value = 0;
break;
}
it++;
Expand Down Expand Up @@ -113,10 +107,9 @@ public void GenerateItems(bool remoteGenerate, int remoteCount)

public int GetItemCount()
{
Button[] displays = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
int count = 0;
for (int i = 0; i < displays.Length; i++)
if (!IsItemSlot(displays[i], "Nothing"))
for (int i = 0; i < Elements.Items.Length; i++)
if (!IsItemSlot(Elements.Items[i], "Nothing"))
count++;
return count;
}
Expand All @@ -125,21 +118,19 @@ public void UpdateLives(string player, int lives, bool set, bool lose, bool sync
{
if (set && lose)
return;
ProgressBar[] displays = new ProgressBar[] { Health1, Health2, Health3, Health4, Health5 };
Button[] players = new Button[] { Player1, Player2, Player3, Player4, Player5 };
if (sync)
Packet.Create(EPacket.UpdateLives).Add(player).Add(lives).Add(set).Add(lose).Send(Sync);
int it = 0;
foreach (Button b in players)
foreach (Button b in Elements.Players)
{
if (IsPlayerSlot(b, player))
{
if (set)
displays[it].Value = lives;
Elements.HealthBars[it].Value = lives;
else if (lose)
displays[it].Value -= lives;
Elements.HealthBars[it].Value -= lives;
else
displays[it].Value += lives;
Elements.HealthBars[it].Value += lives;
break;
}
it++;
Expand Down Expand Up @@ -214,12 +205,11 @@ public bool AddPlayer(string username)
if (Players.Contains(username))
return false;
Players.Add(username);
Button[] displays = new Button[] { Player1, Player2, Player3, Player4, Player5 };
for (int i = 0; i < displays.Length; i++)
for (int i = 0; i < Elements.Players.Length; i++)
{
if (IsPlayerSlot(displays[i], "None"))
if (IsPlayerSlot(Elements.Players[i], "None"))
{
SetPlayerData(displays[i], username, true);
SetPlayerData(Elements.Players[i], username, true);
break;
}
}
Expand Down Expand Up @@ -260,34 +250,28 @@ public bool IsPlayerActive()
public void SetActive(bool active)
{
Shoot.IsEnabled = active;
Button[] items = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
foreach (Button i in items)
foreach (Button i in Elements.Items)
i.IsEnabled = false;
items = new Button[] { Player1, Player2, Player3, Player4, Player5 };
foreach (Button i in items)
foreach (Button i in Elements.Players)
i.IsEnabled = false;
}

public void HideInactivePlayers()
{
Button[] items = new Button[] { Player1, Player2, Player3, Player4, Player5 };
foreach (Button i in items)
foreach (Button i in Elements.Players)
if (IsPlayerSlot(i, "None"))
i.Visibility = Visibility.Hidden;
}

public void PrepareGun()
{
Shoot.IsEnabled = false;
Button[] items = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
foreach (Button i in items)
foreach (Button i in Elements.Items)
i.IsEnabled = false;
items = new Button[] { Player1, Player2, Player3, Player4, Player5 };
ProgressBar[] displays = new ProgressBar[] { Health1, Health2, Health3, Health4, Health5 };
int it = 0;
foreach (Button i in items)
foreach (Button i in Elements.Players)
{
if (!IsPlayerSlot(i, "None") && displays[it].Value > 0)
if (!IsPlayerSlot(i, "None") && Elements.HealthBars[it].Value > 0)
i.IsEnabled = true;
it++;
}
Expand Down Expand Up @@ -328,8 +312,7 @@ public string GetItemType(Button slot)

public void SetAngry(string player)
{
Button[] items = new Button[] { Player1, Player2, Player3, Player4, Player5 };
foreach (Button i in items)
foreach (Button i in Elements.Players)
{
if (IsPlayerSlot(i, player))
{
Expand Down Expand Up @@ -381,29 +364,26 @@ public void SetItemData(Button slot, EItem type)

public int GetHealth()
{
Button[] items = new Button[] { Player1, Player2, Player3, Player4, Player5 };
ProgressBar[] displays = new ProgressBar[] { Health1, Health2, Health3, Health4, Health5 };
int it = 0;
foreach (Button i in items)
foreach (Button i in Elements.Players)
{
if (IsPlayerSlot(i, MyName))
return (int)displays[it].Value;
return (int)Elements.HealthBars[it].Value;
it++;
}
return 0;
}

public bool SetItem(EItem type, bool enable = false)
{
Button[] displays = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
for (int i = 0; i < displays.Length; i++)
for (int i = 0; i < Elements.Items.Length; i++)
{
if (IsItemSlot(displays[i], "Nothing"))
if (IsItemSlot(Elements.Items[i], "Nothing"))
{
displays[i].Visibility = Visibility.Visible;
SetItemData(displays[i], type);
Elements.Items[i].Visibility = Visibility.Visible;
SetItemData(Elements.Items[i], type);
if (enable)
displays[i].IsEnabled = true;
Elements.Items[i].IsEnabled = true;
return true;
}
}
Expand All @@ -412,21 +392,19 @@ public bool SetItem(EItem type, bool enable = false)

public void HideEmptyItemSlots()
{
Button[] displays = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
for (int i = 0; i < displays.Length; i++)
if (IsItemSlot(displays[i], "Nothing"))
displays[i].Visibility = Visibility.Hidden;
for (int i = 0; i < Elements.Items.Length; i++)
if (IsItemSlot(Elements.Items[i], "Nothing"))
Elements.Items[i].Visibility = Visibility.Hidden;
}

public EItem UseItem(string slot)
{
Button it = null;
Button[] displays = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
for (int i = 0; i < displays.Length; i++)
for (int i = 0; i < Elements.Items.Length; i++)
{
if (displays[i].Name == slot)
if (Elements.Items[i].Name == slot)
{
it = displays[i];
it = Elements.Items[i];
break;
}
}
Expand All @@ -445,12 +423,9 @@ public EItem UseItem(string slot)

public void UnlockItems()
{
Button[] displays = new Button[] { Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8 };
for (int i = 0; i < displays.Length; i++)
{
if (!IsItemSlot(displays[i], "Nothing"))
displays[i].IsEnabled = true;
}
for (int i = 0; i < Elements.Items.Length; i++)
if (!IsItemSlot(Elements.Items[i], "Nothing"))
Elements.Items[i].IsEnabled = true;
}

public bool ProcessIsRunning()
Expand Down
25 changes: 20 additions & 5 deletions BSR_Client/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,26 @@
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<ProgressBar Name="Health1" Grid.Row="1" Grid.Column="0" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<ProgressBar Name="Health2" Grid.Row="1" Grid.Column="1" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<ProgressBar Name="Health3" Grid.Row="1" Grid.Column="2" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<ProgressBar Name="Health4" Grid.Row="1" Grid.Column="3" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<ProgressBar Name="Health5" Grid.Row="1" Grid.Column="4" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<Grid Grid.Row="1" Grid.Column="0">
<ProgressBar Name="Health1" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<TextBlock Name="HealthText1" Text="{Binding ElementName=Health1, Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="1">
<ProgressBar Name="Health2" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<TextBlock Name="HealthText2" Text="{Binding ElementName=Health2, Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="2">
<ProgressBar Name="Health3" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<TextBlock Name="HealthText3" Text="{Binding ElementName=Health3, Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="3">
<ProgressBar Name="Health4" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<TextBlock Name="HealthText4" Text="{Binding ElementName=Health4, Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="4">
<ProgressBar Name="Health5" Margin="5" Minimum="0" Maximum="10" Background="#454545"/>
<TextBlock Name="HealthText5" Text="{Binding ElementName=Health5, Path=Value}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
</Grid>
<Button Margin="5" Grid.Row="2" Grid.Column="2" Name="Shoot" Click="Button_Click" IsEnabled="False" BorderBrush="#454545">
<Grid>
<Image Stretch="Fill" Source="textures/shotgun.png" Margin="0,0,0,0"/>
Expand Down
4 changes: 2 additions & 2 deletions BSR_Client/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Shapes;

namespace BSR_Client
{
Expand All @@ -12,6 +10,7 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
Elements = new ElementLib(this);
int r1 = RNG.Next();
int r2 = RNG.Next();
RNG.Next(Math.Min(r1, r2), Math.Max(r1, r2));
Expand Down Expand Up @@ -83,6 +82,7 @@ private void Button_Click(object sender, RoutedEventArgs e)
EItem item = UseItem(action);
if (UsedTrashBin)
{
UsedTrashBin = false;
EItem newitem;
do newitem = (EItem)RNG.Next((int)EItem.Nothing + 1, (int)EItem.Count);
while (newitem == EItem.Trashbin || newitem == item);
Expand Down
4 changes: 2 additions & 2 deletions BSR_Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.3.2.0")]
[assembly: AssemblyFileVersion("1.3.2.0")]
11 changes: 8 additions & 3 deletions BSR_Client/Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace BSR_Client
{
public class Client
{
private TcpClient Socket;
private Stream Stream;
private MainWindow Owner;
private readonly TcpClient Socket;
private readonly Stream Stream;
private readonly MainWindow Owner;
private readonly Mutex Lock;

public Client(string ip, int port, MainWindow owner)
{
Socket = new TcpClient(ip, port);
Stream = Socket.GetStream();
Owner = owner;
Lock = new Mutex();
}

public void Send(string message)
{
Lock.WaitOne();
message = "$" + message;
Task.Delay(100).Wait();
byte[] buffer = Encoding.UTF8.GetBytes(message);
Stream.Write(buffer, 0, buffer.Length);
Lock.ReleaseMutex();
}

public void Start() => Task.Run(Run);
Expand Down

0 comments on commit 828ba8d

Please sign in to comment.