func settlePlayerDebt(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])
	dUUID, err := uuid.FromString(c.URLParams["debtuuid"])

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	debt, err := player.DebtByUUID(dUUID)
	if err != nil {
		return &appError{err, "Cant find debt for player", 404}
	}

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != debt.Creditor {
		return &appError{errors.New("Unauthorized"), "Must be creditor or admin to settle debt", 403}
	}

	err = player.SettleDebt(dUUID)
	if err != nil {
		return &appError{err, "Failed to settle debt", 500}
	}
	w.Header().Set("Location", "/players/"+pUUID.String()+"/debts")
	w.WriteHeader(204)
	return nil
}
func updateCateringVote(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	cateringuuid, err := uuid.FromString(c.URLParams["uuid"])
	playeruuid, err := uuid.FromString(c.URLParams["playeruuid"])
	catering, err := caterings.CateringByUUID(cateringuuid)
	if err != nil {
		return &appError{err, "Cant find catering", 404}
	}

	tempInfo := new(caterings.Vote)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempInfo); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := catering.RemoveVote(playeruuid); err != nil {
		return &appError{err, "Failed to remove old catering vote when updating", 500}
	}

	if err := catering.AddVote(playeruuid, tempInfo.Score); err != nil {
		return &appError{err, "Failed to add updated catering vote", 500}
	}
	w.WriteHeader(204)
	return nil
}
func updatePlayer(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	player, err := players.PlayerByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}
	tempPlayer := new(players.Player)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempPlayer); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := player.SetActive(tempPlayer.Active); err != nil {
		return &appError{err, "Failed to set active status", 500}
	}
	if err := player.SetNick(tempPlayer.Nick); err != nil {
		return &appError{err, "Failed to set nick", 500}
	}
	if err := player.SetProfile(tempPlayer.Profile); err != nil {
		return &appError{err, "Failed to set player profile", 500}
	}
	w.WriteHeader(204)
	return nil
}
func addPlayerQuote(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	var q string
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&q); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) == pUUID {
		return &appError{errors.New("Unauthorized"), "Must be other player or admin to add quote", 403}
	}

	err = player.AddQuote(q)
	if err != nil {
		return &appError{err, "Failed to add quote", 500}
	}
	w.Header().Set("Location", "/players/"+pUUID.String())
	w.WriteHeader(201)
	return nil
}
func setUserPassword(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != pUUID {
		return &appError{errors.New("Unauthorized"), "Must be correct user or admin to set password", 403}
	}

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	type PWUpdate struct {
		Password string
	}

	pwupdate := new(PWUpdate)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(pwupdate); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := player.SetUserPassword(pwupdate.Password); err != nil {
		return &appError{err, "Failed to set password for player", 500}
	}
	w.WriteHeader(204)
	return nil
}
func setUserForPlayer(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	if !c.Env["authIsAdmin"].(bool) {
		return &appError{errors.New("Unauthorized"), "Admins only", 403}
	}
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	player, err := players.PlayerByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}
	tempUser := new(players.User)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempUser); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}
	user, err := players.UserByName(tempUser.Username)
	if err != nil {
		return &appError{err, "Cant find user", 400}
	}

	if err := player.SetUser(*user); err != nil {
		return &appError{err, "Failed to set user for player", 500}
	}
	w.WriteHeader(204)
	return nil
}
func addPlayerDebt(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	nDebt := new(players.Debt)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(nDebt); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !c.Env["authIsAdmin"].(bool) &&
		(c.Env["authPlayer"].(uuid.UUID) == pUUID ||
			c.Env["authPlayer"].(uuid.UUID) != nDebt.Creditor) {
		return &appError{errors.New("Unauthorized"), "Must be creditor or admin to add debt", 403}
	}

	err = player.AddDebt(*nDebt)
	if err != nil {
		return &appError{err, "Failed to add debt", 500}
	}
	w.Header().Set("Location", "/players/"+pUUID.String()+"/debts")
	w.WriteHeader(201)
	return nil
}
func setUserAdmin(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	if !c.Env["authIsAdmin"].(bool) {
		return &appError{errors.New("Unauthorized"), "Must be admin to set admin status", 403}
	}

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	var adminState bool
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&adminState); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := player.SetUserAdmin(adminState); err != nil {
		return &appError{err, "Failed to change settings for user", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #9
0
// Handles the fetch of a particular thought from the map for now.
func thoughtsGetHandler(rw http.ResponseWriter, r *http.Request) {

	log.Info("Received a call to fetch the thoughts")

	vars := mux.Vars(r)
	idString := vars["id"]
	id, err := uuid.FromString(idString)

	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("Invalid url param.")

		httpError(rw, http.StatusBadRequest, err)
		return
	}

	thought, err := tp.ThoughtStorage.GetThought(ThoughtsID{id})
	if err != nil {
		switch err {
		case ErrNoRecordFound:
			httpError(rw, http.StatusNotFound, err)
		default:
			httpError(rw, http.StatusInternalServerError, err)
		}
		return
	}

	resp, _ := json.Marshal(thought)

	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(http.StatusOK)
	rw.Write(resp)
	return
}
func setPlayerGossip(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	nGossip := make(map[string]string)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&nGossip); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != pUUID {
		return &appError{errors.New("Unauthorized"), "Must be player or admin to set gossip", 403}
	}

	err = player.SetGossip(nGossip)
	if err != nil {
		return &appError{err, "Failed to set gossip", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #11
0
func getNewsItem(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	newsItem, err := news.NewsItemByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find the NewsItem", 404}
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(newsItem)
	return nil
}
func getTournament(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(tournament)
	return nil
}
func removeTournamentNoShow(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	tID, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(tID)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}

	pID, err := uuid.FromString(c.URLParams["playeruuid"])

	if !c.Env["authIsAdmin"].(bool) && pID != c.Env["authPlayer"].(uuid.UUID) {
		return &appError{errors.New("Unauthorized"), "Must be given player or admin to remove absentee", 403}
	}

	if err := tournament.RemoveNoShow(pID); err != nil {
		return &appError{err, "Failed to remove absentee for tournament", 500}
	}
	w.WriteHeader(204)
	return nil
}
func getPlayerProfile(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	player, err := players.PlayerByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}
	encoder := json.NewEncoder(w)
	encoder.Encode(player.Profile)
	return nil
}
func showPlayerDebt(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != pUUID {
		return &appError{errors.New("Unauthorized"), "Must be player or admin to show debt", 403}
	}

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	encoder := json.NewEncoder(w)
	encoder.Encode(player.Debts)
	return nil
}
Beispiel #16
0
func (rls *RedisLocationStorage) LoadAll() ([]*Location, error) {
	var locations []*Location
	conn := rls.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", "locations"))
	if err != nil {
		return nil, err
	}
	for _, location := range b {
		uuid, _ := uuid.FromString(location)
		l, err := rls.Load(uuid)
		if err != nil {
			return nil, err
		}
		locations = append(locations, l)
	}
	return locations, nil
}
Beispiel #17
0
func (rnis *RedisNewsItemStorage) LoadAll() ([]*NewsItem, error) {
	var newsitems []*NewsItem
	conn := rnis.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", "newsitems"))
	if err != nil {
		return nil, err
	}
	for _, newsitem := range b {
		uuid, _ := uuid.FromString(newsitem)
		c, err := rnis.Load(uuid)
		if err != nil {
			return nil, err
		}
		newsitems = append(newsitems, c)
	}
	return newsitems, nil
}
Beispiel #18
0
func (rts *RedisTournamentStorage) LoadBySeason(season int) (Tournaments, error) {
	var tournaments Tournaments
	conn := rts.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", fmt.Sprintf("season:%d:tournaments", season)))
	if err != nil {
		return nil, err
	}
	for _, tournament := range b {
		uuid, _ := uuid.FromString(tournament)
		t, err := rts.Load(uuid)
		if err != nil {
			return nil, err
		}
		tournaments = append(tournaments, t)
	}
	return tournaments, nil
}
Beispiel #19
0
func (rcs *RedisCateringStorage) LoadAll() ([]*Catering, error) {
	var caterings []*Catering
	conn := rcs.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", "caterings"))
	if err != nil {
		return nil, err
	}
	for _, catering := range b {
		uuid, _ := uuid.FromString(catering)
		c, err := rcs.Load(uuid)
		if err != nil {
			return nil, err
		}
		caterings = append(caterings, c)
	}
	return caterings, nil
}
Beispiel #20
0
func (rps *RedisPlayerStorage) LoadAll() ([]*Player, error) {
	var players []*Player
	conn := rps.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", "players"))
	if err != nil {
		return nil, err
	}
	for _, player := range b {
		uuid, _ := uuid.FromString(player)
		p, err := rps.Load(uuid)
		if err != nil {
			return nil, err
		}
		players = append(players, p)
	}
	return players, nil
}
Beispiel #21
0
func (rts *RedisTournamentStorage) LoadAll() (Tournaments, error) {
	var tournaments Tournaments
	conn := rts.pool.Get()
	defer conn.Close()
	b, err := redigo.Strings(conn.Do("SMEMBERS", "tournaments"))
	if err != nil {
		return nil, err
	}
	for _, tournament := range b {
		uuid, _ := uuid.FromString(tournament)
		t, err := rts.Load(uuid)
		if err != nil {
			return nil, err
		}
		tournaments = append(tournaments, t)
	}
	return tournaments, nil
}
func updateTournamentInfo(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}
	tempInfo := new(tournaments.Info)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempInfo); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := tournament.UpdateInfo(*tempInfo); err != nil {
		return &appError{err, "Failed to update tournament info", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #23
0
func (rps *RedisPlayerStorage) LoadUser(username string) (*User, error) {
	conn := rps.pool.Get()
	defer conn.Close()
	player, err := redigo.String(conn.Do("GET", fmt.Sprintf("user:%s:player", username)))
	if err != nil {
		return nil, err
	}
	pwhash, err := redigo.String(conn.Do("GET", fmt.Sprintf("user:%s:pwhash", username)))
	if err != nil {
		return nil, err
	}

	uuid, _ := uuid.FromString(player)
	p, err := rps.Load(uuid)
	if err != nil {
		return nil, err
	}
	p.User.password = pwhash
	return &p.User, nil
}
func resetPlayerGossip(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	pUUID, err := uuid.FromString(c.URLParams["uuid"])

	player, err := players.PlayerByUUID(pUUID)
	if err != nil {
		return &appError{err, "Cant find player", 404}
	}

	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != pUUID {
		return &appError{errors.New("Unauthorized"), "Must be admin or player to reset gossip", 403}
	}

	err = player.ResetGossip()
	if err != nil {
		return &appError{err, "Failed to reset gossip", 500}
	}
	w.WriteHeader(204)
	return nil
}
func setTournamentPlayed(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	uuid, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(uuid)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}

	tempInfo := make(map[string]bool)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&tempInfo); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := tournament.SetPlayed(tempInfo["played"]); err != nil {
		return &appError{err, "Failed to update tournament played status", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #26
0
func updateNewsItem(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	newsUUID, err := uuid.FromString(c.URLParams["uuid"])
	newsItem, err := news.NewsItemByUUID(newsUUID)
	if err != nil {
		return &appError{err, "Cant find NewsItem", 404}
	}
	if !c.Env["authIsAdmin"].(bool) && c.Env["authPlayer"].(uuid.UUID) != newsItem.Author {
		return &appError{errors.New("Unauthorized"), "Must be author or admin to update news item", 403}
	}
	tempNewsItem := new(news.NewsItem)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempNewsItem); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := newsItem.UpdateNewsItem(*tempNewsItem); err != nil {
		return &appError{err, "Failed to update news item", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #27
0
func addNewsComment(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	newsUUID, err := uuid.FromString(c.URLParams["uuid"])
	newsItem, err := news.NewsItemByUUID(newsUUID)
	if err != nil {
		return &appError{err, "Cant find NewsItem", 404}
	}
	tempInfo := new(news.Comment)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(tempInfo); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !c.Env["authIsAdmin"].(bool) || tempInfo.Player.IsZero() {
		tempInfo.Player = c.Env["authPlayer"].(uuid.UUID)
	}
	if err := newsItem.AddComment(tempInfo.Player, tempInfo.Content); err != nil {
		return &appError{err, "Failed to add news comment", 500}
	}
	w.WriteHeader(204)
	return nil
}
Beispiel #28
0
func editThoughtsHandler(rw http.ResponseWriter, r *http.Request) {

	vars := mux.Vars(r)
	idStr := vars["id"]

	_, err := uuid.FromString(idStr)
	if err != nil {
		httpError(rw, http.StatusBadRequest, errors.New("unable to parse the identifier."))
		return
	}

	var thought Thought
	decoder := json.NewDecoder(r.Body)
	err = decoder.Decode(&thought)
	if err != nil {
		httpError(rw, http.StatusBadRequest, errors.New("unable to parse the resource"))
		return
	}

	if err = tp.ThoughtStorage.UpdateThought(thought); err != nil {

		if err == ErrNoRowUpdated {
			httpError(rw,
				http.StatusNotFound,
				errors.New("No row updated."))
		} else {
			httpError(rw,
				http.StatusInternalServerError,
				errors.New("Internal server error."))
		}

		return
	}

	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(http.StatusNoContent)
}
func addTournamentNoShow(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	tID, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(tID)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}

	absenteeData := new(tournaments.Absentee)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(absenteeData); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if !c.Env["authIsAdmin"].(bool) || absenteeData.Player.IsZero() {
		absenteeData.Player = c.Env["authPlayer"].(uuid.UUID)
	}

	if err := tournament.AddNoShow(absenteeData.Player, absenteeData.Reason); err != nil {
		return &appError{err, "Failed to set absentee for tournament", 500}
	}
	w.WriteHeader(204)
	return nil
}
func setTournamentResult(c web.C, w http.ResponseWriter, r *http.Request) *appError {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	tID, err := uuid.FromString(c.URLParams["uuid"])
	tournament, err := tournaments.TournamentByUUID(tID)
	if err != nil {
		return &appError{err, "Cant find tournament", 404}
	}

	type Result struct {
		Result []uuid.UUID
	}

	resultData := new(Result)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(resultData); err != nil {
		return &appError{err, "Invalid JSON", 400}
	}

	if err := tournament.SetResult(resultData.Result); err != nil {
		return &appError{err, "Failed to update tournament result", 500}
	}
	w.WriteHeader(204)
	return nil
}