Beispiel #1
0
func (g *Game) CreatePlayer(sid string) *gameObjects.Player {
	db := connect.CreateConnect()
	stmt, _ := db.Prepare(`
        SELECT u.id, u.login, u.class, up.x, up.y
        FROM users u
        INNER JOIN users_position as up ON u.id = up.user_id
        INNER JOIN sessions s ON s.user_id = u.id AND s.sid = ?
    `)
	defer stmt.Close()
	var dbId int64
	var login string
	var class int
	var x, y float64
	stmt.QueryRow(sid).Scan(&dbId, &login, &class, &x, &y)
	if x == consts.DEFAULT_PLAYER_POS_X || y == consts.DEFAULT_PLAYER_POS_Y {
		x, y = g.newPlayerPos(x, y)
	}
	p := gameObjects.NewPlayer(utils.GenerateId(), dbId, class, login, sid, x, y)
	g.players.add(p)
	g.field.LinkToCells(p)
	rows, _ := db.Query("SELECT item_id, amount, place FROM users_inventory WHERE user_id = ?", dbId)
	for rows.Next() {
		var (
			iid           int64
			amount, place int
		)
		rows.Scan(&iid, &amount, &place)
		item := gameObjectsBase.NewItemByID(iid, p, amount)
		p.RestoreItem(item, place)
		g.items.addItem(item)
	}
	rows, _ = db.Query("SELECT item_id, amount, slot FROM users_slots WHERE user_id = ?", dbId)
	for rows.Next() {
		var (
			iid          int64
			amount, slot int
		)
		rows.Scan(&iid, &amount, &slot)
		item := gameObjectsBase.NewItemByID(iid, p, amount)
		p.RestoreSlot(item, slot)
		g.items.addItem(item)
	}
	return p
}
Beispiel #2
0
func (g *Game) putPlayer(json consts.JsonType) consts.JsonType {
	res := utils.JsonAction("putPlayer", "badAction")
	if *consts.TEST && consts.TEST_MODE {
		var requiredFields = map[string]string{
			"x": "badPlacing",
			"y": "badPlacing",
			//"inventory" : "badInventory",
		}
		var ok bool
		if ok, res["result"] = utils.CheckJsonRequest(json, requiredFields); ok {
			inventory, ok := json["inventory"].([]interface{})
			var stats = map[string]interface{}{}
			if json["stats"] != nil {
				stats = json["stats"].(map[string]interface{})
			}
			pt, isGoodPoint := utils.GetPointFromJson(json)
			if !(isGoodPoint && g.field.FreeForObject(pt.X, pt.Y)) {
				res["result"] = "badPlacing"
			} else {
				var class int = consts.PLAYER_CLASS_WARRIOR
				if json["class"] != nil {
					var exists bool
					class, exists = consts.NamePlayerClassMapping[json["class"].(string)]
					if !exists {
						res["result"] = "badClass"
						return res
					}
				}
				p := gameObjects.NewPlayer(utils.GenerateId(), -1, class, "", utils.GenerateSID(), pt.X, pt.Y)
				g.setCharacteristicsToActiveObject(p, stats)
				g.players.add(p)
				g.field.LinkToCells(p)
				if ok {
					for _, itemDesc := range inventory {
						item := gameObjectsBase.ItemFromJson(consts.JsonType(itemDesc.(map[string]interface{})))
						if item != nil {
							g.items.addItem(item)
							p.PickUpItem(item)
						}
					}
					res["inventory"] = p.GetInventoryInfo()
				}
				if slots, okey := json["slots"].(map[string]interface{}); okey {
					idxs := make(map[string]int64)
					for slotName, itemDesc := range slots {
						if itemd, item_converted := itemDesc.(map[string]interface{}); item_converted {
							item := gameObjectsBase.ItemFromJson(consts.JsonType(itemd))
							if item != nil {
								p.PickUpItem(item)
								reason, _ := p.Equip(item, consts.NameSlotMapping[slotName])
								if reason == consts.OK {
									g.items.addItem(item)
									idxs[slotName] = item.GetID()
								} else if reason == consts.BADID {
									res["result"] = "badId"
									fmt.Println("wrong id")
									return res
								} else {
									res["result"] = "badSlot"
									fmt.Println("wrong slot")
									return res
								}
							}
						} else {
							fmt.Println("fail to convert")
							res["result"] = "badSlot"
							return res
						}
					}
					res["slots"] = idxs
				}
				res["sid"] = p.SID
				res["id"] = p.GetID()
				res["fistId"] = p.GetFistID()
				res["result"] = "ok"
			}
		}
	}
	return res
}