summaryrefslogtreecommitdiffhomepage
path: root/Stars45/NetServerDlg.cpp
blob: b1ddb61b83d1969aa69e82b92ea2233a466cb8a0 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*  Project Starshatter 5.0
	Destroyer Studios LLC
	Copyright © 1997-2007. All Rights Reserved.

	SUBSYSTEM:    Stars.exe
	FILE:         NetServerDlg.cpp
	AUTHOR:       John DiCamillo


	OVERVIEW
	========
	Main Menu Dialog Active Window class
*/

#include "MemDebug.h"
#include "NetServerDlg.h"
#include "NetServerConfig.h"
#include "MenuScreen.h"
#include "Starshatter.h"

#include "NetAddr.h"
#include "HttpClient.h"
#include "HttpServer.h"

#include "DataLoader.h"
#include "Button.h"
#include "EditBox.h"
#include "ComboBox.h"
#include "Video.h"
#include "Keyboard.h"
#include "MachineInfo.h"

// +--------------------------------------------------------------------+
// DECLARE MAPPING FUNCTIONS:

DEF_MAP_CLIENT(NetServerDlg, OnApply);
DEF_MAP_CLIENT(NetServerDlg, OnCancel);

// +--------------------------------------------------------------------+

NetServerDlg::NetServerDlg(Screen* s, FormDef& def, MenuScreen* mgr)
: FormWindow(s, 0, 0, s->Width(), s->Height()), manager(mgr)
{
	config = NetServerConfig::GetInstance();
	Init(def);
}

NetServerDlg::~NetServerDlg()
{
}

// +--------------------------------------------------------------------+

void
NetServerDlg::RegisterControls()
{
	edt_name       = (EditBox*)   FindControl(200);
	cmb_type       = (ComboBox*)  FindControl(201);
	edt_game_port  = (EditBox*)   FindControl(202);
	edt_admin_port = (EditBox*)   FindControl(203);
	edt_game_pass  = (EditBox*)   FindControl(204);
	edt_admin_name = (EditBox*)   FindControl(205);
	edt_admin_pass = (EditBox*)   FindControl(206);

	btn_apply      = (Button*) FindControl(1);
	btn_cancel     = (Button*) FindControl(2);

	REGISTER_CLIENT(EID_CLICK,  btn_apply,    NetServerDlg, OnApply);
	REGISTER_CLIENT(EID_CLICK,  btn_cancel,   NetServerDlg, OnCancel);
}

// +--------------------------------------------------------------------+

void
NetServerDlg::Show()
{
	if (!IsShown())
	FormWindow::Show();

	NetServerConfig::Initialize();
	config = NetServerConfig::GetInstance();

	if (config) {
		config->Load();

		char buff[32];

		if (edt_name) {
			edt_name->SetText(config->Name());
			edt_name->SetFocus();
		}

		if (cmb_type)
		cmb_type->SetSelection(config->GetGameType());

		if (edt_game_port) {
			sprintf_s(buff, "%d", config->GetLobbyPort());
			edt_game_port->SetText(buff);
		}

		if (edt_admin_port) {
			sprintf_s(buff, "%d", config->GetAdminPort());
			edt_admin_port->SetText(buff);
		}

		if (edt_game_pass)
		edt_game_pass->SetText(config->GetGamePass());

		if (edt_admin_name)
		edt_admin_name->SetText(config->GetAdminName());

		if (edt_admin_pass)
		edt_admin_pass->SetText(config->GetAdminPass());
	}
}

// +--------------------------------------------------------------------+

void
NetServerDlg::ExecFrame()
{
}

// +--------------------------------------------------------------------+

void
NetServerDlg::OnApply(AWEvent* event)
{
	if (config) {
		if (edt_name)
		config->SetName(edt_name->GetText());

		if (cmb_type)
		config->SetGameType(cmb_type->GetSelectedIndex());

		if (edt_game_port) {
			int port = 0;
			sscanf_s(edt_game_port->GetText(), "%d", &port);
			config->SetLobbyPort((WORD) port);
			config->SetGamePort((WORD) port+1);
		}

		if (edt_admin_port) {
			int port = 0;
			sscanf_s(edt_admin_port->GetText(), "%d", &port);
			config->SetAdminPort((WORD) port);
		}

		if (edt_game_pass)
		config->SetGamePass(edt_game_pass->GetText());

		if (edt_admin_name)
		config->SetAdminName(edt_admin_name->GetText());

		if (edt_admin_pass)
		config->SetAdminPass(edt_admin_pass->GetText());

		
		config->Save();
	}

	Starshatter* stars = Starshatter::GetInstance();

	if (stars) {
		::Print("\nSTART LOCAL SERVER\n\n");
		stars->SetLobbyMode(Starshatter::NET_LOBBY_SERVER);
		manager->ShowNetLobbyDlg();
	}
	else {
		manager->ShowMenuDlg();
	}
}

void
NetServerDlg::OnCancel(AWEvent* event)
{
	NetServerConfig::Close();
	manager->ShowNetClientDlg();
}