forked from Jonathan/Peradventure
		
	Compare commits
	
		
			8 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| d9610bea3b | |||
| e0e6c30752 | |||
| 514d4ebf40 | |||
| 523e33363c | |||
| b72b8942c6 | |||
| 40afb93d61 | |||
| eb8db81789 | |||
| ba73d8f3b6 | 
							
								
								
									
										121
									
								
								app.lua
									
									
									
									
									
								
							
							
						
						
									
										121
									
								
								app.lua
									
									
									
									
									
								
							| @ -1,4 +1,5 @@ | |||||||
| local lapis = require("lapis") | local lapis = require("lapis") | ||||||
|  | local util = require("lapis.util") | ||||||
| local app = lapis.Application() | local app = lapis.Application() | ||||||
| local say = require('lua_scripts/main') | local say = require('lua_scripts/main') | ||||||
| local mapFunc = require('lua_scripts/map') | local mapFunc = require('lua_scripts/map') | ||||||
| @ -8,15 +9,16 @@ local db = require("lapis.db") | |||||||
| local Model = require("lapis.db.model").Model | local Model = require("lapis.db.model").Model | ||||||
|  |  | ||||||
| local map = Model:extend('map') | local map = Model:extend('map') | ||||||
|  | local games = Model:extend('games') | ||||||
|  |  | ||||||
| app:enable("etlua") | app:enable("etlua") | ||||||
| app.layout = require "views.layout" | app.layout = require "views.layout" | ||||||
|  |  | ||||||
| app:post("/clickme", function(self) | app:post("/clickme", function(self) | ||||||
| 	self.text = "Map is already initialized. Beginning game." | 	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 | 	if not self.isPopulated then | ||||||
| 		mapFunc:populate(map) | 		mapFunc:populate(map, self.session.gameref) | ||||||
| 		self.text = "Populated map." | 		self.text = "Populated map." | ||||||
| 	end | 	end | ||||||
| 	return { self.text, layout = false } | 	return { self.text, layout = false } | ||||||
| @ -24,24 +26,135 @@ end) | |||||||
|  |  | ||||||
| app:get("/", function(self) | app:get("/", function(self) | ||||||
| 	self.text = say.hello() | 	self.text = say.hello() | ||||||
| 	self.isPopulated = mapFunc:mapExists(map) | 	self.isPopulated = mapFunc:mapExists(map, self.session.gameref) | ||||||
| 	return { render = "index", | 	return { render = "index", | ||||||
|  |  | ||||||
| 	} | 	} | ||||||
| end) | 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) | app:match("/map/render", function(self) | ||||||
|  | 	if mapFunc:mapExists(map, self.session.gameref) then | ||||||
| 		self.tiles = {} | 		self.tiles = {} | ||||||
| 		for h = 1, 10 do | 		for h = 1, 10 do | ||||||
| 			self.tiles[h] = {} | 			self.tiles[h] = {} | ||||||
| 			for i = 1, 10 do | 			for i = 1, 10 do | ||||||
| 				-- The map database has floor_stone and wall_floor instead of simple ASCII characters | 				-- 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 | 				-- 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) | 				self.tiles[h][i] = mapFunc:mapDecrypt(tile.occupied_by_type) | ||||||
| 			end | 			end | ||||||
| 		end | 		end | ||||||
| 		return { render = "map", layout = false } | 		return { render = "map", layout = false } | ||||||
|  | 	end | ||||||
|  | 		return { layout = false } | ||||||
| 	end) | 	end) | ||||||
|  |  | ||||||
| return app | return app | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ config("development", { | |||||||
|   server = "nginx", |   server = "nginx", | ||||||
|   code_cache = "off", |   code_cache = "off", | ||||||
|   num_workers = "1", |   num_workers = "1", | ||||||
|  |   secret = "M2KisAmerican", | ||||||
|   sqlite = { |   sqlite = { | ||||||
|     database = "database.sqlite3", |     database = "database.sqlite3", | ||||||
|     -- open_flags = ... |     -- open_flags = ... | ||||||
|  | |||||||
							
								
								
									
										
											BIN
										
									
								
								database.sqlite3
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								database.sqlite3
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -4,25 +4,27 @@ local map = { | |||||||
|         { "-", "_", "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", "-", "w", "-", "-", "w", "-", "-", "-", "-" }, |         { "w", "-", "ww", "-", "-", "w", "-", "-", "-", "-" }, | ||||||
|         { "-", "-", "w", "-", "-", "w", "-", "w", "-", "-" }, |         { "-", "-", "ww", "-", "-", "w", "-", "w", "-", "-" }, | ||||||
|         { "-", "-", "-", "-", "-", "-", "-", "w", "-", "-" }, |         { "-", "-", "-", "-", "-", "-", "-", "w", "-", "-" }, | ||||||
|         { "-", "-", "w", "-", "-", "-", "-", "w", "w", "-" }, |         { "-", "-", "ww", "-", "-", "-", "-", "w", "w", "-" }, | ||||||
|         { "-", "-", "w", "-", "-", "-", "-", "-", "w", "-" } |         { "-", "-", "ww", "-", "-", "-", "-", "-", "w", "-" } | ||||||
|  |     }, | ||||||
|  |     tileset = { | ||||||
|  |         display = { "-", "_", "w", "ww" }, | ||||||
|  |         db = { "floor_stone,", "floor_wood", "wall_stone", "wall_wood" } | ||||||
|     }, |     }, | ||||||
|     mapCrypt = function(self, tile) |     mapCrypt = function(self, tile) | ||||||
|         if tile == "-" then |         -- stores tiles as detailed names within the DB | ||||||
|             return "floor_stone" |         for i=1, #self.tileset.display do | ||||||
|         elseif tile == "_" then |             if self.tileset.display[i] == tile then | ||||||
|             return "floor_wood" |                return self.tileset.db[i] | ||||||
|         elseif tile == "w" then |            end | ||||||
|             return "wall_stone" |  | ||||||
|         end |         end | ||||||
|     end, |     end, | ||||||
|     mapDecrypt = function(self, tile) |     mapDecrypt = function(self, tile) | ||||||
|         -- TODO: Rework function so it returns both char and mat type |         -- Decodes tiles into their type (represented by char) and their material | ||||||
|         -- This introduces breaking changes |  | ||||||
|         local tileset = {} |         local tileset = {} | ||||||
|         if string.find(tile, "wall") then |         if string.find(tile, "wall") then | ||||||
|             tileset.char = "w" |             tileset.char = "w" | ||||||
| @ -35,29 +37,24 @@ local map = { | |||||||
|             tileset.material = "wood" |             tileset.material = "wood" | ||||||
|         end |         end | ||||||
|         return tileset |         return tileset | ||||||
|  |  | ||||||
|         -- if tile == "floor_stone" then |  | ||||||
|         --     return "-" |  | ||||||
|         -- elseif tile == "wall_stone" then |  | ||||||
|         --    return "w" |  | ||||||
|         -- end |  | ||||||
|     end, |     end, | ||||||
|     -- Map Populate / check if map exists |     -- Map Populate / check if map exists | ||||||
|     populate = function(self, map) |     populate = function(self, map, game) | ||||||
|         for h = 1, 10 do |         for h = 1, 10 do | ||||||
|             for i = 1, 10 do |             for i = 1, 10 do | ||||||
|                 local tile = map:create({ |                 local tile = map:create({ | ||||||
|                     x = h, |                     x = h, | ||||||
|                     y = i, |                     y = i, | ||||||
|                     occupied_by_type = self:mapCrypt(self.map_tiles[h][i]), |                     occupied_by_type = self:mapCrypt(self.map_tiles[h][i]), | ||||||
|                     occupied_by_id = "" |                     occupied_by_id = "", | ||||||
|  |                     game_ref = game | ||||||
|                 }) |                 }) | ||||||
|             end |             end | ||||||
|         end |         end | ||||||
|     end, |     end, | ||||||
|     mapExists = function(self, map) |     mapExists = function(self, map, game) | ||||||
|         local f = map:select("limit 1") |         local f = map:find({game_ref = game}) | ||||||
|         if not f[1] then |         if not f then | ||||||
|             return false |             return false | ||||||
|         else |         else | ||||||
|             return true |             return true | ||||||
|  | |||||||
							
								
								
									
										20
									
								
								setup_db
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								setup_db
									
									
									
									
									
								
							| @ -6,19 +6,33 @@ sqlite3 database.sqlite3 " | |||||||
| 		x TEXT, | 		x TEXT, | ||||||
| 		y TEXT, | 		y TEXT, | ||||||
| 		occupied_by_type TEXT, | 		occupied_by_type TEXT, | ||||||
| 		occupied_by_id INTEGER | 		occupied_by_id INTEGER, | ||||||
|  | 		game_ref INTEGER | ||||||
| 				); | 				); | ||||||
| 	CREATE TABLE IF NOT EXISTS entity_ref ( | 	CREATE TABLE IF NOT EXISTS entity_ref ( | ||||||
| 		id INTEGER PRIMARY KEY AUTOINCREMENT, | 		id INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||
| 		entity_id INTEGER, | 		entity_id INTEGER, | ||||||
| 		type TEXT, | 		type TEXT, | ||||||
| 		hp INTEGER, | 		hp INTEGER, | ||||||
| 		inventory_ref INTEGER | 		inventory_ref INTEGER, | ||||||
|  | 		game_ref INTEGER | ||||||
| 	); | 	); | ||||||
| 		CREATE TABLE IF NOT EXISTS player_inventory ( | 		CREATE TABLE IF NOT EXISTS player_inventory ( | ||||||
| 		id INTEGER PRIMARY KEY AUTOINCREMENT, | 		id INTEGER PRIMARY KEY AUTOINCREMENT, | ||||||
| 		type TEXT, | 		type TEXT, | ||||||
| 		quantity INTEGER, | 		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 | ||||||
| 	) | 	) | ||||||
| " | " | ||||||
|  | |||||||
| @ -5,3 +5,7 @@ | |||||||
| .stone { | .stone { | ||||||
|     color: gray; |     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 --> | <!-- views/index.etlua --> | ||||||
|  | <!DOCTYPE HTML> | ||||||
| <script src="/static/htmx.min.js"></script> | <script src="/static/htmx.min.js"></script> | ||||||
| <% if not isPopulated then %> | <% if not isPopulated then %> | ||||||
| <div id="clickable"> | <div id="clickable"> | ||||||
| @ -6,18 +7,21 @@ | |||||||
| </div> | </div> | ||||||
| <% end %> | <% end %> | ||||||
|  |  | ||||||
| <article> | <article style="text-align: center;"> | ||||||
|     <p style="text-align: center;">Game</p> |     <h3>Log In / Create Game</h3> | ||||||
|     <main style="width: 50%;" class="container" id="map" hx-get="/map/render" hx-trigger="every 1s,load"> |     <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> | </article> | ||||||
|  |  | ||||||
|  | <a href="/game">Play Now</a> | ||||||
| </main> | </main> | ||||||
|  |  | ||||||
| <!-- | <!-- TODO: On player create use Model to make new game_ref and append game_ref to self_session --> | ||||||
| <ol> |  | ||||||
| <p><% for i, thing in pairs(text) do %> |  | ||||||
|      <li><%= thing %></li> |  | ||||||
|      <% end %> |  | ||||||
| </p> |  | ||||||
| </ol> |  | ||||||
| --> |  | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ | |||||||
| 	<title><%= page_title or "My Page" %></title> | 	<title><%= page_title or "My Page" %></title> | ||||||
| 	<link rel="stylesheet" href="/static/css/pico/pico.min.css" /> | 	<link rel="stylesheet" href="/static/css/pico/pico.min.css" /> | ||||||
| 	<link rel="stylesheet" href="/static/incertitude.css" /> | 	<link rel="stylesheet" href="/static/incertitude.css" /> | ||||||
|  | 	<link rel="stylesheet" href="/static/css/pico.colors.css" /> | ||||||
| </head> | </head> | ||||||
|  |  | ||||||
| <body> | <body> | ||||||
|  | |||||||
							
								
								
									
										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