func (g *Game) putItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("putItem", "badAction") if *consts.TEST && consts.TEST_MODE { var requiredFields = map[string]string{ "x": "badPlacing", "y": "badPlacing", "item": "badItem", } var ok bool if ok, _ = utils.CheckJsonRequest(json, requiredFields); ok { itemDesc, ok1 := json["item"].(map[string]interface{}) pt, isGoodPoint := utils.GetPointFromJson(json) if isGoodPoint && !g.field.IsBlocked(int(pt.X), int(pt.Y)) { res["result"] = "badInventory" if ok1 { item := gameObjectsBase.ItemFromJson(itemDesc) if item != nil { item.ForcePlace(*geometry.MakePoint(pt.X, pt.Y)) g.items.addItem(item) g.field.LinkToCells(item) res["id"] = item.GetID() res["result"] = "ok" } } } else { res["result"] = "badPlacing" } } } return res }
func (g *Game) setInventoryToActiveObject(obj gameObjectsBase.Activer, inventory map[string]interface{}) bool { res := true func() { defer func() { if r := recover(); r != nil { res = false } }() for _, v := range inventory { json, ok := v.(consts.JsonType) if !ok { res = false return } x := gameObjectsBase.ItemFromJson(json) obj.AddItem(x) } }() return res }
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 }