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') local player = require('player') local db = require("lapis.db") local Model = require("lapis.db.model").Model local map = Model:extend('map') local games = Model:extend('games') app:enable("etlua") app.layout = require "views.layout" app:post("/clickme", function(self) self.text = "Map is already initialized. Beginning game." self.isPopulated = mapFunc:mapExists(map, self.session.gameref) if not self.isPopulated then mapFunc:populate(map, self.session.gameref) self.text = "Populated map." end return { self.text, layout = false } end) app:get("/", function(self) self.text = say.hello() self.isPopulated = mapFunc:mapExists(map, self.session.gameref) return { render = "index", } end) app:get("/game", function(self) self.text = say.hello() 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 { self.text, layout = false } end local player_exists = games:find({player_id = self.params.playername}) if player_exists then 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, self.link, layout = false } end if not player_exists then self.text = "Created new player." self.game = games:create({ player_id = self.params.playername }) self.session.gameref = self.game.id self.link = " Play Now" return { self.text, self.link, layout = false } end end) app:match("/map/render", function(self) 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 end) return app