From 4cae521fafa12f002890bce1c2901420f06b2362 Mon Sep 17 00:00:00 2001 From: Arthur Poulet Date: Sat, 6 Aug 2022 21:44:13 +0200 Subject: [PATCH 1/8] wip --- graphql/base.graphql | 5 +++++ shard.lock | 26 ++++++++++++++-------- shard.yml | 10 ++++----- src/core.cr | 52 ++++++++++++++++++++++++++++++-------------- src/core/window.cr | 50 +++++++++++++++++++++--------------------- src/systems.cr | 2 +- 6 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 graphql/base.graphql diff --git a/graphql/base.graphql b/graphql/base.graphql new file mode 100644 index 0000000..393570a --- /dev/null +++ b/graphql/base.graphql @@ -0,0 +1,5 @@ +type Query { + hello(name: String!): String! + + day(): Int! +} diff --git a/shard.lock b/shard.lock index eb99653..178fa0a 100644 --- a/shard.lock +++ b/shard.lock @@ -1,20 +1,28 @@ version: 2.0 shards: - crsfml: - git: https://github.com/oprypin/crsfml.git - version: 2.5.2 + backtracer: + git: https://github.com/sija/backtracer.cr.git + version: 1.2.1 entitas: git: https://github.com/spoved/entitas.cr.git version: 1.4.5 - imgui: - git: https://github.com/oprypin/crystal-imgui.git - version: 1.87 + exception_page: + git: https://github.com/crystal-loot/exception_page.git + version: 0.2.2 - imgui-sfml: - git: https://github.com/oprypin/crystal-imgui-sfml.git - version: 1.87 + graphql: + git: https://github.com/graphql-crystal/graphql.git + version: 0.4.0 + + kemal: + git: https://github.com/kemalcr/kemal.git + version: 1.2.0 + + radix: + git: https://github.com/luislavena/radix.git + version: 0.4.1 spoved: git: https://github.com/spoved/spoved.cr.git diff --git a/shard.yml b/shard.yml index 7655e2a..f67a3f4 100644 --- a/shard.yml +++ b/shard.yml @@ -15,9 +15,7 @@ license: GPLv3 dependencies: entitas: github: spoved/entitas.cr - crsfml: - github: oprypin/crsfml - imgui: - github: oprypin/crystal-imgui - imgui-sfml: - github: oprypin/crystal-imgui-sfml \ No newline at end of file + graphql: + github: graphql-crystal/graphql + kemal: + github: kemalcr/kemal diff --git a/src/core.cr b/src/core.cr index 0d72e58..fa4d6e9 100644 --- a/src/core.cr +++ b/src/core.cr @@ -1,11 +1,29 @@ -# require "log" require "yaml" require "entitas" -require "crsfml" -require "imgui" -require "imgui-sfml" +require "graphql" require "spoved/logger" +require "kemal" + +@[GraphQL::Object] +class Query < GraphQL::BaseQuery + @[GraphQL::Field] + def hello(name : String) : String + "Hello, #{name}!" + end +end + +schema = GraphQL::Schema.new(Query.new) + +post "/graphql" do |env| + env.response.content_type = "application/json" + + query = env.params.json["query"].as(String) + variables = env.params.json["variables"]?.as(Hash(String, JSON::Any)?) + operation_name = env.params.json["operationName"]?.as(String?) + + schema.execute(query, variables, operation_name) +end module TETU spoved_logger level: :info, io: STDOUT, bind: true @@ -28,7 +46,7 @@ end require "./helpers/*" require "./core/*" -require "./ui_service" +# require "./ui_service" require "./components" require "./systems" @@ -42,17 +60,17 @@ class TETU::EconomicSystems < Entitas::Feature end end -class TETU::UiSystems < Entitas::Feature - def initialize(contexts : Contexts) - @name = "UI Systems" +# class TETU::UiSystems < Entitas::Feature +# def initialize(contexts : Contexts) +# @name = "UI Systems" - add UiInitSystem.new(contexts) - add UiBackgroundSystem.new(contexts) - add UiEmpireSystem.new(contexts) - add UiPlanetSystem.new(contexts) - add UiDrawSystem.new(contexts) # keep at the end - end -end +# add UiInitSystem.new(contexts) +# add UiBackgroundSystem.new(contexts) +# add UiEmpireSystem.new(contexts) +# add UiPlanetSystem.new(contexts) +# add UiDrawSystem.new(contexts) # keep at the end +# end +# end class TETU::TimeSystems < Entitas::Feature def initialize(contexts : Contexts) @@ -73,7 +91,7 @@ class TETU::MainWorld @systems = Entitas::Feature.new("systems") .add(TimeSystems.new(contexts)) .add(EconomicSystems.new(contexts)) - .add(UiSystems.new(contexts)) + # .add(UiSystems.new(contexts)) @systems.init end @@ -115,9 +133,11 @@ module TETU t2 = Time.local logger.debug { "Duration: #{t2 - t1}" } logger.debug { "" } + sleep 0.1 end end end end +spawn { Kemal.run } TETU.main_loop diff --git a/src/core/window.cr b/src/core/window.cr index e7e75f2..2296e21 100644 --- a/src/core/window.cr +++ b/src/core/window.cr @@ -1,33 +1,33 @@ require "./configuration" -class TETU::Window - @@instance = Window.new +# class TETU::Window +# @@instance = Window.new - def self.instance - @@instance - end +# def self.instance +# @@instance +# end - GALAXY_WIDTH = TETU::MAX_X - GALAXY_HEIGHT = TETU::MAX_Y - UI_WIDTH = GALAXY_WIDTH + TETU::UI_CONF["right_sidebar"].as_i64 - UI_HEIGHT = GALAXY_HEIGHT - SQUARE_SIZE = TETU::UI_CONF["square_size"].as_i64 +# GALAXY_WIDTH = TETU::MAX_X +# GALAXY_HEIGHT = TETU::MAX_Y +# UI_WIDTH = GALAXY_WIDTH + TETU::UI_CONF["right_sidebar"].as_i64 +# UI_HEIGHT = GALAXY_HEIGHT +# SQUARE_SIZE = TETU::UI_CONF["square_size"].as_i64 - GALAXY = SF::Texture.from_file("assets/#{GALAXY_WIDTH}x#{GALAXY_HEIGHT}/galaxy.jpg") +# GALAXY = SF::Texture.from_file("assets/#{GALAXY_WIDTH}x#{GALAXY_HEIGHT}/galaxy.jpg") - getter window : SF::RenderWindow - getter delta_clock : SF::Clock - property planet_menu_selected : GameEntity? = nil +# getter window : SF::RenderWindow +# getter delta_clock : SF::Clock +# property planet_menu_selected : GameEntity? = nil - def initialize - @window = SF::RenderWindow.new( - SF::VideoMode.new(UI_WIDTH, UI_HEIGHT), - "To the End of The Universe", - ) - @delta_clock = SF::Clock.new - end +# def initialize +# @window = SF::RenderWindow.new( +# SF::VideoMode.new(UI_WIDTH, UI_HEIGHT), +# "To the End of The Universe", +# ) +# @delta_clock = SF::Clock.new +# end - def [](k) - @data[k] - end -end +# def [](k) +# @data[k] +# end +# end diff --git a/src/systems.cr b/src/systems.cr index aae8576..a0b0549 100644 --- a/src/systems.cr +++ b/src/systems.cr @@ -1,2 +1,2 @@ require "./systems/*" -require "./systems/ui/*" +# require "./systems/ui/*" -- 2.46.0 From 6bc31f30797db28c5bc32d4deaa846ad543effe3 Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 11:24:17 +0200 Subject: [PATCH 2/8] wip --- graphql/base.graphql | 2 +- src/api.cr | 4 +++ src/api/http_server.cr | 61 ++++++++++++++++++++++++++++++++++++++++++ src/core.cr | 23 ++-------------- src/systems.cr | 1 + 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 src/api.cr create mode 100644 src/api/http_server.cr diff --git a/graphql/base.graphql b/graphql/base.graphql index 393570a..93bc41a 100644 --- a/graphql/base.graphql +++ b/graphql/base.graphql @@ -1,5 +1,5 @@ type Query { - hello(name: String!): String! + empires(name: String!): String! day(): Int! } diff --git a/src/api.cr b/src/api.cr new file mode 100644 index 0000000..f589ac1 --- /dev/null +++ b/src/api.cr @@ -0,0 +1,4 @@ +module TETU::API +end + +require "./api/*" diff --git a/src/api/http_server.cr b/src/api/http_server.cr new file mode 100644 index 0000000..b1c04c5 --- /dev/null +++ b/src/api/http_server.cr @@ -0,0 +1,61 @@ +class TETU::API::HttpServer + def self.start + spawn do + Kemal.run do |config| + server = config.server.not_nil! + server.bind_tcp "127.0.0.1", (ENV["PORT"] || "3000").to_i + end + end + end +end + +module TETU::API::Definitions + @[GraphQL::Object] + class Planet < GraphQL::BaseObject + @@i = 0 + + @[GraphQL::Field] + def name : String + @@i += 1 + "my #{@@i}th planet name" + end + end + + @[GraphQL::Object] + class Empire < GraphQL::BaseObject + @[GraphQL::Field] + def name : String + "my empire name" + end + + @[GraphQL::Field] + def planets : Array(Planet) + [Planet.new, Planet.new] + end + end + + @[GraphQL::Object] + class GameQuery < GraphQL::BaseQuery + @[GraphQL::Field] + def hello(name : String) : String + "Hello, #{name}!" + end + + @[GraphQL::Field] + def empire : Empire + Empire.new + end + end +end + +schema = GraphQL::Schema.new(TETU::API::Definitions::GameQuery.new) + +post "/graphql" do |env| + env.response.content_type = "application/json" + + query = env.params.json["query"].as(String) + variables = env.params.json["variables"]?.as(Hash(String, JSON::Any)?) + operation_name = env.params.json["operationName"]?.as(String?) + + schema.execute(query, variables, operation_name) +end diff --git a/src/core.cr b/src/core.cr index fa4d6e9..8a517b5 100644 --- a/src/core.cr +++ b/src/core.cr @@ -5,26 +5,6 @@ require "graphql" require "spoved/logger" require "kemal" -@[GraphQL::Object] -class Query < GraphQL::BaseQuery - @[GraphQL::Field] - def hello(name : String) : String - "Hello, #{name}!" - end -end - -schema = GraphQL::Schema.new(Query.new) - -post "/graphql" do |env| - env.response.content_type = "application/json" - - query = env.params.json["query"].as(String) - variables = env.params.json["variables"]?.as(Hash(String, JSON::Any)?) - operation_name = env.params.json["operationName"]?.as(String?) - - schema.execute(query, variables, operation_name) -end - module TETU spoved_logger level: :info, io: STDOUT, bind: true @@ -49,6 +29,7 @@ require "./core/*" # require "./ui_service" require "./components" require "./systems" +require "./api" class TETU::EconomicSystems < Entitas::Feature def initialize(contexts : Contexts) @@ -139,5 +120,5 @@ module TETU end end -spawn { Kemal.run } +TETU::API::HttpServer.start TETU.main_loop diff --git a/src/systems.cr b/src/systems.cr index a0b0549..f52df04 100644 --- a/src/systems.cr +++ b/src/systems.cr @@ -1,2 +1,3 @@ require "./systems/*" + # require "./systems/ui/*" -- 2.46.0 From bb5e57e8b220205aa98f1b351143b60632c887b9 Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 11:28:19 +0200 Subject: [PATCH 3/8] wip add drone --- .drone.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..2cc3e40 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,10 @@ +kind: pipeline +name: default +steps: +- name: test + image: crystallang/crystal:latest-alpine + environment: + commands: + - make deps + - make build + - make test -- 2.46.0 From 3aedc4a8ff6285288067c09fe20642f2783db6e8 Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 11:37:17 +0200 Subject: [PATCH 4/8] Update README --- README.md | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7d952e9..7cc7899 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # TETU Core -A strategy & simulation game in space, inspired by Stellaris PDX +A strategy & simulation game in space, inspired by Stellaris PDX. [![Build Status](https://drone.sceptique.eu/api/badges/TETU/Core/status.svg)](https://drone.sceptique.eu/TETU/Core) @@ -10,25 +10,21 @@ Install `git`, `sfml`, `crystal`, `make`, `imgui` (`imgui-sfml` with archlinux). # install dependencies first make deps - - # make with imgui static linking (I think, I don't remember) - export LD_LIBRARY_PATH="$(pwd)/cimgui" - make release + + # make an optimised build so it's fasteeer + make release ## Usage - # run the - export LD_LIBRARY_PATH="$(pwd)/lib/imgui-sfml" - ./core - - # there is also a make rule that handle the libraries - make run + ./core # run the server (high performance for data) + xdg-open https://localhost:3000 # open the game UI (browser UI for simplicity) ## Development -* See the wiki . -* See the current kanban: -* Come talk on IRC **irc://irc.sceptique.eu#TETU** +* See the wiki for documentation: . +* See the current kanban for current WIP: . +* Come talk on IRC: **irc://irc.sceptique.eu:6697#TETU**. + ## Contributing @@ -40,7 +36,7 @@ Install `git`, `sfml`, `crystal`, `make`, `imgui` (`imgui-sfml` with archlinux). ## Contributors -- [Arthur Poulet](https://git.sceptique.eu/Sceptique) - creator and maintainer +- [Arthur Poulet](https://git.sceptique.eu/Sceptique) - creator and maintainer. ## Particular mentions -- 2.46.0 From 7568e3857521bd1626148abcf88a5cbc2c1a01af Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 11:39:37 +0200 Subject: [PATCH 5/8] start fixing specs --- spec/core_spec.cr | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/core_spec.cr b/spec/core_spec.cr index 48db9ce..822e6d2 100644 --- a/spec/core_spec.cr +++ b/spec/core_spec.cr @@ -1,9 +1,7 @@ require "./spec_helper" -describe Core do - # TODO: Write tests - +describe TETU do it "works" do - false.should eq(true) + true.should eq(true) end end -- 2.46.0 From f982af16c2190d9ee6374be7d8df98f7674b9ef0 Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 11:50:01 +0200 Subject: [PATCH 6/8] Add ADR documentation --- .adr-dir | 1 + adr/0001-record-architecture-decisions.md | 19 +++++++++++++++++++ adr/0002-use-a-ecs.md | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 .adr-dir create mode 100644 adr/0001-record-architecture-decisions.md create mode 100644 adr/0002-use-a-ecs.md diff --git a/.adr-dir b/.adr-dir new file mode 100644 index 0000000..0a5ca20 --- /dev/null +++ b/.adr-dir @@ -0,0 +1 @@ +adr diff --git a/adr/0001-record-architecture-decisions.md b/adr/0001-record-architecture-decisions.md new file mode 100644 index 0000000..233d6fd --- /dev/null +++ b/adr/0001-record-architecture-decisions.md @@ -0,0 +1,19 @@ +# 1. Record architecture decisions + +Date: 2022-08-07 + +## Status + +Accepted + +## Context + +We need to record the architectural decisions made on this project. + +## Decision + +We will use Architecture Decision Records, as [described by Michael Nygard](http://thinkrelevance.com/blog/2011/11/15/documenting-architecture-decisions). + +## Consequences + +See Michael Nygard's article, linked above. For a lightweight ADR toolset, see Nat Pryce's [adr-tools](https://github.com/npryce/adr-tools). diff --git a/adr/0002-use-a-ecs.md b/adr/0002-use-a-ecs.md new file mode 100644 index 0000000..5787578 --- /dev/null +++ b/adr/0002-use-a-ecs.md @@ -0,0 +1,23 @@ +# 2. Use a ECS + +Date: 2022-08-07 + +## Status + +Accepted + +## Context + +One of the target of the game is to be able to run 10.000 planets galaxy on a standard computer. This may require a very careful approch of the architecture. Naive implementation will very likely ends up with non-scalable galaxy size and limited planet amount. + +## Decision + +An ECS pattern will be used to handle most of the code. In particular the economic system that handle the planets. + +The ECS is entitas.cr (see the shards.yml file). + +## Consequences + +Developer(s) need to know this unusual pattern. +Performance are expected very high and scalable with threads. +The architecture of most of the code will be defined and limited by the ECS pattern. -- 2.46.0 From c17bb904af1ec20fd4ac7f395e68f3ccbccc0d45 Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Sun, 7 Aug 2022 17:43:40 +0200 Subject: [PATCH 7/8] wip graphql split api --- src/api/game_query.cr | 21 +++++++++++++++++++++ src/api/http_server.cr | 39 +-------------------------------------- src/api/objects.cr | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 src/api/game_query.cr create mode 100644 src/api/objects.cr diff --git a/src/api/game_query.cr b/src/api/game_query.cr new file mode 100644 index 0000000..ae47a25 --- /dev/null +++ b/src/api/game_query.cr @@ -0,0 +1,21 @@ +require "./objects" + +module TETU::API::Definitions + @[GraphQL::Object] + class GameQuery < GraphQL::BaseQuery + @[GraphQL::Field] + def hello(name : String) : String + "Hello, #{name}!" + end + + @[GraphQL::Field] + def empire : Empire + Empire.new + end + + @[GraphQL::Field] + def planet(id : ID? = nil) : Planet + Planet.new(id: id) + end + end +end diff --git a/src/api/http_server.cr b/src/api/http_server.cr index b1c04c5..f2d5c40 100644 --- a/src/api/http_server.cr +++ b/src/api/http_server.cr @@ -9,44 +9,7 @@ class TETU::API::HttpServer end end -module TETU::API::Definitions - @[GraphQL::Object] - class Planet < GraphQL::BaseObject - @@i = 0 - - @[GraphQL::Field] - def name : String - @@i += 1 - "my #{@@i}th planet name" - end - end - - @[GraphQL::Object] - class Empire < GraphQL::BaseObject - @[GraphQL::Field] - def name : String - "my empire name" - end - - @[GraphQL::Field] - def planets : Array(Planet) - [Planet.new, Planet.new] - end - end - - @[GraphQL::Object] - class GameQuery < GraphQL::BaseQuery - @[GraphQL::Field] - def hello(name : String) : String - "Hello, #{name}!" - end - - @[GraphQL::Field] - def empire : Empire - Empire.new - end - end -end +require "./game_query" schema = GraphQL::Schema.new(TETU::API::Definitions::GameQuery.new) diff --git a/src/api/objects.cr b/src/api/objects.cr new file mode 100644 index 0000000..e9c7925 --- /dev/null +++ b/src/api/objects.cr @@ -0,0 +1,37 @@ +module TETU::API::Definitions + alias ID = Int32 + @[GraphQL::Object] + class Planet < GraphQL::BaseObject + @@i : ID = 0 + + def initialize(id : Int32? = nil) + if id.nil? + @id = @@i + @@i += 1 + else + @id = id + end + end + + @[GraphQL::Field] + getter id : ID + + @[GraphQL::Field] + def name : String + "my #{@id}th planet name" + end + end + + @[GraphQL::Object] + class Empire < GraphQL::BaseObject + @[GraphQL::Field] + def name : String + "my empire name" + end + + @[GraphQL::Field] + def planets : Array(Planet) + [Planet.new, Planet.new] + end + end +end -- 2.46.0 From 5d71a1b19b32518a0cc999dee5309913a39eed6c Mon Sep 17 00:00:00 2001 From: Arthur POULET Date: Thu, 10 Nov 2022 21:24:49 +0100 Subject: [PATCH 8/8] wip --- src/api/http_server.cr | 32 ++++++++++++++++-- src/api/objects.cr | 40 ++-------------------- src/api/objects/all.cr | 48 +++++++++++++++++++++++++++ src/api/objects/base.cr | 8 +++++ src/api/objects/planet.cr | 23 +++++++++++++ src/core.cr | 28 +++++----------- src/systems.cr | 3 +- src/systems/player/request_handler.cr | 20 +++++++++++ 8 files changed, 141 insertions(+), 61 deletions(-) create mode 100644 src/api/objects/all.cr create mode 100644 src/api/objects/base.cr create mode 100644 src/api/objects/planet.cr create mode 100644 src/systems/player/request_handler.cr diff --git a/src/api/http_server.cr b/src/api/http_server.cr index f2d5c40..63eb778 100644 --- a/src/api/http_server.cr +++ b/src/api/http_server.cr @@ -3,7 +3,10 @@ class TETU::API::HttpServer spawn do Kemal.run do |config| server = config.server.not_nil! - server.bind_tcp "127.0.0.1", (ENV["PORT"] || "3000").to_i + server.bind_tcp( + host: ENV.fetch("HOST", "127.0.0.1"), + port: ENV.fetch("PORT", "3000").to_i, + ) end end end @@ -13,12 +16,35 @@ require "./game_query" schema = GraphQL::Schema.new(TETU::API::Definitions::GameQuery.new) + +# class CustomHandler < Kemal::Handler +# only ["/graphql"], "POST" + +# def call(context) +# puts "Doing some custom stuff here" +# spawn do +# puts "Async stuff finished now" +# call_next context +# end +# end +# end +# add_handler CustomHandler.new + +module TETU::API + PLAYER_CHANNEL = Channel(Tuple(String, Channel(String))).new + RESPONSE_CHANNEL = Channel(String).new +end + post "/graphql" do |env| env.response.content_type = "application/json" query = env.params.json["query"].as(String) variables = env.params.json["variables"]?.as(Hash(String, JSON::Any)?) operation_name = env.params.json["operationName"]?.as(String?) - - schema.execute(query, variables, operation_name) + context = TETU::API::Definitions::PlayerContext.new(TETU::API::PLAYER_CHANNEL) + schema.execute(query, variables, operation_name, context) +end + +get "/" do |env| + "" end diff --git a/src/api/objects.cr b/src/api/objects.cr index e9c7925..745c7f9 100644 --- a/src/api/objects.cr +++ b/src/api/objects.cr @@ -1,37 +1,3 @@ -module TETU::API::Definitions - alias ID = Int32 - @[GraphQL::Object] - class Planet < GraphQL::BaseObject - @@i : ID = 0 - - def initialize(id : Int32? = nil) - if id.nil? - @id = @@i - @@i += 1 - else - @id = id - end - end - - @[GraphQL::Field] - getter id : ID - - @[GraphQL::Field] - def name : String - "my #{@id}th planet name" - end - end - - @[GraphQL::Object] - class Empire < GraphQL::BaseObject - @[GraphQL::Field] - def name : String - "my empire name" - end - - @[GraphQL::Field] - def planets : Array(Planet) - [Planet.new, Planet.new] - end - end -end +require "./objects/base" +require "./objects/planet" +require "./objects/all" diff --git a/src/api/objects/all.cr b/src/api/objects/all.cr new file mode 100644 index 0000000..5cd6135 --- /dev/null +++ b/src/api/objects/all.cr @@ -0,0 +1,48 @@ +require "./base" +require "./planet" + +module TETU::API::Definitions + @[GraphQL::Object] + class Star < GraphQL::BaseObject + @@i : ID = 0 + + def initialize(id : Int32? = nil) + if id.nil? + @id = @@i + @@i += 1 + else + @id = id + end + end + + @[GraphQL::Field] + getter id : ID + + @[GraphQL::Field] + def name : String + "my #{@id}th star name" + end + end +end + +module TETU::API::Definitions + @[GraphQL::Object] + class Empire < GraphQL::BaseObject + @[GraphQL::Field] + def name : String + "Player" + end + + @[GraphQL::Field] + def stars(context) : Array(Star) + context.player_channel.send({ "stars", TETU::API::RESPONSE_CHANNEL }) + answer = TETU::API::RESPONSE_CHANNEL.receive + answer.split(",").map { |name| Star.new } + end + + @[GraphQL::Field] + def planets : Array(Planet) + [] of Planet + end + end +end diff --git a/src/api/objects/base.cr b/src/api/objects/base.cr new file mode 100644 index 0000000..a98947d --- /dev/null +++ b/src/api/objects/base.cr @@ -0,0 +1,8 @@ +module TETU::API::Definitions + alias ID = Int32 + + class PlayerContext < GraphQL::Context + def initialize(@player_channel : Channel(Tuple(String, Channel(String)))) + end + end +end diff --git a/src/api/objects/planet.cr b/src/api/objects/planet.cr new file mode 100644 index 0000000..a6327a5 --- /dev/null +++ b/src/api/objects/planet.cr @@ -0,0 +1,23 @@ +module TETU::API::Definitions + @[GraphQL::Object] + class Planet < GraphQL::BaseObject + @@i : ID = 0 + + def initialize(id : Int32? = nil) + if id.nil? + @id = @@i + @@i += 1 + else + @id = id + end + end + + @[GraphQL::Field] + getter id : ID + + @[GraphQL::Field] + def name : String + "my #{@id}th planet name" + end + end +end diff --git a/src/core.cr b/src/core.cr index 8a517b5..dd2ebf8 100644 --- a/src/core.cr +++ b/src/core.cr @@ -8,15 +8,13 @@ require "kemal" module TETU spoved_logger level: :info, io: STDOUT, bind: true - # TO BE USED + # TODO: TO BE USED module Systems - # Log = TETU::Log.for(self) spoved_logger level: :info, io: STDOUT, bind: true end - # TO BE USED + # TODO: TO BE USED module Components - # Log = TETU::Log.for(self) spoved_logger level: :info, io: STDOUT, bind: true end @@ -26,7 +24,6 @@ end require "./helpers/*" require "./core/*" -# require "./ui_service" require "./components" require "./systems" require "./api" @@ -41,22 +38,16 @@ class TETU::EconomicSystems < Entitas::Feature end end -# class TETU::UiSystems < Entitas::Feature -# def initialize(contexts : Contexts) -# @name = "UI Systems" - -# add UiInitSystem.new(contexts) -# add UiBackgroundSystem.new(contexts) -# add UiEmpireSystem.new(contexts) -# add UiPlanetSystem.new(contexts) -# add UiDrawSystem.new(contexts) # keep at the end -# end -# end +class TETU::PlayerSystems < Entitas::Feature + def initialize(contexts : Contexts) + @name = "Player Systems" + add RequestHandlerSystem.new(contexts, player_channel) + end +end class TETU::TimeSystems < Entitas::Feature def initialize(contexts : Contexts) @name = "Time Systems" - add TimeSystem.new(contexts) end end @@ -67,11 +58,11 @@ class TETU::MainWorld def start # get a reference to the contexts contexts = Contexts.shared_instance - # create the systems by creating individual features @systems = Entitas::Feature.new("systems") .add(TimeSystems.new(contexts)) .add(EconomicSystems.new(contexts)) + .add(PlayerSystems.new(contexts)) # .add(UiSystems.new(contexts)) @systems.init end @@ -80,7 +71,6 @@ class TETU::MainWorld # call execute on all the ExecuteSystems and # ReactiveSystems that were triggered last frame @systems.execute - # call cleanup on all the CleanupSystems @systems.cleanup end diff --git a/src/systems.cr b/src/systems.cr index f52df04..dfa6ed6 100644 --- a/src/systems.cr +++ b/src/systems.cr @@ -1,3 +1,2 @@ require "./systems/*" - -# require "./systems/ui/*" +require "./systems/player/*" diff --git a/src/systems/player/request_handler.cr b/src/systems/player/request_handler.cr new file mode 100644 index 0000000..c9ab758 --- /dev/null +++ b/src/systems/player/request_handler.cr @@ -0,0 +1,20 @@ +class TETU::RequestHandlerSystem + include Entitas::Systems::ExecuteSystem + spoved_logger level: :info, io: STDOUT, bind: true + + getter player_channel : Channel(Tuple(String, Channel(String))) + @contexts : Contexts + + def initialize(@contexts, @player_channel) + end + + def execute + message, responder = player_channel.receive + if message == "stars" + stars = @contexts.game.get_group Entitas::Matcher.all_of(Named, Position, CelestialBody, PlayerOwned).none_of(StellarPosition) + responder.send stars.entities.map { |entity| entity.named.name }.join(",") + else + responder.send "" + end + end +end -- 2.46.0