Esempio n. 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,
	})
}
Esempio n. 2
0
func ProcessCreateGuild(res http.ResponseWriter, req *http.Request, base *BaseController) {
	owner := req.PostFormValue("owner")
	name := req.PostFormValue("name")
	if len(name) > 20 || len(name) < 5 {
		base.Session.SetFlash("Guild name must be between 5 - 20 characters", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z]+$`)
	if err != nil {
		http.Error(res, "Error while compiling regexp at process create guild", 500)
		return
	}
	if !rgxp.MatchString(name) {
		base.Session.SetFlash("Guild name can only contain A - z characters", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if !models.CheckCharacterNameOfAccount(owner, base.Account.Id) {
		base.Session.SetFlash("Wrong character selected", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	owner_info := models.GetCharacterByName(owner)
	if models.CheckGuildName(name) {
		base.Session.SetFlash("Guild name already in use", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if models.CheckGuildOwner(owner_info.Id) {
		base.Session.SetFlash("Character already owns a guild", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if models.CheckCharacterInGuild(owner_info.Id) {
		base.Session.SetFlash("Character already in a guild", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	logo, err := ioutil.ReadFile(config.Parser.Style.Template + "/public/images/logo.gif")
	if err != nil {
		http.Error(res, "Error while creating reading base logo "+err.Error(), 500)
		return
	}
	guild_logo, err := os.Create(config.Parser.Style.Template + "/public/guilds/" + name + ".gif")
	if err != nil {
		http.Error(res, "Error while creating your guild logo "+err.Error(), 500)
		return
	}
	defer guild_logo.Close()
	guild_logo.Write(logo)
	err = models.CreateGuild(name, owner_info.Id)
	if err != nil {
		http.Error(res, "Error while creating your guild "+err.Error(), 500)
		return
	}
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Esempio n. 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
}
Esempio n. 4
0
func ShowPlayerEdit(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error processing name url query", 500)
		return
	}
	if !models.CheckCharacterNameOfAccount(name, base.Account.Id) || !models.CheckCharacterAlive(name) {
		http.Redirect(res, req, "/account/manage", 301)
		return
	}
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	character_info := models.GetCharacterByName(name)
	r := &EditCharacterResponse{"account-manage", character_info, csrf_token, base.Session.GetFlashes("error")}
	views.Parser.ExecuteTemplate(res, "edit_character.html", r)
}
Esempio n. 5
0
func ProcessGuildInviteJoin(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
	}
	player, err := url.QueryUnescape(mux.Vars(req)["player"])
	if err != nil {
		http.Error(res, "Error while getting player name", 500)
		return
	}
	player_info := models.GetCharacterByName(player)
	if player_info.Name == "" {
		http.Error(res, "Unknown player", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if guild_info.Name == "" {
		http.Error(res, "Unknown guild", 500)
		return
	}
	if !models.CheckCharacterNameOfAccount(player_info.Name, base.Account.Id) {
		http.Error(res, "Unkown character", 500)
		return
	}
	if !models.CheckGuildInvitation(player_info.Id, guild_info.Id) {
		http.Error(res, "Unkown guild invitation", 500)
		return
	}
	err, _ = models.DeleteGuildInvite(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while deleting guild invitation", 500)
		return
	}
	err = models.JoinGuild(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while joining guild: "+err.Error(), 500)
		return
	}
	base.Session.SetFlash("You joined the guild successfully", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Esempio n. 6
0
func ProcessGuildInviteRemove(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
	}
	player, err := url.QueryUnescape(mux.Vars(req)["player"])
	if err != nil {
		http.Error(res, "Error while getting player name", 500)
		return
	}
	player_info := models.GetCharacterByName(player)
	if player_info.Name == "" {
		http.Error(res, "Unknown player", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if guild_info.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
	}
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	err, _ = models.DeleteGuildInvite(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while deleting guild invitation", 500)
		return
	}
	base.Session.SetFlash("Removed guild invitation successfully!", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Esempio n. 7
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())
}