Exemplo n.º 1
0
func Recommand(filePath string, people []*Person) (err error) {
	dict := make(map[string]interface{}) //people: []*Person, product: Product
	var product *Product
	if people != nil && len(people) > 0 {
		switch len(people) {
		case 1:
			product, err = recommandFor1(people[0])
		case 2:
			product, err = recommandFor2(people)
		default:
			product, err = recommandForMulti(people)
		}
		if err != nil {
			utils.Errorf("Error recommand: %s", err.Error())
		} else {
			utils.Debugf("Product: %s", product.Name)
		}
	}
	dict["file"] = filePath
	dict["people"] = people
	dict["product"] = product
	if WebSocketConn != nil {
		if err = WebSocketConn.WriteJSON(dict); err != nil {
			utils.Errorf("Error write json to websocket: %s", err.Error())
		}
	} else {
		utils.Debugf("Websocket is nil")
	}
	return
}
Exemplo n.º 2
0
func Handle(w http.ResponseWriter, r *http.Request) {
	upgrader := websocket.Upgrader{
		CheckOrigin: func(req *http.Request) bool {
			if req.Host == "127.0.0.1:9090" {
				return true
			} else {
				return false
			}
		},
	}
	var err error
	WebSocketConn, err = upgrader.Upgrade(w, r, nil)
	if err != nil {
		utils.Errorf("Error upgrade http: %s", err.Error())
		return
	}
	for {
		mt, bytes, err := WebSocketConn.ReadMessage()
		if err != nil {
			utils.Errorf("Error read message: %v", err)
			WebSocketConn.Close()
			WebSocketConn = nil
			return
		}
		utils.Debugf("Client msg received: %d, %s", mt, string(bytes))
	}
}
Exemplo n.º 3
0
Arquivo: sdk.go Projeto: m4ker/uberwar
func GetCurrent(accessToken string) (current *CurrentInfo, err error) {
	bytes, err := do(accessToken, "https://sandbox-api.uber.com.cn/v1/requests/current", "GET")
	current = &CurrentInfo{}
	utils.Debugf("Current returns: %s", string(bytes))
	json.Unmarshal(bytes, current)
	return
}
Exemplo n.º 4
0
Arquivo: sdk.go Projeto: m4ker/uberwar
func GetUserProfile(accessToken string) (profile *UserProfile, err error) {
	bytes, err := do(accessToken, "https://sandbox-api.uber.com.cn/v1/me", "GET")
	profile = &UserProfile{}
	utils.Debugf("User profile returns: %s", string(bytes))
	json.Unmarshal(bytes, profile)
	return
}
Exemplo n.º 5
0
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)
			}
		}
	}
}
Exemplo n.º 6
0
func detect(filePath string) {
	sessionId, faces, _, err := faceClient.DetectImg(filePath)
	if err != nil {
		fmt.Printf("Error detect img: %s \n", err.Error())
		os.Exit(0)
	}
	utils.Debugf("Detect result: sessionId=%s, faces: %d", sessionId, len(faces))
	if len(faces) < 1 {
		Recommand(filePath, nil)
	} else {
		people := make([]*Person, len(faces))
		for i, face := range faces {
			people[i] = &Person{
				Age:    face.Attrs.Age.Value,
				Gender: face.Attrs.Gender.Value,
				Glass:  face.Attrs.Glass.Value,
			}
		}
		Recommand(filePath, people)
	}
}
Exemplo n.º 7
0
func recommandFor1(person *Person) (p *Product, err error) {
	utils.Debugf("Recommand for 1 person: %d, %s", person.Age, person.Gender)
	if person.Age < 10 {
		p = &Product{
			Id:   "27",
			Name: "统一冰糖雪梨",
			Img:  "http://img.ubox.cn/box-tmp/m/27/27.jpg?t=20140305101413",
		}
		return
	}
	switch person.Gender {
	case "Male":
		if person.Age < 18 {
			p = &Product{
				Id:   "1",
				Name: "可口可乐",
				Img:  "http://img.ubox.cn/box-tmp/m/1/1.jpg?t=20140305101413",
			}
		} else if person.Age < 30 {
			p = &Product{
				Id:   "5976",
				Name: "杜蕾斯热感超薄3只装",
				Img:  "http://img.ubox.cn/box-tmp/m/5976/5976.jpg?t=20140305101413",
			}
		} else if person.Age < 40 {
			p = &Product{
				Id:   "19251",
				Name: "取悦潮品4D经典名器少女",
				Img:  "http://img.ubox.cn/box-tmp/m/19251/19251.jpg?t=20140305101413",
			}
		} else if person.Age < 60 {
			p = &Product{
				Id:   "18471",
				Name: "XYZ男士劲能延时喷雾",
				Img:  "http://img.ubox.cn/box-tmp/m/18471/18471.jpg?t=20140305101413",
			}
		} else {
			p = &Product{
				Id:   "19250",
				Name: "取悦潮品4D经典名器处女",
				Img:  "http://img.ubox.cn/box-tmp/m/19250/19250.jpg?t=20140305101413",
			}
		}
	case "Female":
		if person.Age < 18 {
			p = &Product{
				Id:   "18459",
				Name: "费洛蒙男女通用香水",
				Img:  "http://img.ubox.cn/box-tmp/m/18459/18459.jpg?t=20140305101413",
			}
		} else if person.Age < 30 {
			p = &Product{
				Id:   "18528",
				Name: "好奇而已纯白诱惑",
				Img:  "http://img.ubox.cn/box-tmp/m/18528/18528.jpg?t=20140305101413",
			}
		} else if person.Age < 40 {
			p = &Product{
				Id:   "19308",
				Name: "美国进口MOVO迷恋型紧",
				Img:  "http://img.ubox.cn/box-tmp/m/19308/19308.jpg?t=20140305101413",
			}
		} else if person.Age < 60 {
			p = &Product{
				Id:   "18395",
				Name: "喵星人伊咪Y防水阴蒂按摩",
				Img:  "http://img.ubox.cn/box-tmp/m/18395/18395.jpg?t=20140305101413",
			}
		} else {
			p = &Product{
				Id:   "27",
				Name: "统一冰糖雪梨",
				Img:  "http://img.ubox.cn/box-tmp/m/27/27.jpg?t=20140305101413",
			}
		}
	}
	return
}
Exemplo n.º 8
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)
}