예제 #1
0
func netHelloHandler(e *Engine) netHandler {
	return func(msg messages.Hello, conn netengine.Connection) []byte {
		// Assign id, create e player instance and add it to addons.Players, send response
		id := <-e.plyIdChan
		conn.GetSessionData().Set("id", id)

		player := Player{
			conn,
			id,
		}
		e.Players[id] = &player

		// Create javascript player object
		e.JsContext.Scope(func(cs v8.ContextScope) {
			function := cs.Global().GetProperty("Fortia").ToObject().GetProperty("initPlayer").ToFunction()
			function.Call(e.JsEngine.GoValueToJsValue(reflect.ValueOf(id)))
		})

		// Send player connect event
		evt := NewGeneralEvent("playerconnect", id)
		e.EmitEvent(evt)

		response := new(messages.HelloResp)
		response.Message = proto.String("OK")
		response.PlayerId = proto.Int32(int32(id))
		encoded, _ := netengine.EncodeMessage(response, int32(messages.MessageTypes_HELLO_RESP))
		return encoded
	}
}
예제 #2
0
// TODO: only download new resources if updated versions
func netGetClResources(e *Engine) netHandler {
	return func(msg messages.GetCLResources, conn netengine.Connection) []byte {
		appendResourceScript := func(dest []*messages.Resource, script *Script) []*messages.Resource {
			res := new(messages.Resource)
			res.Name = proto.String(script.Path)
			kind := messages.ResourceType_RType_Script
			res.Type = &kind
			res.Script = proto.String(string(script.Source))
			res.ShouldExecSCript = proto.Bool(script.ShouldExec)
			dest = append(dest, res)
			return dest
		}

		// get all resources
		resources := make([]*messages.Resource, 0)
		for _, addon := range e.Addons {
			for _, script := range addon.ClientScripts {
				resources = appendResourceScript(resources, script)
			}
		}
		response := new(messages.GetCLResourcesResp)
		response.Resources = resources
		encoded, err := netengine.EncodeMessage(response, int32(messages.MessageTypes_GETCLRESOURCESRESP))
		if err != nil {
			e.Log.Error("Error encoding GetClResources response ", err)
			return make([]byte, 0)

		}
		return encoded
	}
}
예제 #3
0
// Sends a message to a player
func jsUsrMessage(e *Engine) interface{} {
	return func(player *v8.Value, name string, data *v8.Value) {
		if player == nil || data == nil || name == "" {
			log.Error("Tried calling _sendUsrMessage with invalid arguments")
			return
		}
		// Get the player id
		id := 0
		if player.IsInt32() || player.IsUint32() {
			id = int(player.ToInteger())
		} else if player.IsObject() {
			id = int(player.ToObject().GetProperty("id").ToInteger())
		} else {
			e.Log.Error("Unsuported player argument type calling jsUsrMessage: ", player.String())
			return
		}

		RealPlayer, ok := e.Players[int(id)]
		if !ok {
			e.Log.Error("No player found for player id ", id)
			e.Log.Error(string(v8.ToJSON(player)))
			return
		}
		if !RealPlayer.conn.Open() {
			e.Log.Error("Tried to send usermessage to e disconnected player")
			return
		}

		serializedData := v8.ToJSON(data)

		msg := new(messages.FortiaMessage)
		msg.Name = proto.String(name)
		msg.Data = serializedData

		serializedMessage, err := netengine.EncodeMessage(msg, int32(messages.MessageTypes_SERVERCLIENTMESSAGE))
		if err != nil {
			e.Log.Error("Error serializing message")
			return
		}
		err = RealPlayer.conn.Send(serializedMessage)
		if err != nil {
			e.Log.Error("Error sending usermessage: ", err)
			return
		}
		e.Log.Debug("Sent usrmessage of size: ", len(serializedMessage))
		return
	}
}