Advertisement
Guest User

JoiningRoom.lua

a guest
Dec 11th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.76 KB | None | 0 0
  1. -- ===========================================================================
  2. --  Joinging Room
  3. --  Intermediate screen when joining (or establishing) a match
  4. -- ===========================================================================
  5.  
  6. local g_modsActivating = false;
  7. local g_exiting = false;
  8.  
  9. -- ===========================================================================
  10. -- ===========================================================================
  11. function CanManuallyExitGame()
  12.     if (g_modsActivating) then -- Can't manually exit while activating mods.
  13.         return false;
  14.     end
  15.  
  16.     return true;
  17. end
  18.  
  19. function OnCancel()
  20.     if(CanManuallyExitGame()) then
  21.         HandleExitRequest();
  22.     end
  23. end
  24. Controls.CancelButton:RegisterCallback( Mouse.eLClick, OnCancel );
  25.  
  26.  
  27. ----------------------------------------------------------------
  28. -- Input Handler
  29. ----------------------------------------------------------------
  30. function InputHandler( uiMsg, wParam, lParam )
  31.  
  32.     if uiMsg == KeyEvents.KeyDown then
  33.         if (wParam == Keys.VK_ESCAPE) then
  34.             if(CanManuallyExitGame()) then
  35.                 HandleExitRequest();
  36.             end
  37.             return true;
  38.         end
  39.     end
  40.  
  41.     return false;
  42.  
  43. end
  44. ContextPtr:SetInputHandler( InputHandler );
  45.  
  46.  
  47. -------------------------------------------------
  48. -- Leave the game we're trying to join.
  49. -------------------------------------------------
  50. function HandleExitRequest()
  51.     g_exiting = true;
  52.     Matchmaking.LeaveMultiplayerGame();
  53.     UIManager:DequeuePopup( ContextPtr );
  54. end
  55.  
  56. -------------------------------------------------
  57. -- Start transition to the Staging room screen.  Assumes that we've already fully connected to multiplayer game.
  58. -------------------------------------------------
  59. function TransitionToStagingRoom()
  60.     -- Attempt to configure game data if we need to for this multiplayer game.
  61.     if (not ContextPtr:IsHotLoad()) then
  62.         local prevCursor = UIManager:SetUICursor( 1 );
  63.         local bChanged = Modding.ActivateAllowedDLC();
  64.         UIManager:SetUICursor( prevCursor );
  65.     end
  66.  
  67.     -- Send out an event to send us to the staging room after configuring game data.
  68.     Events.SystemUpdateUI( SystemUpdateUIType.RestoreUI, "StagingRoom" );
  69. end
  70.  
  71. function OnSystemUpdateUI( type, tag )
  72.     if( type == SystemUpdateUIType.RestoreUI) then
  73.         if (tag == "StagingRoom") then
  74.             -- If the staging room is being restored, we're done and need to go away.
  75.             UIManager:DequeuePopup( ContextPtr );
  76.         end
  77.     end
  78. end
  79. Events.SystemUpdateUI.Add( OnSystemUpdateUI );
  80.  
  81.  
  82. -------------------------------------------------
  83. -- Event Handler: MultiplayerJoinRoomComplete
  84. -------------------------------------------------
  85. function OnJoinRoomComplete()
  86.     if (not ContextPtr:IsHidden()) then
  87.         if Matchmaking.IsHost() then
  88.             -- If the host, we're already fully in our own game, go to the staging room.
  89.             TransitionToStagingRoom();
  90.         else
  91.             Controls.JoiningLabel:SetText( Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_JOINING_HOST" ));
  92.         end
  93.     end
  94. end
  95. Events.MultiplayerJoinRoomComplete.Add( OnJoinRoomComplete );
  96.  
  97.  
  98. -------------------------------------------------
  99. -- Event Handler: MultiplayerJoinRoomFailed
  100. -------------------------------------------------
  101. function OnJoinRoomFailed( iExtendedError, aExtendedErrorText )
  102.     if (not ContextPtr:IsHidden()) then
  103.         if iExtendedError == NetErrors.MISSING_REQUIRED_DATA then
  104.             local szText = Locale.ConvertTextKey("TXT_KEY_MP_JOIN_FAILED_MISSING_RESOURCES");
  105.  
  106.             -- The aExtendedErrorText will contain an array of the IDs of the resources (DLC/Mods) needed by the game.
  107.             local count = table.count(aExtendedErrorText);
  108.             if(count > 0) then
  109.                 szText = szText .. "[NEWLINE]";
  110.                     for index, value in pairs(aExtendedErrorText) do
  111.                     szText = szText .. "[NEWLINE] [ICON_BULLET]" .. Locale.ConvertTextKey(value);
  112.                 end
  113.             end
  114.             Events.FrontEndPopup.CallImmediate( szText );
  115.         elseif iExtendedError == NetErrors.ROOM_FULL then
  116.             Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_ROOM_FULL" );
  117.         else
  118.             Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_JOIN_FAILED" );
  119.         end
  120.         Matchmaking.LeaveMultiplayerGame();
  121.         UIManager:DequeuePopup( ContextPtr );
  122.     end
  123. end
  124. Events.MultiplayerJoinRoomFailed.Add( OnJoinRoomFailed );
  125.  
  126. -------------------------------------------------
  127. -- Event Handler: MultiplayerConnectionFailed
  128. -------------------------------------------------
  129. function OnMultiplayerConnectionFailed()
  130.     if (not ContextPtr:IsHidden()) then
  131.         -- We should only get this if we couldn't complete the connection to the host of the room
  132.             Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_JOIN_FAILED" );
  133.         Matchmaking.LeaveMultiplayerGame();
  134.         UIManager:DequeuePopup( ContextPtr );
  135.     end
  136. end
  137. Events.MultiplayerConnectionFailed.Add( OnMultiplayerConnectionFailed );
  138.  
  139. -------------------------------------------------
  140. -- Event Handler: MultiplayerGameAbandoned
  141. -------------------------------------------------
  142. function OnMultiplayerGameAbandoned(eReason)
  143.     if (not ContextPtr:IsHidden()) then
  144.         if (eReason == NetKicked.BY_HOST) then
  145.             Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_KICKED" );
  146.         elseif (eReason == NetKicked.NO_ROOM) then
  147.             Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_ROOM_FULL" );
  148.         else
  149.                 Events.FrontEndPopup.CallImmediate( "TXT_KEY_MP_JOIN_FAILED" );
  150.         end
  151.         Matchmaking.LeaveMultiplayerGame();
  152.         UIManager:DequeuePopup( ContextPtr );
  153.     end
  154. end
  155. Events.MultiplayerGameAbandoned.Add( OnMultiplayerGameAbandoned );
  156.  
  157. -------------------------------------------------
  158. -- Event Handler: ConnectedToNetworkHost
  159. -------------------------------------------------
  160. function OnHostConnect()
  161.     if (not ContextPtr:IsHidden()) then
  162.         Controls.JoiningLabel:SetText( Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_JOINING_PLAYERS" ));
  163.     end
  164. end
  165. Events.ConnectedToNetworkHost.Add ( OnHostConnect );
  166.  
  167. -------------------------------------------------
  168. -- Event Handler: MultiplayerConnectionComplete
  169. -------------------------------------------------
  170. function OnConnectionCompete()
  171.     if (not ContextPtr:IsHidden()) then
  172.         if not Matchmaking.IsHost() then
  173.             TransitionToStagingRoom();
  174.         end
  175.     end
  176. end
  177. Events.MultiplayerConnectionComplete.Add( OnConnectionCompete );
  178.  
  179. -------------------------------------------------
  180. -- Event Handler: MultiplayerNetRegistered
  181. -------------------------------------------------
  182. function OnNetRegistered()
  183.     if (not ContextPtr:IsHidden()) then
  184.         Controls.JoiningLabel:SetText( Locale.ConvertTextKey("TXT_KEY_MULTIPLAYER_JOINING_GAMESTATE" ));
  185.     end
  186. end
  187. Events.MultiplayerNetRegistered.Add( OnNetRegistered );
  188.  
  189. -------------------------------------------------
  190. -- Event Handler: PlayerVersionMismatchEvent
  191. -------------------------------------------------
  192. function OnVersionMismatch( iPlayerID, playerName, bIsHost )
  193.     if( bIsHost ) then
  194.         Events.FrontEndPopup.CallImmediate( Locale.ConvertTextKey( "TXT_KEY_MP_VERSION_MISMATCH_FOR_HOST", playerName ) );
  195.         Matchmaking.KickPlayer( iPlayerID );
  196.     else
  197.         Events.FrontEndPopup.CallImmediate( Locale.ConvertTextKey( "TXT_KEY_MP_VERSION_MISMATCH_FOR_PLAYER" ) );
  198.         HandleExitRequest();
  199.     end
  200. end
  201. Events.PlayerVersionMismatchEvent.Add( OnVersionMismatch );
  202.  
  203. -------------------------------------------------
  204. -- Event Handler: BeforeModsDeactivate/AfterModsActivate
  205. -------------------------------------------------
  206. function OnBeforeModsDeactivate()
  207.     g_modsActivating = true;
  208. end
  209. Events.BeforeModsDeactivate.Add( OnBeforeModsDeactivate );
  210.  
  211. function OnAfterModsActivate()
  212.     g_modsActivating = false;
  213. end
  214. Events.AfterModsActivate.Add( OnAfterModsActivate );
  215.  
  216. -------------------------------------------------
  217. -- Show / Hide Handler
  218. -------------------------------------------------
  219. function ShowHideHandler( bIsHide, bIsInit )
  220.     if( not bIsInit ) then
  221.         if not bIsHide then
  222.             g_exiting = false;
  223.             -- Activate only the DLC allowed for this MP game.  Mods will also deactivated/activate too.
  224.             if (not ContextPtr:IsHotLoad()) then
  225.                 local prevCursor = UIManager:SetUICursor( 1 );
  226.                 local bChanged = Modding.ActivateAllowedDLC();
  227.                 UIManager:SetUICursor( prevCursor );
  228.  
  229.                 -- Send out an event to continue on, as the ActivateDLC may have swapped out the UI
  230.                 Events.SystemUpdateUI( SystemUpdateUIType.RestoreUI, "JoiningRoom" );
  231.             end
  232.  
  233.         Controls.JoiningLabel:LocalizeAndSetText( "TXT_KEY_MULTIPLAYER_JOINING_ROOM" );
  234.         end
  235.     end
  236. end
  237. ContextPtr:SetShowHideHandler( ShowHideHandler );
  238.  
  239.  
  240. -------------------------------------------------
  241. -------------------------------------------------
  242. function OnUpdateUI( type, tag, iData1, iData2, strData1)
  243.     if (type == SystemUpdateUIType.RestoreUI and tag == "JoiningRoom" and not g_exiting) then
  244.         LuaEvents.CloseAnyQueuedMy2KPopup();
  245.         if (ContextPtr:IsHidden()) then
  246.             UIManager:QueuePopup(ContextPtr, PopupPriority.JoiningScreen );
  247.         end
  248.     end
  249. end
  250. Events.SystemUpdateUI.Add( OnUpdateUI );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement