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)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } 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) 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 *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } 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) moveAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("move", "ok") p := g.players.getPlayerBySession(json["sid"].(string)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } p.SetDir(consts.NameDirMapping[json["direction"].(string)]) return res }
func (g *Game) setUpConstants(json consts.JsonType) consts.JsonType { res := utils.JsonAction("setUpConst", "badAction") if *consts.TEST && consts.TEST_MODE { var isValidConsts bool = true for name, _ := range consts.NameConstMapping { if json[name] != nil { _, ok := json[name].(float64) isValidConsts = isValidConsts && ok } } if isValidConsts { for name, constant := range consts.NameConstMapping { if json[name] != nil { if v, ok := constant.(*float64); ok { *v = json[name].(float64) } else if v, ok := constant.(*int); ok { *v = int(json[name].(float64)) } } } consts.Refresh() res["result"] = "ok" } } return res }
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) 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) lookAction(sid string) consts.JsonType { res := utils.JsonAction("look", "ok") player := g.players.getPlayerBySession(sid) if player == nil { return nil } visibleSpaceSide := 2*(consts.VISION_RADIUS) + 1 // check this plz //visibleSpaceSide := 2 * (consts.VISION_RADIUS + 1) px, py := int(player.Center.X), int(player.Center.Y) visibleSpace := make([][]string, visibleSpaceSide) visibleObjects := make([]consts.JsonType, 0, 1000) var addedObjects = map[int64]bool{player.GetID(): true} for i := 0; i < visibleSpaceSide; i++ { visibleSpace[i] = make([]string, visibleSpaceSide) for j := 0; j < visibleSpaceSide; j++ { fx, fy := px-consts.VISION_RADIUS+j, py-consts.VISION_RADIUS+i if fx > 0 && fy > 0 && fx < g.field.Width && fy < g.field.Height { visibleSpace[i][j] = string(g.field.GetBackground(fx, fy)) for id, obj := range g.field.GetObjects(fx, fy) { if !addedObjects[id] { visibleObjects = append(visibleObjects, obj.GetInfo()) addedObjects[id] = true } } } else { visibleSpace[i][j] = string(consts.WALL_SYMBOL) } } } /*l := int(math.Max(0.0, float64(px - consts.VISION_RADIUS))) r := int(math.Min(float64(g.field.Width - 1), float64(px + consts.VISION_RADIUS))) t := int(math.Max(0.0, float64(py - consts.VISION_RADIUS))) b := int(math.Min(float64(g.field.Height - 1), float64(py + consts.VISION_RADIUS))) for i := t; i <= b; i++ { for j := l; j <= r; j++ { visibleSpace[i - t][j - l] = string(g.field.GetBackground(j, i)) } }*/ res["map"] = visibleSpace /*visibleObjects := make([]consts.JsonType, 0, 1000) var addedObjects = map[int64] bool {player.GetID() : true} for i := t; i <= b; i++ { for j := l; j <= r; j++ { for id, obj := range g.field.GetObjects(j, i) { if !addedObjects[id] { visibleObjects = append(visibleObjects, obj.GetInfo()) addedObjects[id] = true } } } }*/ res["actors"] = visibleObjects res["x"] = player.Center.X res["y"] = player.Center.Y res["health"] = player.GetHP() res["cur_exp"] = player.GetExp() res["max_exp"] = player.GetMaxExp() return res }
func (g *Game) startTesting() consts.JsonType { res := utils.JsonAction("startTesting", "badAction") if *consts.TEST && !consts.TEST_MODE { consts.TEST_MODE = true res["result"] = "ok" } return res }
func (g *Game) useSkillAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("useSkill", "badPoint") x, ok1 := json["x"].(float64) y, ok2 := json["y"].(float64) if ok1 && ok2 { p := g.players.getPlayerBySession(json["sid"].(string)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } start := p.GetCenter() damage := p.GetCharacteristic(consts.CHARACTERISTIC_INTELLEGENCE) * consts.FIREBALL_DAMAGE_MULTIPLIER g.projectileManager.NewFireBallProjectile(&start, geometry.MakePoint(x, y), damage, consts.FIREBALL_RADIUS, p) res["result"] = "ok" } return res }
func (g *Game) doPlayersAction(action string, json consts.JsonType) consts.JsonType { res := utils.JsonAction(action, "badSid") sid, ok := utils.GetSidFromJson(json) if ok { switch action { case "move": res = g.moveAction(json) case "use": res = g.useAction(json) case "getDictionary": res = g.getDictionaryAction() case "look": res = g.lookAction(sid) case "examine": res = g.examineAction(json) case "startTesting": res = g.startTesting() case "stopTesting": res = g.stopTesting(sid) case "setUpMap": res = g.setUpMap(json) case "pickUp": res = g.pickUpItem(json) case "drop": res = g.dropItem(json) case "destroyItem": res = g.destroyItem(json) case "equip": res = g.equipItem(json) case "unequip": res = g.unequipItem(json) case "moveItem": res = g.moveItem(json) case "getConst": res = g.getConstants() case "setUpConst": res = g.setUpConstants(json) case "putMob": res = g.putMob(json) case "putPlayer": res = g.putPlayer(json) case "putItem": res = g.putItem(json) case "enforce": res = g.enforceAction(json) case "setLocation": res = g.setLocationAction(json) case "useSkill": res = g.useSkillAction(json) case "revive": res = g.reviveAction(json) default: res = g.badAction(action) } } return res }
func (g *Game) getDictionaryAction() consts.JsonType { dict := make(consts.JsonType) for k, v := range g.dictionary { dict[k] = v } res := utils.JsonAction("getDictionary", "ok") res["dictionary"] = dict 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) getConstants() consts.JsonType { res := utils.JsonAction("getConst", "badAction") if *consts.TEST && consts.TEST_MODE { for name, val := range consts.NameConstMapping { res[name] = val } res["result"] = "ok" } 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) reviveAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("revive", "badAction") p := g.players.getPlayerBySession(json["sid"].(string)) c := p.GetCenter() if *consts.FEATURE { if p.Killed() { res["result"] = "ok" g.field.UnlinkFromCells(p) x, y := g.newPlayerPos(c.X, c.Y) p.SetRevivePoint(x, y) } } return res }
func (g *Game) unequipItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("unequip", "badSlot") slotParam := json["slot"] if slotParam != nil { slot, isExistSlot := consts.NameSlotMapping[slotParam.(string)] if isExistSlot { p := g.players.getPlayerBySession(json["sid"].(string)) if *consts.FEATURE { if p.Killed() { return utils.JsonAction(res["action"].(string), "killed") } } isUnequip, slots := p.Unequip(slot) if isUnequip { res["result"] = "ok" if slots != nil { res["slots"] = slots } } } } return res }
func (g *Game) moveItem(json consts.JsonType) consts.JsonType { res := utils.JsonAction("moveItem", "badId") idParam := json["id"] cellParam := json["cell"] if cellParam == nil { res["result"] = "badCell" } else if idParam != nil { cell := int(cellParam.(float64)) p := g.players.getPlayerBySession(json["sid"].(string)) if p.MoveItem(p.Inventory.GetItem(int64(idParam.(float64))), cell) { res["result"] = "ok" } } return res }
func (g *Game) enforceAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("enforce", "badAction") if *consts.TEST && consts.TEST_MODE { enforcedAction, ok := json["enforcedAction"].(map[string]interface{}) if !ok { res["result"] = "badEnforcedAction" } else { res["result"] = "ok" action, ok := enforcedAction["action"].(string) if !ok { action = "" } res["actionResult"] = g.doPlayersAction(action, enforcedAction) } } 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 }
func (g *Game) stopTesting(sid string) consts.JsonType { res := utils.JsonAction("stopTesting", "badAction") if *consts.TEST && consts.TEST_MODE { for name, constant := range consts.NameConstMapping { if v, ok := constant.(*float64); ok { *v = consts.NameDefaultConstMapping[name].(float64) } else if v, ok := constant.(*int); ok { *v = consts.NameDefaultConstMapping[name].(int) } else { return res } } consts.TEST_MODE = false consts.SetDefaultConstantsValues() g.clearObjects(sid) g.field.Clear() res["result"] = "ok" } return res }
func (g *Game) setUpMap(json consts.JsonType) consts.JsonType { res := utils.JsonAction("setUpMap", "badAction") sid := json["sid"].(string) loadingFailed := func() consts.JsonType { res["result"] = "badMap" return res } if *consts.TEST && consts.TEST_MODE { var mapStrs []string data, ok := json["map"].([]interface{}) if ok { for _, arr := range data { var str string chars, ok := arr.([]interface{}) if ok { for _, el := range chars { char, ok := el.(string) if ok && g.inDictionary(char) { str += char } else { return loadingFailed() } } } else { return loadingFailed() } mapStrs = append(mapStrs, str) } } else { return loadingFailed() } if !g.field.LoadFromStrings(mapStrs) { return loadingFailed() } g.clearObjects(sid) res["result"] = "ok" } return res }
func (g *Game) lookAction(sid string) consts.JsonType { res := utils.JsonAction("look", "ok") player := g.players.getPlayerBySession(sid) if player == nil { return nil } YCount := consts.SCREEN_ROW_COUNT XCount := consts.SCREEN_COLUMN_COUNT YRad := (YCount - 1) / 2 XRad := (XCount - 1) / 2 px, py := int(player.Center.X), int(player.Center.Y) visibleSpace := make([][]string, YCount) visibleObjects := make([]consts.JsonType, 0, 1000) var addedObjects = map[int64]bool{player.GetID(): true} for i := 0; i < YCount; i++ { visibleSpace[i] = make([]string, XCount) for j := 0; j < XCount; j++ { fx, fy := px-XRad+j, py-YRad+i if fx > 0 && fy > 0 && fx < g.field.Width && fy < g.field.Height { visibleSpace[i][j] = string(g.field.GetBackground(fx, fy)) for id, obj := range g.field.GetObjects(fx, fy) { if !addedObjects[id] { visibleObjects = append(visibleObjects, obj.GetInfo()) addedObjects[id] = true } } } else { visibleSpace[i][j] = string(consts.WALL_SYMBOL) } } } res["map"] = visibleSpace res["actors"] = visibleObjects res["x"] = player.Center.X res["y"] = player.Center.Y res["health"] = player.GetHP() res["cur_exp"] = player.GetExp() res["max_exp"] = player.GetMaxExp() return res }
func (g *Game) moveAction(json consts.JsonType) consts.JsonType { res := utils.JsonAction("move", "ok") p := g.players.getPlayerBySession(json["sid"].(string)) p.SetDir(consts.NameDirMapping[json["direction"].(string)]) return res }
func (g *Game) badAction(action string) consts.JsonType { return utils.JsonAction(action, "badAction") }
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 }
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) }