Added files via upload

This commit is contained in:
Gabriel Tofvesson 2016-03-21 04:41:02 +01:00
parent 106187bab9
commit 9c0c07e250
28 changed files with 556 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,24 @@
package com.tofvesson.api;
public class Callback implements ICallback{
Runnable r;
int id;
boolean trig=false;
public Callback(Runnable r){this.r=(r!=null)?r:new Runnable(){@Override public void run(){}};}
public Callback(){this(null);}
@Override
public void setCallback(int id){
this.id=id;
trig=true;
r.run();
}
@Override
public int getCallback() {
return id;
}
@Override
public boolean hasCallback() {
return trig;
}
}

View File

@ -0,0 +1,19 @@
package com.tofvesson.api;
public class Coordinate implements ICoordinate{
private int x;
private int y;
public Coordinate(int x, int y){setX(x); setY(y);}
public Coordinate(){this(0, 0);}
@Override
public int getX() {return x;}
@Override
public int getY() {return y;}
@Override
public void setX(int x) {this.x=x;}
@Override
public void setY(int y) {this.y=y;}
}

View File

@ -0,0 +1,7 @@
package com.tofvesson.api;
public interface ICallback {
public abstract void setCallback(int id);
public abstract int getCallback();
public abstract boolean hasCallback();
}

View File

@ -0,0 +1,16 @@
package com.tofvesson.api;
public interface ICoordinate {
public abstract int getX();
public abstract int getY();
public abstract void setX(int x);
public abstract void setY(int y);
public default void setCoordinate(ICoordinate c){
setX(c.getX());
setY(c.getY());
}
public default void setCoordinate(int x, int y){
setX(x);
setY(y);
}
}

View File

@ -0,0 +1,50 @@
package com.tofvesson.api;
import java.util.ArrayList;
import java.util.List;
public interface IGridPosition {
public abstract Type getType();
public default IGridPosition[] getAdjacentOfType(Type type){
List<IGridPosition> i = new ArrayList<>();
int[] posMin=getPosMin();
int[] posMax=getPosMax();
for(int x=posMin[0]; x<posMax[0]; ++x)
for(int y=posMin[1]; y<posMax[1]; ++y)
if((x!=getPos().getX() && y!=getPos().getY()) && getMap().getPosition(x, y).getType()==type)
i.add(getMap().getPosition(x, y));
else if((x!=getPos().getX() && y!=getPos().getY())) --y;
return (IGridPosition[]) i.toArray();
}
public default IGridPosition[] getAdjacentOfValue(int val){
List<IGridPosition> i = new ArrayList<>();
int[] posMin=getPosMin();
int[] posMax=getPosMax();
for(int x=posMin[0]; x<posMax[0]; ++x)
for(int y=posMin[1]; y<posMax[1]; ++y)
if((x!=getPos().getX() && y!=getPos().getY()) && getMap().getPosition(x, y).getValue()==val)
i.add(getMap().getPosition(x, y));
else if((x!=getPos().getX() && y!=getPos().getY())) --y;
return (IGridPosition[]) i.toArray();
}
public abstract void calculateAdjacentMines();
public abstract void overrideType(Type type);
public abstract Type flag();
public abstract IGridPosition setPos(int x, int y);
public abstract IRevealable reveal(ICoordinate[] ignore);
public abstract ICoordinate getPos();
public abstract IMap getMap();
public default int[] getPosMax(){
return new int[]{
(getPos().getX()!=getMap().getSize().getX())?getPos().getX()+1:getPos().getX(),
(getPos().getY()!=getMap().getSize().getY())?getPos().getY()+1:getPos().getY()
};
}
public default int[] getPosMin(){
return new int[]{
(getPos().getX()!=0)?getPos().getX()-1:0,
(getPos().getY()!=0)?getPos().getY()-1:0
};
}
public abstract int getValue();
}

View File

@ -0,0 +1,13 @@
package com.tofvesson.api;
public interface IMap {
public abstract void generate();
public abstract IGridPosition[] getMap();
public abstract ICoordinate getSize();
public abstract IGridPosition getPosition(ICoordinate coord);
public abstract IGridPosition getPosition(int x, int y);
public abstract IGridPosition[][] getSubMap(ICoordinate coord1, ICoordinate coord2);
public abstract IGridPosition[][] getSubMap(int x1, int y1, int x2, int y2);
public default boolean reveal(ICoordinate coord){return reveal(coord.getX(), coord.getY());}
public abstract boolean reveal(int x, int y);
}

View File

@ -0,0 +1,24 @@
package com.tofvesson.api;
public interface IRevealable {
public abstract IGridPosition[] getExpandList();
public abstract IGridPosition[] getNExpandList();
public abstract void addToExpandList(IGridPosition[] i);
public abstract void addToNExpandList(IGridPosition[] i);
public default void expand(ICoordinate[] ignore){
IRevealable[] r = new IRevealable[getExpandList().length];
ICoordinate[] tmp = new ICoordinate[ignore.length+getExpandList().length];
for(int i=0; i<ignore.length; ++i)
tmp[i]=ignore[i];
int i=ignore.length;
for(IGridPosition i1 : getExpandList())
tmp[i++]=i1.getPos();
i=0;
for(IGridPosition i1 : getExpandList())
r[i++]=i1.reveal(tmp);
for(IRevealable r1 : r){
addToExpandList(r1.getExpandList());
addToNExpandList(r1.getNExpandList());
}
}
}

View File

@ -0,0 +1,94 @@
package com.tofvesson.api;
import java.net.Socket;
import java.util.Map;
public interface IServerService extends Runnable{
/**
* Default port to open server on.
*/
public static final int DEFAULT_PORT=0x539;
//Op Codes Start
//Callback codes Start
/**
* Crash callback code.
*/
public static final int ID_CRASH=0x01;
/**
* Graceful (proper) server stop callback code.
*/
public static final int ID_GRACEFUL=0x02;
//Callback codes End
//Server command codes Start
/**
* Server authentication challenge.
*/
public static final int SRV_AUTH_CHAL=0x01;
/**
* Server good message response.
*/
public static final int SRV_RESP_OK=0x02;
/**
* Server bad message response.
*/
public static final int SRV_RESP_BAD=0x04;
/**
* Stopping server broadcast.
*/
public static final int SRV_GLOBAL_STOP=0x08;
//Server command codes End
//Client command codes Start
/**
* Client notify new connection.
*/
public static final int CLI_RQ_NEW=0x01;
/**
* Client authentication challenge response start. End challenge response with "CLI_TASK_STOP".
*/
public static final int CLI_AUTH_CHAL=0x02;
/**
* Client graceful disconnect.
*/
public static final int CLI_AUTH_DC=0x04;
/**
* Client request spectator mode.
*/
public static final int CLI_AUTH_VIEW=0x08;
/**
* Client reveal grid position.
*/
public static final int CLI_TASK_REVEAL=0x10;
/**
* Client toggle grid position flag status.
*/
public static final int CLI_TASK_FLAG=0x20;
/**
* Client load sub-map grid.
*/
public static final int CLI_TASK_LOAD=0x40;
/**
* End-of-data command. Unprecedes data after a task command.
*/
public static final int CLI_TASK_STOP=0x80;
//Client command codes End
//Op Codes End
public abstract String[] getTasklist();
public abstract Map<?, ?> getClientList();
public abstract boolean isClientAuthenticated(int id);
abstract void handleNewClient(Socket s);
abstract void updateTasklist();
abstract void onNewDataListener();
abstract void handleNewData();
abstract void challengeAuth(Socket s);
public abstract ICallback start(Runnable onFinish);
public abstract void stop();
public abstract IMap getMap();
public abstract void loadSubMap(Socket s, ICoordinate start, ICoordinate end);
}

View File

@ -0,0 +1,45 @@
package com.tofvesson.api;
public class Revealable implements IRevealable{
IGridPosition[] expandList;
IGridPosition[] NExpandList;
public Revealable(){
expandList=new IGridPosition[0];
NExpandList=new IGridPosition[0];
}
@Override
public IGridPosition[] getExpandList() {
return expandList;
}
@Override
public IGridPosition[] getNExpandList() {
return NExpandList;
}
@Override
public void addToExpandList(IGridPosition[] i) {
IGridPosition[] tmp = expandList;
expandList = new IGridPosition[expandList.length+i.length];
int i1=0;
for(IGridPosition i2 : tmp)
expandList[i1++]=i2;
for(IGridPosition i2 : i)
expandList[i1++]=i2;
}
@Override
public void addToNExpandList(IGridPosition[] i) {
IGridPosition[] tmp = NExpandList;
NExpandList = new IGridPosition[NExpandList.length+i.length];
int i1=0;
for(IGridPosition i2 : tmp)
NExpandList[i1++]=i2;
for(IGridPosition i2 : i)
NExpandList[i1++]=i2;
}
}

View File

@ -0,0 +1,5 @@
package com.tofvesson.api;
public enum Type {
Mine, MineFlagged, Flagged, Unknown, Open
}

View File

@ -0,0 +1,57 @@
package com.tofvesson.base;
import com.tofvesson.api.ICoordinate;
import com.tofvesson.api.IGridPosition;
import com.tofvesson.api.IMap;
public class Map implements IMap{
ICoordinate size;
double genRatio;
IGridPosition[][] map;
public Map(ICoordinate size){}
@Override
public void generate() {
}
@Override
public IGridPosition[] getMap() {
return null;
}
@Override
public ICoordinate getSize() {
return null;
}
@Override
public IGridPosition getPosition(ICoordinate coord) {
return null;
}
@Override
public IGridPosition getPosition(int x, int y) {
return null;
}
@Override
public IGridPosition[][] getSubMap(ICoordinate coord1, ICoordinate coord2) {
return null;
}
@Override
public IGridPosition[][] getSubMap(int x1, int y1, int x2, int y2) {
return null;
}
@Override
public boolean reveal(int x, int y) {
return false;
}
}

View File

@ -0,0 +1,69 @@
package com.tofvesson.base;
import com.tofvesson.api.Coordinate;
import com.tofvesson.api.ICoordinate;
import com.tofvesson.api.IGridPosition;
import com.tofvesson.api.IMap;
import com.tofvesson.api.IRevealable;
import com.tofvesson.api.Revealable;
import com.tofvesson.api.Type;
public class Position implements IGridPosition {
Type type=Type.Unknown;
ICoordinate pos=new Coordinate();
final IMap map;
int adjacentMines;
public Position(ICoordinate pos, IMap map){this.pos=pos; this.map=map;}
public Position(int x, int y, IMap map){this.pos=new Coordinate(x, y); this.map=map;}
@Override
public Type getType() {return this.type;}
@Override
public void overrideType(Type type) {this.type=type;}
@Override
public Type flag() {
if(type==Type.Flagged)
type=Type.Unknown;
else if(type==Type.MineFlagged)
type=Type.Mine;
else if(type==Type.Unknown)
type=Type.Flagged;
else if(type==Type.Mine)
type=Type.MineFlagged;
return type;
}
@Override
public IRevealable reveal(ICoordinate[] ignore) {
if(type==Type.Mine||type==Type.MineFlagged)
return null;
Revealable r = new Revealable();
r.addToExpandList(getAdjacentOfValue(0));
for(int i=1; i<9; ++i)
r.addToNExpandList(getAdjacentOfValue(i));
return r;
}
@Override
public IGridPosition setPos(int x, int y) {
pos = new Coordinate(x, y);
return this;
}
@Override
public ICoordinate getPos() {return pos;}
@Override
public IMap getMap() {return map;}
@Override
public int getValue() {
return adjacentMines;
}
@Override
public void calculateAdjacentMines() {
adjacentMines=getAdjacentOfType(Type.Mine).length+getAdjacentOfType(Type.MineFlagged).length;
}
}

View File

@ -0,0 +1,133 @@
package com.tofvesson.base;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tofvesson.api.Callback;
import com.tofvesson.api.ICallback;
import com.tofvesson.api.ICoordinate;
import com.tofvesson.api.IMap;
import com.tofvesson.api.IServerService;
public class Server implements IServerService{
public boolean economicMode=false;
private int eco=0;
Thread clientListener;
Map<Integer, Socket> clients;
List<String> tasklist;
Map<Integer, Boolean> auth;
Callback onFinished;
boolean active=false;
final int port;
final Runnable r = new Runnable(){
@Override
public void run() {
try{
while(active){
for(Socket s : clients.values())
if(new BufferedReader(new InputStreamReader(s.getInputStream())).ready()) ;
}
}catch(Exception e){
active=false;
onFinished.setCallback(ID_CRASH);
}
}
};
public Server(int port){this.port=port; active=true;}
public Server(){this(DEFAULT_PORT);}
@Override
public void run() {
clientListener = new Thread(r);
clients = new HashMap<>();
tasklist = new ArrayList<>();
auth = new HashMap<>();
}
@Override
public String[] getTasklist() {
return (String[]) tasklist.toArray();
}
@Override
public Map<Integer, Socket> getClientList() {
return clients;
}
@Override
public boolean isClientAuthenticated(int id) {
return auth.get((Integer)id);
}
@Override
public void handleNewClient(Socket s) {
updateClientList(s, false);
try{
new PrintWriter(s.getOutputStream()).println(SRV_AUTH_CHAL);
}catch(Exception e){e.printStackTrace();}
}
@Override
public void updateTasklist() {
}
@Override
public void onNewDataListener() {
}
@Override
public void handleNewData() {
}
@Override
public void challengeAuth(Socket s) {
}
@Override
public ICallback start(Runnable onFinish) {
return onFinished = new Callback(onFinish);
}
@Override
public void stop() {
if(clientListener.isAlive()) active=false;
try{
for(Socket s : clients.values())
if(!s.isClosed())
new PrintWriter(s.getOutputStream()).println(SRV_GLOBAL_STOP);
}catch(Exception e){e.printStackTrace();}
}
@Override
public IMap getMap() {
// TODO Auto-generated method stub
return null;
}
@Override
public void loadSubMap(Socket s, ICoordinate start, ICoordinate end) {
// TODO Auto-generated method stub
}
private void updateClientList(Socket newClient, boolean overrideEco){
if(!economicMode || (economicMode && eco==4) || overrideEco){
Map<Integer, Socket> cli = new HashMap<>();
int i=0;
for(Socket s : clients.values())
if(!s.isClosed())
cli.put(i++, s);
clients=cli;
}
clients.put(clients.size(), newClient);
if(economicMode) eco=(eco+1)%5;
}
}