Network.gd 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. extends Node
  2. #
  3. signal peer_update
  4. signal accounts_list_update
  5. signal characters_list_update
  6. signal online_accounts_update
  7. signal online_characters_update
  8. signal online_agents_update
  9. #
  10. var Client = null
  11. var ENetServer = null
  12. var WebSocketServer = null
  13. enum EChannel
  14. {
  15. CONNECT = 0,
  16. ACTION,
  17. MAP,
  18. NAVIGATION,
  19. ENTITY,
  20. }
  21. # Auth
  22. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  23. func CreateAccount(accountName : String, password : String, email : String, rpcID : int = NetworkCommons.RidSingleMode) -> bool:
  24. return CallServer("CreateAccount", [accountName, password, email], rpcID, NetworkCommons.DelayLogin)
  25. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  26. func ConnectAccount(accountName : String, password : String, rpcID : int = NetworkCommons.RidSingleMode) -> bool:
  27. return CallServer("ConnectAccount", [accountName, password], rpcID, NetworkCommons.DelayLogin)
  28. @rpc("authority", "call_remote", "reliable", EChannel.CONNECT)
  29. func AuthError(err : NetworkCommons.AuthError, rpcID : int = NetworkCommons.RidSingleMode):
  30. CallClient("AuthError", [err], rpcID)
  31. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  32. func DisconnectAccount(rpcID : int = NetworkCommons.RidSingleMode):
  33. CallServer("DisconnectAccount", [], rpcID)
  34. # Character
  35. @rpc("authority", "call_remote", "reliable", EChannel.CONNECT)
  36. func CharacterInfo(info : Dictionary, equipment : Dictionary, rpcID : int = NetworkCommons.RidSingleMode):
  37. CallClient("CharacterInfo", [info, equipment], rpcID)
  38. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  39. func CreateCharacter(charName : String, traits : Dictionary, attributes : Dictionary, rpcID : int = NetworkCommons.RidSingleMode) -> bool:
  40. return CallServer("CreateCharacter", [charName, traits, attributes], rpcID, NetworkCommons.DelayLogin)
  41. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  42. func DeleteCharacter(charName : String, rpcID : int = NetworkCommons.RidSingleMode) -> bool:
  43. return CallServer("DeleteCharacter", [charName], rpcID, NetworkCommons.DelayLogin)
  44. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  45. func ConnectCharacter(nickname : String, rpcID : int = NetworkCommons.RidSingleMode) -> bool:
  46. return CallServer("ConnectCharacter", [nickname], rpcID, NetworkCommons.DelayLogin)
  47. @rpc("authority", "call_remote", "reliable", EChannel.CONNECT)
  48. func CharacterError(err : NetworkCommons.CharacterError, rpcID : int = NetworkCommons.RidSingleMode):
  49. CallClient("CharacterError", [err], rpcID)
  50. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  51. func DisconnectCharacter(rpcID : int = NetworkCommons.RidSingleMode):
  52. CallServer("DisconnectCharacter", [], rpcID)
  53. @rpc("any_peer", "call_remote", "reliable", EChannel.CONNECT)
  54. func CharacterListing(rpcID : int = NetworkCommons.RidSingleMode):
  55. CallServer("CharacterListing", [], rpcID)
  56. # Respawn
  57. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  58. func TriggerRespawn(rpcID : int = NetworkCommons.RidSingleMode):
  59. CallServer("TriggerRespawn", [], rpcID)
  60. # Warp
  61. @rpc("any_peer", "call_remote", "unreliable", EChannel.MAP)
  62. func TriggerWarp(rpcID : int = NetworkCommons.RidSingleMode):
  63. CallServer("TriggerWarp", [], rpcID)
  64. @rpc("authority", "call_remote", "reliable", EChannel.MAP)
  65. func WarpPlayer(mapID : int, rpcID : int = NetworkCommons.RidSingleMode):
  66. CallClient("WarpPlayer", [mapID], rpcID)
  67. # Entities
  68. @rpc("authority", "call_remote", "reliable", EChannel.MAP)
  69. func AddEntity(agentID : int, entityType : ActorCommons.Type, shape : int, spirit : int, currentShape : int, nick : String, velocity : Vector2, position : Vector2i, orientation : Vector2, state : ActorCommons.State, skillCastID : int, rpcID : int = NetworkCommons.RidSingleMode):
  70. CallClient("AddEntity", [agentID, entityType, shape, spirit, currentShape, nick, velocity, position, orientation, state, skillCastID], rpcID)
  71. @rpc("authority", "call_remote", "reliable", EChannel.MAP)
  72. func RemoveEntity(agentID : int, rpcID : int = NetworkCommons.RidSingleMode):
  73. CallClient("RemoveEntity", [agentID], rpcID)
  74. # Notification
  75. @rpc("any_peer", "call_remote", "unreliable_ordered", EChannel.MAP)
  76. func PushNotification(notif : String, rpcID : int = NetworkCommons.RidSingleMode):
  77. CallClient("PushNotification", [notif], rpcID)
  78. # Navigation
  79. @rpc("any_peer", "call_remote", "unreliable_ordered", EChannel.NAVIGATION)
  80. func SetClickPos(pos : Vector2, rpcID : int = NetworkCommons.RidSingleMode):
  81. CallServer("SetClickPos", [pos], rpcID)
  82. @rpc("any_peer", "call_remote", "reliable", EChannel.NAVIGATION)
  83. func SetMovePos(pos : Vector2, rpcID : int = NetworkCommons.RidSingleMode):
  84. CallServer("SetMovePos", [pos], rpcID, NetworkCommons.DelayInstant)
  85. @rpc("authority", "call_remote", "unreliable_ordered", EChannel.NAVIGATION)
  86. func UpdateEntity(agentID : int, velocity : Vector2, position : Vector2, orientation : Vector2, agentState : ActorCommons.State, skillCastID : int, rpcID : int = NetworkCommons.RidSingleMode):
  87. CallClient("UpdateEntity", [agentID, velocity, position, orientation, agentState, skillCastID], rpcID)
  88. @rpc("authority", "call_remote", "reliable", EChannel.NAVIGATION)
  89. func ForceUpdateEntity(agentID : int, velocity : Vector2, position : Vector2, orientation : Vector2, agentState : ActorCommons.State, skillCastID : int, rpcID : int = NetworkCommons.RidSingleMode):
  90. CallClient("ForceUpdateEntity", [agentID, velocity, position, orientation, agentState, skillCastID], rpcID)
  91. @rpc("any_peer", "call_remote", "reliable", EChannel.NAVIGATION)
  92. func ClearNavigation(rpcID : int = NetworkCommons.RidSingleMode):
  93. CallServer("ClearNavigation", [], rpcID)
  94. # Emote
  95. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  96. func TriggerEmote(emoteID : int, rpcID : int = NetworkCommons.RidSingleMode):
  97. CallServer("TriggerEmote", [emoteID], rpcID)
  98. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  99. func EmotePlayer(senderAgentID : int, emoteID : int, rpcID : int = NetworkCommons.RidSingleMode):
  100. CallClient("EmotePlayer", [senderAgentID, emoteID], rpcID)
  101. # Sit
  102. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  103. func TriggerSit(rpcID : int = NetworkCommons.RidSingleMode):
  104. CallServer("TriggerSit", [], rpcID)
  105. # Chat
  106. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  107. func TriggerChat(text : String, rpcID : int = NetworkCommons.RidSingleMode):
  108. CallServer("TriggerChat", [text], rpcID)
  109. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  110. func ChatAgent(ridAgent : int, text : String, rpcID : int = NetworkCommons.RidSingleMode):
  111. CallClient("ChatAgent", [ridAgent, text], rpcID)
  112. # Context
  113. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  114. func ToggleContext(enable : bool, rpcID : int = NetworkCommons.RidSingleMode):
  115. CallClient("ToggleContext", [enable], rpcID)
  116. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  117. func ContextText(author : String, text : String, rpcID : int = NetworkCommons.RidSingleMode):
  118. CallClient("ContextText", [author, text], rpcID)
  119. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  120. func ContextContinue(rpcID : int = NetworkCommons.RidSingleMode):
  121. CallClient("ContextContinue", [], rpcID)
  122. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  123. func ContextClose(rpcID : int = NetworkCommons.RidSingleMode):
  124. CallClient("ContextClose", [], rpcID)
  125. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  126. func ContextChoice(texts : PackedStringArray, rpcID : int = NetworkCommons.RidSingleMode):
  127. CallClient("ContextChoice", [texts], rpcID)
  128. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  129. func TriggerChoice(choiceID : int, rpcID : int = NetworkCommons.RidSingleMode):
  130. CallServer("TriggerChoice", [choiceID], rpcID)
  131. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  132. func TriggerCloseContext(rpcID : int = NetworkCommons.RidSingleMode):
  133. CallServer("TriggerCloseContext", [], rpcID)
  134. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  135. func TriggerNextContext(rpcID : int = NetworkCommons.RidSingleMode):
  136. CallServer("TriggerNextContext", [], rpcID)
  137. # Interact
  138. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  139. func TriggerInteract(entityID : int, rpcID : int = NetworkCommons.RidSingleMode):
  140. CallServer("TriggerInteract", [entityID], rpcID)
  141. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  142. func TriggerExplore(rpcID : int = NetworkCommons.RidSingleMode):
  143. CallServer("TriggerExplore", [], rpcID)
  144. # Combat
  145. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  146. func TriggerCast(entityID : int, skillID : int, rpcID : int = NetworkCommons.RidSingleMode):
  147. CallServer("TriggerCast", [entityID, skillID], rpcID)
  148. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  149. func TargetAlteration(agentID : int, targetID : int, value : int, alteration : ActorCommons.Alteration, skillID : int, rpcID : int = NetworkCommons.RidSingleMode):
  150. CallClient("TargetAlteration", [agentID, targetID, value, alteration, skillID], rpcID)
  151. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  152. func Casted(agentID : int, skillID: int, cooldown : float, rpcID : int = NetworkCommons.RidSingleMode):
  153. CallClient("Casted", [agentID, skillID, cooldown], rpcID)
  154. # Morph
  155. @rpc("any_peer", "call_remote", "reliable", EChannel.ACTION)
  156. func TriggerMorph(rpcID : int = NetworkCommons.RidSingleMode):
  157. CallServer("TriggerMorph", [], rpcID)
  158. @rpc("authority", "call_remote", "reliable", EChannel.ACTION)
  159. func Morphed(agentID : int, morphID : int, notifyMorphing : bool, rpcID : int = NetworkCommons.RidSingleMode):
  160. CallClient("Morphed", [agentID, morphID, notifyMorphing], rpcID)
  161. # Stats
  162. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  163. func UpdatePublicStats(agentID : int, level : int, health : int, hairstyle : int, haircolor : int, gender : ActorCommons.Gender, race : int, skintone : int, currentShape : int, rpcID : int = NetworkCommons.RidSingleMode):
  164. CallClient("UpdatePublicStats", [agentID, level, health, hairstyle, haircolor, gender, race, skintone, currentShape], rpcID)
  165. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  166. func UpdatePrivateStats(agentID : int, experience : int, gp : int, mana : int, stamina : int, karma : int, weight : float, shape : int, spirit : int, rpcID : int = NetworkCommons.RidSingleMode):
  167. CallClient("UpdatePrivateStats", [agentID, experience, gp, mana, stamina, karma, weight, shape, spirit], rpcID)
  168. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  169. func UpdateAttributes(agentID : int, strength : int, vitality : int, agility : int, endurance : int, concentration : int, rpcID : int = NetworkCommons.RidSingleMode):
  170. CallClient("UpdateAttributes", [agentID, strength, vitality, agility, endurance, concentration], rpcID)
  171. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  172. func TriggerSelect(entityID : int, rpcID : int = NetworkCommons.RidSingleMode):
  173. CallServer("TriggerSelect", [entityID], rpcID)
  174. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  175. func AddAttribute(stat : ActorCommons.Attribute, rpcID : int = NetworkCommons.RidSingleMode):
  176. CallServer("AddAttribute", [stat], rpcID)
  177. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  178. func LevelUp(agentID : int, rpcID : int = NetworkCommons.RidSingleMode):
  179. CallClient("LevelUp", [agentID], rpcID)
  180. # Inventory
  181. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  182. func ItemAdded(itemID : int, customfield : String, count : int, rpcID : int = NetworkCommons.RidSingleMode):
  183. CallClient("ItemAdded", [itemID, customfield, count], rpcID)
  184. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  185. func ItemRemoved(itemID : int, customfield : String, count : int, rpcID : int = NetworkCommons.RidSingleMode):
  186. CallClient("ItemRemoved", [itemID, customfield, count], rpcID)
  187. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  188. func ItemEquiped(agentID : int, itemID : int, customfield : String, state : bool, rpcID : int = NetworkCommons.RidSingleMode):
  189. CallClient("ItemEquiped", [agentID, itemID, customfield, state], rpcID)
  190. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  191. func UseItem(itemID : int, rpcID : int = NetworkCommons.RidSingleMode):
  192. CallServer("UseItem", [itemID], rpcID)
  193. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  194. func DropItem(itemID : int, customfield : String, itemCount : int, rpcID : int = NetworkCommons.RidSingleMode):
  195. CallServer("DropItem", [itemID, customfield, itemCount], rpcID)
  196. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  197. func EquipItem(itemID : int, customfield : String, rpcID : int = NetworkCommons.RidSingleMode):
  198. CallServer("EquipItem", [itemID, customfield], rpcID)
  199. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  200. func UnequipItem(itemID : int, customfield : String, rpcID : int = NetworkCommons.RidSingleMode):
  201. CallServer("UnequipItem", [itemID, customfield], rpcID)
  202. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  203. func RetrieveInventory(rpcID : int = NetworkCommons.RidSingleMode):
  204. CallServer("RetrieveInventory", [], rpcID)
  205. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  206. func RefreshInventory(cells : Array[Dictionary], rpcID : int = NetworkCommons.RidSingleMode):
  207. CallClient("RefreshInventory", [cells], rpcID)
  208. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  209. func RefreshEquipments(agentID : int, equipments : Dictionary, rpcID : int = NetworkCommons.RidSingleMode):
  210. CallClient("RefreshEquipments", [agentID, equipments], rpcID)
  211. # Drop
  212. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  213. func DropAdded(dropID : int, itemID : int, customfield : String, pos : Vector2, rpcID : int = NetworkCommons.RidSingleMode):
  214. CallClient("DropAdded", [dropID, itemID, customfield, pos], rpcID)
  215. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  216. func DropRemoved(dropID : int, rpcID : int = NetworkCommons.RidSingleMode):
  217. CallClient("DropRemoved", [dropID], rpcID)
  218. @rpc("any_peer", "call_remote", "reliable", EChannel.ENTITY)
  219. func PickupDrop(dropID : int, rpcID : int = NetworkCommons.RidSingleMode):
  220. CallServer("PickupDrop", [dropID], rpcID)
  221. # Progress
  222. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  223. func UpdateSkill(skillID : int, level : int, rpcID : int = NetworkCommons.RidSingleMode):
  224. CallClient("UpdateSkill", [skillID, level], rpcID)
  225. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  226. func UpdateBestiary(mobID : int, count : int, rpcID : int = NetworkCommons.RidSingleMode):
  227. CallClient("UpdateBestiary", [mobID, count], rpcID)
  228. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  229. func UpdateQuest(questID : int, state : int, rpcID : int = NetworkCommons.RidSingleMode):
  230. CallClient("UpdateQuest", [questID, state], rpcID)
  231. @rpc("authority", "call_remote", "reliable", EChannel.ENTITY)
  232. func RefreshProgress(skills : Dictionary, quests : Dictionary, bestiary : Dictionary, rpcID : int = NetworkCommons.RidSingleMode):
  233. CallClient("RefreshProgress", [skills, quests, bestiary], rpcID)
  234. # Notify peers
  235. func NotifyNeighbours(agent : BaseAgent, callbackName : String, args : Array, inclusive : bool = true):
  236. if not agent:
  237. assert(false, "Agent is misintantiated, could not notify instance players with " + callbackName)
  238. return
  239. var currentAgentID = agent.get_rid().get_id()
  240. if inclusive and agent is PlayerAgent:
  241. Network.callv(callbackName, [currentAgentID] + args + [agent.rpcRID])
  242. var inst : WorldInstance = WorldAgent.GetInstanceFromAgent(agent)
  243. if inst:
  244. for player in inst.players:
  245. if player != null and player != agent and player.rpcRID != NetworkCommons.RidUnknown:
  246. Network.callv(callbackName, [currentAgentID] + args + [player.rpcRID])
  247. func NotifyInstance(inst : WorldInstance, callbackName : String, args : Array):
  248. if inst:
  249. for player in inst.players:
  250. if player != null:
  251. if player.rpcRID != NetworkCommons.RidUnknown:
  252. Network.callv(callbackName, args + [player.rpcRID])
  253. # Peer calls
  254. func CallServer(methodName : String, args : Array, rpcID : int, actionDelta : int = NetworkCommons.DelayDefault) -> bool:
  255. if rpcID == NetworkCommons.RidSingleMode and not Client.isOffline:
  256. if Peers.Footprint(rpcID, methodName, actionDelta):
  257. Client.multiplayerAPI.rpc(NetworkCommons.RidAuthority, self, methodName, args + [Client.uniqueID])
  258. return true
  259. else:
  260. if Peers.Footprint(rpcID, methodName, actionDelta):
  261. if Peers.IsUsingWebSocket(rpcID):
  262. WebSocketServer.callv.call_deferred(methodName, args + [rpcID])
  263. else:
  264. ENetServer.callv.call_deferred(methodName, args + [rpcID])
  265. return true
  266. return false
  267. func CallClient(methodName : String, args : Array, rpcID : int):
  268. if rpcID == NetworkCommons.RidSingleMode:
  269. Client.callv.call_deferred(methodName, args)
  270. elif Peers.IsUsingWebSocket(rpcID):
  271. WebSocketServer.multiplayerAPI.rpc(rpcID, self, methodName, args)
  272. else:
  273. ENetServer.multiplayerAPI.rpc(rpcID, self, methodName, args)
  274. # Service handling
  275. func Mode(isClient : bool, isServer : bool):
  276. var isOffline : bool = isClient and isServer
  277. if isClient:
  278. Client = NetClient.new(LauncherCommons.isWeb, isOffline, isOffline or NetworkCommons.IsLocal, NetworkCommons.IsTesting)
  279. if isServer:
  280. if NetworkCommons.UseENet:
  281. ENetServer = NetServer.new(false, isOffline, NetworkCommons.IsLocal, NetworkCommons.IsTesting)
  282. if NetworkCommons.UseWebSocket and not isOffline:
  283. WebSocketServer = NetServer.new(true, isOffline, NetworkCommons.IsLocal, NetworkCommons.IsTesting)
  284. func _init():
  285. if not NetworkCommons.OnlineListPath.is_empty():
  286. online_agents_update.connect(OnlineList.UpdateJson)
  287. func Destroy():
  288. if Client:
  289. Client.Destroy()
  290. Client = null
  291. if ENetServer:
  292. ENetServer.Destroy()
  293. ENetServer = null
  294. if WebSocketServer:
  295. WebSocketServer.Destroy()
  296. WebSocketServer = null