Exemple #1
0
 void TcpReader()
 {
     try {
         using (StreamReader reader = new StreamReader(_client.GetStream(), Encoding.ASCII))
         {
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 Debug.Log("Line: " + line);
                 try
                 {
                     Event response = JsonConvert.DeserializeObject <Event>(line);
                     _responseAnalyzer.Analyze(response);
                 }
                 catch (Exception)
                 {
                     Debug.Log("Non-JSON Message: " + line);
                 }
             }
         }
     }
     catch (SocketException socketException) {
         Debug.Log("Socket exception: " + socketException);
     }
 }
Exemple #2
0
    public bool TCPHandshake()
    {
        Event e = new Event();

        e.Type = "Handshake";
        e.Info = new Dictionary <string, string>()
        {
            { "id", playerInfo.PlayerData.ID },
        };
        return(_matchTcp.SendData(JsonConvert.SerializeObject(e)));
    }
Exemple #3
0
    public void NewMatch()
    {
        Event e = new Event();

        e.Type = "New";
        e.Info = new Dictionary <string, string>()
        {
            { "id", playerInfo.PlayerData.ID }
        };
        _matchTcp.SendInitialData(JsonConvert.SerializeObject(e));
    }
Exemple #4
0
 protected override void OnEvent(Event e)
 {
     switch (e.Type)
     {
     case "GetPlayerResp":
         if ((Status)int.Parse(e.Info["status"]) == Status.STATUS_OK)
         {
             playerInfo.PlayerData = JsonConvert.DeserializeObject <PlayerData>(e.Info["user"]);
         }
         break;
     }
 }
    public bool GetPlayer(string id)
    {
        Event e = new Event();

        e.Type = "PlayerController.get";
        e.Info = new Dictionary <string, string>()
        {
            { "_id", id }
        };
        Debug.Log(JsonConvert.SerializeObject(e));
        return(_mainTcp.SendData(JsonConvert.SerializeObject(e)));
    }
    public bool NewPlayer(string username)
    {
        Event e = new Event();

        e.Type = "PlayerController.new";
        e.Info = new Dictionary <string, string>()
        {
            { "username", username }
        };
        Debug.Log(JsonConvert.SerializeObject(e));
        return(_mainTcp.SendData(JsonConvert.SerializeObject(e)));
    }
Exemple #7
0
    protected override void OnEvent(Event e)
    {
        switch (e.Type)
        {
        case "MatchPlace":
            Debug.Log("We Have Match Port");
            PlayerInfo.PlayerData.MatchIP   = e.Info["matchIP"];
            PlayerInfo.PlayerData.MatchPort = int.Parse(e.Info["matchPort"]);
            _matchTcp.Connect(PlayerInfo.PlayerData.MatchIP, PlayerInfo.PlayerData.MatchPort);
            _matchUdp.Connect(PlayerInfo.PlayerData.MatchIP, PlayerInfo.PlayerData.MatchPort);
            _messageHandler.TCPHandshake();
            _messageHandler.UDPHandshake();

            break;

        case "MatchUpdate":
            GameObject o = GameObject.Find(e.Info["Id"]);
            if (o == null)
            {
                Debug.Log("Object " + e.Info["Id"] + " Not Found");
                return;
            }

            o.GetComponent <NetworkDriven>().SetTarget(float.Parse(e.Info["X"]), float.Parse(e.Info["Y"]),
                                                       Mathf.Rad2Deg * float.Parse(e.Info["Angle"]));
            break;

        case "MatchStart":

            break;

        case "FakeID":
            Debug.Log("Fake ID Received");
            PlayerInfo.MatchPlayerID = int.Parse(e.Info["id"]);
            break;

        case "HeroID":
            PlayerInfo.HeroName = e.Info["HeroID"];
            GameObject hero = GameObject.Find(PlayerInfo.HeroName);
            if (hero != null)
            {
                Camera.main.GetComponent <MatchCamera>().target = hero.transform;
            }
            break;

        case "MatchEnd":
            MessageHandler.i.GetPlayer(PlayerPrefs.GetString("id"));
            Debug.Log("Match Ended");
            SceneManager.LoadScene("MenuScene");
            break;
        }
    }
    protected override void OnEvent(Event e)
    {
        switch (e.Type)
        {
        case "Damage":
            if (e.Info["heroID"] == PlayerInfo.HeroName)
            {
                Destroy(GameObject.Find("Heart " + health));
                health--;
            }

            break;
        }
    }
Exemple #9
0
    public bool Analyze(Event response)
    {
        if (map.ContainsKey(response.Type))
        {
            EventBehaviour system = map[response.Type];
            system.AddEvent(response);
        }
        else
        {
            Debug.Log("Unknown Type Of Response\n" + response.Type);
            return(false);
        }

        return(true);
    }
Exemple #10
0
    public void SendInitialData(string text)
    {
        new Thread((() =>
        {
            if (_client == null || !_client.Connected)
            {
                _client = new TcpClient(IPAddress, Port);
            }
            try
            {
                Byte[] data =
                    Encoding.ASCII.GetBytes(text);

                Byte[] bytes = new Byte[1024];
                using (NetworkStream stream = _client.GetStream())
                {
                    int length;
                    stream.Write(data, 0, data.Length);
                    while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        var incomingData = new byte[length];
                        Array.Copy(bytes, 0, incomingData, 0, length);

                        string message = System.Text.Encoding.ASCII.GetString(incomingData);
                        Event response = JsonConvert.DeserializeObject <Event>(message);

                        _client.Close();
                        _client.Dispose();
                        Debug.Log("MatchMaker: " + message);
                        if (_responseAnalyzer.Analyze(response))
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        })).Start();
    }
Exemple #11
0
    protected override void OnEvent(Event e)
    {
        if (e.Type != "NewPlayerResp")
        {
            return;
        }
        switch ((Status)int.Parse(e.Info["status"]))
        {
        case Status.STATUS_DUPLICATE:
            gameObject.GetComponent <InputField>().text = "";
            gameObject.GetComponent <InputField>().placeholder.GetComponent <Text>().text = "Duplicate Entry";
            StartCoroutine(DuplicateMsg());
            break;

        case Status.STATUS_OK:
            Debug.Log("Reached OK");
            PlayerPrefs.SetString("id", e.Info["userId"]);
            _messageHandler.GetPlayer(e.Info["userId"]);
            _manage.RegisterCont();
            break;
        }
    }
Exemple #12
0
    protected override void OnEvent(Event e)
    {
        switch (e.Type)
        {
        case "NewItem":
            GameObject gameObjectDef = prefabMap.GetObject(e.Info["objName"]);
            Vector2    pos           = new Vector2(float.Parse(e.Info["X"]), float.Parse(e.Info["Y"]));
            Quaternion angle         = Quaternion.Euler(0, 0, Mathf.Rad2Deg * float.Parse(e.Info["Angle"]));
            GameObject element       = Instantiate(gameObjectDef, pos, angle);
            element.transform.parent = gameObject.transform;
            element.name             = e.Info["elemName"];
            if (element.name == playerInfo.HeroName)
            {
                Camera.main.GetComponent <MatchCamera>().target = element.transform;
            }
            break;

        case "DelItem":
            string id = e.Info["id"];
            Destroy(GameObject.Find(id));
            break;
        }
    }
Exemple #13
0
 protected abstract void OnEvent(Event e);
Exemple #14
0
 public void AddEvent(Event e)
 {
     _events.Enqueue(e);
 }