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