Example #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
}
Example #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
}
Example #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
}
Example #4
0
func InternalSetDoneBuffer(gid, fid, buff int) (errS, errU error) {
	manager := OPDB.NewManager()
	facs, err := manager.Faction().SelectWhere(manager.GID(gid))
	if my, bad := Check(err, "command set turnbuffer failure on data retrieval", "resource", "factions", "gid", gid); bad {
		return my, nil
	}

	var f overpower.FactionDat
	allDone := buff != 0
	for _, testF := range facs {
		if testF.FID() == fid {
			f = testF
			continue
		}
		if testF.DoneBuffer() == 0 {
			allDone = false
		}
	}
	if f == nil {
		return nil, NewError("faction not found for given gid/fid")
	}

	f.SetDoneBuffer(buff)
	err = manager.Close()
	if my, bad := Check(err, "command set turnbuffer failure on updating faction", "faction", f); bad {
		return my, nil
	}
	if allDone {
		// TODO: Run multiple turns if all players have done buffers > 1
		logE, failE := OPDB.SourceTransact(f.GID(), overpower.RunGameTurn)
		if my, bad := Check(failE, "command setturn done rungame failure", "gid", f.GID()); bad {
			return my, nil
		}
		if logE != nil {
			Log(logE)
		}
	}
	return nil, nil
}
Example #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)
}
Example #6
0
func (h *Handler) GetFullView(gid int) (fv *FullView, errS, errU error) {
	wGID := h.GID(gid)
	games, err := h.M.Game().SelectWhere(wGID)
	if my, bad := Check(err, "GetFullView failure on resource aquisition", "resource", "games", "gid", gid); bad {
		return nil, my, nil
	}
	if len(games) == 0 {
		return nil, nil, NewError("NO GAME FOUND")
	}
	g := games[0]
	turn := g.Turn() - 1
	if turn < 0 {
		return nil, nil, NewError("GAME HAS NOT YET BEGUN")
	}
	facs, err := h.M.Faction().SelectWhere(wGID)
	if my, bad := Check(err, "GetFullView failure on resource aquisition", "resource", "factions", "gid", gid); bad {
		return nil, my, nil
	}
	var userF overpower.FactionDat
	for _, f := range facs {
		if f.Owner() == h.User.String() {
			userF = f
			break
		}
	}
	if userF == nil {
		return nil, nil, NewError("USER HAS NO FACTION FOR GAME")
	}
	userF.SetFullJSON()

	wFID := h.FID(gid, userF.FID())
	wTURN := h.TURN(gid, userF.FID(), turn)

	plVs, err1 := h.M.PlanetView().SelectWhere(wFID)
	shVs, err2 := h.M.ShipView().SelectWhere(wTURN)
	launchOrders, err3 := h.M.LaunchOrder().SelectWhere(wFID)
	mapviews, err4 := h.M.MapView().SelectWhere(wFID)
	laRec, err5 := h.M.LaunchRecord().SelectWhere(wTURN)
	batRec, err6 := h.M.BattleRecord().SelectWhere(wTURN)
	powOrds, err7 := h.M.PowerOrder().SelectWhere(wFID)
	truces, err8 := h.M.Truce().SelectWhere(wFID)
	for i, err := range []error{err1, err2, err3, err4, err5, err6, err7, err8} {
		if my, bad := Check(err, "fill fullview failure", "index", i, "gid", gid, "fid", userF.FID(), "turn", turn); bad {
			return nil, my, nil
		}
	}
	if len(powOrds) == 0 {
		return nil, NewError("FILL FULLVIEW FAILED TO FIND POWERORDER"), nil
	}
	if len(mapviews) == 0 {
		return nil, NewError("FILL FULLVIEW FAILED TO FIND MAPVIEWS"), nil
	}

	sortLARecords(laRec)
	sortLDRecords(batRec)
	sortFactions(facs)
	fv = &FullView{
		Game: g,
		//Faction:       userF,
		Factions:      facs,
		PlanetViews:   plVs,
		ShipViews:     shVs,
		LaunchOrders:  launchOrders,
		PowerOrder:    powOrds[0],
		Truces:        truces,
		MapView:       mapviews[0],
		LaunchRecords: laRec,
		BattleRecords: batRec,
	}
	return fv, nil, nil
}