3

I am doing a little programming exercise in Java to get me back into the swing of things before school. My program will be video game (Team Fortress 2) related and as such I need to store data about a large number of weapons.

My current model is that I have a class for each type of weapon, (e.g. Scout_Primary or Soldier_Secondary) and I will modify the variables in each class depending on what specific weapon is selected. For example if Scout_Primary is "Scattergun" I will modify its base damage and reload speed accordingly.

If you are familiar with TF2 you will know that there is a large amount of weapons. How do I store all the weapons' stats in my program? I do not wish to write giant switch statements containing stats for each weapon in a category. I am very eager to work with SQL more (I have some experience with it but not too much, I have used JDBC before).

Is it possible to create a database and just package it with the program itself so that I don't have to worry about hosting? The only experience I have is with remote databases.

What I would like to do would be to have that local database and perform Selects and pass the data to a constructor. Thanks in advance.

EDIT: I have been looking at Derby, is this the right direction?

0nyx
  • 144
  • 2
  • 12
  • 2
    If the weapons are fixed, you don't need a DB. Just store them in file, as raw binary or in plain text - your choice. – nhahtdh Aug 23 '12 at 18:51
  • 1
    Here is a database you might be interested in http://stackoverflow.com/questions/41233/java-and-sqlite – squiguy Aug 23 '12 at 18:52
  • I would agree with the "just store them in a plain text file" thing, except then you have to decide which lines is for the name, which one for the damage, which one for the description, etc... it can be torture to maintain, even for something that simple! –  Aug 23 '12 at 18:53
  • 1
    Depending on the complexity, you might not need a database at all. Take a look at serialization markup like XML, or YAML. – David B Aug 23 '12 at 18:53
  • ok how about json or any other standard format, I think I used xml for the .NET game I implemented during undergrad, but that was before I learned json. – Samy Vilar Aug 23 '12 at 18:54
  • I would use a database, i prefer mysql. you could also use hibernate to make your mysql tables. and use hibernate query language to get data from then. Its a bit more work but it will pay of in the end! – Jimmy Geers Aug 23 '12 at 18:55
  • Here is a link to a Stackoverflow discussion regarding [file-based databases in Java](http://stackoverflow.com/questions/3575451/file-based-database-in-java). – Philip Tenn Aug 23 '12 at 18:55
  • a db for such a small project would be overkill, and once you work with teams it would mean they would also have to have it set up ... which is why I stick to standard file formats, for this kind of projects. – Samy Vilar Aug 23 '12 at 18:56

5 Answers5

1

Yes, SQL could be appropriate for this. You should look into Apache Derby, its main benefit is that it is lightweight and very easy to embed into a Java application. You can do exactly what you're talking about - use it locally.

The only issue will be bundling the actual data with your application. I would suggest you start with it in a simple file, maybe JSON or XML encoded, or even just CSV - basically anything you choose. Then when the program starts, you check to see if the database exists. If not, you create it, read the file, and store the data from the file into the database. Then you're ready to do querying with SELECT statements or whatever else you want.

Joe K
  • 18,204
  • 2
  • 36
  • 58
  • I have never worked with JSON. I do hear about it quite a bit though, looks like it's time to do some reading. If you know of any good tutorials that would be great. I'll be googling in the meantime. – 0nyx Aug 23 '12 at 19:13
  • It's pretty simple, just a way of formatting structured data as a string. It might be overkill for this purpose, but there are plenty of good libraries for encoding to it/decoding from it. – Joe K Aug 23 '12 at 20:18
0

Why do you need different class for every weapon? I guess creating a class weapon would be better choice, or rather an abstract class which can be extended by various other categories of weapons. How about storing the weapons in a collection (list, map, set depending upon the kind of operation you want to perform). If you aim at storing game stats and launching the game on as a web based app, using database is recommended

Seeder
  • 113
  • 7
0

Whatever you choose, you will most likely have to use SOME sort of database with which to hold your weapons. SQLite is one of the smallest and easiest to implement, although it requires some background knowledge of SQL.

You could also store all the weapons in a plain-text file formatted with JSON or XML... I would opt for JSON because it would involve less typing on your part, but XML is also easy enough to do (and am pretty sure there's an XML parser available already). JSON is, however, easier to implement even though you still have to download the source from json.org.

{
"weapons": [
    { "name":"Scattergun" , "damage":"5" },
    { "name":"Flamethrower" , "damage":"8" , "pyro":"yes" }
]
}

As you can see I have no clue about TF2.

0

I would recommend using a database, especially because you mentioned having experience with JDBC.

If you do not want to have to worry about installing a database engine such as MySQL or Postgres on your server, you could use H2 Database Engine, which can be distributed with your Java Application.

H2 is written in Java, and has a JDBC API.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
0

I think a simple class weapon could do this:

class Weapon {
  private WeaponType weaponType;
  private ClassType classType;
  private int damage;
  ...
}

enum WeaponType {
  PRIMARY, SECONDARY, ..
}

enum ClassType {
  SCOUT, SOLDIER, ...
}

You can also map this to/from json with libraries like Jackson. Json is a lot simpler compared to a database if you have to update values from time to time by hand.

micha
  • 47,774
  • 16
  • 73
  • 80