示例#1
0
文件: timer.go 项目: m4ker/uberwar
func check() {
	//检查爆炸
	for sid, user := range users {
		for _, tower := range towers {
			if tower.Faction == user.Faction {
				continue
			}
			if utils.GetEarthDistance(user.Lat, user.Lng, tower.Lat, tower.Lng) < 10 { //Boom!
				cacheKey := fmt.Sprintf("tripwar_boom_%s_%s", user.Id, tower.Id)
				if utils.GetCache(cacheKey) != "" {
					continue
				}
				utils.Debugf("Boom! user: %s, tower: %s", user.Name, tower.Id)
				user.Hp -= 1
				users[sid] = user
				msgs[sid] = append(msgs[sid], "你碰到地雷,被炸掉1滴血")
				owner, osid := getUser(tower.UserId)
				owner.Score += 10
				owner.TotalScore += 10
				users[osid] = owner
				utils.SetCache(cacheKey, "1", 300)
			}

		}
	}
	//检查相遇
	for sid, user := range users {
		for _, user2 := range users {
			if user.Id == user2.Id {
				continue
			}
			if utils.GetEarthDistance(user.Lat, user.Lng, user2.Lat, user2.Lng) < 10 { //Papapa!
				cacheKey := fmt.Sprintf("tripwar_meets_%s_%s", user.Id, user2.Id)
				if utils.GetCache(cacheKey) != "" {
					continue
				}
				utils.Debugf("Papapa! user: %s, meets: %s", user.Name, user2.Name)
				user.Hp += 1
				user.Mana += 1
				users[sid] = user
				msgs[sid] = append(msgs[sid], "你遇到"+user2.Name+",获得1滴血和1魔法")
				utils.SetCache(cacheKey, "1", 300)
			}
		}
	}
}
示例#2
0
func handleOauthRedirect(w http.ResponseWriter, r *http.Request) {
	code := r.FormValue("code")
	utils.Debugf("Oauth redirect, code: %s", code)
	resp, err := http.PostForm("https://login.uber.com.cn/oauth/v2/token", url.Values{
		"client_secret": {conf.Get("uber_client_secret")},
		"client_id":     {"DV6gJE19BMU6LIh0jJYAvtFyel8rpCfe"},
		"grant_type":    {"authorization_code"},
		"redirect_uri":  {"https://tripwar.laoyou.mobi/oauth_redirect"},
		"code":          {code},
	})
	if err != nil {
		utils.Errorf("Error post to oauth: %s", err.Error())
		w.Write([]byte("500"))
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	result := &OauthResult{}
	if err = json.Unmarshal(body, result); err != nil {
		utils.Errorf("Error unmarshal oauth result: %s", err.Error())
		w.Write([]byte("500"))
		return
	}
	utils.Debugf("access token: %s", result.AccessToken)
	sid := uniuri.NewLen(10)
	sids = append(sids, sid)
	utils.SetCache(sid+"_access_token", result.AccessToken, 86400)
	go func(token string) {
		profile, err := uber.GetUserProfile(token)
		if err != nil {
			utils.Errorf("Error get user profile: %s", err.Error())
			return
		}
		user := &models.User{
			Id:              profile.Uuid,
			Name:            profile.FirstName + " " + profile.LastName,
			UberOpenId:      profile.Uuid,
			Faction:         int64(rand.Intn(2)),
			TotalScore:      10000,
			Score:           0,
			Mana:            10,
			Hp:              10,
			Lng:             116.310228,
			Lat:             39.979233,
			LocationBearing: 33,
		}
		utils.Debugf("user: %v", user)
		users[sid] = user
	}(result.AccessToken)
	go func(sid string) {
		timer := time.NewTicker(30 * time.Second)
		for {
			select {
			case <-timer.C:
				token := utils.GetCache(sid + "_access_token")
				current, _ := uber.GetCurrent(token)
				utils.Debugf("current: %v", current)
				sidCurrent[sid] = current
			}
		}
	}(sid)
	http.Redirect(w, r, "http://tripwar.laoyou.mobi/html/index.html?sid="+sid, 302)
}