예제 #1
0
func ItemFromJson(itemDesc consts.JsonType) Itemer {
	var res Itemer
	errorMsg := "badItem"
	var requiredFields = map[string]string{
		"weight":  errorMsg,
		"class":   errorMsg,
		"type":    errorMsg,
		"bonuses": errorMsg,
		"effects": errorMsg,
	}
	if ok, _ := utils.CheckJsonRequest(itemDesc, requiredFields); ok {
		weight, ok1 := itemDesc["weight"].(float64)
		class, ok2 := itemDesc["class"].(string)
		itemType, ok3 := itemDesc["type"].(string)
		bonuses, ok4 := itemDesc["bonuses"].([]interface{})
		effects, ok5 := itemDesc["effects"].([]interface{})
		if ok1 && ok2 && ok3 && ok4 && ok5 {
			kind := &ItemKind{
				weight:   int(weight),
				class:    consts.NameItemClassMapping[class],
				itemType: consts.NameItemTypeMapping[itemType],
				bonuses:  make([]*Bonus, 0, 100),
				effects:  make([]Effecter, 0, 100),
			}
			for _, bonusDesc := range bonuses {
				kind.bonuses = append(kind.bonuses, BonusFromJson(consts.JsonType(bonusDesc.(map[string]interface{}))))
			}
			for _, effectDesc := range effects {
				kind.effects = append(kind.effects, EffectFromJson(consts.JsonType(effectDesc.(map[string]interface{}))))
			}
			return newItem(kind, nil)
			// return &Item{NewGameObject(utils.GenerateId(), *geometry.MakePoint(0, 0)), &kind, nil}
		}
	}
	return res
}
예제 #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
}