Exemple #1
0
func (this *Connect) BuyDiamondRequest() error {

	request := &protodata.BuyDiamondRequest{}
	if err := Unmarshal(this.Request.GetSerializedString(), request); err != nil {
		return this.Send(lineNum(), err)
	}

	index := int(request.GetProductIndex())

	payCenter := models.ConfigPayCenterList()
	product := payCenter[index-1]

	now := time.Now()
	orderId := fmt.Sprintf("101%s%05d", now.Format("200601021504"), rand.Intn(100000))

	user, err := models.User.User(this.Uid)
	if err != nil {
		return this.Send(lineNum(), err)
	}

	order := &models.OrderData{
		OrderId:    orderId,
		PlatId:     user.PlatId,
		Uid:        this.Uid,
		Money:      product.Money,
		Diamond:    product.Diamond,
		CreateTime: now.Unix()}

	if err := models.DB().Insert(order); err != nil {
		return this.Send(lineNum(), err)
	}

	return this.Send(StatusOK, &protodata.BuyDiamondResponse{OrderId: proto.String(orderId)})
}
Exemple #2
0
func CountOnline() {
	go func() {
		//t := time.Tick(time.Second * 10)
		t := time.Tick(time.Minute * 5)
		for {
			select {
			case <-t:
				playerMap.Lock.RLock()
				online := len(playerMap.Map)
				playerMap.Lock.RUnlock()
				fmt.Println("Online Num:", online)
				models.DB().Exec("INSERT INTO `stat_online`(`online_num`,`online_time`) VALUES (? , NOW())", online)
			}
		}
	}()
}
Exemple #3
0
func (this *Connect) FriendList() error {

	request := new(protodata.FriendListRequest)
	if err := Unmarshal(this.Request.GetSerializedString(), request); err != nil {
		return this.Send(lineNum(), err)
	}

	uidList := models.User.UidList(request.GetSnsIds(), int(request.GetPlatId()))

	response := new(protodata.FriendListResponse)
	if len(uidList) == 0 {
		return this.Send(StatusOK, response)
	}

	FriendAction := models.NewFriendAction(this.Uid)

	var list1, list2 []*protodata.FriendData
	//	friendList := models.Role.FriendList(uidList)
	// --------- 临时写法 --------- //
	var friendList []*models.RoleData
	models.DB().Select(&friendList, "SELECT * FROM `role` ORDER BY `role_kill_num` DESC LIMIT 50")

	// --------- 临时写法 --------- //

	var find bool
	for index, f := range friendList {
		if f.Uid == this.Uid {
			find = true
		}
		fdata := new(protodata.FriendData)
		fdata.Uid = proto.Int64(f.Uid)
		fdata.Num = proto.Int32(int32(index + 1))
		fdata.Point = proto.Int32(int32(f.KillNum))
		fdata.LeaderId = proto.Int32(int32(f.GeneralBaseId))
		fdata.Name = proto.String(f.Name)
		if _, ok := FriendAction.Map[f.Uid]; ok {
			fdata.IsGive = proto.Bool(true)
		} else {
			fdata.IsGive = proto.Bool(false)
		}
		list1 = append(list1, fdata)
	}

	if !find {
		fdata := new(protodata.FriendData)
		if this.Role.KillNum == friendList[len(friendList)-1].KillNum {
			*fdata.Num = *list1[len(list1)-1].Num + 1
		} else {
			sql := "SELECT COUNT(*) FROM `role` WHERE `role_kill_num` > ?"
			num, err := models.DB().SelectInt(sql, this.Role.KillNum)
			if err != nil {
				return this.Send(lineNum(), err)
			}
			fdata.Num = proto.Int32(int32(num + 1))
		}
		fdata.Uid = proto.Int64(this.Role.Uid)
		fdata.Point = proto.Int32(int32(this.Role.KillNum))
		fdata.LeaderId = proto.Int32(int32(this.Role.GeneralBaseId))
		fdata.Name = proto.String(this.Role.Name)
		fdata.IsGive = proto.Bool(true)
		list1 = append(list1, fdata)
	}

	// --------- 临时写法 --------- //
	friendList = make([]*models.RoleData, 0)
	models.DB().Select(&friendList, "SELECT * FROM `role` ORDER BY `role_unlimited_max_num` DESC LIMIT 50")

	// --------- 临时写法 --------- //
	//for i := 0; i < len(friendList); i++ {
	//	for j := len(friendList) - 1; j > i; j-- {
	//		if friendList[j].UnlimitedMaxNum > friendList[j-1].UnlimitedMaxNum {
	//			friendList[j-1], friendList[j] = friendList[j], friendList[j-1]
	//		}
	//	}
	//}

	find = false
	for index, f := range friendList {
		if f.Uid == this.Uid {
			find = true
		}
		fdata := new(protodata.FriendData)
		fdata.Uid = proto.Int64(f.Uid)
		fdata.Num = proto.Int32(int32(index + 1))
		fdata.Point = proto.Int32(int32(f.UnlimitedMaxNum))
		fdata.LeaderId = proto.Int32(int32(f.GeneralBaseId))
		fdata.Name = proto.String(f.Name)
		if _, ok := FriendAction.Map[f.Uid]; ok {
			fdata.IsGive = proto.Bool(true)
		} else {
			fdata.IsGive = proto.Bool(false)
		}
		list2 = append(list2, fdata)
	}

	if !find {
		fdata := new(protodata.FriendData)
		if this.Role.UnlimitedMaxNum == friendList[len(friendList)-1].UnlimitedMaxNum {
			*fdata.Num = *list1[len(list1)-1].Num + 1
		} else {
			sql := "SELECT COUNT(*) FROM `role` WHERE `role_unlimited_max_num` > ?"
			num, err := models.DB().SelectInt(sql, this.Role.UnlimitedMaxNum)
			if err != nil {
				return this.Send(lineNum(), err)
			}
			fdata.Num = proto.Int32(int32(num + 1))
		}
		fdata.Uid = proto.Int64(this.Role.Uid)
		fdata.Point = proto.Int32(int32(this.Role.UnlimitedMaxNum))
		fdata.LeaderId = proto.Int32(int32(this.Role.GeneralBaseId))
		fdata.Name = proto.String(this.Role.Name)
		fdata.IsGive = proto.Bool(true)
		list2 = append(list2, fdata)
	}

	response.FriendList1 = list1
	response.FriendList2 = list2
	response.GiveNum = proto.Int32(int32(len(FriendAction.Map)))
	response.GiveMax = proto.Int32(5)
	return this.Send(StatusOK, response)
}
Exemple #4
0
func httpConfirm(w http.ResponseWriter, r *http.Request) {

	defer func() {
		if err := recover(); err != nil {
			log.Critical("RPC Panic: %v", err)
		}
	}()

	orderId := r.FormValue("order_id")
	if orderId == "" {
		return
	}

	OrderData, err := models.Order.Order(orderId)
	if err != nil {
		return
	}

	conn := playerMap.Get(OrderData.Uid)

	var RoleData *models.RoleData
	if conn != nil && conn.Role != nil {
		RoleData = conn.Role
	} else {
		RoleData, err = models.Role.Role(OrderData.Uid)
		if err != nil {
			return
		}
	}

	// ----- 启动事务 -----
	if OrderData.Status != 1 {
		log.Warn("Order status not is '1' ORDERID: %s", orderId)
		return
	}

	Transaction, err := models.DB().Begin()
	if err != nil {
		log.Warn("%v", err)
		return
	}

	OrderData.Status = 2

	affected_rows, err := Transaction.Update(OrderData)
	if err != nil || affected_rows != 1 {
		Transaction.Rollback()
		log.Warn("%v", err)
		return
	}

	oldDiamond := RoleData.Diamond
	RoleData.Diamond += OrderData.Diamond
	RoleData.UnixTime = time.Now().Unix()

	_, err = Transaction.Update(RoleData)
	if err != nil {
		RoleData.Diamond = oldDiamond
		Transaction.Rollback()
		log.Warn("%v", err)
		return
	}

	// ----- 提交 -----
	if Transaction.Commit() != nil {
		RoleData.Diamond = oldDiamond
		Transaction.Rollback()
		log.Warn("%v", err)
		return
	}

	models.InsertSubDiamondFinanceLog(OrderData.Uid, models.FINANCE_BUY_DIAMOND, oldDiamond, RoleData.Diamond, fmt.Sprintf("orderId: %s", orderId))

	if conn != nil {
		*conn.Request.CmdId = 10121
		*conn.Request.CmdIndex = 10121
		conn.Send(StatusOK, &protodata.PaySuccessResponse{Role: roleProto(RoleData)})
	}
}