Danbias/Code/Game/LanServer/StandAloneLauncher/Form1.cs

159 lines
5.7 KiB
C#
Raw Normal View History

2014-02-14 10:46:23 +01:00
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;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Threading;
using System.Timers;
using System.IO;
2014-02-14 10:46:23 +01:00
namespace StandAloneLauncher
{
public partial class Form1 : Form
{
System.Windows.Interop.StandaloneGameServerCLI gameServer;
bool serverIsRunning = false;
bool gameIsStarted = false;
2014-02-14 10:46:23 +01:00
public Form1()
{
InitializeComponent();
string[] maps = Directory.GetFiles("..\\Content\\Worlds\\");
for (int i = 0; i < maps.Length; i++)
{
string temp = maps[i].Split('\\').Last() ;
this.mapName.Items.Add(temp);
}
2014-02-18 13:12:08 +01:00
this.gameModes.SelectedIndex = 0;
this.mapName.SelectedIndex = 0;
}
public bool Initiate()
{
this.gameServer = new StandaloneGameServerCLI();
return true;
}
public void Run()
{
while (this.Created)
{
Application.DoEvents();
this.gameServer.ServerUpdate();
2014-02-18 13:12:08 +01:00
this.labelClientsConnected.Text = "Clients connected: " + this.gameServer.GetClientsConnectedCount().ToString();
}
}
private void button1_serverToggle_Click(object sender, EventArgs e)
{
if (this.serverIsRunning)
{
this.serverIsRunning = false;
this.gameServer.ServerStop();
this.listenPort.Enabled = true;
this.serverName.Enabled = true;
this.lanBroadcast.Enabled = true;
this.serverToggle.Text = "Start server";
this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Server terminated!\n");
this.panel_commands.Visible = false;
this.panelServerCommands.Visible = false;
}
else
{
ServerInitDesc desc = new ServerInitDesc();
//desc.mainOptions.broadcast = this.lanBroadcast.Checked;
desc.mainOptions.listenPort = (int)this.listenPort.Value;
desc.mainOptions.serverName = this.serverName.Text;
if (this.gameServer.ServerInitiate(desc) == DanBiasServerReturn.DanBiasServerReturn_Sucess)
{
this.serverIsRunning = true;
GameServerInfo info = this.gameServer.ServerGetInfo();
this.Text = this.serverName.Text + " - " + info.serverIp;
this.listenPort.Enabled = false;
this.serverName.Enabled = false;
this.lanBroadcast.Enabled = false;
this.serverToggle.Text = "Stop server";
this.gameServer.ServerStart();
this.panel_commands.Visible = true;
this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Server initiated!\n\tListening on port " + this.listenPort.Value.ToString() + "\n\tLocal IP: " + info.serverIp + "\n");
this.panelServerCommands.Visible = true;
}
else
{
this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Failed to initiate the server!");
}
}
2014-02-14 10:46:23 +01:00
}
private void buttonStartGame_Click(object sender, EventArgs e)
{
if (!gameIsStarted)
{
//this.gameServer.GameSetGameMode(this.gameModes.SelectedText);
this.gameServer.GameSetGameTime((int)this.timeLimit.Value);
this.gameServer.GameSetMapName(this.mapName.Text);
this.gameServer.GameSetMaxClients((int)this.nrOfClients.Value);
if (!(gameIsStarted = this.gameServer.GameStart(this.forceStart.Checked)))
{
this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Failed to start the game session!\n");
}
else
{
this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Game session started!\n");
this.buttonStartGame.Text = "Stop Game";
this.mapName.Enabled = false;
this.nrOfClients.Enabled = false;
this.gameModes.Enabled = false;
this.timeLimit.Enabled = false;
this.forceStart.Enabled = false;
}
}
else
{
this.gameIsStarted = false;
this.buttonStartGame.Text = "Start Game";
this.mapName.Enabled = true;
this.nrOfClients.Enabled = true;
this.gameModes.Enabled = true;
this.timeLimit.Enabled = true;
this.forceStart.Enabled = true;
}
}
2014-02-18 13:12:08 +01:00
private void FormClosingEvent(object sender, FormClosingEventArgs e)
{
if (serverIsRunning)
{
this.gameServer.ServerStop();
}
}
private void buttonAddNewDataField_Click(object sender, EventArgs e)
{
this.dataProtocolFields.RowCount++;
this.dataProtocolFields.SetRow(this.buttonsAtBottom, this.dataProtocolFields.RowCount - 1);
Panel p = new Panel();
p = this.panel2;
this.dataProtocolFields.RowStyles.Add(new RowStyle(SizeType.Absolute, 27));
}
2014-02-14 10:46:23 +01:00
}
}