[Cherbot-commit] SF.net SVN: cherbot: [96] trunk/src/net/sf/cherbot
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-06-24 19:53:38
|
Revision: 96
http://svn.sourceforge.net/cherbot/?rev=96&view=rev
Author: christianhujer
Date: 2007-06-24 12:53:33 -0700 (Sun, 24 Jun 2007)
Log Message:
-----------
Improved PlayerModule, added ServerModule (unfinished).
Modified Paths:
--------------
trunk/src/net/sf/cherbot/PlayerModule.java
Added Paths:
-----------
trunk/src/net/sf/cherbot/Server.java
trunk/src/net/sf/cherbot/ServerModule.java
Modified: trunk/src/net/sf/cherbot/PlayerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/PlayerModule.java 2007-06-24 18:32:26 UTC (rev 95)
+++ trunk/src/net/sf/cherbot/PlayerModule.java 2007-06-24 19:53:33 UTC (rev 96)
@@ -14,12 +14,12 @@
import java.util.List;
import java.util.ArrayList;
-/**
- * Created by IntelliJ IDEA.
+/** Module for managing players.
* @author <a href="mailto:ch...@ri...">Christian Hujer</a>
*/
public class PlayerModule implements PersistentModule {
+ /** The players. */
private final List<Player> players = new ArrayList<Player>();
/** {@inheritDoc} */
@@ -32,14 +32,32 @@
final String name = result.getString(3);
players.add(new Player(id, serverId, name));
}
+ stmt.close();
}
/** {@inheritDoc} */
public void save(@NotNull final Connection con) throws SQLException {
createTables(con);
final PreparedStatement lookup = con.prepareStatement("SELECT id FROM Player WHERE id = ?");
- final PreparedStatement update = con.prepareStatement("UPDATE Player SET id = ?, serverId = ?, name = ? WHERE id = ?");
+ final PreparedStatement update = con.prepareStatement("UPDATE Player SET serverId = ?, name = ? WHERE id = ?");
final PreparedStatement insert = con.prepareStatement("INSERT INTO Player (id, serverId, name) VALUES (?, ?, ?)");
+ for (final Player player : players) {
+ lookup.setInt(1, player.getId());
+ if (lookup.executeQuery().next()) {
+ update.setInt(1, player.getServerId());
+ update.setString(2, player.getName());
+ update.setInt(3, player.getId());
+ update.executeUpdate();
+ } else {
+ insert.setInt(1, player.getId());
+ insert.setInt(2, player.getServerId());
+ insert.setString(3, player.getName());
+ insert.executeUpdate();
+ }
+ }
+ lookup.close();
+ update.close();
+ insert.close();
}
/** Creates the tables if they don't exist.
@@ -50,6 +68,7 @@
try {
final PreparedStatement stmt = con.prepareStatement("CREATE TABLE Player (id INT, serverId INT, name VARCHAR(32)");
stmt.execute();
+ stmt.close();
} catch (final SQLException ignore) {
System.err.println(ignore);
}
Added: trunk/src/net/sf/cherbot/Server.java
===================================================================
--- trunk/src/net/sf/cherbot/Server.java (rev 0)
+++ trunk/src/net/sf/cherbot/Server.java 2007-06-24 19:53:33 UTC (rev 96)
@@ -0,0 +1,74 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot;
+
+import org.jetbrains.annotations.NotNull;
+
+/** A Server represents a single server instance like a certain IRC network, a certain crossfire server or a certain daimonin server.
+ * The server will be used to establish a connection.
+ * A Cherbot account with server-specific properties like username and password will be associated with the server.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ * @todo add server type and parameters
+ */
+public class Server {
+
+ /** The id of this server. */
+ private int id;
+
+ /** The host name of this server. */
+ @NotNull private String hostname;
+
+ /** The port of this server. */
+ private int port;
+
+ /** Create a Server.
+ * @param id Id of this server.
+ * @param hostname Hostname of this server.
+ * @param port Port of this server.
+ */
+ public Server(final int id, @NotNull final String hostname, final int port) {
+ this.id = id;
+ this.hostname = hostname;
+ this.port = port;
+ }
+
+ /** Returns the id of this server.
+ * @return The id of this server.
+ */
+ public int getId() {
+ return id;
+ }
+
+ /** Returns the hostname of this server.
+ * @return The hostname of this server.
+ */
+ @NotNull public String getHostname() {
+ return hostname;
+ }
+
+ /** Sets the hostname of this server.
+ * @param hostname The hostname of this serer.
+ */
+ public void setHostname(@NotNull final String hostname) {
+ this.hostname = hostname;
+ }
+
+ /** Returns the port of this server.
+ * @return The port of this server.
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /** Sets the port of this server.
+ * @param port The port of this server.
+ */
+ public void setPort(final int port) {
+ this.port = port;
+ }
+
+} // class Server
Property changes on: trunk/src/net/sf/cherbot/Server.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: trunk/src/net/sf/cherbot/ServerModule.java
===================================================================
--- trunk/src/net/sf/cherbot/ServerModule.java (rev 0)
+++ trunk/src/net/sf/cherbot/ServerModule.java 2007-06-24 19:53:33 UTC (rev 96)
@@ -0,0 +1,81 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot;
+
+import org.jetbrains.annotations.NotNull;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.ArrayList;
+import java.util.List;
+
+/** Module for managing servers.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class ServerModule implements PersistentModule {
+
+ /** The players. */
+ private final List<Server> servers = new ArrayList<Server>();
+
+ /** {@inheritDoc} */
+ public void load(@NotNull final Connection con) throws SQLException {
+ final PreparedStatement stmt = con.prepareStatement("SELECT id, serverId, name FROM Player");
+ final ResultSet result = stmt.executeQuery();
+ while (result.next()) {
+ final int id = result.getInt(1);
+ final String host = result.getString(2);
+ final int port = result.getInt(3);
+ servers.add(new Server(id, host, port));
+ }
+ stmt.close();
+ }
+
+ /** {@inheritDoc} */
+ public void save(@NotNull final Connection con) throws SQLException {
+ createTables(con);
+ final PreparedStatement lookup = con.prepareStatement("SELECT id FROM Server WHERE id = ?");
+ final PreparedStatement update = con.prepareStatement("UPDATE Server SET host = ?, port = ? WHERE id = ?");
+ final PreparedStatement insert = con.prepareStatement("INSERT INTO Server (id, host, port) VALUES (?, ?, ?)");
+ for (final Server server : servers) {
+ lookup.setInt(1, server.getId());
+ if (lookup.executeQuery().next()) {
+ update.setString(1, server.getHostname());
+ update.setInt(2, server.getPort());
+ update.setInt(3, server.getId());
+ update.executeUpdate();
+ } else {
+ insert.setInt(1, server.getId());
+ insert.setString(2, server.getHostname());
+ insert.setInt(3, server.getPort());
+ insert.executeUpdate();
+ }
+ }
+ lookup.close();
+ update.close();
+ insert.close();
+ }
+
+ /** Creates the tables if they don't exist.
+ * @param con Connection to use for creating the tables.
+ * @throws SQLException In case of persistence problems.
+ */
+ private void createTables(@NotNull final Connection con) throws SQLException {
+ try {
+ final PreparedStatement stmt = con.prepareStatement("CREATE TABLE Server (id INT, host VARCHAR(64), port INT");
+ stmt.execute();
+ stmt.close();
+ } catch (final SQLException ignore) {
+ System.err.println(ignore);
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void parseMessage(@NotNull final Channel channel, @NotNull final String msg) {
+ }
+
+} // class ServerModule
Property changes on: trunk/src/net/sf/cherbot/ServerModule.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|