Peradventure/app.lua

161 lines
2.6 KiB
Lua
Raw Normal View History

2024-03-16 18:29:52 -05:00
local lapis = require("lapis")
local util = require("lapis.util")
2024-03-16 18:29:52 -05:00
local app = lapis.Application()
2024-03-16 20:00:13 -05:00
local say = require('lua_scripts/main')
2024-03-18 15:31:53 -05:00
local mapFunc = require('lua_scripts/map')
2024-03-17 16:03:28 -05:00
local player = require('player')
local db = require("lapis.db")
local Model = require("lapis.db.model").Model
2024-03-16 18:29:52 -05:00
2024-03-18 15:31:53 -05:00
local map = Model:extend('map')
2024-03-21 15:16:19 -05:00
local games = Model:extend('games')
2024-03-17 16:54:08 -05:00
2024-03-16 18:29:52 -05:00
app:enable("etlua")
2024-03-17 16:03:28 -05:00
app.layout = require "views.layout"
2024-03-16 18:29:52 -05:00
2024-03-16 20:32:35 -05:00
app:post("/clickme", function(self)
2024-03-18 19:06:37 -05:00
self.text = "Map is already initialized. Beginning game."
self.isPopulated = mapFunc:mapExists(map, self.session.gameref)
2024-03-18 19:06:37 -05:00
if not self.isPopulated then
mapFunc:populate(map, self.session.gameref)
self.text = "Populated map."
end
return { self.text, layout = false }
2024-03-16 20:32:35 -05:00
end)
2024-03-16 20:00:13 -05:00
app:get("/", function(self)
self.text = say.hello()
self.isPopulated = mapFunc:mapExists(map, self.session.gameref)
return { render = "index",
2024-03-17 16:03:28 -05:00
}
2024-03-16 18:29:52 -05:00
end)
2024-03-19 16:37:12 -05:00
app:get("/game", function(self)
self.text = say.hello()
self.isPopulated = mapFunc:mapExists(map, self.session.gameref)
2024-03-19 16:37:12 -05:00
return { render = "game",
}
end)
2024-03-21 15:16:19 -05:00
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 = "<a href='/game'> Play Now</a>"
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 = "<a href='/game'> Play Now</a>"
return {
self.text, self.link, layout = false
}
end
2024-03-21 15:16:19 -05:00
end)
2024-03-18 15:31:53 -05:00
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, game_ref = self.session.gameref })
self.tiles[h][i] = mapFunc:mapDecrypt(tile.occupied_by_type)
end
end
return { render = "map", layout = false }
end
return { layout = false }
end)
2024-03-18 15:31:53 -05:00
2024-03-16 18:29:52 -05:00
return app
2024-03-21 15:16:19 -05:00