Esempio n. 1
0
func (this *GameRouter) Get() {

	// Set App_Name
	this.Data["App_Name"] = App_Name

	// XSRF
	this.Data["xsrf"] = template.HTML(this.XsrfFormHtml())

	// Get User Session
	var user Session_User
	u := this.GetSession("_User")
	if u != nil {
		user = u.(Session_User)
		this.Data["User"] = user
	} else {
		this.Data["User"] = false
	}

	// Get id
	id, _ := strconv.Atoi(this.Ctx.Params[":id"])

	// Get game
	game := models.GameById(id)

	// Check if users can bet
	timeSub := TimeSub(game.Timestarted).Minutes()
	if game.Isover == 1 || timeSub < 15 {
		this.Data["CanBet"] = false
	} else {
		this.Data["CanBet"] = true
	}

	// Set Error alert (From Post)
	this.Data["PostResult"] = false
	v := this.GetSession("BetSuccess")
	if v != nil {
		this.Data["PostResult"] = true
		this.DelSession("BetSuccess")
		this.Data["BetSuccess"] = v.(bool)
		if !v.(bool) {
			e := this.GetSession("Error")
			this.DelSession("Error")
			if e != nil {
				this.Data["Error"] = GetError(e)
			}
		}
	}

	//  Set game info
	this.Data["Game"] = game

	this.TplNames = "game.html"
}
Esempio n. 2
0
func (this *AdminRouter) postData(sec string) {
	inputs := this.Input()
	switch sec {
	case "Game_New":
		teamA := inputs.Get("TeamA")
		teamB := inputs.Get("TeamB")
		oddsa := inputs.Get("Oddsa")
		oddsb := inputs.Get("Oddsb")
		concede := inputs.Get("Concede")
		scoresum := inputs.Get("ScoreSum")
		starttime := inputs.Get("TimeStart")
		Type := inputs.Get("Type")
		slice := []interface{}{"Football", "Basketball"}

		// Check if input matches
		if models.ValidOdds(oddsa) && models.ValidOdds(oddsb) && models.ValidScore(concede) && models.ValidScore(scoresum) && models.ValidStarttime(starttime) && In_slice(Type, slice) {
			oddsa_float64, _ := strconv.ParseFloat(oddsa, 64)
			oddsb_float64, _ := strconv.ParseFloat(oddsb, 64)
			concede_float64, _ := strconv.ParseFloat(concede, 64)
			scoresum_float64, _ := strconv.ParseFloat(scoresum, 64)
			var isfootball int
			if Type == "Football" {
				isfootball = 1
			}

			if !models.NewGame(models.Game_Detail{Isfootball: isfootball, Teama: teamA, Teamb: teamB, Oddsa: oddsa_float64, Oddsb: oddsb_float64, Concede: concede_float64, Scoresum: scoresum_float64, Timestarted: starttime, Timecreated: time.Now().Format(layout)}) {
				models.Log(models.Log_Struct{"error", "Create New Game:", errors.New("Failed to create new game")})
			}
		}
	case "Game_Result":
		gid := inputs.Get("gid")
		result := inputs.Get("result")

		// Check if input matches
		if models.ValidResult(result) && models.ValidGid(gid) {
			r := strings.Split(result, ":")
			score_a, _ := strconv.Atoi(r[0])
			score_b, _ := strconv.Atoi(r[1])
			score_sum := score_a + score_b

			id, _ := strconv.Atoi(gid)

			// Update the game
			if !models.UpdateGameById(score_a, score_b, id) {
				panic("Cant Update Result")
			}

			// Get the game by id, get game scoresum, odds, concede
			game := models.GameById(id)
			sum := game.Scoresum
			oddsa := game.Oddsa
			oddsb := game.Oddsb
			concede := game.Concede

			// Calculate the result, A_Win, B_Win, Odd, Even, Large, Small
			odds := oddsa
			a_or_b := "A_Win"
			if score_a-int(concede) <= score_b {
				a_or_b = "B_Win"
				odds = oddsb
			}
			oddeven := "Odd"
			if score_sum%2 == 0 {
				oddeven = "Even"
			}
			largesmall := "Large"
			if score_sum <= int(sum) {
				largesmall = "Small"
			}

			g_result := []interface{}{a_or_b, oddeven, largesmall}
			// Update_Distribution, TODO:
			if !models.CalculateResult(id, odds, ProfitAddr, g_result) {
				panic("Cant Calculate Result")
			}
		}
	}
	this.Ctx.Redirect(302, "/admin")
}