Exemple #1
0
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
}
Exemple #2
0
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
}
Exemple #3
0
func BonusFromJson(bonusDesc consts.JsonType) *Bonus {
	var requiredFields = map[string]string{
		"stat":              "badItem",
		"effectCalculation": "badItem",
		"value":             "badItem",
	}
	if ok, _ := utils.CheckJsonRequest(bonusDesc, requiredFields); ok {
		statParam, ok1 := bonusDesc["stat"].(string)
		effectCalculationParam, ok2 := bonusDesc["effectCalculation"].(string)
		val, ok3 := bonusDesc["value"].(float64)
		if ok1 && ok2 && ok3 {
			return NewBonus(consts.NameCharacteristicMapping[statParam], consts.NameBonusMapping[effectCalculationParam], int(val))
		}
	}
	return nil
}
Exemple #4
0
func bonusEffectFromJson(effectDesc consts.JsonType) *BonusEffect {
	var res *BonusEffect
	errorMsg := "badItem"
	var requiredFields = map[string]string{
		"duration": errorMsg,
		"bonus":    errorMsg,
	}
	if ok, _ := utils.CheckJsonRequest(effectDesc, requiredFields); ok {
		duration, ok1 := effectDesc["duration"].(int)
		bonusDesc, ok2 := effectDesc["bonus"].(map[string]interface{})
		if ok1 && ok2 {
			res = newBonusEffect(time.Duration(duration), BonusFromJson(bonusDesc))
		}
	}
	return res
}
Exemple #5
0
func onGoingEffectFromJson(effectDesc consts.JsonType) *OnGoingEffect {
	var res *OnGoingEffect
	errorMsg := "badItem"
	var requiredFields = map[string]string{
		"stat":     errorMsg,
		"duration": errorMsg,
		"value":    errorMsg,
	}
	if ok, _ := utils.CheckJsonRequest(effectDesc, requiredFields); ok {
		stat, ok1 := effectDesc["stat"].(string)
		duration, ok2 := effectDesc["duration"].(int)
		value, ok3 := effectDesc["value"].(int)
		if ok1 && ok2 && ok3 {
			res = newOnGoingEffect(time.Duration(duration), consts.NameCharacteristicMapping[stat], value)
		}
	}
	return res
}
Exemple #6
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
}
Exemple #7
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
}
Exemple #8
0
func JsonHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")

	w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
	w.Header().Set("Access-Control-Allow-Headers", "Content-type")
	w.Header().Set("Content-type", "application/json")
	// w.Header().Set("Status Code", "200")
	if r.Method == "OPTIONS" {
		fmt.Fprintf(w, "OK")
		return
	}
	body, _ := ioutil.ReadAll(r.Body)
	var rawData interface{}
	json.Unmarshal(body, &rawData)
	data, ok2 := rawData.(map[string]interface{})
	if !ok2 {
		fmt.Println(data, rawData, r.Body)
	}
	var (
		action   string = data["action"].(string)
		response string
	)
	var requiredFields = map[string]string{}
	switch action {
	case "logout":
		requiredFields["sid"] = "badSid"
	case "login":
		requiredFields["login"] = "******"
		requiredFields["password"] = "******"
	case "register":
		requiredFields["login"] = "******"
		requiredFields["password"] = "******"
	}
	var ok bool
	res := utils.JsonAction(action, "badAction")
	if ok, res["result"] = utils.CheckJsonRequest(data, requiredFields); !ok {
		r, _ := json.Marshal(res)
		response = string(r)
	} else {
		switch action {
		case "startTesting":
			response = createResponse(action, "ok")
		case "logout":
			if sid, err := data["sid"].(string); err {
				response = logoutAction(sid)
			} else {
				response = createResponse(action, "badSid")
			}
		case "login":
			login, err1 := data["login"].(string)
			passwd, err2 := data["password"].(string)
			if err1 && err2 {
				response = loginAction(login, passwd)
			} else {
				response = createResponse(action, "invalidCredentials")
			}
		case "register":
			login, err1 := data["login"].(string)
			passwd, err2 := data["password"].(string)
			class, _ := data["class"].(string)
			if err1 && err2 {
				response = registerAction(login, passwd, class)
			} else if err1 {
				response = createResponse(action, "badLogin")
			} else if err2 {
				response = createResponse(action, "badPassword")
			}
		}
	}
	fmt.Fprintf(w, "%s", response)
}