Пример #1
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)
}
Пример #2
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
}