Skip to content
This repository was archived by the owner on May 8, 2022. It is now read-only.

Network

ChickenStorm edited this page Sep 12, 2020 · 2 revisions

Network

Network is an autoloaded module which you can access anywhere in the code.

Network serves two purposes at the moment : centralize necessary information to perform API calls, and managing the WebSocket events.

API calls

If you want your node to perform an API call, call

Network.req(calling_object, method_to_trigger, route, method = HTTPClient.METHOD_GET, headers=PoolStringArray(), body="", params=[])
  • calling_object is the traget object where the call back is.
  • method_to_trigger the methode name as a string.
  • route is the relative url to call.
  • method is an int that descrive the HTTP method (see HTTPClient enum).
  • headers the header of the request (as an PoolStringArray).
  • body the body of the request.
  • params are the parameters that should be transmitted to the call back.

After the request resolve or fail, method_to_trigger in calling_object is called with arguments

  • err : the error code of the resuqtest (0 is there was no error)
  • response_code : the http code of the response (see here)
  • headers : header of the response
  • body : Body of the response as a PoolByteArray
  • the arguments passed in params

Example :

func _request_assignation(quantity):
	Network.req(self, "_on_ship_assigned",
		"/api/games/" + _game_data.id +
			"/systems/" + _game_data.selected_state.selected_system.id +
			"/fleets/" + _game_data.selected_state.selected_fleet.id +
			"/ship-groups/",
		HTTPClient.METHOD_POST,
		[ "Content-Type: application/json" ],
		JSON.print({"category" : ship_category.category, "quantity" : quantity}),
		[quantity, _game_data.selected_state.selected_fleet]
	)


func _on_ship_assigned(err, response_code, headers, body, quantity, fleet):
	if err:
		ErrorHandler.network_response_error(err)
	if response_code == HTTPClient.RESPONSE_NO_CONTENT: # change to the wanted response code
		# handle response here
	else:
		var result = JSON.parse(body.get_string_from_utf8()).result
		# handle error here

WebSocket

A websocket event is pushed by the server and notifies the client of any operation performed by the server or another player which affects the game state.

The documentation of existing WS event are presented here

Implement a new WS event

You just have to add a new signal in network.gd. This signal must have the same name as the counterpart Rust Action enum.

For example, if the Rust action is "CombatEnded", you shall have the following signal :

signal CombatEnded(data)

where data is the JSON content of the WS event. You can name it as you want.

Then, you just have to connect the signal in the nodes which needs to be aware of the event :

func ready():
    Network.connect("CombatEnded", self, "_on_combat_ended")

func _on_combat_ended(data):
    print(data)

Wiki

home

Repository and contributing

Game

Architecture

Game element

Good practice

Clone this wiki locally