Пример #1
0
func GameAction(game gamedef.Game) rack.Middleware {
	ws := websocketer.New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		c, p := V(vars).Get()
		conn := websocketer.V(vars).GetSocket()

		if p != nil {
			p.(*player.Player).AddConnection(conn)
		} else {
			V(vars).Set(c, (*player.Spectator)(conn))
		}
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		_, p := V(vars).Get()
		conn := websocketer.V(vars).GetSocket()

		p.RemoveConnection(conn)
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		c, p := V(vars).Get()
		m := (websocketer.V)(vars).GetMessage()

		c.PlayerAction(p, *m.(*string))
	}))
	return ws

}
Пример #2
0
func (this descriptor) addDispatchAction(funcs map[string]rack.Middleware, name string, method reflect.Method) {
	name = strings.ToLower(name)
	d := new(dispatchAction)
	d.descriptor = this
	d.name = name
	d.action = rack.Func(func(vars map[string]interface{}, next func()) {
		copy := reflect.New(this.t)
		mapper := copy.Interface().(ModelMap)
		mapper.SetRackVars(this, vars, next)
		method.Func.Call([]reflect.Value{reflect.Indirect(copy)})
		if !mapper.IsFinished() {
			next()
		}
	})

	funcs[name] = d
}
Пример #3
0
func (this dispatchAction) Run(vars map[string]interface{}, next func()) {
	actions := rack.New()
	actions.Add(this.action)
	switch (httper.V)(vars).GetRequest().Method {
	case "GET":
		//if it was a get, the default action should be to render the template corresponding with the action
		actions.Add(renderer.Renderer{this.routeName + "/" + this.name})
	case "POST", "PUT":
		//if it was a put or a post, we the default action should be to redirect to the affected item
		actions.Add(rack.Func(func(vars map[string]interface{}, next func()) {
			urler, isUrler := vars[this.varName].(Urler)
			if !isUrler {
				panic("Object doesn't have an URL to direct to")
			}
			(redirecter.V)(vars).Redirect(urler.Url())
		}))
	case "DELETE":
		//I'm not currently sure what the default action for deletion should be, perhaps redirecting to the parent route
	default:
		panic("Unknown method")
	}
	actions.Run(vars, next)
}