Skip to content

Commit

Permalink
Lots of stuff
Browse files Browse the repository at this point in the history
- Potential fix for DNS problems
- Window flashes when you get a new pack
- Status text box displays number of cards in maindeck and sideboard
- Card preview window always shows within screen bounds
- Don't quit after alerts in the connect window
- Hide draft window from Alt-Tab in server mode
- Don't show pack counts after draft is complete
- Don't quit to connect window on disconnect after draft is complete
  • Loading branch information
thquinn committed Feb 26, 2015
1 parent 900d2b9 commit 0aa3614
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 64 deletions.
4 changes: 2 additions & 2 deletions CardWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions CardWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@ public void SetImage(Image image)
Height = image.Height;
pictureBox1.Image = image;
}

// Sets the midpoint of the control, screen bounds permitting.
public void SetLocation(Point point)
{
point.X = Util.Clamp(0, point.X - Width / 2, Screen.PrimaryScreen.Bounds.Width - Width);
point.Y = Util.Clamp(0, point.Y - Height / 2, Screen.PrimaryScreen.Bounds.Height - Height);
Location = point;
}
}
}
5 changes: 4 additions & 1 deletion ConnectWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ private void button1_Click(object sender, EventArgs e)
else if (textBox2.Text.Contains('|') || textBox2.Text.Contains(';'))
MessageBox.Show("Your alias contains disallowed characters.");
else
{
DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
this.Close();
}
}

// Start Server.
Expand All @@ -43,6 +45,7 @@ private void button2_Click(object sender, EventArgs e)
ServerWindow serverWindow = new ServerWindow();
DialogResult = System.Windows.Forms.DialogResult.Abort;
this.Hide();
Owner.Owner = serverWindow;
serverWindow.Show();
}

Expand Down
25 changes: 21 additions & 4 deletions DeckBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class DeckBuilder : Panel
public static readonly Color DRAGGED_STROKE_COLOR = Color.Gold;
public static readonly int DRAGGED_STROKE_THICKNESS = 5;

public DraftWindow draftWindow;
public CardWindow cardWindow;
private List<List<DeckBuilderCard>[]> columns;
private DeckBuilderCard draggedCard = null;
Expand Down Expand Up @@ -135,6 +136,7 @@ private void LayoutControls()
Controls.SetChildIndex(card, columns[column][row].Count - cardNum);
}
indicator.BringToFront();
SetCardCounts();
}

protected override void OnMouseDown(MouseEventArgs e)
Expand All @@ -156,11 +158,11 @@ protected override void OnMouseDown(MouseEventArgs e)
return;

// Reposition card form and draw.
float x = card.Left + card.Width / 2f - (CARD_WIDTH / 2f);
float y = card.Top + card.Height / 2f - (CARD_HEIGHT / 2f);
Point point = PointToScreen(new Point((int)Math.Round(x), (int)Math.Round(y)));
cardWindow.SetImage(DraftWindow.GetImage(card.cardName));
cardWindow.Location = point;
float x = card.Left + card.Width / 2f;
float y = card.Top + card.Height / 2f;
Point point = PointToScreen(new Point((int)Math.Round(x), (int)Math.Round(y)));
cardWindow.SetLocation(point);
cardWindow.Show();
Focus();
}
Expand Down Expand Up @@ -328,6 +330,21 @@ public int GetMaxFirstRowLength()
return output;
}

public void SetCardCounts()
{
int maindeck = 0;
for (int column = 0; column < columns.Count - 1; column++)
for (int row = 0; row < 2; row++)
for (int cardNum = 0; cardNum < columns[column][row].Count; cardNum++)
maindeck++;
int sideboard = 0;
for (int row = 0; row < 2; row++)
for (int cardNum = 0; cardNum < columns[columns.Count - 1][row].Count; cardNum++)
sideboard++;
if (draftWindow != null && maindeck + sideboard > 0)
draftWindow.SetCardCounts(maindeck, sideboard);
}

public String GetCockatriceDeck()
{
Dictionary<string, int> quantities = new Dictionary<string, int>();
Expand Down
30 changes: 23 additions & 7 deletions DraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ public class DraftClient
DraftWindow draftWindow;
EventDrivenTCPClient client;
string alias;
bool draftDone = false;

public DraftClient(DraftWindow draftWindow, string hostname, string alias)
{
this.draftWindow = draftWindow;
this.alias = alias;

IPHostEntry hostEntry = Dns.GetHostEntry(hostname);
client = new EventDrivenTCPClient(hostEntry.AddressList[0], 10024, false);
IPAddress address;
if (!IPAddress.TryParse(hostname, out address))
{
IPHostEntry hostEntry = Dns.GetHostEntry(hostname);
if (hostEntry.AddressList.Length == 0)
{
draftWindow.PrintLine("Couldn't resolve hostname!");
return;
}
address = hostEntry.AddressList[0];
}
client = new EventDrivenTCPClient(address, 10024, false);
client.DataEncoding = Encoding.UTF8;
client.ConnectionStatusChanged += new EventDrivenTCPClient.delConnectionStatusChanged(client_ConnectionStatusChanged);
client.DataReceived += new EventDrivenTCPClient.delDataReceived(client_DataReceived);
Expand All @@ -34,12 +45,15 @@ void client_ConnectionStatusChanged(EventDrivenTCPClient sender, EventDrivenTCPC
else if (status == EventDrivenTCPClient.ConnectionStatus.DisconnectedByHost || status == EventDrivenTCPClient.ConnectionStatus.DisconnectedByUser)
{
draftWindow.PrintLine("Connection failed: " + status.ToString());
draftWindow.Invoke(new MethodInvoker(delegate
if (!draftDone)
{
draftWindow.ClearDraftPicker();
draftWindow.OpenConnectWindow();
}));
draftWindow.ClearCardPool();
draftWindow.Invoke(new MethodInvoker(delegate
{
draftWindow.ClearDraftPicker();
draftWindow.OpenConnectWindow();
}));
draftWindow.ClearCardPool();
}
}
}
void client_DataReceived(EventDrivenTCPClient sender, object data)
Expand Down Expand Up @@ -127,7 +141,9 @@ private void HandleMessage(string msg)
}
else if (parts[0] == "DONE")
{
draftDone = true;
draftWindow.PrintLine("The draft has ended.");
draftWindow.ClearPackCounts();
}
else
draftWindow.PrintLine("Unknown message from server: " + msg);
Expand Down
8 changes: 4 additions & 4 deletions DraftPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ protected override void OnMouseDown(MouseEventArgs e)
return;

// Reposition card form and draw.
float x = (i % perRow) * (spacing + CARD_WIDTH * scale) + spacing + (CARD_WIDTH * scale / 2) - (CARD_WIDTH / 2);
float y = (i / perRow) * (spacing + CARD_HEIGHT * scale) + spacing + (CARD_HEIGHT * scale / 2) - (CARD_HEIGHT / 2);
Point point = PointToScreen(new Point((int)Math.Round(x), (int)Math.Round(y)));
cardWindow.SetImage(DraftWindow.GetImage(cardNames[i]));
cardWindow.Location = point;
float x = (i % perRow) * (spacing + CARD_WIDTH * scale) + spacing + (CARD_WIDTH * scale / 2);
float y = (i / perRow) * (spacing + CARD_HEIGHT * scale) + spacing + (CARD_HEIGHT * scale / 2);
Point point = PointToScreen(new Point((int)Math.Round(x), (int)Math.Round(y)));
cardWindow.SetLocation(point);
cardWindow.Show();
Focus();
}
Expand Down
6 changes: 4 additions & 2 deletions DraftServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace IsochronDrafter
{
public class DraftServer
{
private static readonly int NUM_RARES_IN_PACK = 1;
private static readonly int NUM_UNCOMMONS_IN_PACK = 3;
private static readonly int NUM_RARES_IN_PACK = 0;
private static readonly int NUM_UNCOMMONS_IN_PACK = 4;
private static readonly int NUM_COMMONS_IN_PACK = 10;

ServerWindow serverWindow;
Expand Down Expand Up @@ -144,6 +144,7 @@ private void HandleMessage(TcpServerConnection connection, string msg)
}
else
{
// Reconnect user to draft.
serverWindow.PrintLine("<" + GetAlias(connection) + "> has new alias " + parts[1] + ".");
aliases.TryAdd(connection, parts[1]);
TrySendMessage(connection, "OK|ALIAS");
Expand All @@ -156,6 +157,7 @@ private void HandleMessage(TcpServerConnection connection, string msg)
TrySendMessage(connection, "CARD_POOL|" + string.Join("|", draftState.cardPool));
if (draftState.boosters.Count > 0)
TrySendMessage(connection, "BOOSTER|" + string.Join("|", draftState.boosters[0]));
SendPackCounts();
}
else
{
Expand Down
72 changes: 36 additions & 36 deletions DraftWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions DraftWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class DraftWindow : Form
public CardWindow cardWindow;
public DraftClient draftClient;
public bool canPick = true;
public string packCounts = "", statusText = "";
public string packCounts = "", statusText = "", cardCounts = "";

public DraftWindow()
{
Expand All @@ -28,6 +28,7 @@ public DraftWindow()
cardWindow = new CardWindow();
cardWindow.Visible = false;
draftPicker.cardWindow = cardWindow;
deckBuilder.draftWindow = this;
deckBuilder.cardWindow = cardWindow;
}

Expand Down Expand Up @@ -84,14 +85,25 @@ public void SetPackCounts(string message)
packCounts += "\r\n" + parts[i] + " has " + parts[i + 1] + (parts[i + 1] == "1" ? " pack." : " packs.");
SetStatusTextBox();
}
public void ClearPackCounts()
{
packCounts = "";
SetStatusTextBox();
}
public void SetCardCounts(int maindeck, int sideboard)
{
cardCounts = "Your main deck contains " + maindeck + (maindeck == 1 ? " card" : " cards") + " and your sideboard contains " + sideboard + (sideboard == 1 ? " card." : " cards.");
SetStatusTextBox();
}
private void SetStatusTextBox()
{
statusTextBox.Invoke(new MethodInvoker(delegate
{
if (packCounts.Length == 0)
statusTextBox.Text = statusText.Trim();
else
statusTextBox.Text = statusText.Trim() + "\r\n\r\n" + packCounts.Trim();
statusTextBox.Text = statusText.Trim();
if (packCounts != "")
statusTextBox.Text += "\r\n\r\n" + packCounts.Trim();
if (cardCounts != "")
statusTextBox.Text += "\r\n\r\n" + cardCounts.Trim();
statusTextBox.SelectionStart = statusTextBox.Text.Length;
statusTextBox.ScrollToCaret();
}));
Expand All @@ -102,6 +114,11 @@ public void PopulateDraftPicker(string message)
booster.RemoveAt(0);
PrintLine("Received booster with " + booster.Count + (booster.Count == 1 ? " card." : " cards."));
draftPicker.Populate(booster);
this.Invoke(new MethodInvoker(delegate
{
if (Form.ActiveForm != this)
FlashWindow.Flash(this);
}));
}
public void ClearDraftPicker()
{
Expand Down
Loading

0 comments on commit 0aa3614

Please sign in to comment.