Skip to content

Instantly share code, notes, and snippets.

@BransonGitomeh
Last active July 28, 2020 19:29
Show Gist options
  • Save BransonGitomeh/3e80b04830e47b5c290c10d37304490f to your computer and use it in GitHub Desktop.
Save BransonGitomeh/3e80b04830e47b5c290c10d37304490f to your computer and use it in GitHub Desktop.
extends Node
onready var types = get_node('/root/action_types')
func game_set_start_time(time):
return {
'type': types.GAME_SET_START_TIME,
'time': time
}
func player_set_name(name):
return {
'type': types.PLAYER_SET_NAME,
'name': name
}
func player_set_token(token):
return {
'type': types.PLAYER_SET_TOKEN,
'token': token
}
func player_set_health(health):
return {
'type': types.PLAYER_SET_HEALTH,
'health': health
}
func move_player(force,pos):
return {
'type': types.MOVE_PLAYER,
'force': force,
'pos': pos,
}
func vehicle_accelerate():
return {
'type': types.VEHICLE_ACCELERATE,
}
func vehicle_de_accelerate():
return {
'type': types.VEHICLE_DE_ACCELERATE
}
func vehicle_free():
return {
'type': types.VEHICLE_FREE
}
func locomotion(state):
return {
'type': types.LOCOMOTION,
"walking":state.walking,
"driving":state.driving
}
extends Control
onready var actions = get_node('/root/actions')
onready var reducers = get_node('/root/reducers')
onready var store = get_node('/root/store')
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# export this
var HOST = "134.209.154.232"
var PORT = 7350
const context = "UI"
var session:NakamaSession;
var token;
var scheme = "http"
var host = HOST
var port = PORT
var server_key = "defaultkey"
var channel
onready var client := Nakama.create_client(server_key, host, port, scheme)
onready var socket := Nakama.create_socket_from(client)
func monitor_state():
$state/log.text = str(store.get_state())
func logger(text):
$logger/log.text = "\n " + context + " -" + text + "\n" + $logger/log.text
func _on_success():
# logger("Connection Success!!")
pass;
func _on_failure(code, message):
logger("Connection Failure!!\nCode: " + str(code) + " Message: " + message)
func _on_fail_ssl_handshake():
logger("SSL Handshake Error!!")
# Called when the node enters the scene tree for the first time.
func _ready():
store.create(
[
{'name': 'game', 'instance': reducers},
{'name': 'player', 'instance': reducers},
{'name': 'movement', 'instance': reducers},
{'name': 'acceleration', 'instance': reducers},
{'name': 'locomotion', 'instance': reducers},
]
# [{'name': '_on_store_changed', 'instance': self}]
)
logger(str(actions))
# SETUP INNITIAL STATE HERE
store.dispatch(actions.game_set_start_time(OS.get_unix_time()))
store.dispatch(actions.player_set_name('Me'))
store.dispatch(actions.player_set_health(100))
store.dispatch(actions.move_player(Vector2(0,0), "none"))
store.dispatch(actions.vehicle_free())
# store.dispatch(actions.PEDAL("UP"))
#start game with player walking mode
store.dispatch(actions.locomotion({"walking": false, "driving": true}))
logger("Checking INTERNET connection")
CheckNConnection.connect("connection_success", self, "_on_success")
CheckNConnection.connect("error_connection_failed", self, "_on_failure")
CheckNConnection.connect("error_ssl_handshake", self, "_on_fail_ssl_handshake")
logger("Attempting to CONNECT")
logger("client -" + str(client))
logger("Attempting AUTHENTICATE")
var rng = RandomNumberGenerator.new()
rng.randomize()
var my_random_number = rng.randf_range(-10.0, 10.0)
var email = str(my_random_number) + "@farmblazergame.com"
var password = "batsignal"
# Use yield(client.function(), "completed") to wait for the request to complete.
session = yield(
client.authenticate_email_async(email, password), "completed"
)
# print("session -", session)
if session.is_exception():
logger("Error authenticating " + str(session))
return
logger("UserID allocated is " + session.user_id)
logger("Username allocated is " + session.username)
store.dispatch(actions.player_set_name(session.username))
store.dispatch(actions.player_set_token(session.token))
token = session.token
logger(session.token) # raw JWT token
logger(session.user_id)
logger(session.username)
logger("Session has expired: %s" % session.expired)
logger("Session expires at: %s" % session.expire_time)
socket.connect("connected", self, "_on_socket_connected")
socket.connect("closed", self, "_on_socket_closed")
socket.connect("received_error", self, "_on_socket_error")
yield(socket.connect_async(session), "completed")
logger("Done")
logger("Joining general channel")
var room_name = "General"
socket.connect("received_channel_message", self, "_on_channel_message_received")
channel = yield(socket.join_chat_async(room_name, NakamaClient.ChannelType.Room), "completed")
var content = {"hello": "world"} # Content MUST be a dictionary.
var send_ack = yield(socket.write_chat_message_async(channel.id, content), "completed")
logger(str(send_ack))
logger("READY TO PLAY!")
func _on_channel_message_received(message):
logger("Received: %s" + str(message))
logger("Message has channel id: %s" % message.channel_id)
logger("Message content: %s" % message.content)
func _on_socket_connected():
logger("Socket connected.")
func _on_socket_closed():
logger("Socket closed.")
func _on_socket_error(err):
logger("Socket error %s" % err)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
# logger("test")
# log any state changes
monitor_state()
func _on_get_user_data_pressed():
var account = yield(client.get_account_async(session), "completed")
logger("User id: %s" % account.user.id)
logger("User username: '%s'" % account.user.username)
logger("Account virtual wallet: %s" % str(account.wallet))
func _on_health_test_pressed():
logger("Testing Ping to other clients in room")
var content = {"hello": "world 2"}
var send_ack = yield(socket.write_chat_message_async(channel.id, content), "completed")
print(send_ack)
func _on_re_auth_test_pressed():
var invalid_authtoken = store.get_state().player.token
var restored_session = NakamaClient.restore_session(invalid_authtoken)
if restored_session.expired:
return logger("Session has expired. Must reauthenticate!")
logger("Session is valid. no need to reauthenticate!")
pass # Replace with function body.
extends Node
onready var types = get_node('/root/action_types')
onready var store = get_node('/root/store')
func game(state, action):
if action['type'] == action_types.GAME_SET_START_TIME:
var next_state = store.shallow_copy(state)
next_state['start_time'] = action['time']
return next_state
return state
func player(state, action):
if action['type'] == action_types.PLAYER_SET_NAME:
var next_state = store.shallow_copy(state)
next_state['name'] = action['name']
return next_state
if action['type'] == action_types.PLAYER_SET_HEALTH:
var next_state = store.shallow_copy(state)
next_state['health'] = action['health']
return next_state
if action['type'] == action_types.PLAYER_SET_TOKEN:
var next_state = store.shallow_copy(state)
next_state['token'] = action['token']
return next_state
return state
func movement(state, action):
if action['type'] == action_types.MOVE_PLAYER:
var next_state = store.shallow_copy(state)
next_state['pos'] = action['pos']
next_state['force'] = action['force']
return next_state
return state
func locomotion(state, action):
if action['type'] == action_types.LOCOMOTION:
print("state,action")
var next_state = store.shallow_copy(state)
next_state['walking'] = action['walking']
next_state['driving'] = action['driving']
return next_state
return state
func acceleration(state, action):
if action['type'] == action_types.VEHICLE_ACCELERATE:
var next_state = store.shallow_copy(state)
next_state['direction'] = 1
return next_state
if action['type'] == action_types.VEHICLE_DE_ACCELERATE:
var next_state = store.shallow_copy(state)
next_state['direction'] = -1
return next_state
if action['type'] == action_types.VEHICLE_FREE:
var next_state = store.shallow_copy(state)
next_state['direction'] = 0
return next_state
return state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment