Category Archives: scala

crud4playplugin

this plugin can be used to generate the boiler plate code to access a database and provide a rest api for it that offers create read update delete. I use Slick for the database access.

from the demo blog project for example

GET /tables/blog ( with optional query params sortBy and direction)

GET /tables/blog// (this queries database where = . You can also use optional query params sortBy and direction)

GET /tables/blog/

POST /tables/blog

PUT /tables/blog

DELETE /tables/blog/:blog_id

Note if GET / queries a timestamp it should be passes a long value not text representation

slick,controller and routes generated from single configuration file.

You have to create the database as well as configure it in application.conf

There are sample test projects under directory

https://github.com/bernardjason/crud4playplugin/tree/master/src/sbt-test/crudplugin

while a fuller project
https://github.com/bernardjason/tracking

which is deployed on Heroku here
https://bjason-tracking.herokuapp.com/#

spiderzap

This is an augmented reality game developed with libgdx and scala. You must have a gyroscope on your smartphone. The aim is to kill the spiders dropping down, as well as stop the spiders making alien webs. You can get closer to the spiders by holding the bottom of the screen and walking. To fire press anywhere else apart from the bottom of the screen. The spiders will always keep coming.

The walking is simply implemented by checking vibrations of the phone and assuming if you hold the bottom of the screen then you are moving in the real world.

plodder

you tube demo

This is a Play Framework application that stores markdown pages in a Sqlite database accessed using REST services.

The intention of the application is that it is deployed as a WAR file running alongside another web application. You can create notes about your application as you develop as well as keep those notes beside the application. I was inspired by poor documentation of webservices in a WSDL when I’m after are use cases for a webservice or something akin to a hello world that makes the usage of the service clear. This is also why I hooked in the SoapUI libraries so that I could import a WSDL and generated sample requests and responses. Also with a number of security caveats it is possible to also hit a “TryIt” button and call the webservice.

There is no accounting for multi user access to this application or security of any kind. What’s needed and not done yet is a facility to export the markdown documents in the Sqlite database and generate static pages.

I’m using the excellent javascript library https://github.com/chjj/marked to actually do the presentation work here.

building

I’ve checked in a demo Sqlite database with the code btw

clone the code, then

./activator run

Visit from a browser (tested with firefox and chrome)

http://127.0.0.1:9000/plodder/

deploying as a war


./activator war

then copy file plodder-1.0-SNAPSHOT.war to your application then make sure its context path is /plodder. There is an example application cxfdemo that uses plodder here.

create a page

Once running, I am assuming using activator run rather than as a WAR for now go to

http://127.0.0.1:9000/plodder/

From the top menu select “edit”

The next page shows the path that the page would be saved to, use /hello and the box on the left is where the markdown source goes. To create a new page hit the create button and to update an existing page use save. These 2 buttons equate to POST and PUT a page of markdown while there is a get to.

run time environment

Visit

http://127.0.0.1:9000/plodder/markdown/home

this accesses a home page for your application. If it doesn’t exist it creates a list of pages. There is no ability to edit pages now.

BEEB

Bernard’s Enterprise Edition Basic

Basic for the JVM. The github project at https://github.com/bernardjason/beeb allows a basic program to be developed and compiled to Java classes.

YouTube demo video

The project was/is a rough and limited attempt at implementing something like BBC Basic on the Java Virtual Machine. There are plenty of things missed out as well as a couple of extras like making it capable of being a webserver, database access and some JSON support.

I’ve included a version of the hangman program in the BBC user guide as well as a simple webserver that offers a blog writing to a database in BEEB basic.

play rest swagger and some google charts

I’ve got an article published in Linux Format issue 214 https://www.linuxformat.com/archivesabout Rest,Swagger and Play with a demo application using Google Charts to display a timeseries overtime. There are 2 demos, one tracking the share price of BT compared to FTSE 100 and the temperature difference between London and Darlington.

The share price information uses Yahoo’s YQL to query a source of stock quotes yahoo.finance.quotes.
stockprice_graph

The temperature information is available from Open Weather Map API
temperature_graph

The database for Play implementation is Sqlite. The play implementation is 2.4 as 2.5 was too fresh to support Swagger at the time. I wanted to show how easy it is to add Swagger annotations to a simple interface.
weather_api

Finally as it’s Play it was easy to deploy this to Heroku. Albeit changing the database from Sqlite to Postgres. See
https://secure-retreat-29275.herokuapp.com/

and the 2 charts

Source code is available here
https://github.com/bernardjason/rest-swagger-app

weather chart
stock chart

hole in one, demo android/dekstop scala game using libgdx

I’ve produce this very simple game

source code available here https://github.com/bernardjason/holeinone

I’ve pulled together some code I had to show how to put together something using libgdx and scala that can run on either desktop or android platform. I’ve not tried it with a Mac. This wont work with HTML flavour libgdx as it’s scala.

The game uses the arrow keys, mouse or screen press to move the gun. Press and hold the spacebar, mouse button or screen then release to fire a missile at the bouncing ball.

The intention of this code is something to pick the bones out of or use as a start for something else.

Things of note
file local.properties contains the location of the android sdk. So this needs to be changed to where you’ve installed it.

core/build.gradle has been changed to build scala source

class FlatTerrain extends class BaseTerrain. This class supports an uneven terrain as well as the simple flat terrain shown in the game.

class bernardcjason.gdx.basic.shape.Cuboid shows mapping parts of a texture to a cube. This is a change to the usual mapping of a texture shape which is
base

to a linear

terrain

For collision detection I’ve not used Bullet physics but rather sphere collision bernardcjason.gdx.basic.shape.CollideSphere.

drivedog

I’ve created a utility to try and mimic what Dropbox does on Linux for Google Drive. Google hasn’t provided as yet a Linux tool to keep a local Linux directory and Google Drive in sync, unlike Dropbox.

The project is available here
https://github.com/bernardjason/drivedog

The utility uses Google’s api library rather than using the REST api directly so integration with Scala was straight forward. I also provided some integration with the Linux status tray as well as using Swing for Scala which was something new for me. The utility is capable of being nohup’ed safely, and as it uses sbt assembly it is self contained, needing only a jdk 1.7 or greater to run.

My aim with this tool was to provide a way for me to backup files from my Linux box to the free 15gb I have on Google.

One comment I did see for another tool for Linux called “drive”, “Background sync is not just hard, it is stupid”. I’d agree it is harder than I thought, I’ll see how I get on now I’m using my own tool in anger as to how stupid I was writing it.

Start using Scala

Go to core project and the desktop project, right click and select Configure, then add Scala nature

You should now have in the build path

“Scala Library Container”

Screenshot-Scala - Scala IDE

you need a Scala nature on both projects for adding Scala code to the core as well as running the program via desktop project. That said all logic should sit in core project if you are to run the final app on android and desktop.

find the SimpleGame.java class in the core project and delete it.

Create a new SimpleGame.scala class instead in the same package, with the following code

 

package bernardcjason.libgdx
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Colorclass SimpleGame extends ApplicationAdapter {
    lazy val batch = new SpriteBatch()
    lazy val font = new BitmapFont()
    override def create() {
        font.setColor(Color.BLUE);
    }
    override def render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        font.draw(batch, "Hello world", Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
        batch.end();
    }
}

 

to enable scala when building from command line add

cat core/build.gradle
apply plugin: “scala”
dependencies{
compile “org.scala-lang:scala-library:2.11.6”
}

cat gradle.properties
org.gradle.daemon=true
org.gradle.jvmargs=-Xms128m -Xmx512m
org.gradle.configureondemand=true
org.gradle.jvmargs=-XX:MaxPermSize=512m

./gradlew clean desktop:run

./gradlew clean android:installDebug android:run

See https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline#running-the-desktop-project