<?xml version="1.0" encoding="utf-8" ?>
<sector name="ServerUpdate">
    
    <tGameStateOut>
        <rGameTime> tTimer.fTime </rGameTime>
        <rPlayer1Pos> tPlayer.vTranslate </rPlayer1Pos>
        <rPlayer1Rot> tPlayer.xRotate </rPlayer1Rot>
        <rPlayer1Mom> tPlayer.vMomentum </rPlayer1Mom>
        <rPlayer1Score> tScoreboard.iPlayer1Score </rPlayer1Score>
        <rPlayer2Score> tScoreboard.iPlayer2Score </rPlayer2Score>
        <rBallPos> tBall.vTranslate </rBallPos>
        <rBallOwner> tBall.iOwner </rBallOwner>
        <rNorthGoalPos> tNorthGoal.vTranslate </rNorthGoalPos>
        <rSouthGoalPos> tSouthGoal.vTranslate </rSouthGoalPos>
    </tGameStateOut>
    
    <tUpdateGameState prototype="Entity">
        <!-- Flag for sending data. -->
        <iSendNetworkData>1</iSendNetworkData>
    
        <!-- Datums for ActionNetworkSend. -->
        <fTimePrevRcvd>0.0</fTimePrevRcvd>
        <fLocalTimePrevRcvd>0.0</fLocalTimePrevRcvd>
        <iSeqNum>1</iSeqNum>
        <sMsg>0</sMsg>
        <iPacketSizeOut>0</iPacketSizeOut>
        
        <Update>
            <tNetReceive prototype="ActionNetworkReceive">
                <rSocket>iSocket</rSocket>
                <!-- sMsgData, iMsgDataSize, and iMsgDataType are hidden and updated if DATA; are sent as the header-->
                <iUpdateType>updt</iUpdateType>
                <iFirstType>frst</iFirstType>
                <iOverType>over</iOverType>
                <iCond>0</iCond>
                <expr> iCond = ( iMsgDataType == iUpdateType ) </expr>
                <if>
                    <rCondition>iCond</rCondition>
                    <then>
                        <tTestAction prototype = "ActionUnpackNetMsg">
                            <rMsgData> sMsgData </rMsgData>
                            <rSize> iMsgDataSize </rSize>
                            <!-- The order of this must match tGameStateOut from 
                                ClientUpdate.xml. -->
                            <rPlayer2Pos> tOtherPlayer.vTranslate </rPlayer2Pos>
                            <rPlayer2Rot> tOtherPlayer.xRotate </rPlayer2Rot>
                            <rPlayer2Mom> tOtherPlayer.vMomentum </rPlayer2Mom>
                            <rPlayer2FV> vForward2 </rPlayer2FV>
                            <rP2ShootFlag> tBall.iP2WantsToShoot </rP2ShootFlag>
                        </tTestAction>
                    </then>
                    <else>
                        <expr> iCond = ( iMsgDataType == iFirstType )</expr>
                        <if>
                            <rCondition>iCond</rCondition>
                            <then>
                                <tUnloadPlayerWait prototype="ActionUnloadSector">
                                  <sSectorName>PlayerWait</sSectorName>
                                </tUnloadPlayerWait>
                                <expr> iGameRunning = 1 </expr>
                            </then>
                          <else>
                            <expr>iCond = ( iMsgDataType == iOverType )</expr>
                            <if>
                              <rCondition> iCond </rCondition>
                              <then>
                                <expr> iGameRunning = 0 </expr>
                                <expr> iGameOver = 1 </expr>


                                <tActionUnloadKeyBoard prototype="ActionUnloadSector">
                                  <sSectorName>GeneralInput</sSectorName>
                                </tActionUnloadKeyBoard>
                                <tThrowPauseGame prototype="ActionEvent">
                                  <iSubtype>paus</iSubtype>
                                  <fDelay>0.0</fDelay>
                                </tThrowPauseGame>

                                <tThrowEndGame prototype="ActionEvent">
                                  <iSubtype>over</iSubtype>
                                  <fDelay>4.0</fDelay>
                                </tThrowEndGame>

                                <tStopVehicleSound prototype="ActionStopSound">
                                  <sFilename> vehicle.wav </sFilename>
                                </tStopVehicleSound>

                                <tPlayWinSound prototype="ActionPlaySound">
                                  <sFilename> win.wav </sFilename>
                                </tPlayWinSound>
                                
                                <tStopDrumLoop prototype="ActionStopSound">
                                  <sFilename> drums.wav </sFilename>
                                </tStopDrumLoop>

                                <tActionLoad prototype ="ActionLoad">
                                  <sFilename>GameOver.xml</sFilename>
                                </tActionLoad>

                              </then>
                            </if>
                          </else>
                        </if>
                    </else>
                </if>
            </tNetReceive>
            
            <iCond> 0 </iCond>
            <expr> iCond = ( iSendNetworkData == 1 ) </expr>
            <if>
            <rCondition> iCond </rCondition>
                <then>
                    <tPackMsg prototype="ActionPackNetMsg">
                        <iPacketType>updt</iPacketType>
                        <rPacketTable>tGameStateOut</rPacketTable>
                        <rPacketMsg>sMsg</rPacketMsg>
                        <rPacketSizeOut>iPacketSizeOut</rPacketSizeOut>
                    </tPackMsg>
                    
                    <tGameStateSend prototype="ActionNetworkSend">
                        <rTimePrevRcvd>fTimePrevRcvd</rTimePrevRcvd>
                        <rLocalTimePrevRcvd>fLocalTimePrevRcvd</rLocalTimePrevRcvd>
                        <rSeqNum>iSeqNum</rSeqNum>
                        <rMsgData>sMsg</rMsgData>
                        <rMsgLengths>iPacketSizeOut</rMsgLengths>
                        <rSocket>iSocket</rSocket>
                        <rPeerPortNum>iPeerPort</rPeerPortNum>
                        <rPeerIPAddr>iChosenIPAddress</rPeerIPAddr>
                    </tGameStateSend>
                </then>
            </if>
        </Update>
                
        <tReactGameOver prototype="ReactionAttributed" iSynchronous="0">
            <iMatch>over</iMatch>
            <iMask>-1</iMask>
            
            <tActionList prototype="ActionList">
                <!-- Game ended, change flag to halt data stuff. -->
                <expr>iSendNetworkData = 0</expr>
            
                <tActionGameOver prototype="ActionUnloadSector">
                    <sSectorName>ServerUpdate</sSectorName>
                </tActionGameOver>
                <tUnload prototype="ActionUnloadSector">
                    <sSectorName>ServerSysCode</sSectorName>
                </tUnload>
                
                <!-- Send DISCONNECT message to server. -->
                <sMsg>"DISCONNECT;"</sMsg>
                <iMsgLength>-1</iMsgLength>
                <iSeqNum>0</iSeqNum>
                <tSendDisconnect 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>
                </tSendDisconnect>
            </tActionList>
        </tReactGameOver>
    </tUpdateGameState>
    
</sector>
