示例#1
0
func ShowCharacter(res http.ResponseWriter, req *http.Request, base *BaseController) {
	logged_bool := CheckIfLogged(base)
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error getting character name", 500)
		return
	}
	if !models.CheckCharacterName(name) {
		http.Redirect(res, req, "/", 301)
		return
	}
	character_info := models.GetCharacterByName(name)
	character_list, err := models.GetAccountCharacters(character_info.Account_id)
	if err != nil {
		http.Error(res, "Error getting character list", 500)
		return
	}
	character_deaths, err := models.GetCharacterDeaths(name)
	if err != nil {
		http.Error(res, "Error getting character deaths", 500)
		return
	}
	views.Parser.ExecuteTemplate(res, "character_view.html", &CharacterviewResponse{
		"character_view",
		logged_bool,
		character_info,
		character_list,
		character_deaths,
	})
}
示例#2
0
文件: home.go 项目: Cloakaac/core
func ProcessCharacterCreate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	if models.GetAccountCharactersCount(base.Account.Id) >= config.Parser.Character.Max {
		base.Session.SetFlash("You cant have more than "+strconv.Itoa(config.Parser.Character.Max)+" characters!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	captcha_response, err := captcha.Verify(req.PostFormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error checking captcha response", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	name := req.PostFormValue("name")
	sex := SexToInt(req.PostFormValue("sex"))
	vocation := VocationToInt(req.PostFormValue("vocation"))
	town_id := models.CheckTownExists(req.PostFormValue("town"))
	if town_id == -1 {
		base.Session.SetFlash("Unkown town name", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	if len(name) <= 5 {
		base.Session.SetFlash("This name is too short. Please choose another name!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z]+$`)
	if err != nil {
		http.Error(res, "Error compiling RegExp at register", 500)
		return
	}
	if !rgxp.MatchString(name) {
		base.Session.SetFlash("This name cannot be used because it contains a forbidden word or combination of letters. Please choose another name!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	if models.CheckCharacterName(name) {
		base.Session.SetFlash("This name is already used. Please choose another name", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	err = models.CreateCharacter(name, sex, vocation, base.Account.Id, town_id)
	if err != nil {
		http.Error(res, "Error while creating your character: "+err.Error(), 500)
		return
	}
	views.Parser.ExecuteTemplate(res, "create_character_success.html", &CreateCharacterSuccessResponse{"account-manage", name, config.Parser.Spawn.Name})
}
示例#3
0
func ProcessGuildInvite(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	player_name := req.PostFormValue("player")
	if !models.CheckCharacterName(player_name) {
		base.Session.SetFlash("Player not found", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	player_info := models.GetCharacterByName(player_name)
	if models.CheckCharacterInGuild(player_info.Id) {
		base.Session.SetFlash("Player is already in a guild", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.CreateGuildInvite(guild_info.Id, player_info.Id)
	if err != nil {
		log.Println(err)
		http.Error(res, "Error while inviting player", 301)
		return
	}
	base.Session.SetFlash("Player invited to the guild", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
	return
}
示例#4
0
func ShowCharacterSignature(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error getting character name", 500)
		return
	}
	if !models.CheckCharacterName(name) {
		http.Redirect(res, req, "/", 301)
		return
	}
	character_info := models.GetCharacterByName(name)
	signature_handler, err := os.Open(config.Parser.Style.Template + "/public/signatures/" + character_info.Name + ".png")
	if err == nil {
		signature_stats, err := signature_handler.Stat()
		if err != nil {
			http.Error(res, "Error while reading signature stats", 500)
			return
		}
		if signature_stats.ModTime().Unix()+(1*60) > time.Now().Unix() {
			signature_out, err := ioutil.ReadAll(signature_handler)
			if err != nil {
				http.Error(res, "Error while reading signature bytes", 500)
				return
			}
			res.Header().Set("Content-type", "image/png")
			res.Write(signature_out)
			return
		}
	}
	defer signature_handler.Close()
	file, err := os.Open(config.Parser.Style.Template + "/public/images/signature.png")
	if err != nil {
		http.Error(res, "Error while opening signature image", 500)
		return
	}
	rgba := image.NewRGBA(image.Rect(0, 0, 495, 134))
	signature, _, err := image.Decode(file)
	if err != nil {
		http.Error(res, "Error while decoding signature image", 500)
		return
	}
	draw.Draw(rgba, rgba.Bounds(), signature, image.ZP, draw.Src)
	font_bytes, err := ioutil.ReadFile(config.Parser.Style.Template + "/public/fonts/Aller_Bd.ttf")
	if err != nil {
		log.Println(err)
		http.Error(res, "Error while opening signature font", 500)
		return
	}
	font, err := freetype.ParseFont(font_bytes)
	if err != nil {
		log.Println(err)
		http.Error(res, "Error while processing font file", 500)
		return
	}
	text_drawer := freetype.NewContext()
	text_drawer.SetDPI(72)
	text_drawer.SetFont(font)
	text_drawer.SetFontSize(14)
	text_drawer.SetClip(rgba.Bounds())
	text_drawer.SetDst(rgba)
	text_drawer_strings := []*SignatureDraw{
		&SignatureDraw{
			"Name: " + character_info.Name,
			image.Black,
			freetype.Pt(20, 30),
		},
		&SignatureDraw{
			"Sex: " + character_info.Sex_str,
			image.NewUniform(color.RGBA{0, 0, 0, 255}),
			freetype.Pt(20, 50),
		},
		&SignatureDraw{
			"Vocation: " + character_info.Vocation_str,
			image.NewUniform(color.RGBA{0, 0, 0, 255}),
			freetype.Pt(20, 70),
		},
		&SignatureDraw{
			"Level: " + strconv.Itoa(character_info.Level),
			image.NewUniform(color.RGBA{0, 0, 0, 255}),
			freetype.Pt(20, 90),
		},
		&SignatureDraw{
			"Last login: "******"Error while drawing text", 500)
			return
		}
	}
	buffer_signature := bytes.Buffer{}
	b := bufio.NewWriter(&buffer_signature)
	err = png.Encode(b, rgba)
	if err != nil {
		http.Error(res, "Error while encoding PNG file", 500)
		return
	}
	err = b.Flush()
	if err != nil {
		http.Error(res, "Error while writing the PNG file", 500)
		return
	}
	signature_out, err := os.Create(config.Parser.Style.Template + "/public/signatures/" + character_info.Name + ".png")
	if err != nil {
		http.Error(res, "Error while saving signature", 500)
		return
	}
	defer signature_out.Close()
	signature_out.Write(buffer_signature.Bytes())
	res.Header().Set("Content-type", "image/png")
	res.Write(buffer_signature.Bytes())
}