|
@@ -12,16 +12,24 @@ import (
|
|
|
"github.com/name5566/leaf/gate"
|
|
|
)
|
|
|
|
|
|
+var list []*msg.ShopItem = make([]*msg.ShopItem, 0)
|
|
|
+
|
|
|
func init() {
|
|
|
console.Log("注册BuyShopItem")
|
|
|
}
|
|
|
|
|
|
func OnBuy(args []interface{}) {
|
|
|
m := args[0].(*msg.BuyShopItem)
|
|
|
- console.Log("玩家购买了商品id为:", m.Id)
|
|
|
a := args[1].(gate.Agent)
|
|
|
user_id := a.UserData().(*msg.UserInfo).UserId
|
|
|
- user.AddUserCoin(100, user_id)
|
|
|
+ console.Log("玩家购买了商品id为:", m.Id)
|
|
|
+ item := getShopItemById(m.Id)
|
|
|
+ if item != nil {
|
|
|
+ user.AddUserCoin(item.Num, user_id)
|
|
|
+ } else {
|
|
|
+ console.Log("OnBuy error:", m.String())
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
type ShopItem struct {
|
|
@@ -34,13 +42,15 @@ type ShopItem struct {
|
|
|
}
|
|
|
|
|
|
func GetShopList(shop_type int) ([]*msg.ShopItem, error) {
|
|
|
+ if len(list) > 0 {
|
|
|
+ return list, nil
|
|
|
+ }
|
|
|
rows, err := mysqlmgr.Query("SELECT * FROM shop_config WHERE shop_type = ? LIMIT 100 ", shop_type)
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("query failed: %v", err)
|
|
|
}
|
|
|
defer rows.Close()
|
|
|
|
|
|
- var results []*msg.ShopItem
|
|
|
var nullTime sql.NullTime // 用于处理可能的NULL时间值
|
|
|
for rows.Next() {
|
|
|
var shop_item ShopItem
|
|
@@ -54,7 +64,7 @@ func GetShopList(shop_type int) ([]*msg.ShopItem, error) {
|
|
|
); err != nil {
|
|
|
return nil, fmt.Errorf("failed to scan row: %v", err)
|
|
|
}
|
|
|
- results = append(results, &msg.ShopItem{
|
|
|
+ list = append(list, &msg.ShopItem{
|
|
|
Id: fmt.Sprint(shop_item.ID),
|
|
|
Name: shop_item.Name,
|
|
|
Price: int32(shop_item.Price),
|
|
@@ -66,5 +76,14 @@ func GetShopList(shop_type int) ([]*msg.ShopItem, error) {
|
|
|
return nil, fmt.Errorf("rows error: %v", err)
|
|
|
}
|
|
|
|
|
|
- return results, nil
|
|
|
+ return list, nil
|
|
|
+}
|
|
|
+
|
|
|
+func getShopItemById(id string) *msg.ShopItem {
|
|
|
+ for _, v := range list {
|
|
|
+ if v.Id == id {
|
|
|
+ return v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|