diff --git a/src/client/css.css b/src/client/css.css new file mode 100644 index 0000000..e69de29 diff --git a/src/client/index.html b/src/client/index.html new file mode 100644 index 0000000..27918b7 --- /dev/null +++ b/src/client/index.html @@ -0,0 +1,10 @@ + + + + + + + +

Hello, world

+ + diff --git a/src/client/js.js b/src/client/js.js new file mode 100644 index 0000000..fa2782c --- /dev/null +++ b/src/client/js.js @@ -0,0 +1,107 @@ +class DB { + constructor() { + if (typeof(localStorage) === "undefined") { + throw new Error('Hmmm... no "localStorage" support'); + } + } + + get(key) { + b = localStorage.getItem(key); + if (b) { + b = JSON.parse(b); + } else { + b = null; + } + return b + } + + set(key, value) { + b = JSON.stringify(value); + localStorage.setItem(key, b); + } + + del(key) { + localStorage.removeItem(key); + } +} + +class Requests { + put(remote, body, callback) { + this.http("put", remote, callback, body); + } + + post(remote, body, callback) { + this.http("post", remote, callback, body); + } + + get(remote, callback) { + this.http("get", remote, callback, null); + } + + http(method, remote, callback, body) { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == XMLHttpRequest.DONE) { + callback(xmlhttp.responseText, xmlhttp.status); + } + }; + xmlhttp.open(method, remote, true); + if (typeof body == "undefined") { + body = null; + } + xmlhttp.send(body); + } +} + +class Games { + constructor() { + this.db = new DB(); + this.requests = new Requests(); + } + + list(cb) { + this.requests.get("/api/games", (text) => { + var list = JSON.parse(text); + cb(list); + }); + } + + get(id, cb) { + this.requests.get("/api/games/"+id, (text, status) => { + if (status != 200) { + throw new Error("bad status getting game: "+status+": "+text); + } + var game = JSON.parse(text); + cb(game); + }); + } + + create(id, cb) { + this.requests.post("/api/games/"+id, null, (text, status) => { + if (status != 200) { + throw new Error("bad status creating game: "+status+": "+text); + } + var game = JSON.parse(text); + cb(game); + }); + } + + update(id, game, cb) { + this.requests.put("/api/games/"+id, JSON.stringify(game), (text, status) => { + if (status != 200) { + throw new Error("bad status updating game: "+status+": "+text); + } + var game2 = JSON.parse(text); + cb(game2); + }); + } +} + +games = new Games() +games.create("id", console.log); +games.list((ids) => { + console.log(ids); + ids.forEach((id) => { + games.get(id, console.log); + }); +}); diff --git a/src/server/game.go b/src/server/game.go index 667087c..43458f4 100644 --- a/src/server/game.go +++ b/src/server/game.go @@ -16,10 +16,10 @@ type Game struct { } type Player struct { - ID string `json:",omitempty"` - Name string `json:",omitempty"` - Card string `json:",omitempty"` - Balance Currency `json:",omitempty"` + ID string + Name string + Card string + Balance Currency } type Currency int