<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Building Browsergames &#187; SQL</title>
	<atom:link href="http://buildingbrowsergames.com/category/code/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://buildingbrowsergames.com</link>
	<description>Ever wanted to build a browsergame?</description>
	<lastBuildDate>Mon, 29 Mar 2010 14:00:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Saving Database Space through Bit-masking</title>
		<link>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/</link>
		<comments>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 14:00:34 +0000</pubDate>
		<dc:creator>gostyloj</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=283</guid>
		<description><![CDATA[This is a trick you can use to increase the efficiency and readability of your project.  It is an argument for good up front design as utilizing this is only plausible when you take the time and effort at the beginning.  The following is a real world example from my game TerraTanks.
The problem [...]]]></description>
			<content:encoded><![CDATA[<p>This is a trick you can use to increase the efficiency and readability of your project.  It is an argument for good up front design as utilizing this is only plausible when you take the time and effort at the beginning.  The following is a real world example from my game TerraTanks.</p>
<p>The problem is that you have an object with a ton of properties that are incredibly similar and they describe the object in a yes|no fashion.  In my case, players can do 24 types of research and the state of the player is &#8220;yes, I have done that particular research&#8221; or &#8220;no, I have not done that research&#8221;.</p>
<p>One solution is to make a table with a column that associates with the player id and a boolean column for every type of research that you have.  Now if you have 24 types of research your table is 25 columns big.  This can get out of hand pretty quickly.  The table becomes hard to read and you have to use different code (or procedurally dynamic code) to set individual columns.</p>
<p>Another solution is to add a column to your player definition table and make it type INT UNSIGNED.  Then you let your code efficiently handle interpreting the integer as the player&#8217;s research definition through bit masking.  Here&#8217;s how it works.</p>
<p>The maximum value of an unsigned INT in MySQL is <code class="literal">4294967295. </code> In binary this number looks like 11111111111111111111111111111111.  That is 32 1&#8217;s in a row.  Each of those digits can describe a research type as &#8216;have&#8217; (it is a 1) or &#8216;have not&#8217; (it is a 0).  Now in a global file for your code you need to define each research type as a number that is a power of 2.  It would look something like this in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$g_shield_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>              <span style="color: #666666; font-style: italic;">// in binary 001</span>
<span style="color: #000088;">$g_armor_piercing_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// in binary 010</span>
<span style="color: #000088;">$g_mining_research</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// in binary 100</span></pre></div></div>

<p>
Now if you want to know whether you have a particular research you would perform a bitmask operation on the integer you retrieve from your database using the &amp; operator.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// will mask players research and return true if the mining bit is set to 1</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">research</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$g_mining_research</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>
The bit masking procedure is extremely efficient and fast and you can see how it compresses all the research information into the size of an integer.  Also, if you want to know everything about a player&#8217;s research you only have to retrieve a single integer from the database.</p>
<p>Assigning research to a player is also very easy.  Simply bitwise OR the current research integer with the set bitmask using the | operator:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newPlayerResearch</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">research</span> <span style="color: #339933;">|</span> <span style="color: #000088;">$g_shield_research</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can technically add the two numbers to get the same result, but this is unsafe because if you add the research when it is already there it will throw everything off.</p>
<p>There are some pitfalls to using this trick.  While it is easy to add another type of research just by assigning its mask to the next highest power of 2, you are limited to 32 total research types.  One way to get around this is to make the column type BIGINT which would give you 64 bits to work with, but at the end of the day you are still limited.  Also, once a game starts the research you choose for that bit position is pretty much stuck there unless you want to do some math maintenance.</p>
<p>While this trick will tend to make your code more readable because database statements won&#8217;t be as long, your database entry will not be human readable so it could slow down your debugging efforts.</p>
<p>So there you have it.  A very powerful tool if used wisely.  Please design well before you start writing code.  It makes life easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/09/04/saving-database-space-through-bit-masking/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Diary of a Browsergame: Setting up the database</title>
		<link>http://buildingbrowsergames.com/2008/07/14/diary-of-a-browsergame-setting-up-the-database/</link>
		<comments>http://buildingbrowsergames.com/2008/07/14/diary-of-a-browsergame-setting-up-the-database/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 14:00:02 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[diaryofabrowsergame]]></category>
		<category><![CDATA[workingtitle]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=83</guid>
		<description><![CDATA[One of the first things I do for any new project of mine is design the database &#8211; so that&#8217;s what&#8217;s going to happen to Working Title today. This design will probably change slowly over the course of development &#8211; it&#8217;s just a good starting point for now.
To begin with, we&#8217;ll need a users table.

CREATE [...]]]></description>
			<content:encoded><![CDATA[<p>One of the first things I do for any new project of mine is design the database &#8211; so that&#8217;s what&#8217;s going to happen to Working Title today. This design will probably change slowly over the course of development &#8211; it&#8217;s just a good starting point for now.</p>
<p>To begin with, we&#8217;ll need a users table.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	username text<span style="color: #66cc66;">,</span>
	password text<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And once we have that, a <em>stats</em> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	display_name text<span style="color: #66cc66;">,</span>
	short_name varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>If this all looks familiar so far, that&#8217;s because it should &#8211; this is the exact same table structure(currently, anyway) as the game we&#8217;ve been working on. I&#8217;m taking all of the design decisions and changes that we made in our other project, and doing my best to apply them here &#8211; thereby continually improving my code, and my design.</p>
<p>Because Working Title will have a few distinct &#8216;entities&#8217;, my <em>entity_stats</em> is going to look a little bit different than the one we&#8217;re using right now. According to <a href='http://buildingbrowsergames.com/workingtitle/design/1.0.html'>the design document</a>, there are 4 main entities &#8211; players, rooms, monsters, and items. Those will make the enum in the <em>entity_stats</em> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> entity_stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	stat_id int<span style="color: #66cc66;">,</span>
	entity_id int<span style="color: #66cc66;">,</span>
	value text<span style="color: #66cc66;">,</span>
	entity_type ENUM<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'User'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Monster'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Room'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Thinking about it a little more, I&#8217;m pretty sure that I don&#8217;t <strong>need</strong> separate tables for each of the entities that can exist within Working Title &#8211; all I need is a single table to keep track of their name, ID, description, and type. So that&#8217;s what I&#8217;m going to do:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> entities <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	name text<span style="color: #66cc66;">,</span>
	entity_type ENUM<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Monster'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Room'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	description text<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>One of the things that the design document mentions is that &#8220;users will move between rooms&#8221; &#8211; which means that I will need way to track which room a user is currently in. I&#8217;m going to use a stat to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Room'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_room'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>There are also supposed to be players, monsters, <strong>and</strong> items inside rooms &#8211; so I&#8217;ll need a table to keep track of all the entities within a room:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> room_entities <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	room int<span style="color: #66cc66;">,</span>
	entity_id int<span style="color: #66cc66;">,</span>
	entity_type ENUM<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Monster'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'User'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Item'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>	
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Monsters will be able to drop items, so I&#8217;m going to need to add a table to do that. Because all they&#8217;re going to be able to drop is items(not other monsters or rooms or anything), I can make it so that the <em>monster_drops</em> table only contains information on what items, and what the percentage chance is for them to drop:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> monster_drops <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	monster int<span style="color: #66cc66;">,</span>
	item int<span style="color: #66cc66;">,</span>
	drop_rate int<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The next table I need to add is one to track a user&#8217;s inventory &#8211; so I&#8217;ll add the <em>user_items</em> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> user_items <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	user_id int<span style="color: #66cc66;">,</span>
	item int<span style="color: #66cc66;">,</span>
	quantity int<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The design document mentions <em>&#8220;____ of holding&#8221;</em>&apos;s, which is something I haven&#8217;t quite figured out yet. I <strong>could</strong> just use a stat to keep track of the player&#8217;s current inventory limit, or I could come up with a better idea. For the moment, I&#8217;m going to leave this piece of functionality out of my database design &#8211; I&#8217;m not sure how to build it yet.(If you know a good way to do this, send me an e-mail at <a href='mailto:buildingbrowsergames@gmail.com'>buildingbrowsergames@gmail.com</a>, or comment on this blog post)</p>
<p>Based on the list of commands players can use, there are a few stats that I can add into the database right now:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Maximum Health'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'max_hp'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Health'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_hp'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Weapon'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_weapon'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Armor'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_armor'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Ring (Left Hand)'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_ring_left'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Ring (Right Hand)'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_ring_right'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Gold In Bank'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'bank_gc'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Gold In Hand'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'hand_gc'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Level'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_lvl'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Current Experience'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'cur_exp'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> stats<span style="color: #66cc66;">&#40;</span>display_name<span style="color: #66cc66;">,</span>short_name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Experience to Next Level'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'next_exp'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>That sets me up with some of my starter stats, although it&#8217;s definitely not all of them &#8211; I&#8217;m sure I&#8217;ll end up adding more stats as Working Title grows into more of a finished product.</p>
<p>The last table that I&#8217;m going to add for now is simply for keeping track of exits off of rooms: <em>room_exits</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> room_exits <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	name text<span style="color: #66cc66;">,</span>
	from_room int<span style="color: #66cc66;">,</span>
	to_room int<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I&#8217;ll be tracking the <em>entity_id</em> of both of the rooms, and using the <em>name</em> value to select which exit. For example, an exit to the North from room 1 to 2 might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> room_exits<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>from_room<span style="color: #66cc66;">,</span>to_room<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'North'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>With all this finished, there&#8217;s only one more system to add &#8211; spells. I&#8217;m not quite sure how I want to implement spells yet, either &#8211; do I want to have them do a set amount of damage, or base it on level, or something else? I&#8217;m going to hold off on designing any database tables for my spells system for now &#8211; once I have Working Title in a playable state, I&#8217;ll work on adding spells. For now though, I&#8217;m going to stick with what I have &#8211; I&#8217;ve got the basics of my database in place, and I can safely start developing now.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/14/diary-of-a-browsergame-setting-up-the-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Designing a flexible items system</title>
		<link>http://buildingbrowsergames.com/2008/07/02/designing-a-flexible-items-system/</link>
		<comments>http://buildingbrowsergames.com/2008/07/02/designing-a-flexible-items-system/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 14:00:36 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=75</guid>
		<description><![CDATA[When building an inventory system, there are a lot of different approaches that you can take. Some developers simply hard-code everything, while others make sure that nothing is hard-coded &#8211; and in between both of these two extremes is a comfortable middle point, where we have most of the flexibility of not having anything hard-coded, [...]]]></description>
			<content:encoded><![CDATA[<p>When building an inventory system, there are a lot of different approaches that you can take. Some developers simply hard-code everything, while others make sure that <strong>nothing</strong> is hard-coded &#8211; and in between both of these two extremes is a comfortable middle point, where we have most of the flexibility of not having anything hard-coded, while still being able to develop with the same level of ease as if we had hard-coded it.</p>
<p>Designing an inventory system like this takes time &#8211; you can&#8217;t just throw everything together and hope it works. You need to plan it out first.</p>
<p>One question you need to ask yourself is: what about different item types? If there are weapons, armor, and general items, how will you handle that?</p>
<p>A hard-coded solution would be to create three tables &#8211; <em>weapons</em>,<em>armor</em>, and <em>items</em>. But at that point, you have a problem; how will you link specific items to something like monsters? You would need to create three <strong>more</strong> tables &#8211; <em>monster_weapons</em>, <em>monster_armor</em>, and <em>monster_items</em>. And then figuring out what particular item a monster might drop at any given time would be a major headache.</p>
<p>An easier solution to this is to create a single <em>items</em> table, with a &#8216;type&#8217; column that can be used to track the type &#8211; that way, we only need to link monsters up to single items, and we can retrieve all of the monster&#8217;s droppables with a single, simple database query. Here&#8217;s what a simple items table might look like:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> items <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	name text<span style="color: #66cc66;">,</span>
	type text<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>At this point, we now have a single table that we can use to track our different items &#8211; and the flexbility to categorize them all by type if we so choose. This allows us to do something like this to create three different items(each a different type):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Wooden Armor'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'armor'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Wooden Sword'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'weapon'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> items<span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span>type<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Red Potion'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'consumable'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>By building our items table in this way, we only need one table to link a monster to the items that they will drop &#8211; and we can easily make them drop any type of item we want them to. We can also create an <em>item_stats</em> table, so that all of our items can have stats applied to them:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> item_stats <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	item_id int<span style="color: #66cc66;">,</span>
	stat_id int<span style="color: #66cc66;">,</span>
	value text<span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And this way, we can leverage our stats system to allow our items to have the same stats as our players or our monsters. We can easily add and remove stats for any item we choose &#8211; if a potion should have a defence of 4 and a maximum HP of 8, we can do that. The flexibility is there.</p>
<p>While it&#8217;s not the <strong>perfect</strong> system, this inventory system is &#8216;good enough&#8217; &#8211; as long as the only thing you know is that you will need to add and remove item types, and have different items with different stats, this item system will work perfectly for you.</p>
<p class='blurb'>Do you know of a better way to design an inventory system? Shoot me an e-mail at <a href='mailto:buildingbrowsergames@gmail.com'>buildingbrowsergames@gmail.com</a>, or leave a message in the comments!</p>
<p></p>
<p><strong>Edit:</strong> <a href='http://www.seelenradio.de/'>sepp</a> has pointed out that the <em>item.type</em> column would be a perfect location to use what&#8217;s known as an enum. According to the <a href='http://dev.mysql.com/doc/refman/5.1/en/enum.html'>MySQL documentation on enums</a>, an enum is &#8220;a string object with a value chosen from a list of allowed values that are enumerated explicitly in the column specification at table creation time.&#8221; &#8211; which is perfect for what we need. Because we will only have so many item types within our game, using an enum will ensure that we can&#8217;t have situations where we make a typo and end up with an item with a type of &#8216;weapoh&#8217; or anything. Here&#8217;s the table creation code, using a basic enum to set up 3 item types:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> items <span style="color: #66cc66;">&#40;</span>
	id int <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
	name text<span style="color: #66cc66;">,</span>
	type ENUM<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Weapon'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Armor'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Usable'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/02/designing-a-flexible-items-system/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

