Esempio n. 1
0
func (h *Handler) CommandNewFaction(g overpower.GameDat, facs []overpower.FactionDat, f overpower.FactionDat, password, facname string) (errServer, errUser error) {
	if g.Turn() > 0 {
		return nil, NewError("GAME IN PROGRESS")
	}
	if f != nil {
		return nil, NewError("USER ALREADY HAS FACTION FOR THIS GAME")
	}
	if g.HasPassword() {
		if !ValidText(password) || !g.IsPassword(password) {
			return nil, NewError("BAD PASSWORD")
		}
	}
	if !ValidText(facname) {
		return nil, NewError("BAD FACTION NAME")
	}
	lwFName := strings.ToLower(facname)
	for _, f := range facs {
		if strings.ToLower(f.Name()) == lwFName {
			return nil, NewError("FACTION NAME ALREADY IN USE FOR THIS GAME")
		}
	}
	newF := &models.Faction{
		GID:   g.GID(),
		Owner: h.User.String(),
		Name:  facname,
	}
	h.M.CreateFaction(newF)
	err := h.M.Close()
	if my, bad := Check(err, "data creation error", "type", "faction", "gid", g.GID(), "user", h.User, "facname", facname); bad {
		return my, nil
	}
	return nil, nil
}
Esempio n. 2
0
func (h *Handler) CommandQuitGame(g overpower.GameDat, f overpower.FactionDat, turnStr string) (errServer, errUser error) {
	turnI, err := strconv.Atoi(turnStr)
	if err != nil || turnI != g.Turn() {
		return nil, NewError("FORM SUBMISSION TURN DOES NOT MATCH GAME TURN")
	}
	f.DELETE()
	return h.M.Close(), nil
}
Esempio n. 3
0
func (h *Handler) CommandDropFaction(g overpower.GameDat, f overpower.FactionDat) (errServer, errUser error) {
	if g.Turn() > 0 {
		return nil, NewError("GAME IN PROGRESS")
	}
	if f == nil {
		return nil, NewError("USER HAS NO FACTION FOR THIS GAME")
	}
	f.DELETE()
	err := h.M.Close()
	if my, bad := Check(err, "command drop faction failure", "game", g, "faction", f); bad {
		return my, nil
	}
	return nil, nil
}
Esempio n. 4
0
func (h *Handler) CommandForceTurn(g overpower.GameDat, turnStr string) (errServer, errUser error) {
	if g == nil {
		return nil, NewError("USER HAS NO GAME TO PROGRESS")
	}
	if g.Turn() < 1 {
		return nil, NewError("GAME HAS NOT YET BEGUN")
	}
	turnI, err := strconv.Atoi(turnStr)
	if err != nil || turnI != g.Turn() {
		return nil, NewError("FORM SUBMISSION TURN DOES NOT MATCH GAME TURN")
	}
	logE, failE := OPDB.SourceTransact(g.GID(), overpower.RunGameTurn)
	if my, bad := Check(failE, "failure on running turn", "gid", g.GID()); bad {
		return my, nil
	}
	if logE != nil {
		Log(logE)
	}
	return nil, nil
}
Esempio n. 5
0
func (h *Handler) CommandSetDoneBuffer(g overpower.GameDat, f overpower.FactionDat, turnStr, buffStr string) (errServer, errUser error) {
	if g.Turn() < 1 {
		return nil, NewError("GAME HAS NOT YET BEGUN")
	}
	turnI, err := strconv.Atoi(turnStr)
	if err != nil || turnI != g.Turn() {
		return nil, NewError("FORM SUBMISSION TURN DOES NOT MATCH GAME TURN")
	}
	buffI, err := strconv.Atoi(buffStr)
	if err != nil {
		return nil, NewError("UNPARSABLE TURN BUFFER VALUE")
	}
	if buffI < 0 {
		buffI = -1
	}
	if buffI == f.DoneBuffer() {
		return nil, nil
	}
	return InternalSetDoneBuffer(f.GID(), f.FID(), buffI)
}
Esempio n. 6
0
func (h *Handler) CommandStartGame(g overpower.GameDat, facs []overpower.FactionDat, exodus bool) (errServer, errUser error) {
	if g == nil {
		return nil, NewError("USER HAS NO GAME TO START")
	}
	if g.Turn() > 0 {
		return nil, NewError("GAME ALREADY IN PROGRESS")
	}
	if len(facs) < 1 {
		return nil, NewError("GAME HAS NO PLAYERS")
	}
	f := func(source overpower.Source) (logE, failE error) {
		return nil, overpower.MakeGalaxy(source, exodus)
	}
	logE, failE := OPDB.SourceTransact(g.GID(), f)
	if my, bad := Check(failE, "command startgame failure", "gid", g.GID()); bad {
		return my, nil
	}
	if logE != nil {
		Log(logE)
	}
	return nil, nil
}
Esempio n. 7
0
func pageOPHome(w http.ResponseWriter, r *http.Request) {
	h := MakeHandler(w, r)
	if !h.LoggedIn {
		http.Redirect(w, r, "/auth/login", http.StatusFound)
		return
	}
	if r.URL.Path != "/overpower/home" {
		http.Redirect(w, r, "/overpower/home", http.StatusFound)
		return
	}
	var hasG bool
	var g overpower.GameDat

	games, err := h.M.Game().Select("owner", h.User.String())
	if my, bad := Check(err, "resource failure on OP home page", "resource", "game", "owner", h.User.String()); bad {
		h.HandleServerError(w, r, my)
		return
	}
	if len(games) != 0 {
		g = games[0]
		hasG = true
	}

	var gFacs []overpower.FactionDat
	var gHasF bool
	if hasG {
		gFacs, err = h.M.Faction().SelectWhere(h.GID(g.GID()))
		if my, bad := Check(err, "resource error in homepage", "resource", "faction", "user", h.User, "gid", g.GID()); bad {
			h.HandleServerError(w, r, my)
			return
		}
		gHasF = len(gFacs) > 0
	}
	if r.Method == "POST" {
		if DBLOCK {
			h.HandleUserError(w, r, "GAME DOWN FOR DAYLY MAINT: 10-20MIN")
			return
		}
		action := r.FormValue("action")
		var errS, errU error
		switch action {
		case "nextturn":
			turn := r.FormValue("turn")
			errS, errU = h.CommandForceTurn(g, turn)
		case "setautos":
			dayBool := [7]bool{}
			dayBool[0] = r.FormValue("sunday") == "on"
			dayBool[1] = r.FormValue("monday") == "on"
			dayBool[2] = r.FormValue("tuesday") == "on"
			dayBool[3] = r.FormValue("wednesday") == "on"
			dayBool[4] = r.FormValue("thursday") == "on"
			dayBool[5] = r.FormValue("friday") == "on"
			dayBool[6] = r.FormValue("saturday") == "on"

			errS, errU = h.CommandSetAutos(g, dayBool)

		case "startgame":
			exodus := r.FormValue("exodus") == "on"
			errS, errU = h.CommandStartGame(g, gFacs, exodus)
		case "newgame":
			gamename, password := r.FormValue("gamename"), r.FormValue("password")
			facname, towin := r.FormValue("facname"), r.FormValue("towin")
			errS, errU = h.CommandNewGame(g, password, gamename, facname, towin)
		case "dropgame":
			errS, errU = h.CommandDropGame(g)
		default:
			errU = NewError("UNKNOWN ACTION TYPE")
		}
		if my, bad := Check(errS, "page op home action failure", "action", action, "user", h.User.String(), "game", g); bad {
			h.HandleServerError(w, r, my)
		} else if errU != nil {
			h.HandleUserError(w, r, errU.Error())
		} else {
			http.Redirect(w, r, r.URL.Path, http.StatusFound)
		}
		return
	}
	m := h.DefaultApp()
	m["user"] = h.User.String()
	if hasG {
		m["game"] = g
		m["active"] = g.Turn() > 0
	}
	if gHasF {
		m["gfactions"] = gFacs
		days := g.AutoDays()
		var any bool
		for _, b := range days {
			if b {
				any = true
				break
			}
		}
		if !any {
			m["noauto"] = true
		}
	}
	oFacs, err := h.M.Faction().Select("owner", h.User.String())
	if my, bad := Check(err, "resource error in homepage", "resource", "faction", "owner", h.User); bad {
		h.HandleServerError(w, r, my)
		return
	}
	oHasF := len(oFacs) > 0
	if oHasF {
		facGames := make([]overpower.GameDat, 0, len(oFacs))
		for _, f := range oFacs {
			games, err := h.M.Game().SelectWhere(h.GID(f.GID()))
			if my, bad := Check(err, "resource error in homepage", "gid", f.GID(), "fac", f, "owner", h.User); bad {
				h.HandleServerError(w, r, my)
				return
			} else {
				facGames = append(facGames, games...)
			}
		}
		m["ofactions"] = oFacs
		m["ogames"] = facGames
	}
	h.Apply(TPOPHOME, w)
}