func (g *Game) useAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("use", "badId") res["message"] = "some string" id, ok := utils.GetIdFromJson(json) //todo if ok { p := g.players.getPlayerBySession(json["sid"].(string)) xParam := json["x"] yParam := json["y"] res["result"] = "badSlot" if xParam != nil && yParam != nil { if item := p.GetItem(id); item != nil && item.IsWeapon() { if p.IsEquippedItem(item) { // if item.Get p.SetAttackPoint(xParam.(float64), yParam.(float64)) res["message"] = fmt.Sprintf("attack point (%f, %f)", xParam.(float64), yParam.(float64)) res["result"] = "ok" } } } else { p.UseItem(int64(json["id"].(float64))) res["result"] = "ok" } } return res }
func (g *Game) equipItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("equip", "badId") id, ok := utils.GetIdFromJson(json) if ok { json["action"] = "pickUp" g.doPlayersAction("pickUp", json) res["result"] = "badSlot" slotParam := json["slot"] if slotParam != nil { slot, isExistSlot := consts.NameSlotMapping[slotParam.(string)] if isExistSlot { p := g.players.getPlayerBySession(json["sid"].(string)) item := p.GetItem(id) if item != nil { reason, slots := p.Equip(item, slot) if reason == consts.OK { if slots != nil { res["slots"] = slots } res["result"] = "ok" } else if reason == consts.BADID { res["result"] = "badId" } else if reason == consts.BADSLOT { res["result"] = "badSlot" } } else { res["result"] = "badId" } } } } fmt.Println(res) return res }
func (g *Game) destroyItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("destroyItem", "badId") id, ok := utils.GetIdFromJson(json) if ok { item := g.items.getItem(id) p := g.players.getPlayerBySession(json["sid"].(string)) if item != nil && (item.IsOwner(p) || (!item.HasOwner() && geometry.Distance(p.GetCenter(), item.GetCenter()) <= consts.PICK_UP_RADIUS)) { var amount int = 1 if json["amount"] != nil { amount = int(json["amount"].(float64)) } // if item.GetAmount() - amount <= 0 { g.items.deleteItem(item) if item.IsOwner(p) { _, new_item := p.DeleteItem(item, amount) if new_item != nil { g.items.addItem(new_item) } } else if !item.HasOwner() { g.field.UnlinkFromCells(item) } // } else { // item.DecAmount(amount) // } res["result"] = "ok" } } return res }
func (g *Game) dropItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("drop", "badId") id, ok := utils.GetIdFromJson(json) if ok { item := g.items.getItem(id) p := g.players.getPlayerBySession(json["sid"].(string)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } if item != nil && item.IsOwner(p) { var amount int = 1 if json["amount"] != nil { amount = int(json["amount"].(float64)) } _, new_item := p.DropItem(item, amount) if new_item != nil { g.items.addItem(new_item) } g.field.LinkToCells(item) res["result"] = "ok" } } return res }
func (g *Game) pickUpItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("pickUp", "badId") id, ok := utils.GetIdFromJson(json) if ok { item := g.items.getItem(id) p := g.players.getPlayerBySession(json["sid"].(string)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } if item != nil && !item.HasOwner() && geometry.Distance(p.GetCenter(), item.GetCenter()) <= float64(consts.PICK_UP_RADIUS) { if p.CanPickUp(item) { ok, i := p.PickUpItem(item) if ok { g.field.UnlinkFromCells(item) if item.GetID() != i.GetID() { g.items.deleteItem(item) item = nil } res["result"] = "ok" } } else { res["result"] = "tooHeavy" } } } return res }
func (g *Game) examineAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("examine", "badId") id, ok := utils.GetIdFromJson(json) if ok { obj, isExists := g.getObjectById(id) if isExists { for k, v := range obj.GetFullInfo() { res[k] = v } p := g.players.getPlayerById(id) if p != nil { res["inventory"] = p.GetInventoryInfo() } res["result"] = "ok" } } return res }