-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMainSplash.cs
160 lines (131 loc) · 4.9 KB
/
frmMainSplash.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using Lidgren.Library.Network;
namespace Ymfas {
public partial class frmMainSplash : Form {
enum MainSplashState
{
None,
Searching,
Connecting
};
private frmGameLobby GameLobby;
public const int MAX_CONNECT_TIME = 10000; //max time to spend connecting in ms
private MainSplashState splashState;
private int ticksConnecting;
private YmfasClient ymfasClient = null;
private YmfasServer ymfasServer = null;
public frmMainSplash() {
InitializeComponent();
splashState = MainSplashState.None;
}
private void btnExit_Click(object sender, EventArgs e) {
//Exit the game
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void btnHost_Click(object sender, EventArgs e) {
//Host a game
// create a client and a server for the game lobby to use
ymfasServer = new YmfasServer(txtName.Text);
ymfasClient = new YmfasClient(txtName.Text);
ymfasClient.Update();
ymfasClient.Connect(IPAddress.Loopback);
//Enter lobby
GameLobby = new frmGameLobby(ymfasClient, ymfasServer);
GameLobby.ShowDialog();
// if we actually started a game
if (GameLobby.DialogResult == DialogResult.OK) {
ymfasClient = GameLobby.Client;
ymfasServer = GameLobby.Server;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
private void btnJoin_Click(object sender, EventArgs e) {
//Initiate search for servers
grpServerList.Visible = true;
ymfasClient = new YmfasClient(txtName.Text);
btnJoin.Enabled = false;
btnHost.Enabled = false;
ymfasClient.SearchSessions();
//Handle server search in the timer object
splashState = MainSplashState.Searching;
}
private void timer_Tick(object sender, EventArgs e) {
//looking for servers
if (splashState == MainSplashState.Searching) {
Console.Out.WriteLine("searching...");
//update the engine state
ymfasClient.Update();
//attempt to find a new server
Lidgren.Library.Network.NetServerInfo session = ymfasClient.GetLocalSession();
if (session != null) {
String hostname = ymfasClient.GetHostNameFromIP(session.RemoteEndpoint.Address.ToString());
lstServers.Items.Add(hostname + " - " + session.RemoteEndpoint.Address.ToString());
}
}
//connecting to a server
if (splashState == MainSplashState.Connecting) {
// update the engine state
ymfasClient.Update();
//time out if too long
if (ticksConnecting * timer.Interval >= MAX_CONNECT_TIME) {
splashState = MainSplashState.Searching;
ymfasClient.Dispose();
ymfasClient = null;
MessageBox.Show("Connection attempt failed.");
}
else {
ticksConnecting++;
//check for successful connection
if (ymfasClient.Status == NetConnectionStatus.Connected) {
splashState = MainSplashState.None;
// join lobby
GameLobby = new frmGameLobby(ymfasClient);
GameLobby.ShowDialog();
if (GameLobby.DialogResult == DialogResult.OK)
{
ymfasClient = GameLobby.Client;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
}
}
private void lstServers_SelectedIndexChanged(object sender, EventArgs e)
{
try {
String temp = ((String)lstServers.Items[lstServers.SelectedIndex]);
txtConnectIP.Text = temp.Substring(temp.LastIndexOf("-") + 2);
}
catch(Exception err) {
System.Console.WriteLine(err.Message);
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
btnConnect.Enabled = false;
ymfasClient.Connect(txtConnectIP.Text);
splashState = MainSplashState.Connecting;
ticksConnecting = 0;
}
public YmfasServer Server
{
get { return ymfasServer; }
}
public YmfasClient Client
{
get { return ymfasClient; }
}
}
}