Example #1
0
func handleGetStatus(w http.ResponseWriter, r *http.Request) {
	sid := r.FormValue("sid")
	user := users[sid]
	user.Lat, user.Lng = utils.ParseFloat(r.FormValue("lat")), utils.ParseFloat(r.FormValue("lng"))
	users[sid] = user
	others := make([]*models.User, 0)
	for otherSid, other := range users {
		if sid != otherSid {

			others = append(others, other)
		}
	}
	dict := map[string]interface{}{
		"game_state": 1,
		"hp":         user.Hp,
		"mana":       user.Mana,
		"towers":     towers,
		"users":      others,
		"msgs":       msgs[sid],
	}
	msg := &Msg{
		Head: MsgHead{
			Code: 200,
			Desc: "OK",
		},
		Body: dict,
	}
	bytes, _ := json.Marshal(msg)
	w.Write(bytes)
}
Example #2
0
func handleBuild(w http.ResponseWriter, r *http.Request) {
	sid := r.FormValue("sid")
	user := users[sid]
	var msg *Msg
	if user.Mana < 1 {
		msg = &Msg{
			Head: MsgHead{
				Code: 400,
				Desc: "Mana < 1",
			},
		}
	} else {
		user.Mana -= 1
		users[sid] = user
		lat, lng := utils.ParseFloat(r.FormValue("lat")), utils.ParseFloat(r.FormValue("lng"))
		msg = &Msg{
			Head: MsgHead{
				Code: 200,
				Desc: "OK",
			},
		}
		tower := &models.Tower{
			Id:      uniuri.NewLen(10),
			UserId:  user.Id,
			Faction: user.Faction,
			Lat:     lat,
			Lng:     lng,
		}
		towers = append(towers, tower)
		dict := map[string]interface{}{
			"tower_id":  tower.Id,
			"mana_left": user.Mana,
		}
		msg.Body = dict
	}

	bytes, _ := json.Marshal(msg)
	w.Write(bytes)
}