<?xml version="1.0" encoding="utf-8" ?> 
<!-- This script is run after the player registers a name with the
    server. -->
<!-- On this screen players can join or create a game. -->
<sector name="ServerGameSelect">
    
    <!-- Screen Entity -->
    <tGameSelect prototype="ScreenEntity">
        
        <!-- RENDER SECTION -->
        
        <!-- User Message -->
        <sUserMsg>" "</sUserMsg>
        <sUserMsg index="1"> Type the name of the game you wish </sUserMsg>
        <sUserMsg index="2"> to join, or type a new name to </sUserMsg>
        <sUserMsg index="3"> to create one. </sUserMsg>
        <sUserMsg index="4">">"</sUserMsg>
        
        <!-- Game List Title -->
        <sGameList> Available Games: </sGameList>
        
        <Render>
            <sFont> Courier New </sFont>
            <vColor> {1, 1, 1, 1} </vColor>
            
            <!-- Menu Title -->
            <sTitle> Welcome To The Game Select Screen </sTitle>
            <sTitle index="1">- - Join or create a game</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 -->
            <vUserMsgPos> {20, 150, 0, 0} </vUserMsgPos>
            <iUserMsgSize> 20 </iUserMsgSize>
            <tUserMsgRenderer prototype="ActionRenderText">
                <rText> sUserMsg </rText>
                <rPosition> vUserMsgPos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iUserMsgSize </rSize>
            </tUserMsgRenderer>
            
            <!-- The user-typed game name -->
            <vGamePos> {40, 230, 0, 0} </vGamePos>
            <iGameSize> 20 </iGameSize>
            <tGameRenderer prototype="ActionRenderText">
                <rText> sGame </rText>
                <rPosition> vGamePos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iGameSize </rSize>
            </tGameRenderer>
            
            <!-- Render the game list -->
            <vGLPos> {400, 80, 0, 0} </vGLPos>
            <iGLSize> 20 </iGLSize>
            <tGLRenderer prototype="ActionRenderText">
                <rText> sGameList </rText>
                <rPosition> vGLPos </rPosition>
                <rColor> vColor </rColor>
                <rFont> sFont </rFont>
                <rSize> iGLSize </rSize>
            </tGLRenderer>
            
            <!-- Show the registered name in the corner -->
            <vNamePos> {10, 420, 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 -->
        
        <!-- Datums for ActionNetworkSend. -->
        <fTimePrevRcvd>0.0</fTimePrevRcvd>
        <fLocalTimePrevRcvd>0.0</fLocalTimePrevRcvd>
        <iSeqNum>0</iSeqNum>
        <sMsg>0</sMsg>
        <iMsgLength>-1</iMsgLength>
        
        <!-- 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>
                <expr>"iCond = ( sMsgType == "GAME" )"</expr>
                <if>
                    <rCondition>iCond</rCondition>
                    <then>
                        <!-- We successfully joined or created a game. -->
                        <!-- The received packet is similar to what we sent
                            except that the 3rd field marks if we're the host. -->
                        <expr>"sGame = sMsgData [ 1 ]"</expr>
                        <tUnload prototype="ActionUnloadSector">
                            <sSectorName>ServerGameSelect</sSectorName>
                        </tUnload>
                        <tUnload2 prototype="ActionUnloadSector">
                            <sSectorName>VirtualKeyboard</sSectorName>
                        </tUnload2>
                        
                        <tStopTheme prototype="ActionStopSound">
                          <sFilename> ballblazer.wav </sFilename>
                        </tStopTheme>
                        
                        <tLoad2 prototype="ActionLoad">
                            <sFilename>GeneralInput.xml</sFilename>
                            <iSynch>0</iSynch>
                        </tLoad2>
                        <!-- sMsgData[2] is 1 if we're the host, and 0 otherwise. -->
                        <expr>"iIAmHost = ( sMsgData [ 2 ] == "1" )"</expr>
                        <if>
                            <!-- The host uses a different XML from the other client. -->
                            <rCondition>iIAmHost</rCondition>
                            <then>
                                <tLoad prototype="ActionLoad">
                                    <sFilename>CleanXML.xml</sFilename>
                                    <iSynch>1</iSynch>
                                </tLoad>
                                <tLoad1 prototype="ActionLoad">
                                    <sFilename>ServerUpdate.xml</sFilename>
                                    <iSynch>0</iSynch>
                                </tLoad1>
                                <tLoad2 prototype="ActionLoad">
                                    <sFilename>PlayerWait.xml</sFilename>
                                    <iSynch>0</iSynch>
                                </tLoad2>
                            </then>
                            <else>
                                <tLoad prototype="ActionLoad">
                                    <sFilename>Player2.xml</sFilename>
                                    <iSynch>1</iSynch>
                                </tLoad>
                                <tLoad1 prototype="ActionLoad">
                                    <sFilename>ClientUpdate.xml</sFilename>
                                    <iSynch>0</iSynch>
                                </tLoad1>
                                
                            </else>
                        </if>
                    </then>
                <else>
                    <expr>"iCond = ( sMsgType == "SYSTEM" )"</expr>
                    <if>
                        <rCondition>iCond</rCondition>
                        <then>
                            <!-- Could not join the game specified. -->
                            <expr>"sGame = """</expr>
                            <expr>"sUserMsg = sMsgData [ 0 ]"</expr>
                        </then>
                    <else>
                        <expr>"iCond = ( sMsgType == "GAME_INFO" )"</expr>
                        <if>
                            <rCondition>iCond</rCondition>
                            <then>
                                <!-- We received our game info packet. -->
                                <expr>"sGameList [ 1 ] = "1." + sMsgData [ 0 ]"</expr>
                                <expr>"sGameList [ 2 ] = "2." + sMsgData [ 1 ]"</expr>
                                <expr>"sGameList [ 3 ] = "3." + sMsgData [ 2 ]"</expr>
                                <expr>"sGameList [ 4 ] = "4." + sMsgData [ 3 ]"</expr>
                                <expr>"sGameList [ 5 ] = "5." + sMsgData [ 4 ]"</expr>
                                <expr>"sGameList [ 6 ] = "6." + sMsgData [ 5 ]"</expr>
                                <expr>"sGameList [ 7 ] = "7." + sMsgData [ 6 ]"</expr>
                                <expr>"sGameList [ 8 ] = "8." + sMsgData [ 7 ]"</expr>
                                <expr>"sGameList [ 9 ] = "9." + sMsgData [ 8 ]"</expr>
                                <expr>"sGameList [ 10 ] = "10." + sMsgData [ 9 ]"</expr>
                            </then>
                        </if>
                    </else>
                    </if>
                </else>
                </if>
            </tNetReceive>
            
            <!-- Periodically request game data from server. -->
            <iTimeToWait>2</iTimeToWait>
            <iCond>0</iCond>
            <expr>"iTimeToWait -= 1"</expr>
            <expr>"iCond = ( iTimeToWait LTE 0 )"</expr>
            <if>
                <rCondition>iCond</rCondition>
                <then>
                    <sMsg>"GAME_INFO;10;"</sMsg>
                    <iMsgLength>-1</iMsgLength>
                    <expr>"sMsg = "GAME_INFO;10;""</expr>
                    <expr>iTimeToWait = 100</expr>
                    <expr>iMsgLength = - 1</expr>
                    <tRequestGameInfo 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>
                    </tRequestGameInfo>
                </then>
            </if>
        </Update>
        <!-- END UPDATE SECTION -->
        
        <!-- REACTION SECTION -->
        <tREnter prototype="ReactionInput">
            <iSoughtInputs>entr</iSoughtInputs>
            
            <!-- Packet: "GAME;userName;gameName;numPlayers;" -->
            <expr>"sMsg = "GAME;" + sFName + ";""</expr>
            <expr>"sMsg += sGame + ";2;""</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>ServerGameSelect</sSectorName>
		    </tUnload1>
		    <tLoad prototype="ActionLoad">
		        <sFilename>ServerNameSelect.xml</sFilename>
		        <iSynch>0</iSynch>
		    </tLoad>
		</tEscapeReaction>
        
        <tRBackspace prototype="ReactionInput">
            <iSoughtInputs>bspc</iSoughtInputs>
            
            <!-- Clear out the input string. -->
            <expr>"sGame = """</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>
            
            <expr>" sGame += sInputValue "</expr>
        </tRAlpha>
        <!-- END REACTION SECTION -->
        
    </tGameSelect>
    
</sector>