Esempio n. 1
0
func CharacterInventory(w http.ResponseWriter, r *http.Request) {
	var err error

	if !isTemplateLoaded {
		err = LoadTemplates()
		if err != nil {
			log.Println("Error loading templates:", err.Error())
			//TODO: exit properly
			return
		}
	}

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error loading Config:", err.Error())
		//TODO: exit properly
		return
	}

	db, err := database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		//TODO: exit properly
		return
	}
	defer db.Close()

	type Index struct {
		Name string
		*Site
		Character *character.CharacterOutput
	}

	resp := Index{
		Name: "Testing2",
		Site: SiteInit(),
		//Character: &character.CharacterOutput{}, //Name: "Testing!", Id: 123},
	}
	params := r.URL.Query()
	id := params.Get("cid")
	if len(id) == 0 {
		//log.Println("Empty ID!")

		id = "2"
	}

	resp.Character, err = character.GetById(db, id)

	if err != nil {
		log.Println("Error getting ID from DB:", err.Error())
		return
	}
	//resp := someStruct{}
	err = contentTemplate["character/inventory"].Execute(w, resp)
	if err != nil {
		log.Println("Error executing index", err.Error())
	}

}
Esempio n. 2
0
func ActionAdd(w http.ResponseWriter, r *http.Request) {
	log.Println("add!")
	var err error

	type Index struct {
		Status    int
		Message   string
		Inventory []*inventory.InventoryOutput
	}

	resp := Index{
		Status:  0,
		Message: "Unkown Error.",
	}
	resp.Message = "Add to character inventory"
	params, err := parseEventParameters(r)

	if err != nil {
		resp.Message = err.Error()
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding:", err.Error())
		}
		return
	}

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error getting config", err.Error())
		return
	}

	db, err := database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		//TODO: exit properly
		return
	}

	err = inventory.ActionAdd(db, params)
	if err != nil {
		resp.Message = "Failed to add action: " + err.Error()
		log.Println("Failed to add action:" + err.Error())
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	resp.Status = 1
	resp.Message = "Added action successfully."
	//resp.Message = fmt.Sprintf("There are %d inventory items.", len(inv))
	err = json.NewEncoder(w).Encode(resp)
	if err != nil {
		log.Println("Error requesting RestIndex:", err.Error())
	}
}
Esempio n. 3
0
func Start(addr string) (err error) {

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error loading config:", err.Error())
		fmt.Println("Press enter to exit.")
		exit := ""
		fmt.Scan(&exit)
		os.Exit(1)
	}
	//Do a quick DB test
	_, err = database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		fmt.Println("Press enter to exit.")
		exit := ""
		fmt.Scan(&exit)
		os.Exit(1)
	}

	template.LoadTemplates()
	//r := pat.New()

	http.HandleFunc("/", Index)
	http.HandleFunc("/item/", template.ItemIndex)
	http.HandleFunc("/character/", template.CharacterSearch)
	http.HandleFunc("/character/search/", template.CharacterSearch)
	http.HandleFunc("/character/inventory/", template.CharacterInventory)

	http.HandleFunc("/map/editor/", template.MapEditor)

	http.HandleFunc("/item/editor/", template.ItemEditor)
	http.HandleFunc("/rest/", rest.Index)
	http.HandleFunc("/rest/item/getbyid", rest.ItemGetById)
	http.HandleFunc("/rest/item/searchbyname", rest.ItemSearchByName)
	http.HandleFunc("/rest/inventory/getbycharacterid", rest.InventoryGetByCharacterId)
	http.HandleFunc("/rest/inventory/add", inventory.ActionAdd)
	http.HandleFunc("/rest/inventory/move", inventory.ActionMove)
	http.HandleFunc("/rest/inventory/remove", inventory.ActionRemove)
	http.HandleFunc("/rest/inventory/update", inventory.ActionUpdate)

	http.HandleFunc("/rest/map/getbyshortname/", rest.MapGetByShortname)

	//http.Handle("/", r)
	log.Println("Started Web Server on", addr)
	go openBrowser(addr)
	err = http.ListenAndServe(addr, nil)
	fmt.Println("Press enter to exit.")
	exit := ""
	fmt.Scan(&exit)
	os.Exit(1)
	return
}
Esempio n. 4
0
func CharacterSearch(w http.ResponseWriter, r *http.Request) {

	type Index struct {
		Name string
		*Site
		Characters      []*character.CharacterOutput
		LastSearchQuery string
	}

	resp := Index{
		Name: "Testing2",
		Site: SiteInit(),
		//Character: &character.CharacterOutput{}, //Name: "Testing!", Id: 123},

	}

	params := r.URL.Query()
	name := params.Get("name")
	resp.LastSearchQuery = name

	if len(name) > 0 {

		config, err := eqemuconfig.GetConfig()
		if err != nil {
			log.Println("Error loading Config:", err.Error())
			//TODO: exit properly
			return
		}

		db, err := database.Connect(config)
		if err != nil {
			log.Println("Error connecting to DB:", err.Error())
			//TODO: exit properly
			return
		}
		defer db.Close()

		characters, err := character.FindAllByName(db, name)
		if err != nil {
			log.Println("Error finding all names", err.Error())
			return
		}
		resp.Characters = characters

	}
	err := contentTemplate["character/search"].Execute(w, resp)
	if err != nil {
		log.Println("Error executing search", err.Error())
	}
	return
}
Esempio n. 5
0
func MapGetByShortname(w http.ResponseWriter, r *http.Request) {
	var err error

	type Index struct {
		*Site
		Lines []Line
		Mobs  []Mob
		Grids []Grid
	}

	resp := Index{
		Site: SiteInit(),
	}

	if r.Method != "POST" {
		resp.Message = "Only POST methods are accepted."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	name := r.FormValue("name")
	if len(name) == 0 {
		resp.Message = "Post value of name required."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding:", err.Error())
		}
		return
	}

	//bMap, err := ioutil.ReadFile(fmt.Sprintf("webserver/rest/map/%s_1.txt", name))

	bMap, err := Asset(fmt.Sprintf("rest/map/%s_1.txt", name))
	if err != nil {
		resp.Message = "Error finding map: " + err.Error()
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error decoding json:", err.Error())
		}
		return
	}

	reader := csv.NewReader(strings.NewReader(string(bMap)))
	records, err := reader.ReadAll()
	if err != nil {
		log.Fatal(err)
	}

	//iterate each entry
	for _, record := range records {

		entries := strings.Split(record[0], " ")
		drawType := entries[0]
		if drawType == "L" {
			line := Line{}
			line.X1, err = strconv.ParseFloat(strings.TrimSpace(entries[1]), 64) //entries[1]), 64)
			line.Y1, err = strconv.ParseFloat(strings.TrimSpace(record[1]), 64)
			line.X2, err = strconv.ParseFloat(strings.TrimSpace(record[3]), 64)
			line.Y2, err = strconv.ParseFloat(strings.TrimSpace(record[4]), 64)

			line.X1 += 2000
			line.Y1 += 2000
			line.X2 += 2000
			line.Y2 += 2000

			line.X1 /= 5
			line.Y1 /= 5
			line.X2 /= 5
			line.Y2 /= 5
			//outData += fmt.Sprintf("svg.line(g, %f, %f, %f, %f);\n", line.x1, line.y1, line.x2, line.y2)
			resp.Lines = append(resp.Lines, line)
		}
	}

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error getting config", err.Error())
		return
	}

	db, err := database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		//TODO: exit properly
		return
	}

	npcs, err := spawn.GetSpawnsByZone(db, name)
	if err != nil {
		log.Println("Error getting npcs:", err.Error())
	}

	for _, npc := range npcs {
		mob := Mob{
			X:        -npc.X,
			Y:        -npc.Y,
			Pathgrid: npc.Pathgrid,
		}
		mob.X += 2000
		mob.Y += 2000
		mob.X /= 5
		mob.Y /= 5
		resp.Mobs = append(resp.Mobs, mob)
	}

	grids, err := grid.GetGridsByZone(db, name)
	if err != nil {
		log.Println("Error getting grids:", err.Error())
	}

	lastIndex := 0
	gridPack := Grid{}

	for _, grid := range grids {
		if lastIndex != grid.Gridid {
			resp.Grids = append(resp.Grids, gridPack)
			lastIndex = grid.Gridid
			gridPack = Grid{}
			gridPack.Index = grid.Gridid
		}

		newEntry := GridEntry{
			X:     -grid.X,
			Y:     -grid.Y,
			Z:     grid.Z,
			Pause: grid.Pause,
		}
		//newEntry.X += 2000
		//newEntry.Y += 2000
		newEntry.X /= 50
		newEntry.Y /= 50

		gridPack.Entries = append(gridPack.Entries, newEntry)
	}
	if len(grids) > 0 {
		resp.Grids = append(resp.Grids, gridPack)
	}

	//log.Println(resp.Grids)

	resp.Status = 1
	resp.Message = "Here's the map"
	err = json.NewEncoder(w).Encode(resp)
	if err != nil {
		log.Println("Error requesting RestIndex:", err.Error())
	}
}
Esempio n. 6
0
func ItemSearchByName(w http.ResponseWriter, r *http.Request) {
	var err error

	type Index struct {
		*Site
		Items []*item.ItemOutput
	}

	resp := Index{
		Site: SiteInit(),
	}
	resp.Message = "Search for item by name"

	if r.Method != "POST" {
		resp.Message = "Only POST methods are accepted."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	name := r.FormValue("name")
	if len(name) == 0 {
		resp.Message = "Post value of name required."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding:", err.Error())
		}
		return
	}

	strOffset := r.FormValue("offset")
	offset, err := strconv.ParseInt(strOffset, 10, 64)
	if err != nil {
		offset = 0
	}
	if offset < 0 {
		offset = 0
	}
	if offset > 1000 {
		offset = 1000
	}

	strLimit := r.FormValue("limit")
	limit, err := strconv.ParseInt(strLimit, 10, 64)
	if err != nil {
		limit = 0
	}
	if limit < 0 {
		limit = 0
	}
	if limit > 30 {
		limit = 30
	}

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error getting config", err.Error())
		return
	}

	db, err := database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		//TODO: exit properly
		return
	}

	items, err := item.SearchByName(db, name, limit, offset)
	if err != nil {
		resp.Message = "Failed to get inventory: " + err.Error()
		log.Println("Failed to get inventory:" + err.Error())
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	resp.Items = items
	resp.Status = 1
	resp.Message = fmt.Sprintf("There are %d items.", len(items))
	err = json.NewEncoder(w).Encode(resp)
	if err != nil {
		log.Println("Error requesting RestIndex:", err.Error())
	}
}
Esempio n. 7
0
func InventoryGetByCharacterId(w http.ResponseWriter, r *http.Request) {
	var err error

	type Index struct {
		*Site
		Inventory []*inventory.InventoryOutput
	}

	resp := Index{
		Site: SiteInit(),
	}
	resp.Message = "Get inventory by character ID"

	if r.Method != "POST" {
		resp.Message = "Only POST methods are accepted."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	id := r.FormValue("id")
	if len(id) == 0 {
		resp.Message = "Post value of id required."
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding:", err.Error())
		}
		return
	}

	config, err := eqemuconfig.GetConfig()
	if err != nil {
		log.Println("Error getting config", err.Error())
		return
	}

	db, err := database.Connect(config)
	if err != nil {
		log.Println("Error connecting to DB:", err.Error())
		//TODO: exit properly
		return
	}

	inv, err := inventory.GetByCharacterId(db, id)
	if err != nil {
		resp.Message = "Failed to get inventory: " + err.Error()
		log.Println("Failed to get inventory:" + err.Error())
		err = json.NewEncoder(w).Encode(resp)
		if err != nil {
			log.Println("Error responding post fail:", err.Error())
		}
		return
	}

	resp.Inventory = inv
	resp.Status = 1
	resp.Message = fmt.Sprintf("There are %d inventory items.", len(inv))
	err = json.NewEncoder(w).Encode(resp)
	if err != nil {
		log.Println("Error requesting RestIndex:", err.Error())
	}
}