Ejemplo n.º 1
0
        public SlotMachineSpinRes Spin(SlotMachineSpinReq model)
        {
            List <SlotSymbol> symbolsList = new List <SlotSymbol>();

            var user = _context.Users
                       .Where(u => u.Id == model.UserId)
                       .SingleOrDefault();

            //Check if user exists and have enough balance for the spin
            if (user.Balance < model.Bet)
            {
                return(null);
            }

            //Remove the bet from user's balance
            user.Balance -= model.Bet;
            _context.SaveChanges();

            //Fill list with random symbols based on certain odds
            for (var i = 0; i < 9; i++)
            {
                symbolsList.Add(getSymbolFromRandomNumber());
            }

            //Calculate reward(winning) from the spin
            var reward = calculateReward(symbolsList, model.Bet);

            //DB object to insert in 'SlotMachineSpins'
            var dbSpinData = new SlotMachineSpin {
                Id     = 0,
                Time   = DateTime.Now,
                User   = user,
                Game   = model.Game,
                Bet    = model.Bet,
                Reward = reward
            };

            //Insert the object
            _context.SlotMachineSpins.Add(dbSpinData);
            _context.SaveChanges();

            //Add reward from the spin to user's balance
            user.Balance += reward;
            _context.SaveChanges();

            //Return slot symbols to display in FE and the reward(winning)
            return(new SlotMachineSpinRes {
                SymbolsList = symbolsList,
                Reward = reward
            });
        }
Ejemplo n.º 2
0
    IEnumerator Spin_Coroutine()
    {
        IsBusy = true;
        Reward = 0;

        int reward;

        E_SlotMachineSymbol[] matrix;

        // create cloud action
        SlotMachineSpin action = new SlotMachineSpin(CloudUser.instance.authenticatedUserID);

        GameCloudManager.AddAction(action);

        // spin symbols
        {
            m_WinAnim.Stop();
            m_SpinAnim.Stop();

            ShowSpinAnimation(true);

            // play spin sound
            GetComponent <AudioSource>().loop = true;
            GetComponent <AudioSource>().clip = m_SpinSound;
            GetComponent <AudioSource>().Play();

            // play start sound
            GetComponent <AudioSource>().PlayOneShot(m_StartSound, AudioListener.volume);            //Added volume, otherwise in webplayer it was always playing on max

            // start slot machine
            m_SpinAnim.Play(START_ANIM);
            while (m_SpinAnim.isPlaying == true)
            {
                yield return(new WaitForEndOfFrame());
            }

            m_SpinAnim.Play(SPIN_ANIM);
        }

        // wait for cloud action to finish
        {
            float delay = 0.5f;
            do
            {
                yield return(new WaitForSeconds(delay));

                delay = 0.1f;
            } while (action.isDone == false);

            // get result from cloud
            if (action.isSucceeded == true)
            {
                reward = action.Reward;
                matrix = action.Matrix;

                SetMachineState(action.Matrix);
            }
            else
            {
                reward = 0;
                matrix = new E_SlotMachineSymbol[9];

                // update slot machine slots
                SetMachineState(E_SlotMachineSymbol.First, false);
            }
        }

        // stop slot machine
        {
            // stop slots
            m_SpinAnim.Play(STOP_ANIM);

            float oneStopDeuration = m_SpinAnim.GetClip(STOP_ANIM).length / 3.0f;
            for (int idx = 0; idx < 3; ++idx)
            {
                GetComponent <AudioSource>().PlayOneShot(m_StopSound, AudioListener.volume);                //Added volume, otherwise in webplayer it was always playing on max

                yield return(new WaitForSeconds(oneStopDeuration));
            }

            // stop symbol spinning
            ShowSpinAnimation(false);

            // stop spin sound
            GetComponent <AudioSource>().Stop();

            // play win anim if needed
            if (reward > 0)
            {
                int[] indexes = GetWinningSymbols(matrix);

                m_WinAnim.Play();

                GetComponent <AudioSource>().loop = false;

                float step = reward * 0.1f > 4.0f ? 4.0f / reward : 0.1f;
                float temp = (float)Reward;
                do
                {
                    temp  += reward * step;
                    Reward = Mathf.Min(reward, (int)temp);

                    GetComponent <AudioSource>().clip = m_WinSound;
                    GetComponent <AudioSource>().Play();

                    if (m_WinAnim.isPlaying == false)
                    {
                        m_WinAnim.Play();
                    }

                    foreach (var index in indexes)
                    {
                        Slot slot = GetSlot(index);
                        slot.Shine = !slot.Shine;
                    }

                    yield return(new WaitForSeconds(0.075f));

                    foreach (var index in indexes)
                    {
                        Slot slot = GetSlot(index);
                        slot.Shine = !slot.Shine;
                    }

                    yield return(new WaitForSeconds(0.025f));
                } while (Reward < reward);

                m_WinAnim.Stop();

                foreach (var index in indexes)
                {
                    Slot slot = GetSlot(index);
                    slot.Shine = false;
                }

                Reward = reward;
            }
        }

        // done
        IsBusy = false;
    }