<?xml version="1.0" encoding="utf-8" ?> 
<!-- This script is run after the player connects to the server but
    before they are allowed to play a game. -->
<!-- On this screen players must enter a name to register as. -->
<sector name="ServerNameSelect">
    
    <!-- Screen Entity -->
    <tNameSelect prototype="ScreenEntity">
        
        <!-- User Message -->
        <sUserMsg>" "</sUserMsg>
        <sUserMsg index="1"> Enter your name below: </sUserMsg>
        <sUserMsg index="2">">"</sUserMsg>
        
        <!-- RENDER SECTION -->
        <Render>
            <sFont> Courier New </sFont>
            <vColor> {1, 1, 1, 1} </vColor>
            
            <!-- Menu Title -->
            <sTitle> Server Connect Screen </sTitle>
            <sTitle index="1">- - Name registration</sTitle>
            <vTitlePos> (10, 10, 0, 0} </vTitlePos>
            <iTitleSize> 20 </iTitleSize>
            <tTitleRenderer prototype="ActionRenderText">
                <rText> sTitle </rText>
                <rPosition> vTitlePos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iTitleSize </rSize>
            </tTitleRenderer>
            
            <!-- Render User Message -->
            <vMsgPos> {100, 230, 0, 0} </vMsgPos>
            <iMsgSize> 20 </iMsgSize>
            <tMsgRenderer prototype="ActionRenderText">
                <rText> sUserMsg </rText>
                <rPosition> vMsgPos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iMsgSize </rSize>
            </tMsgRenderer>
            
            <!-- The user-typed name -->
            <vNamePos> {110, 270, 0, 0} </vNamePos>
            <iNameSize> 20 </iNameSize>
            <tNameRenderer prototype="ActionRenderText">
                <rText> sFName </rText>
                <rPosition> vNamePos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iNameSize </rSize>
            </tNameRenderer>
        </Render>
        <!-- END RENDER SECTION -->
        
        <!-- UPDATE SECTION -->
        <Update>
            <tNetReceive prototype="ActionNetworkReceive">
                <rSocket>iSocket</rSocket>
                
                <!-- When this action receives a packet from the network,
                    this stuff below is executed. -->
                <iCond>0</iCond>
                <!-- The ActionNetworkReceive class has members which are
                    accessible here for us to know what was in the packet. -->
                <expr>"iCond = ( sMsgType == "NAME" )"</expr>
                <if>
                    <rCondition>iCond</rCondition>
                    <then>
                        <!-- We successfully registered our name with the server
                            so store it (again) and move to the next screen. -->
                        <expr>"sFName = sMsgData [ 0 ]"</expr>
                        <tUnload prototype="ActionUnloadSector">
                            <sSectorName>ServerNameSelect</sSectorName>
                        </tUnload>
                        <tLoad prototype="ActionLoad">
                            <sFilename>ServerGameSelect.xml</sFilename>
                            <iSynch>0</iSynch>
                        </tLoad>
                    </then>
                <else>
                    <expr>"iCond = ( sMsgType == "BAD_NAME" )"</expr>
                    <if>
                        <rCondition>iCond</rCondition>
                        <then>
                        <!-- The user tried to register a name that is bad,
                            so we tell them and let them try again. -->
                            <expr>"sFName = """</expr>
                            <expr>"sUserMsg = sMsgData [ 0 ]"</expr>
                        </then>
                    </if>
                </else>
                </if>
            </tNetReceive>
        </Update>
        <!-- END UPDATE SECTION -->
        
        <fTimePrevRcvd>0.0</fTimePrevRcvd>
        <fLocalTimePrevRcvd>0.0</fLocalTimePrevRcvd>
        <iSeqNum>0</iSeqNum>
        <sMsg>0</sMsg>
        <iMsgLength>-1</iMsgLength>
        
        <!-- REACTION SECTION -->
        <tREnter prototype="ReactionInput">
            <!-- When ENTER is pressed, construct the datagram and send. -->
            <iSoughtInputs>entr</iSoughtInputs>
            <expr>"sMsg = "NAME;" + sFName + ";""</expr>
            <expr>iMsgLength = - 1</expr>
            <tSendNameRequest prototype="ActionNetworkSend">
                <rTimePrevRcvd>fTimePrevRcvd</rTimePrevRcvd>
                <rLocalTimePrevRcvd>fLocalTimePrevRcvd</rLocalTimePrevRcvd>
                <rSeqNum>iSeqNum</rSeqNum>
                <rMsgData>sMsg</rMsgData>
                <rMsgLengths>iMsgLength</rMsgLengths>
                <rSocket>iSocket</rSocket>
                <rPeerPortNum>iPeerPort</rPeerPortNum>
                <rPeerIPAddr>iChosenIPAddress</rPeerIPAddr>
            </tSendNameRequest>
        </tREnter>
        
		<!-- Escape key handling. -->
		<tEscapeReaction prototype="ReactionInput">
		    <iSoughtInputs>escp</iSoughtInputs>
		    
		    <tUnload1 prototype="ActionUnloadSector">
		        <sSectorName>ServerNameSelect</sSectorName>
		    </tUnload1>
		    <tUnload2 prototype="ActionUnloadSector">
		        <sSectorName>ServerSysCode</sSectorName>
		    </tUnload2>
		    <tLoad prototype="ActionLoad">
		        <sFilename>MenuScreen.xml</sFilename>
		        <iSynch>0</iSynch>
		    </tLoad>
		</tEscapeReaction>
        
        <tRBackspace prototype="ReactionInput">
            <iSoughtInputs>bspc</iSoughtInputs>
            
            <!-- Clear out the input string. -->
            <expr>"sFName = """</expr>
        </tRBackspace>
        
        <tRAlpha prototype="ReactionInput">
            <iSoughtInputs index="0">btna</iSoughtInputs>
            <iSoughtInputs index="1">btnb</iSoughtInputs>
            <iSoughtInputs index="2">btnc</iSoughtInputs>
            <iSoughtInputs index="3">btnd</iSoughtInputs>
            <iSoughtInputs index="4">btne</iSoughtInputs>
            <iSoughtInputs index="5">btnf</iSoughtInputs>
            <iSoughtInputs index="6">btng</iSoughtInputs>
            <iSoughtInputs index="7">btnh</iSoughtInputs>
            <iSoughtInputs index="8">btni</iSoughtInputs>
            <iSoughtInputs index="9">btnj</iSoughtInputs>
            <iSoughtInputs index="10">btnk</iSoughtInputs>
            <iSoughtInputs index="11">btnl</iSoughtInputs>
            <iSoughtInputs index="12">btnm</iSoughtInputs>
            <iSoughtInputs index="13">btnn</iSoughtInputs>
            <iSoughtInputs index="14">btno</iSoughtInputs>
            <iSoughtInputs index="15">btnp</iSoughtInputs>
            <iSoughtInputs index="16">btnq</iSoughtInputs>
            <iSoughtInputs index="17">btnr</iSoughtInputs>
            <iSoughtInputs index="18">btns</iSoughtInputs>
            <iSoughtInputs index="19">btnt</iSoughtInputs>
            <iSoughtInputs index="20">btnu</iSoughtInputs>
            <iSoughtInputs index="21">btnv</iSoughtInputs>
            <iSoughtInputs index="22">btnw</iSoughtInputs>
            <iSoughtInputs index="23">btnx</iSoughtInputs>
            <iSoughtInputs index="24">btny</iSoughtInputs>
            <iSoughtInputs index="25">btnz</iSoughtInputs>
            <!-- sFName is a string I have, but sInputValue is filled in by
                ReactionInput to match the key that was pressed! -->
            <expr>" sFName += sInputValue "</expr>
        </tRAlpha>
        <!-- END REACTION SECTION -->
        
    </tNameSelect>
    
</sector>