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) setLocationAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("setLocation", "badPlacing") pt, ok := utils.GetPointFromJson(json) if ok { g.players.getPlayerBySession(json["sid"].(string)).ForcePlace(*pt) res["result"] = "ok" } return res }
func (g *Game) putMob(json consts.JsonType) consts.JsonType { res := utils.JsonAction("putMob", "badAction") if *consts.TEST && consts.TEST_MODE { var requiredFields = map[string]string{ "x": "badPlacing", "y": "badPlacing", "flags": "badFlag", "race": "badRace", "dealtDamage": "badDamage", } var ok bool if ok, res["result"] = utils.CheckJsonRequest(json, requiredFields); ok { damage, ok := json["dealtDamage"].(string) if ok && utils.IsDiceDesc(damage) { flags := json["flags"].([]interface{}) var stats = map[string]interface{}{} if json["stats"] != nil { stats, ok = json["stats"].(map[string]interface{}) if !ok { res["result"] = "badStats" } } var inventory = map[string]interface{}{} if json["inventory"] != nil { inventory, ok = json["inventory"].(map[string]interface{}) if !ok { res["result"] = "badInventory" } } pt, isGoodPoint := utils.GetPointFromJson(json) race, isExistRace := consts.NameRaceMapping[json["race"].(string)] if !(isGoodPoint && g.field.FreeForObject(pt.X, pt.Y)) { res["result"] = "badPlacing" } else if !gameObjectsFlags.CheckFlags(flags) { res["result"] = "badFlag" } else if !isExistRace { res["result"] = "badRace" } else { m := gameObjects.NewTestMob(pt.X, pt.Y, race, damage, flags) if !g.setCharacteristicsToActiveObject(m, stats) { res["result"] = "badStats" return res } if !g.setInventoryToActiveObject(m, inventory) { res["result"] = "badInventory" return res } res["id"] = g.mobs.registerMob(m) res["result"] = "ok" } } else { res["result"] = "badDamage" } } } return res }
func (g *Game) setLocationAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("setLocation", "badPlacing") pt, ok := utils.GetPointFromJson(json) if ok { p := g.players.getPlayerBySession(json["sid"].(string)) g.field.UnlinkFromCells(p) p.ForcePlace(*pt) g.field.LinkToCells(p) res["result"] = "ok" } 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 }