Example #1
0
func (pl *Player) RPC_PQuery() ([]interface{}, error) {
	const tileDist = 1.5

	tx, ty := geom.LL2TilePos(float64(pl.Lat), float64(pl.Lon))
	maxLat, maxLon := geom.TilePos2LL(tx+tileDist, ty-tileDist)
	minLat, minLon := geom.TilePos2LL(tx-tileDist, ty+tileDist)

	//log.Println("q pos:", minLon, maxLon, minLat, maxLat)
	//log.Println("q que:", qtPlayers.Query(minLon, maxLon, minLat, maxLat))

	vals := qtPlayers.Query(minLon, maxLon, minLat, maxLat)
	ret := make([]interface{}, 0, len(vals))
	for i := 0; i < len(vals); i++ {
		op := vals[i]
		//ignore self
		if op.V == pl.PId {
			continue
		}
		rop := make([]interface{}, 3)
		rop[0] = op.Y
		rop[1] = op.X
		rop[2] = op.V
		ret = append(ret, rop)
	}

	return ret, nil
}
Example #2
0
func (mt *MapTile) ScanToken(token string) (err error) {
	defer func() {
		if r := recover(); r != nil {
			var ok bool
			err, ok = r.(error)
			if !ok {
				err = fmt.Errorf("Err: %v", r)
			}
		}
	}()

	xy := strings.Split(mt.MapPos, ",")
	if len(xy) != 2 {
		return errors.New("invalid_mappos:" + mt.MapPos)
	}

	log.Println("MapTile.Scan:", mt.MapPos)
	xInt, _ := strconv.Atoi(xy[0])
	yInt, _ := strconv.Atoi(xy[1])
	x, y := float64(xInt)+0.5, float64(yInt)+0.5
	lat, lon := geom.TilePos2LL(x, y)
	latStr := strconv.FormatFloat(lat, 'f', 6, 64)
	lonStr := strconv.FormatFloat(lon, 'f', 6, 64)

	apiKeysStr := GetConfigValue("MapsApiKeys", "")
	apiKeys := strings.Split(apiKeysStr, ",")
	if len(apiKeys) <= 0 || len(apiKeysStr) <= 0 {
		return errors.New("missing_api_keys")
	}

	apiKey := apiKeys[poi_apikey_index]
	getTypes := "establishment"
	getRadius := "134"

	url := "https://maps.googleapis.com/maps/api/place/search/json?location=" + latStr + "," + lonStr + "&radius=" + getRadius + "&sensor=true&key=" + apiKey + "&language=de&types=" + getTypes
	if len(token) > 0 {
		url += "&pagetoken=" + token
	}

	log.Println("POIScan:", url)
	response, err := http.Get(url)
	if err != nil {
		return err
	}
	defer response.Body.Close()
	result, err := util.JsonDecode(response.Body)
	if err != nil {
		return err
	}
	if status, ok := result["status"]; ok {
		statusStr := status.(string)
		if statusStr != "OK" && statusStr != "ZERO_RESULTS" {

			if statusStr == "OVER_QUERY_LIMIT" {
				if apiKey == apiKeys[poi_apikey_index] {
					poi_apikey_index = (poi_apikey_index + 1) % len(apiKeys)
					if mt.scanfail >= len(apiKeys) {
						return errors.New(statusStr)
					}
					mt.scanfail++
					mt.Scan()
					return nil
				}
			}

			err = errors.New(statusStr)
			log.Println("POIScan err:", result)
			return err
		}
	}
	res := result["results"].([]interface{})
	//log.Println(len(res))
	pois := make([]*POI, 0)

	for i := 0; i < len(res); i++ {
		data := res[i].(map[string]interface{})
		poi := &POI{}
		poi.ID = data["id"].(string)
		poi.Name = data["name"].(string)
		location := data["geometry"].(map[string]interface{})["location"].(map[string]interface{})
		poi.Addr = data["vicinity"].(string)
		poi.Lat = location["lat"].(float64)
		poi.Lon = location["lng"].(float64)
		poi.MapPos = LL2MapPos(poi.Lat, poi.Lon)
		if poi.MapPos != mt.MapPos {
			continue
		}
		//log.Println(i, poi.MapPos, poi.MapPos == mt.MapPos)
		types := data["types"].([]interface{})
		poi.Types = ""
		for i, value := range types {
			if i > 0 {
				poi.Types += ","
			}
			poi.Types += value.(string)
		}
		pois = append(pois, poi)

		tblPOI.Delete(poi)
		tblPOI.Create(poi)
	}
	//pid = result["user_id"].(string)
	//mt.Pois = pois
	DoRepeat(func() bool {
		mt.Scanned = POIScanVer
		mt.PoisVersion++
		return tblMapTile.WriteParam(mt, &db.RWParams{ReadOnWriteConflict: true})
	}, WRITE_REPEAT)

	//log.Println(">>>>>> PoisVersion:", mt.Version, mt.PoisVersion, mt)

	nextToken, _ := result["next_page_token"]
	if nextToken != nil {
		strToken, _ := nextToken.(string)
		if len(strToken) > 0 {
			go func() {
				time.Sleep(time.Millisecond * 2000)
				mt.ScanToken(strToken)
			}()
		}
	}

	return nil
}