Beispiel #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,
	})
}
Beispiel #2
0
func ShowHouse(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
	}
	house_info := models.GetHouseByName(name)
	log.Println(house_info)
	if house_info.Name == "" {
		http.Error(res, "Unknown house name", 500)
		return
	}
	character_list, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting character list:"+err.Error(), 500)
		return
	}
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	success := base.Session.GetFlashes("success")
	views.Parser.ExecuteTemplate(res, "house_view.html", &HouseViewResponse{
		house_info,
		success,
		character_list,
		csrf_token,
	})
}
Beispiel #3
0
func ProcessRanksUpdate(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", url.QueryEscape(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
	}
	level3 := req.PostFormValue("level3")
	level2 := req.PostFormValue("level2")
	level1 := req.PostFormValue("level1")
	if len(level1) > 15 || len(level2) > 15 || len(level3) > 15 {
		base.Session.SetFlash("Ranks cant have more than 15 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z-0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling RegExp at guild ranks", 500)
		return
	}
	if !rgxp.MatchString(level3) || !rgxp.MatchString(level2) || !rgxp.MatchString(level1) {
		base.Session.SetFlash("Ranks cant contain invalid characters only A-z and 0-9 are allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.UpdateGuildRanks(guild_info.Id, level3, level2, level1)
	if err != nil {
		http.Error(res, "Error while updating guild ranks", 500)
		return
	}
	base.Session.SetFlash("Guild ranks updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Beispiel #4
0
func ShowAdminAccountBan(res http.ResponseWriter, req *http.Request, base *BaseController) {
	// TODO: check if is already banned
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting character list: "+err.Error(), 500)
		return
	}
	views.Parser.ExecuteTemplate(res, "admin_account_ban.html", &AdminAccountBanResponse{
		characters,
	})
}
Beispiel #5
0
func ProcessMotdUpdate(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
	}
	motd := req.PostFormValue("motd")
	if len(motd) > 255 {
		base.Session.SetFlash("The MOTD cant be longer than 255 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z 0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling regexp", 500)
		return
	}
	if !rgxp.MatchString(motd) {
		base.Session.SetFlash("The MOTD contains invalid characters you can only use A-Z or 0-9", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	err = models.UpdateGuildMotd(guild_info.Id, motd)
	if err != nil {
		http.Error(res, "Error while updating guild MOTD", 500)
		return
	}
	base.Session.SetFlash("Guild MOTD updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Beispiel #6
0
func ShowAccountManage(res http.ResponseWriter, req *http.Request, base *BaseController) {
	character_list, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting character list", 500)
		log.Println(err)
		return
	}
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	r := &ManageResponse{"account-manage", base.Account, character_list, csrf_token}
	views.Parser.ExecuteTemplate(res, "manage.html", r)
}
Beispiel #7
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
}
Beispiel #8
0
func ShowGuilds(res http.ResponseWriter, req *http.Request, base *BaseController) {
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	character_list, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error getting character list", 500)
		return
	}
	guild_list, err := models.GetGuildList()
	if err != nil {
		log.Println(err)
		http.Error(res, "Error getting guild list", 500)
		return
	}
	r := &GuildsResponse{"guilds", csrf_token, base.Session.GetFlashes("error"), character_list, guild_list}
	views.Parser.ExecuteTemplate(res, "guilds.html", r)
}
Beispiel #9
0
func ShowAdminAccountView(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while getting account name: "+err.Error(), 500)
		return
	}
	account_info := models.GetAccountByName(name)
	account_characters, err := models.GetAccountCharacters(account_info.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters: "+err.Error(), 500)
		return
	}
	success := base.Session.GetFlashes("success")
	views.Parser.ExecuteTemplate(res, "admin_account_view.html", &AdminAccountViewResponse{
		account_info,
		account_characters,
		models.CheckAccountBanned(account_info.Id),
		success,
	})
}
Beispiel #10
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)
}
Beispiel #11
0
func ShowGuild(res http.ResponseWriter, req *http.Request, base *BaseController) {
	// TODO: guild invitations
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while getting guild name", 500)
		return
	}
	if !models.CheckGuildName(name) {
		base.Session.SetFlash("Guild doesnt exists", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	guild_info, err := models.GetGuildByName(name)
	if err != nil {
		http.Error(res, "Error while getting guild information", 500)
		return
	}
	character_list, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting character list", 500)
		return
	}
	owner := true
	if !checkAccountOwner(character_list, guild_info) {
		owner = false
	}
	invited := true
	invited_account_characters := getInvitedPlayers(character_list, guild_info)
	if len(invited_account_characters) == 0 {
		invited = false
	}
	r := &GuildViewResponse{"guilds", csrf_token, guild_info, base.Session.GetFlashes("error"), base.Session.GetFlashes("success"), owner, invited, invited_account_characters}
	views.Parser.ExecuteTemplate(res, "view_guild.html", r)
}
Beispiel #12
0
func ProcessLogoUpload(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
	}
	file_logo, _, err := req.FormFile("logo")
	if err != nil {
		http.Error(res, "Error while reading logo image", 500)
		return
	}
	defer file_logo.Close()
	logo_image, format, err := image.Decode(file_logo)
	if !checkValidImageFormat(format) {
		base.Session.SetFlash("Guild logo image format is not allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	if err != nil {
		http.Error(res, "Error while decoding logo image", 500)
		return
	}
	logo_out, err := os.Create(config.Parser.Style.Template + "/public/guilds/" + guild_info.Name + ".gif")
	if err != nil {
		http.Error(res, "Error while creating logo stdout", 500)
		return
	}
	defer logo_out.Close()
	resized_logo := resize.Resize(80, 80, logo_image, resize.Lanczos3)
	err = png.Encode(logo_out, resized_logo)
	if err != nil {
		http.Error(res, "Error while encoding logo image", 500)
		return
	}
	base.Session.SetFlash("Guild logo updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}