diff --git a/app.lua b/app.lua index f9dff04..eed884b 100644 --- a/app.lua +++ b/app.lua @@ -1,4 +1,5 @@ local lapis = require("lapis") +local util = require("lapis.util") local app = lapis.Application() local say = require('lua_scripts/main') local mapFunc = require('lua_scripts/map') @@ -15,9 +16,9 @@ app.layout = require "views.layout" app:post("/clickme", function(self) self.text = "Map is already initialized. Beginning game." - self.isPopulated = mapFunc:mapExists(map) + self.isPopulated = mapFunc:mapExists(map, self.session.gameref) if not self.isPopulated then - mapFunc:populate(map) + mapFunc:populate(map, self.session.gameref) self.text = "Populated map." end return { self.text, layout = false } @@ -25,7 +26,7 @@ end) app:get("/", function(self) self.text = say.hello() - self.isPopulated = mapFunc:mapExists(map) + self.isPopulated = mapFunc:mapExists(map, self.session.gameref) return { render = "index", } @@ -33,13 +34,14 @@ end) app:get("/game", function(self) self.text = say.hello() - self.isPopulated = mapFunc:mapExists(map) + self.isPopulated = mapFunc:mapExists(map, self.session.gameref) return { render = "game", } end) app:post("/character-create", function(self) + self.params.playername = util.trim(self.params.playername) if self.params.playername == "" then self.text = "Player name must not be nil. Try again." return { @@ -48,9 +50,12 @@ app:post("/character-create", function(self) end local player_exists = games:find({player_id = self.params.playername}) if player_exists then - self.text = "Player name already exists. Try again." + self.text = "Welcome back, " .. self.params.playername .. "." + local game = games:find({player_id = self.params.playername}) + self.session.gameref = game.id + self.link = " Play Now" return { - self.text, layout = false + self.text, self.link, layout = false } end if not player_exists then @@ -67,18 +72,20 @@ app:post("/character-create", function(self) end) app:match("/map/render", function(self) - self.tiles = {} - for h = 1, 10 do - self.tiles[h] = {} - for i = 1, 10 do - -- The map database has floor_stone and wall_floor instead of simple ASCII characters - -- So we run mapFunc:mapDecrypt to convert it back to the desired characters for visual display - local tile = map:find({ x = h, y = i }) - self.tiles[h][i] = mapFunc:mapDecrypt(tile.occupied_by_type) + if mapFunc:mapExists(map, self.session.gameref) then + self.tiles = {} + for h = 1, 10 do + self.tiles[h] = {} + for i = 1, 10 do + -- The map database has floor_stone and wall_floor instead of simple ASCII characters + -- So we run mapFunc:mapDecrypt to convert it back to the desired characters for visual display + local tile = map:find({ x = h, y = i }) + self.tiles[h][i] = mapFunc:mapDecrypt(tile.occupied_by_type) + end end + return { render = "map", layout = false } end - return { render = "map", layout = false } -end) + end) return app @@ -112,6 +119,22 @@ return app + + + + + + + + + + + + + + + + diff --git a/database.sqlite3 b/database.sqlite3 index a189636..4413c81 100644 Binary files a/database.sqlite3 and b/database.sqlite3 differ diff --git a/lua_scripts/map.lua b/lua_scripts/map.lua index 9da9245..43b3fe5 100644 --- a/lua_scripts/map.lua +++ b/lua_scripts/map.lua @@ -52,9 +52,9 @@ local map = { end end end, - mapExists = function(self, map) - local f = map:select("limit 1") - if not f[1] then + mapExists = function(self, map, game) + local f = map:find({game_ref = game}) + if not f then return false else return true diff --git a/views/game.etlua b/views/game.etlua index 6def7a1..d12a8b5 100644 --- a/views/game.etlua +++ b/views/game.etlua @@ -7,7 +7,7 @@ <% end %>

Game

-
+
<% render("views.player_ui") %> diff --git a/views/index.etlua b/views/index.etlua index 59709e6..88adbe8 100644 --- a/views/index.etlua +++ b/views/index.etlua @@ -9,7 +9,7 @@

Log In / Create Game

-

Player will be created if player name does not already exist.

+

Player will be created if player name does not already exist. Names may not contain whitespace.