_c# tutorial on the realization of the lottery function in C # MVC mode

Source: Internet
Author: User

Recent project requirements of a lottery function to organize the next, the language is not good, write a bad do not spit groove, a smile and pass. Okay, let's start with this lottery feature. Because it involves the company's projects, some sensitive places have been replaced with Chinese.

First add the name of the prize in the background and the probability as shown:

Background code Nothing to say, let's take a look at the front-end code.

The following styles are made from HTML:

 <div class= "TURNTABLE-BG" >
       
          <div class= "pointer" >
            
          </div>
          <div class=" Rotate ">
            
          </div>
        </div>



Here you should pay attention to the angle of the cut. Otherwise the rotation of the corresponding angle is wrong.

Add the Click event to the draw pointer below:

function rotate () {$.ajax ({type: ' POST ', url: '/Controller/method ', DataType: ' json ', Cache:false, Error:function () {alert (' ERROR!
        ');
      return false;
          }, Success:function (data) {if (data = = 1 | | data = = 3) {var brotate = true;
          if (data = = 1) {alert ("Please login!");
          }if (data = = 3) {alert ("Sorry, your lottery is insufficient!")  
          } else {var angles = parseint (data.angle);//angle var txt = data.prize;//Awards
          var awards = parseint (data.id); var brotate = false;

          If Brotate is true, you cannot click var rotatefn = rotatefn_method (awards, Angles, TXT);

          if (brotate) return;
                Switch (awards) {case 0://var angle = [26, 88, 137, 185, 235, 287, 337];
              Rptatefn_method three parameters are background outgoing, 0,337 for turntable rotation corresponding to the angle, the last one is the name Rotatefn_method (0, 337, ' Prize name ');
  Break          Case 1://var angle = [88, 137, 185, 235, 287];
                Rotatefn_method (1, 26, ' Prize name ');
              Break
                Case 2://var angle = [137, 185, 235, 287];
              Rotatefn_method (2, 88, ' Prize name ');
            Break
              Case 3://var angle = [137, 185, 235, 287];
              Rotatefn_method (3, 137, ' Prize name ');
            Break
              Case 4://var angle = [185, 235, 287];
              Rotatefn_method (4, 185, ' Prize name ');
            Break
              Case 5://var angle = [185, 235, 287];
              Rotatefn_method (5, 235, ' Prize name ');
            Break
              Case 6://var angle = [235, 287];
              Rotatefn_method (6, 235, ' Prize name ');
            Break
              Case 7://var angle = [287];
              Rotatefn_method (7, 282, ' Prize name ');
          Break
  }
        }

      }
    }); }//Rotation event FUNCTIOn Rotatefn_method (awards, Angles, txt) {var brotate = false;
    Brotate =!brotate;
    $ (' #turntable '). Stoprotate ();  $ (' #turntable '). Rotate ({angle:0, Animateto:angles + 1800, duration:8000, Callback:function ()
        {$ ("#Pl h4"). HTML ("Congratulations on your" + txt + "");
      } brotate =!brotate;
}
    })
  };
  Refreshes the current page function Locateend () {window.location.reload ();


 }

This will allow the turntable to rotate and eject the prizes. js in the comments should be more clear, I will not be more on the interpretation of JS, the following to say the logic of the controller calculation probability, the reason is not to write the probability calculation in JS, because the JS file is easily tampered with, presumably we all know. Words not much said, the following code:

Determine if the user is logged in if (admin!= null) {//Get background settings data, see Figure 1 var ldlist = hr.
         Luckdrawlist (); Since all the sensitive places involved in the company's project have been replaced list<double> dt = Ldlist.select (i => i. probability) in Chinese.
         ToList (); Create a numeric double[] Array = dt.
        ToArray ();
        angle int angle = 0;
        Prize var prize = "";
         var id = 0;
         Put the probability into the value, call the following get method to calculate the number of var Pl = Get (Array) that the probability returned is in the value;
           Get the prize table in the PL product var model = LDLIST[PL];
            ID, Angle Angle Prize prize if (model. prize grade = = 1) {angle = 26;
            prize = model. Prizes;
          id = 1;
            The IF (model. prize Grade = = 7) {angle = 88;
            prize = model. Prizes;
          id = 2;
            The IF (model. prize Grade = = 4) {angle = 137;
            prize = model. Prizes;
          id = 3;
            The IF (model. prize Grade = = 3) {angle = 185; PriZe = model. Prizes;
          id = 4;
            The IF (model. prize Grade = = 6) {angle = 235;
            prize = model. Prizes;
          id = 5;
            The IF (model. prize grade = = 2) {angle = 282;
            prize = model. Prizes;
          id = 7;
            The IF (model. prize Grade = = 5) {angle = 337;
            prize = model. Prizes;
          id = 0;
        Return Json (New {angle = angle, prize = prize, id = ID});
      }return Json (data);


 }

Part of the calculation probability:

<summary>
    ///Get lottery results
    ///</summary>
    ///<param name= "prob" > the extraction probability of each item </param>
    ///<returns> Returns the location of the array where the items are drawn </returns>
    private static int get (double[] prob)
    {
      int result = 0;
      int n = (int) (prob. Sum () * 1000);      Calculate probability sum, enlarge 1000 times times
      Random r = rnd;
      float x = (float) r.next (0, N)/1000;    A numerical for

      (int i = 0; i < prob) that randomly generates the sum of 0~ probabilities. Count (); i++)
      {
        double pre = prob. Take (i). Sum ();     Interval lower bound
        double next = prob. Take (i + 1). Sum ();  Upper bound
        if (x >= pre && x < Next)        //If within the range of the interval, returns the result exit loop
        {Results
          = i;
          break;
        }
      }
      return result;
    }
    private static Random rnd = new Random ();

Basically these, there are insufficient places to ask you to help add, thank you for your reading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.