LssLevelInfo.gd 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. extends VBoxContainer
  2. signal closed
  3. const LEVEL_INFO_URL := "https://levelsharesquare.com/api/levels/"
  4. var level_id := ""
  5. var has_downloaded := false
  6. signal level_play
  7. func _ready() -> void:
  8. set_process(false)
  9. func open(container: OnlineLevelContainer) -> void:
  10. has_downloaded = FileAccess.file_exists("user://custom_levels/downloaded/" + container.level_id + ".lvl")
  11. show()
  12. %Download.text = "DOWNLOAD"
  13. if has_downloaded:
  14. %OnlinePlay.grab_focus()
  15. else:
  16. %Download.grab_focus()
  17. setup_visuals(container)
  18. level_id = container.level_id
  19. await get_tree().physics_frame
  20. set_process(true)
  21. func setup_visuals(container: OnlineLevelContainer) -> void:
  22. $Panel/AutoScrollContainer.scroll_pos = 0
  23. $Panel/AutoScrollContainer.move_direction = -1
  24. %LSSDescription.text = "Fetching Description..."
  25. %SelectedOnlineLevel.level_name = container.level_name
  26. %SelectedOnlineLevel.level_author = container.level_author
  27. %SelectedOnlineLevel.level_id = container.level_id
  28. %SelectedOnlineLevel.thumbnail_url = container.thumbnail_url
  29. %SelectedOnlineLevel.level_thumbnail = container.level_thumbnail
  30. %SelectedOnlineLevel.difficulty = container.difficulty
  31. %SelectedOnlineLevel.setup_visuals()
  32. $Description.request(LEVEL_INFO_URL + container.level_id)
  33. %Download.visible = not has_downloaded
  34. %OnlinePlay.visible = has_downloaded
  35. func _process(_delta: float) -> void:
  36. if Input.is_action_just_pressed("ui_back"):
  37. close()
  38. func close() -> void:
  39. hide()
  40. closed.emit()
  41. set_process(false)
  42. func download_level() -> void:
  43. DirAccess.make_dir_recursive_absolute("user://custom_levels/downloaded")
  44. var url = "https://levelsharesquare.com/api/levels/" + level_id + "/code"
  45. print(url)
  46. $DownloadLevel.request(url, [], HTTPClient.METHOD_GET)
  47. %Download.text = "DOWNLOADING..."
  48. func open_lss() -> void:
  49. OS.shell_open("https://levelsharesquare.com/levels/" + str(level_id))
  50. func on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
  51. var string = body.get_string_from_utf8()
  52. var json = JSON.parse_string(string)
  53. %LSSDescription.text = Global.sanitize_string(json["level"]["description"])
  54. func level_downloaded(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
  55. var string = body.get_string_from_utf8()
  56. var json = JSON.parse_string(string)
  57. var file = FileAccess.open("user://custom_levels/downloaded/" + level_id + ".lvl", FileAccess.WRITE)
  58. var data = null
  59. if json.levelData is Array:
  60. data = get_json_from_bytes(json.levelData)
  61. else:
  62. data = json.levelData
  63. file.store_string(JSON.stringify(data))
  64. file.close()
  65. %Download.hide()
  66. %OnlinePlay.show()
  67. %OnlinePlay.grab_focus()
  68. func play_level() -> void:
  69. var file_path := "user://custom_levels/downloaded/" + level_id + ".lvl"
  70. LevelEditor.level_file = JSON.parse_string(FileAccess.open(file_path, FileAccess.READ).get_as_text())
  71. level_play.emit()
  72. func get_json_from_bytes(json := []) -> String:
  73. return PackedByteArray(json).get_string_from_ascii()