Arvel  Miller

Arvel Miller

1599762180

Setup a Simple Server-Side Application for Your Game Using Strapi.js

In the previous part, we successfully installed and ran a Strapi application. Now it is time to build the API. But first let’s take a close look at what a game server like this can provide us:

  • It can authenticate users.
  • We can implement game logics.
  • Users can see leaderboads.
  • We can change some of the game elements through the server.
  • and lots of other functionalities.

Controllers, Services and Routes

For building an API in Strapi, you need to understand 3 main concepts.

**Services: **Services serve as global methods which you can use everywhere in your application. Their main job is to reduce the use of some repeatable functions by being available globally.

**Controllers: **Controllers are the main part of your API. The main functions go here.

**Routes: **Routes are bounded to controller methods. They are actually Urls which are pointing to a specific method, so each time you call that Url the method gets executed.

Know that you’re familiar with these 3 concepts, we are ready to create a remote config system for our game.

Remote Config

Creating Model

Let’s say we have an RPG game with different heroes, hero types, buildings, maps, items and etc. Each hero has its own specifications such as damage power (if it is melee), range (if it’s of range type), health, mana and so many other parameters. These things need to be balanced and tweaked by game designers frequently. So it is not a good idea to put them inside the client. The best solution is to get all of those data from your server at the very beginning of the game and keep them for later use.

For creating such a functionality, the very first step is to create a model of data. It should be one single model to cover everything that need a config in the game, such as heroes, hero types, items and etc. So we need these fields:

  • **Name **(it defines to what specific part of the game, this object belongs)
  • **Type **(it tells us the type of this object)
  • **Data **(the main data that we want to tweak or adjust later)

Now that we have a model in mind, we open the Strapi’s admin panel:

Image for post

Strapi admin panel

here you can see that Strapi has already made a collection (or content-type) called Users. We need something like that for our Remote Config. A collection that stores our config data.

open the Content-Types Builder:

Image for post

under the Collection Types in the grey column, you can see the blue text saying Create new collection type. Click on it:

Image for post

a window pops open. In the Display name write Remote Config and press Continue. Now you can see this window:

Image for post

here you should create those 3 fields that we talked about. Name,_Type _and Data. For name you can choose _Text _or UID. I recommend you to select _UID _because it guarantees that the content of that field is unique in entire collection. So I select the UID:

Image for post

inside the Name field write name (with small letter because it’s better :) )

and click on +Add another field, For our _Type _field you can select Text:

Image for post

Image for post

#strapi #games #javascript #game-server #nodejs

What is GEEK

Buddha Community

Setup a Simple Server-Side Application for Your Game Using Strapi.js
Arvel  Miller

Arvel Miller

1599762180

Setup a Simple Server-Side Application for Your Game Using Strapi.js

In the previous part, we successfully installed and ran a Strapi application. Now it is time to build the API. But first let’s take a close look at what a game server like this can provide us:

  • It can authenticate users.
  • We can implement game logics.
  • Users can see leaderboads.
  • We can change some of the game elements through the server.
  • and lots of other functionalities.

Controllers, Services and Routes

For building an API in Strapi, you need to understand 3 main concepts.

**Services: **Services serve as global methods which you can use everywhere in your application. Their main job is to reduce the use of some repeatable functions by being available globally.

**Controllers: **Controllers are the main part of your API. The main functions go here.

**Routes: **Routes are bounded to controller methods. They are actually Urls which are pointing to a specific method, so each time you call that Url the method gets executed.

Know that you’re familiar with these 3 concepts, we are ready to create a remote config system for our game.

Remote Config

Creating Model

Let’s say we have an RPG game with different heroes, hero types, buildings, maps, items and etc. Each hero has its own specifications such as damage power (if it is melee), range (if it’s of range type), health, mana and so many other parameters. These things need to be balanced and tweaked by game designers frequently. So it is not a good idea to put them inside the client. The best solution is to get all of those data from your server at the very beginning of the game and keep them for later use.

For creating such a functionality, the very first step is to create a model of data. It should be one single model to cover everything that need a config in the game, such as heroes, hero types, items and etc. So we need these fields:

  • **Name **(it defines to what specific part of the game, this object belongs)
  • **Type **(it tells us the type of this object)
  • **Data **(the main data that we want to tweak or adjust later)

Now that we have a model in mind, we open the Strapi’s admin panel:

Image for post

Strapi admin panel

here you can see that Strapi has already made a collection (or content-type) called Users. We need something like that for our Remote Config. A collection that stores our config data.

open the Content-Types Builder:

Image for post

under the Collection Types in the grey column, you can see the blue text saying Create new collection type. Click on it:

Image for post

a window pops open. In the Display name write Remote Config and press Continue. Now you can see this window:

Image for post

here you should create those 3 fields that we talked about. Name,_Type _and Data. For name you can choose _Text _or UID. I recommend you to select _UID _because it guarantees that the content of that field is unique in entire collection. So I select the UID:

Image for post

inside the Name field write name (with small letter because it’s better :) )

and click on +Add another field, For our _Type _field you can select Text:

Image for post

Image for post

#strapi #games #javascript #game-server #nodejs

NBB: Ad-hoc CLJS Scripting on Node.js

Nbb

Not babashka. Node.js babashka!?

Ad-hoc CLJS scripting on Node.js.

Status

Experimental. Please report issues here.

Goals and features

Nbb's main goal is to make it easy to get started with ad hoc CLJS scripting on Node.js.

Additional goals and features are:

  • Fast startup without relying on a custom version of Node.js.
  • Small artifact (current size is around 1.2MB).
  • First class macros.
  • Support building small TUI apps using Reagent.
  • Complement babashka with libraries from the Node.js ecosystem.

Requirements

Nbb requires Node.js v12 or newer.

How does this tool work?

CLJS code is evaluated through SCI, the same interpreter that powers babashka. Because SCI works with advanced compilation, the bundle size, especially when combined with other dependencies, is smaller than what you get with self-hosted CLJS. That makes startup faster. The trade-off is that execution is less performant and that only a subset of CLJS is available (e.g. no deftype, yet).

Usage

Install nbb from NPM:

$ npm install nbb -g

Omit -g for a local install.

Try out an expression:

$ nbb -e '(+ 1 2 3)'
6

And then install some other NPM libraries to use in the script. E.g.:

$ npm install csv-parse shelljs zx

Create a script which uses the NPM libraries:

(ns script
  (:require ["csv-parse/lib/sync$default" :as csv-parse]
            ["fs" :as fs]
            ["path" :as path]
            ["shelljs$default" :as sh]
            ["term-size$default" :as term-size]
            ["zx$default" :as zx]
            ["zx$fs" :as zxfs]
            [nbb.core :refer [*file*]]))

(prn (path/resolve "."))

(prn (term-size))

(println (count (str (fs/readFileSync *file*))))

(prn (sh/ls "."))

(prn (csv-parse "foo,bar"))

(prn (zxfs/existsSync *file*))

(zx/$ #js ["ls"])

Call the script:

$ nbb script.cljs
"/private/tmp/test-script"
#js {:columns 216, :rows 47}
510
#js ["node_modules" "package-lock.json" "package.json" "script.cljs"]
#js [#js ["foo" "bar"]]
true
$ ls
node_modules
package-lock.json
package.json
script.cljs

Macros

Nbb has first class support for macros: you can define them right inside your .cljs file, like you are used to from JVM Clojure. Consider the plet macro to make working with promises more palatable:

(defmacro plet
  [bindings & body]
  (let [binding-pairs (reverse (partition 2 bindings))
        body (cons 'do body)]
    (reduce (fn [body [sym expr]]
              (let [expr (list '.resolve 'js/Promise expr)]
                (list '.then expr (list 'clojure.core/fn (vector sym)
                                        body))))
            body
            binding-pairs)))

Using this macro we can look async code more like sync code. Consider this puppeteer example:

(-> (.launch puppeteer)
      (.then (fn [browser]
               (-> (.newPage browser)
                   (.then (fn [page]
                            (-> (.goto page "https://clojure.org")
                                (.then #(.screenshot page #js{:path "screenshot.png"}))
                                (.catch #(js/console.log %))
                                (.then #(.close browser)))))))))

Using plet this becomes:

(plet [browser (.launch puppeteer)
       page (.newPage browser)
       _ (.goto page "https://clojure.org")
       _ (-> (.screenshot page #js{:path "screenshot.png"})
             (.catch #(js/console.log %)))]
      (.close browser))

See the puppeteer example for the full code.

Since v0.0.36, nbb includes promesa which is a library to deal with promises. The above plet macro is similar to promesa.core/let.

Startup time

$ time nbb -e '(+ 1 2 3)'
6
nbb -e '(+ 1 2 3)'   0.17s  user 0.02s system 109% cpu 0.168 total

The baseline startup time for a script is about 170ms seconds on my laptop. When invoked via npx this adds another 300ms or so, so for faster startup, either use a globally installed nbb or use $(npm bin)/nbb script.cljs to bypass npx.

Dependencies

NPM dependencies

Nbb does not depend on any NPM dependencies. All NPM libraries loaded by a script are resolved relative to that script. When using the Reagent module, React is resolved in the same way as any other NPM library.

Classpath

To load .cljs files from local paths or dependencies, you can use the --classpath argument. The current dir is added to the classpath automatically. So if there is a file foo/bar.cljs relative to your current dir, then you can load it via (:require [foo.bar :as fb]). Note that nbb uses the same naming conventions for namespaces and directories as other Clojure tools: foo-bar in the namespace name becomes foo_bar in the directory name.

To load dependencies from the Clojure ecosystem, you can use the Clojure CLI or babashka to download them and produce a classpath:

$ classpath="$(clojure -A:nbb -Spath -Sdeps '{:aliases {:nbb {:replace-deps {com.github.seancorfield/honeysql {:git/tag "v2.0.0-rc5" :git/sha "01c3a55"}}}}}')"

and then feed it to the --classpath argument:

$ nbb --classpath "$classpath" -e "(require '[honey.sql :as sql]) (sql/format {:select :foo :from :bar :where [:= :baz 2]})"
["SELECT foo FROM bar WHERE baz = ?" 2]

Currently nbb only reads from directories, not jar files, so you are encouraged to use git libs. Support for .jar files will be added later.

Current file

The name of the file that is currently being executed is available via nbb.core/*file* or on the metadata of vars:

(ns foo
  (:require [nbb.core :refer [*file*]]))

(prn *file*) ;; "/private/tmp/foo.cljs"

(defn f [])
(prn (:file (meta #'f))) ;; "/private/tmp/foo.cljs"

Reagent

Nbb includes reagent.core which will be lazily loaded when required. You can use this together with ink to create a TUI application:

$ npm install ink

ink-demo.cljs:

(ns ink-demo
  (:require ["ink" :refer [render Text]]
            [reagent.core :as r]))

(defonce state (r/atom 0))

(doseq [n (range 1 11)]
  (js/setTimeout #(swap! state inc) (* n 500)))

(defn hello []
  [:> Text {:color "green"} "Hello, world! " @state])

(render (r/as-element [hello]))

Promesa

Working with callbacks and promises can become tedious. Since nbb v0.0.36 the promesa.core namespace is included with the let and do! macros. An example:

(ns prom
  (:require [promesa.core :as p]))

(defn sleep [ms]
  (js/Promise.
   (fn [resolve _]
     (js/setTimeout resolve ms))))

(defn do-stuff
  []
  (p/do!
   (println "Doing stuff which takes a while")
   (sleep 1000)
   1))

(p/let [a (do-stuff)
        b (inc a)
        c (do-stuff)
        d (+ b c)]
  (prn d))
$ nbb prom.cljs
Doing stuff which takes a while
Doing stuff which takes a while
3

Also see API docs.

Js-interop

Since nbb v0.0.75 applied-science/js-interop is available:

(ns example
  (:require [applied-science.js-interop :as j]))

(def o (j/lit {:a 1 :b 2 :c {:d 1}}))

(prn (j/select-keys o [:a :b])) ;; #js {:a 1, :b 2}
(prn (j/get-in o [:c :d])) ;; 1

Most of this library is supported in nbb, except the following:

  • destructuring using :syms
  • property access using .-x notation. In nbb, you must use keywords.

See the example of what is currently supported.

Examples

See the examples directory for small examples.

Also check out these projects built with nbb:

API

See API documentation.

Migrating to shadow-cljs

See this gist on how to convert an nbb script or project to shadow-cljs.

Build

Prequisites:

  • babashka >= 0.4.0
  • Clojure CLI >= 1.10.3.933
  • Node.js 16.5.0 (lower version may work, but this is the one I used to build)

To build:

  • Clone and cd into this repo
  • bb release

Run bb tasks for more project-related tasks.

Download Details:
Author: borkdude
Download Link: Download The Source Code
Official Website: https://github.com/borkdude/nbb 
License: EPL-1.0

#node #javascript

Setup a Simple Server-Side Application for Your Game Using Strapi.js — Part 1

“Note that this tutorial is done in Windows OS. For Linux you may need to make some changes and modifications ;)”

It’s a fact that having a server-side application, can help developers to add lots of functionalities to their games. They can have a score leaderboard, some game configs they want to change without client updates, occasional events, handling online asset bundles, validating purchases, or even implementing game logic on the server! And that’s just a few of the functionalities I’m talking about!

Today I’m going to introduce something that can help you meet your very basic needs for a game web application. Something that you can easily deploy on a server (Windows/Linux) and use it in a game, no matter what engine or tool you’re using (Unity, Unreal, Game Maker, …).

#unity #strapi #game-server #nodejs #web-applications

Ray  Patel

Ray Patel

1625843760

Python Packages in SQL Server – Get Started with SQL Server Machine Learning Services

Introduction

When installing Machine Learning Services in SQL Server by default few Python Packages are installed. In this article, we will have a look on how to get those installed python package information.

Python Packages

When we choose Python as Machine Learning Service during installation, the following packages are installed in SQL Server,

  • revoscalepy – This Microsoft Python package is used for remote compute contexts, streaming, parallel execution of rx functions for data import and transformation, modeling, visualization, and analysis.
  • microsoftml – This is another Microsoft Python package which adds machine learning algorithms in Python.
  • Anaconda 4.2 – Anaconda is an opensource Python package

#machine learning #sql server #executing python in sql server #machine learning using python #machine learning with sql server #ml in sql server using python #python in sql server ml #python packages #python packages for machine learning services #sql server machine learning services

Eldora  Bradtke

Eldora Bradtke

1597206720

How to Generate Server-Side PDF Reports With Puppeteer, D3 & Handlebars

Looking for a way to create a design-heavy, data-driven, beautifully styled PDF report—server-side with similar tools to what you are already using on the front-end? Stop your Google search. You’ve come to the right place. I was in the same boat as you a few months ago while helping a client with this exact problem. In order to accomplish this feat, I developed a four-step solution using Puppeteer, D3, and handlebars. In this post, I’ll give you step by step instructions on creating server-side pdf reports. Let’s dive in.

An example of a PDF page generated using this method.

An example of a PDF page generated using this method.

In this post, we’ll cover:

  • Setting up Puppeteer and Handlebars
  • Creating a generator to make our PDF
  • Building out a handlebars template
  • Adding the finishing touches

The CHallenges of Creating These PDF Reports:

Because we’re using a template framework to access standard web technologies along with Puppeteer to manage the PDF, we’ll need to think about these things during development:

  • Pages will manually need to be constrained.
  • We won’t have access to CSS media props other than “screen.” (no “page-break-after” or the print media type)
  • We won’t be able to use dev tools to debug irregularities once the PDF is compiled and rendered.
  • Puppeteer itself adds extra build time and size to your deployments.
  • Generating a report can take a while depending on file size.

For this example, let’s assume we already have the base of our project up and running Node/Express, and some type of ORM and DB solutions. We’re all set to feed our sweet, sweet data into a report.

The Tools We Need to Make This Happen

Handlebars

HTML templating framework from the Mustache family. This allows for Partial templating (fancy talk for components) and custom and built-in helper functionality to expand on our logic.

npm install handlebars

Example using partials and built-in blocks

{{#each poleComparison as |page|}}
<div class="page">
  {{#each page.pairs as |polePair|}}
    {{> comparison-header polePair=polePair }}
        <div class="comparison-tables">
            {{> comparison-body polePair=polePair }}
        </div>
  {{/each}}
  {{> footer @root }}
</div>
{{/each}}

Puppeteer

A node library that will provide us access to a chrome headless instance for generating the PDF based on our compiled Handlebars templates.

npm install puppeteer

A list of use cases:

  • Generate screenshots and PDFs of pages.
  • Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. “SSR” (Server-Side Rendering)).
  • Create an up-to-date, automated testing environment.
  • Test Chrome Extensions.

D3 (Data-Driven Documents)

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.

<script src="https://d3js.org/d3.v5.min.js"></script> 

#javascript #server-side #d3.js #js #handlebars #puppeteer #server-side development