Пример #1
0
func process(realm Entity.Realm, cacheClient Cache.Client) (job Job) {
	// misc
	realmManager := Entity.NewRealmManager(realm.Region, cacheClient)
	job = newJob(realm)

	// fetching the auction info
	var (
		auctionResponse *Auction.Response
		err             error
	)
	if auctionResponse, err = Auction.Get(realm, cacheClient.ApiKey); err != nil {
		job.Err = errors.New(fmt.Sprintf("Auction.Get() failed (%s)", err.Error()))
		return
	}

	// optionally halting on empty response
	if auctionResponse == nil {
		job.ResponseFailed = true
		return
	}

	file := auctionResponse.Files[0]

	// checking whether the file has already been downloaded
	lastModified := time.Unix(file.LastModified/1000, 0)
	if !realm.LastDownloaded.IsZero() && (realm.LastDownloaded.Equal(lastModified) || realm.LastDownloaded.After(lastModified)) {
		job.AlreadyChecked = true
		return
	}

	// fetching the actual auction data
	if job.AuctionDataResponse = AuctionData.Get(realm, file.Url); job.AuctionDataResponse == nil {
		job.ResponseFailed = true
		return
	}

	// dumping the auction data for parsing after itemize-results are tabulated
	if err = job.dumpData(); err != nil {
		job.Err = errors.New(fmt.Sprintf("DownloadRealm.Job.dumpData() failed (%s)", err.Error()))
		return
	}

	// flagging the realm as having been downloaded
	realm.LastDownloaded = lastModified
	realm.LastChecked = time.Now()
	if realm, err = realmManager.Persist(realm); err != nil {
		job.Err = errors.New(fmt.Sprintf("RealmManager.Persist() failed (%s)", err.Error()))
		return
	}

	return
}
Пример #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	configPath := flag.String("config", "", "Config path")
	flag.Parse()

	output := Util.Output{StartTime: time.Now()}
	output.Write("Starting...")

	// init
	var (
		err         error
		cacheClient Cache.Client
	)
	if cacheClient, _, _, err = Misc.Init(*configPath, false); err != nil {
		output.Write(fmt.Sprintf("Misc.Init() fail: %s", err.Error()))
		return
	}

	regionManager := Entity.NewRegionManager(cacheClient)
	var regions []Entity.Region
	if regions, err = regionManager.FindAll(); err != nil {
		output.Write(fmt.Sprintf("RegionManager.FindAll() fail: %s", err.Error()))
		return
	}

	characterCount := 0
	for _, region := range regions {
		realmManager := Entity.NewRealmManager(region, cacheClient)
		var realms []Entity.Realm
		if realms, err = realmManager.FindAll(); err != nil {
			output.Write(fmt.Sprintf("RealmManager.FindAll() fail: %s", err.Error()))
			return
		}

		for _, realm := range realms {
			characterManager := Character.NewManager(realm, cacheClient)
			var names []string
			if names, err = characterManager.GetNames(); err != nil {
				output.Write(fmt.Sprintf("CharacterManager.GetNames() fail: %s", err.Error()))
				return
			}
			characterCount += len(names)
		}
	}
	output.Write(fmt.Sprintf("Characters in the world: %d", characterCount))

	output.Conclude()
}
Пример #3
0
func NewManager(realm Entity.Realm, client Cache.Client) Manager {
	return Manager{
		Realm:        realm,
		RealmManager: Entity.NewRealmManager(realm.Region, client),
	}
}
Пример #4
0
/*
	funcs
*/
func Init(configPath string, flushDb bool) (client Cache.Client, regions []Entity.Region, regionRealms map[int64][]Entity.Realm, err error) {
	// opening the config file
	var configFile Config.File
	if configFile, err = Config.New(configPath); err != nil {
		return
	}

	// connecting the client
	if client, err = Cache.NewClient(configFile); err != nil {
		return
	}
	if flushDb {
		if err = client.FlushDb(); err != nil {
			return
		}
	}

	// gathering the regions and realms
	regionRealms = make(map[int64][]Entity.Realm)
	regionManager := Entity.NewRegionManager(client)
	if flushDb {
		regions = make([]Entity.Region, len(configFile.Regions))
		for i, configRegion := range configFile.Regions {
			regions[i] = Entity.Region{
				Name:      configRegion.Name,
				Host:      configRegion.Host,
				Queryable: configRegion.Queryable,
			}
		}
		if regions, err = regionManager.PersistAll(regions); err != nil {
			return
		}

		for _, region := range regions {
			var response Status.Response
			if response, err = Status.Get(region, client.ApiKey); err != nil {
				return
			}

			realms := make([]Entity.Realm, len(response.Realms))
			for i, responseRealm := range response.Realms {
				realms[i] = Entity.Realm{
					Name:        responseRealm.Name,
					Slug:        responseRealm.Slug,
					Battlegroup: responseRealm.Battlegroup,
					Type:        responseRealm.Type,
					Status:      responseRealm.Status,
					Population:  responseRealm.Population,
					Region:      region,
				}
			}
			realmManager := Entity.NewRealmManager(region, client)
			if regionRealms[region.Id], err = realmManager.PersistAll(realms); err != nil {
				return
			}
		}
	} else {
		if regions, err = regionManager.FindAll(); err != nil {
			return
		}
		for _, region := range regions {
			realmManager := Entity.NewRealmManager(region, client)
			if regionRealms[region.Id], err = realmManager.FindAll(); err != nil {
				return
			}
		}
	}

	return
}