示例#1
0
    public void AbrirCofre()
    {
        chest_button.interactable = false;
        bool isVIP  = IsVip(selected_chest_type);
        int  nItems = isVIP ? 4 : 2;   //Cantidad de elementos que salen según el tipo de cofre

        if (selected_chest_type == Chest.Cutre2)
        {
            nItems++;
        }
        List <string> inventario  = Items.Instance.GetYourItems();
        List <string> nuevosItems = new List <string>();

        for (var x = 0; x < nItems; x++)
        {
            nuevosItems.Add(Items.Instance.GetRandomItemID(selected_chest_type));
        }

        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference.Child("Inventario").Child(GameManager.Instance.GetUserID());

        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsCompleted)
            {
                PlayerDB userData = JsonUtility.FromJson <PlayerDB>(task.Result.GetRawJsonValue());
                GetComponent <Canvas>().targetDisplay = 1;
                chest.GetComponent <Animator>().Play("Chest_Abrir");
                CofreAbierto.Instance.GuardarNuevosItems(nuevosItems);
                EquipDB eDB = userData.equipamiento;
                if (!isVIP)
                {
                    userData.data.cofres.RemoveAt(0);
                }
                else
                {
                    userData.data.cofres_VIP.RemoveAt(0);
                }

                foreach (string s in nuevosItems)
                {
                    inventario.Add(s);
                }

                userData.items        = inventario;
                userData.equipamiento = eDB;

                string json = JsonUtility.ToJson(userData);

                reference.SetRawJsonValueAsync(json).ContinueWith((obj2) =>
                {
                    if (obj2.IsCompleted)
                    {
                        CanvasBase.Instance.LoadYourItems();
                        UpdateChestsAmount();
                    }
                });
            }
        });
    }
    public void GuardarEquipSetting(Equipment equipment)
    {
        EquipDB equip = new EquipDB()
        {
            head = equipment.head.ID_string,
            body = equipment.body.ID_string,
            arms = equipment.arms.ID_string,
            legs = equipment.legs.ID_string
        };
        string json = JsonUtility.ToJson(equip);

        FirebaseDatabase.DefaultInstance.RootReference.Child("Inventario").Child(GameManager.Instance.GetUserID()).Child("equipamiento").SetRawJsonValueAsync(json);
    }
    private void GuardarInventarioDB()
    {
        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference.Child("Inventario").Child(GameManager.Instance.GetUserID());

        reference.GetValueAsync().ContinueWith(task =>
        {
            if (task.IsCompleted)
            {
                List <string> inventario = Items.Instance.GetYourItems();
                PlayerDB userData        = JsonUtility.FromJson <PlayerDB>(task.Result.GetRawJsonValue());
                userData.data.gold       = userData.data.gold + precio;
                EquipDB eDB = new EquipDB()
                {
                    head = GameManager.Instance.player.criatura.equipment.head.ID_string,
                    body = GameManager.Instance.player.criatura.equipment.body.ID_string,
                    arms = GameManager.Instance.player.criatura.equipment.arms.ID_string,
                    legs = GameManager.Instance.player.criatura.equipment.legs.ID_string,
                };
                GameManager.Instance.userdb = userData.data;
                userData.items        = inventario;
                userData.equipamiento = eDB;

                string json = JsonUtility.ToJson(userData);

                reference.SetRawJsonValueAsync(json).ContinueWith((obj2) =>
                {
                    if (obj2.IsCompleted)
                    {
                        print("Guardado.");
                        SetActiveBotons(true);
                        UpdateAdn();
                    }
                });
            }
        });
    }
    public void BTN_HUEVO()
    {
        transform.Find("Huevo").GetComponent <Button>().interactable = false;

        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;

        string rHeadgear = Items.Instance.GetRandomItemID(Equip_Position.Head, 1);
        string rBody     = Items.Instance.GetRandomItemID(Equip_Position.Body, 1);
        string rArms     = Items.Instance.GetRandomItemID(Equip_Position.Arms, 1);
        string rLegs     = Items.Instance.GetRandomItemID(Equip_Position.Legs, 1);

        Equipment equipment = new Equipment()
        {
            head = Items.Instance.ItemByID(rHeadgear),
            body = Items.Instance.ItemByID(rBody),
            arms = Items.Instance.ItemByID(rArms),
            legs = Items.Instance.ItemByID(rLegs)
        };

        List <string> itemList = new List <string>();

        itemList.Add(rHeadgear);
        itemList.Add(rBody);
        itemList.Add(rArms);
        itemList.Add(rLegs);

        UserDB userData = new UserDB()
        {
            //chests = 0,
            //chests_VIP = 0,
            gold             = 0,
            gold_VIP         = 0,
            victorias        = 0,
            derrotas         = 0,
            coronas          = 0,
            elo              = 1000,
            last_time_reward = new TimeSpan(DateTime.UtcNow.Ticks).Ticks.ToString(),
            nivel            = "0100",
            cofres           = new List <int>()
            {
                1, 2
            },
            cofres_VIP = new List <int>()
        };

        EquipDB equip = new EquipDB()
        {
            head = rHeadgear,
            body = rBody,
            arms = rArms,
            legs = rLegs
        };

        PlayerDB userInfo = new PlayerDB()
        {
            items        = itemList,
            data         = userData,
            equipamiento = equip,
            invitation   = new List <string>()
            {
                "inv1",
                "inv2"
            },
            dataTurn = ""
        };

        string jsonObjetos = JsonUtility.ToJson(userInfo);
        string jsonEquip   = JsonUtility.ToJson(equip);

        if (GameManager.Instance.GetUserID() != null)
        {
            reference.Child("Inventario").Child(GameManager.Instance.GetUserID()).SetRawJsonValueAsync(jsonObjetos).ContinueWith((obj2) =>
            {
                if (obj2.IsCompleted)
                {
                    transform.Find("Huevo").gameObject.SetActive(false);
                    GoMainMenu();
                }

                if (obj2.IsFaulted)
                {
                    transform.Find("Huevo").GetComponent <Button>().interactable = true;
                }
            });
        }
        else
        {
            //Si no se ha generado el usuario, intentar volver a conectar y generar nuevo huevo.

            Social.Active.Authenticate(Social.localUser, (bool success) => {
                if (success)
                {
                    BTN_HUEVO();
                }
                else
                {
                    Message.Instance.NewMessage("No se ha podido conectar con el servidor.");
                }
            });
        }
    }
    public Equipment ObtenerEquipSetting()
    {
        Equipment e     = new Equipment();
        EquipDB   equip = new EquipDB();

        FirebaseDatabase.DefaultInstance.RootReference.Child("Inventario").Child(GameManager.Instance.GetUserID()).Child("equipamiento").GetValueAsync().ContinueWith(task => {
            if (task.IsCompleted)
            {
                equip = JsonUtility.FromJson <EquipDB>(task.Result.GetRawJsonValue());
                e     = new Equipment()
                {
                    head = Items.Instance.ItemByID(equip.head),
                    body = Items.Instance.ItemByID(equip.body),
                    arms = Items.Instance.ItemByID(equip.arms),
                    legs = Items.Instance.ItemByID(equip.legs)
                };

                Player player = new Player()
                {
                    nombre   = Social.localUser.userName,
                    ID       = GameManager.Instance.GetUserID(),
                    criatura = new Criatura()
                    {
                        nombre      = GameManager.Instance.GetUserName(),
                        attack_att  = 0,
                        defense_att = 0,
                        luck_att    = 0,
                        skill_att   = 0,
                        equipment   = e
                    }
                };

                Menu.Instance.loadedEquipment = new Equipment()
                {
                    head = new Equipable_Item()
                    {
                        ID_string = e.head.ID_string
                    },
                    arms = new Equipable_Item()
                    {
                        ID_string = e.arms.ID_string
                    },
                    back = new Equipable_Item()
                    {
                        ID_string = e.back.ID_string
                    },
                    body = new Equipable_Item()
                    {
                        ID_string = e.body.ID_string
                    },
                    legs = new Equipable_Item()
                    {
                        ID_string = e.legs.ID_string
                    }
                };
                GameManager.Instance.player   = player;
                BattleSystem.Instance.player1 = player;
                PaseBatalla.Instance.MostrarPlayerInfo();
                CanvasBase.Instance.StatsRefresh();
                StartCoroutine(Animations.Instance.CargarHeadPose(1, player.criatura.equipment.head.ID, onEnded => {
                    StartCoroutine(GameManager.Instance.MostrarJugador(player, 1, new Vector3(8, 35, -1), false));
                    //Animations.Instance.blink_player1 = StartCoroutine(Animations.Instance.Pestañeo(1));
                }));
            }
        });
        return(e);
    }