Advertisement
Guest User

Untitled

a guest
May 17th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.02 KB | None | 0 0
  1. //Errors
  2. Assets/Game/Scripts/LobbyGUI.cs(87,27): error CS0122: `templateExtensionResponse._cmd' is inaccessible due to its protection level
  3. Assets/Game/Scripts/LobbyGUI.cs(89,56): error CS0122: `templateExtensionResponse.err' is inaccessible due to its protection level
  4. Assets/Game/Scripts/LobbyGUI.cs(90,33): error CS0122: `templateExtensionResponse._cmd' is inaccessible due to its protection level
  5. Assets/Game/Scripts/LobbyGUI.cs(91,57): error CS0122: `templateExtensionResponse.err' is inaccessible due to its protection level
  6.  
  7. //Code
  8. using UnityEngine;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Runtime.InteropServices;
  13. using System.Security.Permissions;
  14. using System.Text;
  15. using SmartFoxClientAPI;
  16. using SmartFoxClientAPI.Data;
  17.  
  18. public class templateExtensionResponse : System.Object{
  19.     String _cmd = "";
  20.     String err = "";
  21. }
  22. public class LobbyGUI : MonoBehaviour
  23. {
  24.     private SmartFoxClient smartFox;
  25.     private string zone = "simpleChat";
  26.     private string username = "";
  27.     private string password = "";
  28.     private string loginErrorMessage = "";
  29.     private bool isLoggedIn;
  30.  
  31.     private bool roomListReceived = false;
  32.  
  33.     private string newMessage = "";
  34.     private ArrayList messages = new ArrayList();
  35.     // Locker to use for messages collection to ensure its cross-thread safety
  36.     private System.Object messagesLocker = new System.Object();
  37.    
  38.     private Vector2 chatScrollPosition, userScrollPosition;
  39.  
  40.     private int roomSelection = 0;
  41.     private string [] roomStrings;
  42.    
  43.     public GUISkin gSkin;
  44.    
  45.     void Start()
  46.     {
  47.         if (SmartFox.initialized)
  48.         {
  49.             smartFox = SmartFox.Connection;
  50.         }
  51.         else
  52.         {
  53.             Application.LoadLevel("connection");
  54.         }
  55.  
  56.         // Register callbacks
  57.         SFSEvent.onLogin += OnLogin;
  58.         SFSEvent.onLogout += OnLogout;
  59.         SFSEvent.onConnectionLost += OnDisconnect;
  60.         SFSEvent.onRoomListUpdate += OnRoomList;
  61.         SFSEvent.onJoinRoom += OnJoinRoom;
  62.         SFSEvent.onPublicMessage += OnPublicMessage;
  63.         SFSEvent.onDebugMessage += OnDebugMessage;
  64.         SFSEvent.onExtensionResponse += OnExtensionResponse;
  65.     }
  66.    
  67.     private void UnregisterSFSSceneCallbacks() {
  68.         // This should be called when switching scenes, so callbacks from the backend do not trigger code in this scene
  69.         SFSEvent.onLogin -= OnLogin;
  70.         SFSEvent.onLogout -= OnLogout;
  71.         SFSEvent.onConnectionLost -= OnDisconnect;
  72.         SFSEvent.onRoomListUpdate -= OnRoomList;
  73.         SFSEvent.onJoinRoom -= OnJoinRoom;
  74.         SFSEvent.onPublicMessage -= OnPublicMessage;
  75.         SFSEvent.onDebugMessage -= OnDebugMessage;
  76.         SFSEvent.onExtensionResponse -= OnExtensionResponse;
  77.     }
  78.  
  79.     // Various SFS callbacks
  80.     void OnLogin(bool success, string name, string error)
  81.     {
  82.         Debug.Log("On Login callback got: " + success + " : " + error + " : " + name);
  83.  
  84.         if (success) {
  85.             isLoggedIn = true;
  86.         } else {
  87.             loginErrorMessage = error;
  88.             Debug.Log("Login error: "+error);
  89.         }
  90.     }
  91.     void OnExtensionResponse(System.Object resObj, String type){
  92.         templateExtensionResponse result = new templateExtensionResponse();
  93.         result = (templateExtensionResponse)resObj;
  94.         if(result._cmd == "loginOK"){
  95.             //login okay.
  96.             OnLogin(true, username, result.err);
  97.         }else if(result._cmd == "loginKO"){
  98.             OnLogin(false, username, result.err);
  99.         }
  100.     }
  101.  
  102.  
  103.     void OnLogout()
  104.     {
  105.         Debug.Log("OnLogout");
  106.         isLoggedIn = false;
  107.         roomListReceived = false;
  108.     }
  109.  
  110.     void OnDisconnect()
  111.     {
  112.         Debug.Log("OnDisconnect");
  113.         isLoggedIn = false;
  114.         roomListReceived = false;
  115.         UnregisterSFSSceneCallbacks();
  116.     }
  117.    
  118.     public void OnDebugMessage(string message)
  119.     {
  120.         Debug.Log("[SFS DEBUG] " + message);
  121.     }
  122.  
  123.     void OnRoomList(Hashtable roomList)
  124.     {  
  125.         try {
  126.             List<string> rooms = new List<string>();
  127.            
  128.             foreach (int roomId in roomList.Keys)
  129.             {                  
  130.                 Room room = (Room)roomList[roomId];
  131.                
  132.                 if (room.IsPrivate())
  133.                 {
  134.                     continue;
  135.                 }
  136.    
  137.                 Debug.Log("Room id: " + roomId + " has name: " + room.GetName());
  138.                
  139.                 rooms.Add (room.GetName());
  140.             }
  141.            
  142.             roomListReceived = true;
  143.                    
  144.             roomStrings = rooms.ToArray();
  145.                                    
  146.             // Users always have to be in a room, so lets go to the Hall as our main "lobby"
  147.             if (smartFox.GetActiveRoom() == null) {
  148.                 smartFox.JoinRoom("The Hall");
  149.             }
  150.                        
  151.         }
  152.         catch (Exception e) {
  153.             Debug.Log("Room list error: "+e.Message+" "+e.StackTrace);
  154.         }
  155.     }
  156.  
  157.     void OnJoinRoom(Room room)
  158.     {
  159.         Debug.Log("Room " + room.GetName() + " joined successfully");
  160.         lock (messagesLocker) {
  161.             messages.Clear();
  162.         }
  163.     }
  164.  
  165.     void OnPublicMessage(string message, User sender, int roomId)
  166.     {
  167.         // We use lock here to ensure cross-thread safety on the messages collection
  168.         lock (messagesLocker) {
  169.             messages.Add(sender.GetName() + " said " + message);
  170.         }
  171.        
  172.         chatScrollPosition.y = Mathf.Infinity;
  173.         Debug.Log("User " + sender.GetName() + " said: " + message);
  174.     }
  175.  
  176.    
  177.    
  178.     // Finally draw all the lobby GUI
  179.     void OnGUI()
  180.     {
  181.         GUI.skin = gSkin;
  182.    
  183.         GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo");
  184.        
  185.         // Login
  186.         if (!isLoggedIn)
  187.         {
  188.             GUI.Label(new Rect(10, 90, 100, 100), "Zone: ");
  189.             zone = GUI.TextField(new Rect(100, 90, 200, 20), zone, 25);
  190.  
  191.             GUI.Label(new Rect(10, 116, 100, 100), "Userame: ");
  192.             username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);
  193.  
  194.             GUI.Label(new Rect(10, 142, 100, 100), "Password: ");
  195.             password = GUI.TextField(new Rect(100, 142, 200, 20), password, 25);
  196.  
  197.             GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
  198.  
  199.             if (GUI.Button(new Rect(100, 166, 100, 24), "Login")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
  200.             {
  201.                 smartFox.Login(zone, username, password);
  202.             }
  203.         }
  204.         else
  205.         {
  206.             // Standard view
  207.             if (GUI.Button(new Rect(580, 478, 90, 24), "Logout"))
  208.             {
  209.                 smartFox.Logout();
  210.             }
  211.  
  212.             // Basic info
  213.             //GUI.Label(new Rect(10, 40, 200, 100), "Logged in as " + smartFox.myUserName);
  214.             Room currentActiveRoom = smartFox.GetActiveRoom();
  215.  
  216.             if (currentActiveRoom != null)
  217.             {
  218.                 GUI.Label(new Rect(498, 248, 180, 40), "Current room: " + currentActiveRoom.GetName());
  219.             }  
  220.            
  221.             // Room list
  222.             if (roomListReceived)
  223.             {
  224.                 GUI.Box(new Rect(490, 80, 180, 170), "Room List");                 
  225.                
  226.                 GUILayout.BeginArea (new Rect(500, 110, 150, 130));                                    
  227.                     roomSelection = GUILayout.SelectionGrid (roomSelection, roomStrings, 1, "RoomListButton");
  228.                     if (roomStrings[roomSelection] != currentActiveRoom.GetName())
  229.                     {
  230.                         smartFox.JoinRoom(roomStrings[roomSelection]);
  231.                     }
  232.                    
  233.                 GUILayout.EndArea();
  234.             }
  235.  
  236.             // Room chat window
  237.             if (currentActiveRoom != null)
  238.             {
  239.            
  240.                 // User list
  241.                 GUI.Box(new Rect(490, 270, 180, 200), "Users");
  242.                
  243.                 GUILayout.BeginArea (new Rect(500, 300, 150, 160));
  244.                     userScrollPosition = GUILayout.BeginScrollView (userScrollPosition, GUILayout.Width (150), GUILayout.Height (160));
  245.                         GUILayout.BeginVertical();
  246.  
  247.                             foreach (User user in currentActiveRoom.GetUserList().Values)
  248.                             {
  249.                                 GUILayout.Label(user.GetName());
  250.                             }
  251.                
  252.                         GUILayout.EndVertical();
  253.                     GUILayout.EndScrollView ();
  254.                 GUILayout.EndArea();
  255.                        
  256.  
  257.  
  258.                 // Chat history
  259.                 GUI.Box(new Rect(10, 80, 470, 390), "Chat");
  260.  
  261.                 GUILayout.BeginArea (new Rect(20, 110, 450, 350));
  262.                     chatScrollPosition = GUILayout.BeginScrollView (chatScrollPosition, GUILayout.Width (450), GUILayout.Height (350));
  263.                         GUILayout.BeginVertical();
  264.                        
  265.                         // We use lock here to ensure cross-thread safety on the messages collection
  266.                         lock (messagesLocker) {
  267.                             foreach (string message in messages)
  268.                             {
  269.                                 GUILayout.Label(message);
  270.                             }
  271.                         }
  272.                
  273.                         GUILayout.EndVertical();
  274.                     GUILayout.EndScrollView ();
  275.                 GUILayout.EndArea();
  276.  
  277.                 // Send message
  278.                 newMessage = GUI.TextField(new Rect(10, 480, 370, 20), newMessage, 50);
  279.                 if (GUI.Button(new Rect(390, 478, 90, 24), "Send")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n'))
  280.                 {
  281.                     smartFox.SendPublicMessage(newMessage, currentActiveRoom.GetId());
  282.                     newMessage = "";
  283.                 }
  284.                
  285.             }
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement