Example #1
0
func loginAction(login, pass string) string {
	result := map[string]interface{}{"result": "invalidCredentials"}
	if isExistUser(login, pass) {
		findSIDAndLogOut(login)
		db := connect.CreateConnect()
		sid := utils.GenerateSID()
		stmt, _ := db.Prepare("CALL add_user_session(?, ?)")
		defer stmt.Close()
		_, err := stmt.Exec(login, sid)
		if err == nil {
			host, _ := os.Hostname()
			//
			result["action"] = "login"
			//
			result["sid"] = sid
			result["result"] = "ok"
			result["webSocket"] = "ws://" + host + consts.SERVER_PORT + "/websocket"
			// result["webSocket"] = "ws://" + "192.168.173.230" + consts.SERVER_PORT + "/websocket"
			p := engine.GetInstance().CreatePlayer(sid)
			result["id"] = p.GetID()
			result["fistId"] = p.GetFistID()
		}
	}
	resJSON, _ := json.Marshal(result)
	return string(resJSON)
}
Example #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
}