This repository was archived by the owner on May 28, 2020. It is now read-only.
forked from darktohka/NetworkSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionForm.cs
More file actions
119 lines (100 loc) · 4.37 KB
/
Copy pathConnectionForm.cs
File metadata and controls
119 lines (100 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NetworkSimulation
{
public partial class ConnectionForm : Form
{
public int objectId;
public NetworkObject from;
public List<NetworkCable> connectedCables = new List<NetworkCable>();
public List<NetworkObject> potentialCons = new List<NetworkObject>();
public ConnectionForm(int objectId)
{
this.objectId = objectId;
this.from = Settings.GetSingleton().GetObject(objectId);
InitializeComponent();
}
private void ConnectionForm_Load(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
connectedCables.Clear();
List<NetworkCable> conns = Settings.GetSingleton().GetConnections(objectId);
List<int> connectedIds = new List<int>();
foreach (int trace in from.GetGraphTrace())
{
connectedIds.Add(trace);
}
conns.Sort((x, y) => x.GetSecondObject(from).GetFloor().CompareTo(y.GetSecondObject(from).GetFloor()));
foreach (NetworkCable cable in conns)
{
NetworkObject second = cable.GetSecondObject(from);
connectedIds.Add(second.GetObjectId());
listBox1.Items.Add("Floor " + (second.GetFloor() + 1) + "'s " + second.GetObjectTypeName() + " - " + second.GetName());
connectedCables.Add(cable);
}
potentialCons.Clear();
foreach (NetworkObject obj in Settings.GetSingleton().GetObjects()) {
if (obj.GetObjectId() == objectId || Math.Abs(obj.GetFloor() - from.GetFloor()) > 1 || connectedIds.Contains(obj.GetObjectId()))
{
continue;
}
potentialCons.Add(obj);
}
potentialCons.Sort((x, y) => x.GetFloor().CompareTo(y.GetFloor()));
foreach (NetworkObject obj in potentialCons) {
listBox2.Items.Add("Floor " + (obj.GetFloor() + 1) + "'s " + obj.GetObjectTypeName() + " - " + obj.GetName());
}
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show("Are you sure you want to remove this connection?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
NetworkCable connectedCable = connectedCables[listBox1.SelectedIndex];
connectedCables.RemoveAt(listBox1.SelectedIndex);
connectedCable.RemoveCable();
Settings.SaveSettings();
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
MessageBox.Show("Successfully removed connection!");
Close();
}
private void connectButton_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex == -1)
{
return;
}
if (from.GetConnectionCount() >= from.GetMaxConnections())
{
MessageBox.Show("All of our ports on the device " + from.GetName() + " are filled! Please remove a connection first.");
return;
}
NetworkObject obj = potentialCons[listBox2.SelectedIndex];
if (obj.GetConnectionCount() >= obj.GetMaxConnections())
{
MessageBox.Show("All of our ports on the device " + obj.GetName() + " are filled! Please remove a connection on " + obj.GetName() + " first.");
return;
}
potentialCons.RemoveAt(listBox2.SelectedIndex);
NetworkCable cable = new NetworkCable(Settings.GetSingleton().AllocateCableId(), from.GetObjectId(), obj.GetObjectId());
Settings.GetSingleton().AddNetworkCable(cable);
Settings.SaveSettings();
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
MessageBox.Show("Wow! Successfully connected to the " + obj.GetObjectTypeName() + " " + obj.GetName() + " on floor " + (obj.GetFloor() + 1) + "!");
Close();
}
}
}