Exemple #1
0
func init() {
	pt.SetAction(func(r *pt.Request) *pt.Response {
		return r.HtmlResponse("map/main", nil)
	}, "/map")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		x, _ := r.Int("x")
		y, _ := r.Int("y")
		w, _ := r.Int("w")
		h, _ := r.Int("h")

		locs := model.MapModel.Rect(x, y, w, h)
		return r.JsonResponse(locs)
	}, "/map/rect")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		m := model.MapModel
		return r.JsonResponse([]int64{m.Metal(), m.Energy(), int64(m.RefreshState)})
	}, "/map/sum")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		state, has := r.Int("state")
		m := model.MapModel

		if !has {
			state = model.RefreshStateOn
			if m.RefreshState == model.RefreshStateOn {
				state = model.RefreshStateOff
			} else {
				state = model.RefreshStateOn
			}
		}

		m.RefreshState = state
		return r.TextResponse("done")
	}, "/map/refresh")
}
Exemple #2
0
func init() {
	pt.SetAction(func(r *pt.Request) *pt.Response {
		user, _ := r.Session.Get("user").(*model.User)
		if user == nil {
			return r.ErrorResponse(403, "no permisson")
		}

		x, _ := r.Int("x")
		y, _ := r.Int("y")
		name, _ := r.String("name")

		if len(user.Bases()) > 0 {
			return r.ErrorResponse(403, "you are not a new player")
		}
		loc := model.MapModel.Location(x, y)
		if loc == nil {
			return r.ErrorResponse(400, "coordinates not exists")
		}
		if loc.Geo != model.MapGeoLand {
			return r.ErrorResponse(400, "can't build base here")
		}
		if loc.Base() != nil {
			return r.ErrorResponse(400, "a base has already built here")
		}
		if !loc.Lock() {
			return r.ErrorResponse(400, "location was locked by others")
		}
		defer loc.Unlock()

		base := model.NewBase(name, user, loc)
		if model.BaseModel.Save(base) {
			user.AddBase(base)
			return r.JsonResponse(base)
		}
		return r.ErrorResponse(500, "can't save base to db")
	}, "/base/born")

	pt.WsaMap["test"] = func(wsm *pt.Wsm) {
		aa, _ := wsm.String("a")
		ii, _ := wsm.String("i")
		ff, _ := wsm.String("f")
		fmt.Printf("%v, %v, %v\n", aa, ii, ff)
		wsm.Send("test", map[string]interface{}{"aa": 1}, nil)
	}
}
Exemple #3
0
func init() {
	pt.SetAction(func(r *pt.Request) *pt.Response {
		user, _ := r.Session.Get("user").(*model.User)
		if user == nil {
			return r.ErrorResponse(403, "not signed in")
		}

		bid, _ := r.Int64("bid")
		if bid == 0 {
			return r.ErrorResponse(400, "missing base id")
		}

		base := user.Bases()[bid]
		if base == nil {
			return r.ErrorResponse(400, "error base id")
		}

		stope := model.NewStope(base)
		model.StopeModel.Save(stope)
		return r.JsonResponse(stope)
	}, "/stope/new")
}
Exemple #4
0
func init() {
	pt.SetAction(func(r *pt.Request) *pt.Response {
		if r.Method == "POST" {
			name, _ := r.String("name")
			email, _ := r.String("email")
			if len(email) == 0 {
				return r.ErrorResponse(400, "email is empty")
			}

			passwd, _ := r.String("passwd")
			if len(passwd) == 0 {
				return r.ErrorResponse(400, "password is empty")
			}

			user := &model.User{
				Name:      name,
				Email:     email,
				UpdatedAt: time.Now(),
				CreatedAt: time.Now(),
			}
			user.SetPasswd(passwd)
			if model.UserModel.Save(user) {
				r.Session.Set("user", user)
				return r.RedirectResponse("/user", 302)
			}
			return r.ErrorResponse(500, "server bussy try later")
		}
		return r.HtmlResponse("user/signup", nil)
	}, "/signup")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		if r.Method == "POST" {
			email, _ := r.String("email")
			if len(email) == 0 {
				return r.ErrorResponse(400, "email is empty")
			}

			passwd, _ := r.String("passwd")
			if len(passwd) == 0 {
				return r.ErrorResponse(400, "password is empty")
			}

			m := model.UserModel
			if user := m.UserByEmail(email); user != nil &&
				user.CheckPasswd(passwd) {
				r.Session.Set("user", user)
				return r.RedirectResponse("/user", 302)
			}

			return r.ErrorResponse(400, "email or password error")
		}

		return r.HtmlResponse("user/signin", nil)
	}, "/signin")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		r.Session.Set("user", nil)
		return r.TextResponse("done")
	}, "/signout")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		id, _ := r.Int64("$1")
		var user *model.User
		if id == 0 {
			user, _ = r.Session.Get("user").(*model.User)
		} else {
			user = model.UserModel.User(id)
		}

		if id == 0 && user != nil {
			return r.RedirectResponse(fmt.Sprintf("/user/%d", user.Id), 302)
		}

		return r.HtmlResponse("user/show", user)
	}, `/user/(\d+)`, "/user", "/")
}
Exemple #5
0
func init() {
	pt.SetAction(func(r *pt.Request) *pt.Response {
		if !IsGrantd(r.Session, "RoleAdmin") {
			return r.ErrorResponse(403, "permission denied")
		}

		key, _ := r.String("key")

		page, _ := r.Int64("page")
		if page <= 0 {
			page = 1
		}

		size, _ := r.Int64("size")
		if size <= 0 {
			size = 40
		}

		sort, _ := r.String("sort")
		if sort == "" {
			sort = "id"
		}

		order, _ := r.String("order")
		if order == "" {
			order = "desc"
		}

		m := map[string]string{sort: order}
		users := model.
			UserModel.
			Search(key, m, size, (page-1)*size)

		return r.HtmlResponse("admin/user/list", users)
	}, "/admin/user")

	pt.SetAction(func(r *pt.Request) *pt.Response {
		id, _ := r.Int64("$1")
		user := model.UserModel.User(id)
		if user == nil {
			return r.ErrorResponse(404, "user not found")
		}

		return r.HtmlResponse("admin/user/detail", user)
	}, `/admin/user/(\d+)`)

	pt.SetAction(func(r *pt.Request) *pt.Response {
		if r.Method != "post" {
			return r.ErrorResponse(400, "must be post")
		}
		id, _ := r.Int64("$1")
		user := model.UserModel.User(id)
		if user == nil {
			return r.ErrorResponse(404, "user not found")
		}

		desc, _ := r.String("desc")
		name, _ := r.String("role")
		if name == "" {
			return r.ErrorResponse(400, "need a role name")
		}

		if user.IsGrantd(name) {
			return r.RedirectResponse(fmt.Sprintf("/admin/user/%d", id), 302)
		}

		role := model.RoleModel.RoleByName(name)
		if role == nil {
			role = &model.Role{
				Name:      name,
				Desc:      desc,
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
			if !model.RoleModel.Save(role) {
				return r.ErrorResponse(500, "can't save role "+name)
			}
		}

		if user.AddRole(role) {
			return r.RedirectResponse(fmt.Sprintf("/admin/user/%d", id), 302)
		}
		return r.ErrorResponse(500, "can't add role "+name+" to user")
	}, `/admin/user/(\d+)/add_role`)
}