1 Replies - 583 Views - Last Post: 18 December 2009 - 12:59 AM Rate Topic: -----

#1 Dophert  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 60
  • Joined: 19-October 09

Game server problem

Posted 17 December 2009 - 02:23 PM

So I'm writing a multiplayer game server. Yet I'm having trouble logging in on a second account. I don't know
where my problem lies and I've been searching for quite some time now. I don't expect much people around
here to help me with my problem, but least I can do is try.

PS: If you need more classes, ask. Since I have about 20.


package org.hannes;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collections;

import java.util.HashMap;
import java.util.Map;
import org.hannes.model.Player;
import org.hannes.net.Connection;
import org.hannes.util.FutureManager;
import org.xsocket.connection.INonBlockingConnection;

/**
 *
 * @author Hannes de Coole
 */
public class World implements Runnable {

	private final Map<INonBlockingConnection, Connection> connections;

	private static class SingletonContainer {
		private static final World singleton = new World();
	}

	private World() {
		connections = Collections.synchronizedMap(new HashMap<INonBlockingConnection, Connection>());
		FutureManager.getSingleton().scheduleContinuous(this, 500);
	}

	public static World getWorld() {
		return SingletonContainer.singleton;
	}

	public void addConnection(INonBlockingConnection nbc) {
		if(connections.containsKey(nbc)) {
			System.out.println(nbc.getRemoteAddress() + " is logging in twice.");
			return;
		}
		Connection c = new Connection(nbc);
		connections.put(nbc, c);
		int slot = getSlot(c);
		if(slot > 0) c.setSlot(slot);
	}

	public void removeConnection(INonBlockingConnection nbc) {
		connections.remove(nbc);
		rebuildPlayerList();
		System.out.println("Someone logged out.");
	}

	public Connection getConnection(INonBlockingConnection nbc) {
		return connections.get(nbc);
	}

	public void write(ByteBuffer buffer) throws IOException {
		synchronized(connections) {
			for(INonBlockingConnection nbc : connections.keySet()) {
				Connection c = connections.get(nbc);
				c.getNbc().write(buffer);
			}
		}
	}

	public void run() {
		synchronized(connections) {
			for(INonBlockingConnection nbc : connections.keySet()) {
				Connection c = connections.get(nbc);
				c.getPlayer().writeBuffer();
			}
		}
	}

	public void rebuildPlayerList() {
		ByteBuffer buffer = ByteBuffer.allocate(1 + 16 * connections.size());
		buffer.put((byte) 2);
		synchronized(connections) {
			for(INonBlockingConnection nbc : connections.keySet()) {
				Connection c = connections.get(nbc);
				Player p = c.getPlayer();
				buffer.putInt(p.getParent().getSlot());
				buffer.putInt(p.getType());
				buffer.putInt(p.getX());
				buffer.putInt(p.getY());
			}
		}
		buffer.flip();
		synchronized(connections) {
			for(INonBlockingConnection nbc : connections.keySet()) {
				Connection c = connections.get(nbc);
				Player p = c.getPlayer();
				p.getBuffer().put(buffer);
				buffer.flip();
			}
		}
	}

	public int getSlot(Connection c) {
		for(int slot = 0; slot < 2048; slot++) {
			synchronized(connections) {
				for(INonBlockingConnection nbc : connections.keySet()) {
					Connection connection = connections.get(nbc);
					if(slot == connection.getSlot())
						return slot;
				}
			}
			return slot;
		}
		return -1;
	}

}




package org.hannes.model;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hannes.net.Connection;

/**
 *
 * @author Hannes de Coole
 */
public class Player extends Entity {

	private int x;
	private int y;
	private int type;

	private String name;
	private String pass;
	private ByteBuffer buffer = ByteBuffer.allocate(2048);
	private Connection parent;

	public Player(Connection parent) {
		this.parent = parent;
	}

	public void processWalk(int x, int y) {
		
	}

	public void sendPlayer() {
		buffer.put((byte) 1);
		buffer.putInt(parent.getSlot()); // slot
		buffer.putInt(type); // type
		buffer.putInt(x); // x
		buffer.putInt(y); //y
	}

	public void writeBuffer() {
		try {
			buffer.flip();
			parent.getNbc().write(buffer);
			buffer = ByteBuffer.allocate(2048);
		} catch (IOException ex) {
			Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return pass;
	}

	public void setPassword(String pass) {
		this.pass = pass;
	}

	public ByteBuffer getBuffer() {
		return buffer;
	}

	public Connection getParent() {
		return parent;
	}

	public String getPass() {
		return pass;
	}

	public int getType() {
		return type;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

}




package org.hannes.net;

import java.io.IOException;
import org.hannes.model.Player;
import org.xsocket.connection.INonBlockingConnection;

/**
 *
 * @author Hannes de Coole
 */
public class Connection {

	private int slot;
	private Player p;
	private INonBlockingConnection nbc;
	public Connection(INonBlockingConnection nbc) {
		this.nbc = nbc;
	}

	public Player getPlayer() {
		return p;
	}

	public void setPlayer(Player p) {
		this.p = p;
	}

	public INonBlockingConnection getNbc() {
		return nbc;
	}

	public void destroy() throws IOException {
		nbc.close();
	}

	public int getSlot() {
		return slot;
	}

	public void setSlot(int slot) {
		this.slot = slot;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Game server problem

#2 Dophert  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 60
  • Joined: 19-October 09

Re: Game server problem

Posted 18 December 2009 - 12:59 AM

I have fixed my problem. I wasn't checking if the player was null or not, sorry for posting such a small error.

EDIT: Can't I lock my own topics?

This post has been edited by Dophert: 18 December 2009 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1