1
0
forked from 0ad/0ad
0ad/binaries/data/mods/public/gui/gamesetup/gamesetup_mp.js
2010-07-06 20:51:45 +00:00

106 lines
1.9 KiB
JavaScript

var g_IsConnecting = false;
var g_GameType; // "server" or "client"
function init()
{
}
function cancelSetup()
{
if (g_IsConnecting)
Engine.DisconnectNetworkGame();
Engine.PopGuiPage();
}
function startConnectionStatus(type)
{
g_GameType = type;
g_IsConnecting = true;
getGUIObjectByName("connectionStatus").caption = "Connecting to server...";
}
function onTick()
{
if (!g_IsConnecting)
return;
while (true)
{
var message = Engine.PollNetworkClient();
if (!message)
break;
log("Net message: "+uneval(message));
switch (message.type)
{
case "netstatus":
switch (message.status)
{
case "connected":
getGUIObjectByName("connectionStatus").caption = "Registering with server...";
break;
case "authenticated":
Engine.PopGuiPage();
Engine.PushGuiPage("page_gamesetup.xml", { "type": g_GameType });
return; // don't process any more messages - leave them for the game GUI loop
case "disconnected":
cancelSetup();
reportDisconnect(message.reason);
return;
default:
error("Unrecognised netstatus type "+message.status);
break;
}
break;
default:
error("Unrecognised net message type "+message.type);
break;
}
}
}
function switchSetupPage(oldpage, newpage)
{
getGUIObjectByName(oldpage).hidden = true;
getGUIObjectByName(newpage).hidden = false;
}
function startHost(playername, servername)
{
try
{
Engine.StartNetworkHost(playername);
}
catch (e)
{
cancelSetup();
messageBox(400, 200,
"Cannot host game: " + e.message + ".",
"Error", 2);
}
startConnectionStatus("server");
// TODO: ought to do something(?) with servername
}
function startJoin(playername, ip)
{
try
{
Engine.StartNetworkJoin(playername, ip);
}
catch (e)
{
cancelSetup();
messageBox(400, 200,
"Cannot join game: " + e.message + ".",
"Error", 2);
}
startConnectionStatus("client");
}