Advertisement
Romulo_Gomes

Lobby

Jan 13th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.66 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. using Sfs2X;
  7. using Sfs2X.Core;
  8. using Sfs2X.Logging;
  9. using Sfs2X.Requests;
  10. using Sfs2X.Entities;
  11. using Sfs2X.Entities.Data;
  12. using Sfs2X.Entities.Variables;
  13. using Sfs2X.Entities.Managers;
  14.  
  15. public class Lobby : MonoBehaviour
  16. {
  17.     private SmartFox sfs;
  18.     private Room lobbyRoom;
  19.     private User user;
  20.    
  21.     private bool isConnected = false;
  22.     private bool isConnecting = false;
  23.    
  24.     private Hashtable roomHashtables;
  25.  
  26.     private List<RoomVariable> roomVariables = new List<RoomVariable>();
  27.    
  28.     private const int serverPort = 9933;
  29.  
  30.     private const string zoneName = "BasicExamples";
  31.     private const string lobbyName = "The Lobby";
  32.     private const string idGroup = "CustomIdGroup";
  33.     private const string roomName = "CustomRoom";
  34.  
  35.     public GameObject interfaceMain;
  36.     public GameObject interfaceCreate;
  37.     public GameObject interfaceSearch;
  38.     public GameObject interfaceRoom;
  39.  
  40.     public string serverIp = "127.0.0.1";
  41.     public string userName = "Billy";
  42.  
  43.     private void Awake ()
  44.     {
  45.         sfs = new SmartFox();
  46.         sfs.ThreadSafeMode = true;
  47.  
  48.         sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
  49.         sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
  50.         sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
  51.         sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
  52.         sfs.AddEventListener(SFSEvent.ROOM_ADD, OnCreateRoom);
  53.         sfs.AddEventListener(SFSEvent.ROOM_CREATION_ERROR, OnCreateRoomError);
  54.         sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);
  55.         sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnJoinRoomError);
  56.         sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
  57.         sfs.AddEventListener(SFSEvent.ROOM_REMOVE, OnRemoveRoom);
  58.  
  59.         ConnectSmartFox();
  60.     }
  61.  
  62.     private void Update ()
  63.     {
  64.         sfs.ProcessEvents();
  65.  
  66.         if(!this.isConnected)
  67.         {
  68.             if(!this.isConnecting)
  69.             {
  70.                 this.isConnecting = true;
  71.                 ConnectSmartFox();
  72.             }
  73.         }
  74.         else
  75.         {
  76.             //if (sfs.GetRoomByName(roomName) != null) Debug.Log(sfs.GetRoomByName(roomName));
  77.  
  78.             //if(Input.GetKeyDown(KeyCode.A)) Debug.Log(GetRoomsByGroup().Count);
  79.         }
  80.     }
  81.  
  82.     public void ToogleTelas (bool b0, bool b1, bool b2, bool b3)
  83.     {
  84.         interfaceMain.SetActive(b0);
  85.         interfaceCreate.SetActive(b1);
  86.         interfaceSearch.SetActive(b2);
  87.         interfaceRoom.SetActive(b3);
  88.     }
  89.    
  90.     public void ActiveButtons ()
  91.     {
  92.         interfaceCreate.SetActive(false);
  93.         interfaceSearch.SetActive(false);
  94.         interfaceRoom.SetActive(false);
  95.         interfaceMain.SetActive(true);
  96.     }
  97.    
  98.     private void CallMainMenu ()
  99.     {
  100.         Application.LoadLevel("StartMenu");
  101.     }
  102.  
  103.     private void ConfigUserPrivileges (User tempUser)
  104.     {
  105.         user = tempUser;
  106.         user.PrivilegeId = 1;
  107.     }
  108.  
  109.     private void ConnectSmartFox ()
  110.     {
  111.         sfs.Connect(serverIp, serverPort);
  112.     }
  113.  
  114.     public void DisconnectSmartFox()
  115.     {
  116.         if(sfs.IsConnected) sfs.Disconnect();
  117.  
  118.         CallMainMenu();
  119.     }
  120.  
  121.     public int GetNumberOfRooms ()
  122.     {
  123.         return sfs.GetRoomListFromGroup("default").Count;
  124.     }
  125.  
  126.     public int GetNumberOfRoomsByGroup ()
  127.     {
  128.         return sfs.GetRoomListFromGroup(idGroup).Count;
  129.     }
  130.  
  131.     public int GetNumberOfRoomsByGroup (String group)
  132.     {
  133.         return sfs.GetRoomListFromGroup(group).Count;
  134.     }
  135.  
  136.     public List<Room> GetRooms ()
  137.     {
  138.         return sfs.GetRoomListFromGroup("default");
  139.     }
  140.  
  141.     public List<Room> GetRoomsByGroup()
  142.     {
  143.         return sfs.GetRoomListFromGroup(idGroup);
  144.     }
  145.    
  146.     public List<Room> GetRoomsByGroup(String group)
  147.     {
  148.         return sfs.GetRoomListFromGroup(group);
  149.     }
  150.  
  151.     private void OnApplicationQuit ()
  152.     {
  153.         if(sfs.IsConnected) sfs.Disconnect();
  154.     }
  155.  
  156.     public void CreateRoom (string roomName, Hashtable tempArgs, string[] tempProps)
  157.     {
  158.         roomVariables = new List<RoomVariable>();
  159.         roomHashtables = tempArgs;
  160.  
  161.         for(int i = 0; i < tempProps.Length; i++) roomVariables.Add(new SFSRoomVariable(tempProps[i], tempArgs[tempProps[i]]));
  162.  
  163.         RoomSettings settings = new RoomSettings(roomName);
  164.         settings.MaxUsers = 4;
  165.         settings.GroupId = idGroup;
  166.         settings.Name = roomName;
  167.         settings.Events = new RoomEvents();
  168.         settings.Events.AllowUserCountChange = true;
  169.         settings.Events.AllowUserEnter = true;
  170.         settings.Events.AllowUserExit = true;
  171.         settings.Variables = roomVariables;
  172.  
  173.         //sfs.Send(new CreateRoomRequest(settings));
  174.         //sfs.Send(new CreateRoomRequest(settings, true));
  175.         sfs.Send(new CreateRoomRequest(settings, true, lobbyRoom));
  176.     }
  177.  
  178.     public void SendMessage (string message)
  179.     {
  180.         sfs.Send(new PublicMessageRequest(message));
  181.     }
  182.  
  183.     public void JoinRoom(string roomName)
  184.     {
  185.         sfs.Send(new JoinRoomRequest(roomName));
  186.     }
  187.    
  188.     public void JoinRandomRoom()
  189.     {
  190.         List<Room> allRooms = GetRoomsByGroup();
  191.  
  192.         if(allRooms.Count < 1) return;
  193.  
  194.         List<Room> tempRooms = new List<Room>();
  195.         System.Random rand = new System.Random();
  196.  
  197.         for(int i = 0; i < allRooms.Count; i++)
  198.         {
  199.             if(allRooms[i].GetVariable("password") == "" &&
  200.                allRooms[i].UserCount < 4) tempRooms.Add(allRooms[i]);
  201.         }
  202.  
  203.         int randNum = rand.Next(0, tempRooms.Count);
  204.  
  205.         sfs.Send(new JoinRoomRequest(tempRooms[randNum].Name));
  206.     }
  207.  
  208.     //---EventListeners---//
  209.  
  210.     private void OnConnection (BaseEvent e)
  211.     {
  212.         if((bool)e.Params["success"])
  213.         {
  214.             Debug.Log("You're connected!");
  215.             sfs.Send(new LoginRequest(userName, "", zoneName));
  216.             isConnected = true;
  217.         }
  218.         else
  219.         {
  220.             Debug.Log("Connection Failed! We'll try to connect again!");
  221.             isConnected = false;
  222.             isConnecting = false;
  223.         }
  224.     }
  225.  
  226.     private void OnConnectionLost (BaseEvent e)
  227.     {
  228.         Debug.Log("Lost connection!  We'll try to connect again!");
  229.  
  230.         isConnected = false;
  231.         isConnecting = false;
  232.     }
  233.  
  234.     private void OnLogin(BaseEvent e)
  235.     {
  236.         ConfigUserPrivileges((User)e.Params["user"]);
  237.         Debug.Log("Logged in: " + e.Params["user"]);
  238.  
  239.         JoinRoom(lobbyName);
  240.     }
  241.    
  242.     private void OnLoginError(BaseEvent e)
  243.     {
  244.         Debug.Log("Login error (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
  245.     }
  246.  
  247.     private void OnCreateRoom (BaseEvent e)
  248.     {
  249.         Room sala = (Room)e.Params["room"];
  250.         RoomManager.Instance().CreateNewRoom(sala.Name, roomHashtables, sala.UserCount);
  251.  
  252.         ToogleTelas(false, false, false, true);
  253.  
  254.         Debug.Log("A new Room was added: " + sala);
  255.     }
  256.    
  257.     private void OnCreateRoomError (BaseEvent e)
  258.     {
  259.         CreateMenu cMenu = interfaceCreate.GetComponent<CreateMenu>();
  260.         cMenu.popupAviso.SetActive(true);
  261.         cMenu.popupText.text = "An error occurred while " + '\n' +
  262.                                "attempting to create the Room:" + '\n' +
  263.                                e.Params["reason"];
  264.  
  265.         Debug.Log("An error occurred while attempting to create the Room: " + e.Params["reason"]);
  266.     }
  267.  
  268.     private void OnJoinRoom (BaseEvent e)
  269.     {
  270.         Room sala = (Room)e.Params["room"];
  271.         lobbyRoom = sala;
  272.  
  273.         if(sala.Name != lobbyName)
  274.         {
  275.             Hashtable tempArgs = new Hashtable();
  276.             tempArgs.Add("password", sala.GetVariable("password").GetStringValue());
  277.             tempArgs.Add("scenario", sala.GetVariable("scenario").GetIntValue());
  278.             tempArgs.Add("mode", sala.GetVariable("mode").GetIntValue());
  279.             tempArgs.Add("modeType", sala.GetVariable("modeType").GetIntValue());
  280.             tempArgs.Add("itensOptions", sala.GetVariable("itensOptions").GetIntValue());
  281.             tempArgs.Add("maxPoitns", sala.GetVariable("maxPoitns").GetIntValue());
  282.             tempArgs.Add("minutes", (float)sala.GetVariable("maxPoitns").GetDoubleValue());
  283.  
  284.             RoomManager.Instance().CreateNewRoom(sala.Name, tempArgs, sala.UserCount);
  285.         }
  286.  
  287.         Debug.Log("Joined Room: " + e.Params["room"]);
  288.     }
  289.  
  290.     private void OnJoinRoomError(BaseEvent e)
  291.     {
  292.         Debug.Log("JoinRoom (" + e.Params["errorCode"] + "): " + e.Params["errorMessage"]);
  293.     }
  294.  
  295.     private void OnRemoveRoom(BaseEvent e)
  296.     {
  297.         List<Room> salas = GetRooms();
  298.  
  299.         //user.
  300.     }
  301.  
  302.     private void OnPublicMessage (BaseEvent e)
  303.     {
  304.         string message = (string)e.Params["message"];
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement