forked from Jonathan/Peradventure
		
	Compare commits
	
		
			10 Commits
		
	
	
		
			5fd5394c8e
			...
			master
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| d9610bea3b | |||
| e0e6c30752 | |||
| 514d4ebf40 | |||
| 523e33363c | |||
| b72b8942c6 | |||
| 40afb93d61 | |||
| eb8db81789 | |||
| ba73d8f3b6 | |||
| a3efe2369d | |||
| d7bc833df7 | 
							
								
								
									
										121
									
								
								app.lua
									
									
									
									
									
								
							
							
						
						
									
										121
									
								
								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')
 | 
			
		||||
@ -8,15 +9,16 @@ 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.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 }
 | 
			
		||||
@ -24,24 +26,135 @@ 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",
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
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 = "<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
 | 
			
		||||
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 })
 | 
			
		||||
				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)
 | 
			
		||||
 | 
			
		||||
return app
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,7 @@ config("development", {
 | 
			
		||||
  server = "nginx",
 | 
			
		||||
  code_cache = "off",
 | 
			
		||||
  num_workers = "1",
 | 
			
		||||
  secret = "M2KisAmerican",
 | 
			
		||||
  sqlite = {
 | 
			
		||||
    database = "database.sqlite3",
 | 
			
		||||
    -- open_flags = ...
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								database.sqlite3
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								database.sqlite3
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							@ -1,26 +1,30 @@
 | 
			
		||||
local map = {
 | 
			
		||||
    map_tiles = {
 | 
			
		||||
        { "-", "-", "w", "w", "-", "-", "-", "-", "-", "-" },
 | 
			
		||||
        { "-", "-", "w", "w", "-", "-", "-", "-", "-", "-" },
 | 
			
		||||
        { "-", "_", "w", "w", "-", "-", "-", "-", "-", "-" },
 | 
			
		||||
        { "-", "-", "w", "w", "-", "w", "-", "-", "-", "-" },
 | 
			
		||||
        { "w", "-", "-", "-", "w", "w", "-", "-", "-", "-" },
 | 
			
		||||
        { "w", "-", "w", "-", "-", "w", "w", "w", "-", "-" },
 | 
			
		||||
        { "w", "-", "w", "-", "-", "w", "-", "-", "-", "-" },
 | 
			
		||||
        { "-", "-", "w", "-", "-", "w", "-", "w", "-", "-" },
 | 
			
		||||
        { "w", "-", "ww", "-", "-", "w", "w", "w", "-", "-" },
 | 
			
		||||
        { "w", "-", "ww", "-", "-", "w", "-", "-", "-", "-" },
 | 
			
		||||
        { "-", "-", "ww", "-", "-", "w", "-", "w", "-", "-" },
 | 
			
		||||
        { "-", "-", "-", "-", "-", "-", "-", "w", "-", "-" },
 | 
			
		||||
        { "-", "-", "w", "-", "-", "-", "-", "w", "w", "-" },
 | 
			
		||||
        { "-", "-", "w", "-", "-", "-", "-", "-", "w", "-" }
 | 
			
		||||
        { "-", "-", "ww", "-", "-", "-", "-", "w", "w", "-" },
 | 
			
		||||
        { "-", "-", "ww", "-", "-", "-", "-", "-", "w", "-" }
 | 
			
		||||
    },
 | 
			
		||||
    tileset = {
 | 
			
		||||
        display = { "-", "_", "w", "ww" },
 | 
			
		||||
        db = { "floor_stone,", "floor_wood", "wall_stone", "wall_wood" }
 | 
			
		||||
    },
 | 
			
		||||
    mapCrypt = function(self, tile)
 | 
			
		||||
        if tile == "-" then
 | 
			
		||||
            return "floor_stone"
 | 
			
		||||
        elseif tile == "w" then
 | 
			
		||||
            return "wall_stone"
 | 
			
		||||
        -- stores tiles as detailed names within the DB
 | 
			
		||||
        for i=1, #self.tileset.display do
 | 
			
		||||
            if self.tileset.display[i] == tile then
 | 
			
		||||
               return self.tileset.db[i]
 | 
			
		||||
           end
 | 
			
		||||
        end
 | 
			
		||||
    end,
 | 
			
		||||
    mapDecrypt = function(self, tile)
 | 
			
		||||
        -- TODO: Rework function so it returns both char and mat type
 | 
			
		||||
        -- This introduces breaking changes
 | 
			
		||||
        -- Decodes tiles into their type (represented by char) and their material
 | 
			
		||||
        local tileset = {}
 | 
			
		||||
        if string.find(tile, "wall") then
 | 
			
		||||
            tileset.char = "w"
 | 
			
		||||
@ -33,29 +37,24 @@ local map = {
 | 
			
		||||
            tileset.material = "wood"
 | 
			
		||||
        end
 | 
			
		||||
        return tileset
 | 
			
		||||
 | 
			
		||||
        -- if tile == "floor_stone" then
 | 
			
		||||
        --     return "-"
 | 
			
		||||
        -- elseif tile == "wall_stone" then
 | 
			
		||||
        --    return "w"
 | 
			
		||||
        -- end
 | 
			
		||||
    end,
 | 
			
		||||
    -- Map Populate / check if map exists
 | 
			
		||||
    populate = function(self, map)
 | 
			
		||||
    populate = function(self, map, game)
 | 
			
		||||
        for h = 1, 10 do
 | 
			
		||||
            for i = 1, 10 do
 | 
			
		||||
                local tile = map:create({
 | 
			
		||||
                    x = h,
 | 
			
		||||
                    y = i,
 | 
			
		||||
                    occupied_by_type = self:mapCrypt(self.map_tiles[h][i]),
 | 
			
		||||
                    occupied_by_id = ""
 | 
			
		||||
                    occupied_by_id = "",
 | 
			
		||||
                    game_ref = game
 | 
			
		||||
                })
 | 
			
		||||
            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
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								setup_db
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								setup_db
									
									
									
									
									
								
							@ -6,19 +6,33 @@ sqlite3 database.sqlite3 "
 | 
			
		||||
		x TEXT,
 | 
			
		||||
		y TEXT,
 | 
			
		||||
		occupied_by_type TEXT,
 | 
			
		||||
		occupied_by_id INTEGER
 | 
			
		||||
		occupied_by_id INTEGER,
 | 
			
		||||
		game_ref INTEGER
 | 
			
		||||
				);
 | 
			
		||||
	CREATE TABLE IF NOT EXISTS entity_ref (
 | 
			
		||||
		id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
		entity_id INTEGER,
 | 
			
		||||
		type TEXT,
 | 
			
		||||
		hp INTEGER,
 | 
			
		||||
		inventory_ref INTEGER
 | 
			
		||||
		inventory_ref INTEGER,
 | 
			
		||||
		game_ref INTEGER
 | 
			
		||||
	);
 | 
			
		||||
		CREATE TABLE IF NOT EXISTS player_inventory (
 | 
			
		||||
		id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
		type TEXT,
 | 
			
		||||
		quantity INTEGER,
 | 
			
		||||
		func_ref TEXT
 | 
			
		||||
		game_ref INTEGER
 | 
			
		||||
	);
 | 
			
		||||
	CREATE TABLE IF NOT EXISTS games (
 | 
			
		||||
		id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
		player_id TEXT
 | 
			
		||||
	);
 | 
			
		||||
	CREATE TABLE IF NOT EXISTS players (
 | 
			
		||||
		id INTEGER PRIMARY KEY AUTOINCREMENT,
 | 
			
		||||
		player_name TEXT,
 | 
			
		||||
		x INTEGER,
 | 
			
		||||
		y INTEGER,
 | 
			
		||||
		health INTEGER,
 | 
			
		||||
		game_ref INTEGER
 | 
			
		||||
	)
 | 
			
		||||
"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								static/incertitude.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								static/incertitude.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
.wood {
 | 
			
		||||
    color: brown;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.stone {
 | 
			
		||||
    color: gray;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
h1 {
 | 
			
		||||
    --pico-font-family: Pacifico, cursive;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								views/game.etlua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								views/game.etlua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
<!DOCTYPE HTML>
 | 
			
		||||
<script src="/static/htmx.min.js"></script>
 | 
			
		||||
<% if not isPopulated then %>
 | 
			
		||||
<div id="clickable">
 | 
			
		||||
    <button hx-post="/clickme" hx-target="#clickable" onclick="location.reload()">Start</button>
 | 
			
		||||
</div>
 | 
			
		||||
<% end %>
 | 
			
		||||
<article class="container-fluid">
 | 
			
		||||
    <div class="grid">
 | 
			
		||||
    <script></script>
 | 
			
		||||
    <% if isPopulated then %>
 | 
			
		||||
    <div>
 | 
			
		||||
        <p style="text-align: center;">Map</p>
 | 
			
		||||
        <main style="width: 50%;" class="container" id="map" hx-get="/map/render" hx-trigger="every 5s,load">
 | 
			
		||||
    </div>
 | 
			
		||||
    </main>
 | 
			
		||||
        <div>
 | 
			
		||||
            <% render("views.player_ui") %>
 | 
			
		||||
        </div>
 | 
			
		||||
    <% end %>
 | 
			
		||||
    </div>
 | 
			
		||||
</article>
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
<!-- views/index.etlua -->
 | 
			
		||||
<!DOCTYPE HTML>
 | 
			
		||||
<script src="/static/htmx.min.js"></script>
 | 
			
		||||
<% if not isPopulated then %>
 | 
			
		||||
<div id="clickable">
 | 
			
		||||
@ -6,18 +7,21 @@
 | 
			
		||||
</div>
 | 
			
		||||
<% end %>
 | 
			
		||||
 | 
			
		||||
<article>
 | 
			
		||||
    <p style="text-align: center;">Game</p>
 | 
			
		||||
    <main style="width: 50%;" class="container" id="map" hx-get="/map/render" hx-trigger="every 1s,load">
 | 
			
		||||
<article style="text-align: center;">
 | 
			
		||||
    <h3>Log In / Create Game</h3>
 | 
			
		||||
    <p>Player will be created if player name does not already exist. Names may not contain whitespace.</p>
 | 
			
		||||
    <form>
 | 
			
		||||
    <div class="grid">
 | 
			
		||||
        <input type="text" name="playername">
 | 
			
		||||
        <input type="submit" hx-post="/character-create" hx-trigger="click" value="Execute" hx-target="#replace">
 | 
			
		||||
    </div>
 | 
			
		||||
    </form>
 | 
			
		||||
    <div>
 | 
			
		||||
        <p id="replace"></p>
 | 
			
		||||
    </div>
 | 
			
		||||
</article>
 | 
			
		||||
 | 
			
		||||
<a href="/game">Play Now</a>
 | 
			
		||||
</main>
 | 
			
		||||
 | 
			
		||||
<!--
 | 
			
		||||
<ol>
 | 
			
		||||
<p><% for i, thing in pairs(text) do %>
 | 
			
		||||
     <li><%= thing %></li>
 | 
			
		||||
     <% end %>
 | 
			
		||||
</p>
 | 
			
		||||
</ol>
 | 
			
		||||
-->
 | 
			
		||||
<!-- TODO: On player create use Model to make new game_ref and append game_ref to self_session -->
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,8 @@
 | 
			
		||||
	<meta charset="UTF-8">
 | 
			
		||||
	<title><%= page_title or "My Page" %></title>
 | 
			
		||||
	<link rel="stylesheet" href="/static/css/pico/pico.min.css" />
 | 
			
		||||
	<link rel="stylesheet" href="/static/incertitude.css" />
 | 
			
		||||
	<link rel="stylesheet" href="/static/css/pico.colors.css" />
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
 | 
			
		||||
@ -2,7 +2,7 @@
 | 
			
		||||
<% for h=1,10 do %>
 | 
			
		||||
    <div class="grid">
 | 
			
		||||
    <% for i=1,10 do %>
 | 
			
		||||
       <div><%= tiles[h][i].char %></div>
 | 
			
		||||
       <div class="<%= tiles[h][i].material %>"><%= tiles[h][i].char %></div>
 | 
			
		||||
    <% end %>
 | 
			
		||||
    <br>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								views/player_ui.etlua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								views/player_ui.etlua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
<!DOCTYPE HTML>
 | 
			
		||||
 | 
			
		||||
<p>Look at me!</p>
 | 
			
		||||
		Reference in New Issue
	
	Block a user