Skip to content

Commit

Permalink
OGS basic play works. TODO: add waiting window for OGS, handle playin…
Browse files Browse the repository at this point in the history
…g DGS and then OGS games
  • Loading branch information
cerisara committed Jun 6, 2015
1 parent c0e8173 commit 9bfc493
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 67 deletions.
24 changes: 20 additions & 4 deletions res/layout/other_buttons.xml
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" >
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
Expand Down Expand Up @@ -56,14 +58,28 @@
android:layout_height="wrap_content"
android:text="List saved games" />

<CheckBox
<CheckBox
android:id="@+id/checkBoxDGS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:checked="true"
android:text="DGS" />
<CheckBox
android:id="@+id/checkBoxOGS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:checked="true"
android:text="OGS" />
<CheckBox
android:id="@+id/checkBoxClientServer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:checked="true"
android:text="connect to Client Server" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
77 changes: 44 additions & 33 deletions src/fr/xtof54/jsgo/Game.java
Expand Up @@ -301,28 +301,32 @@ public boolean accept(File arg0, String arg1) {
}

public static void loadStatusGames(final ServerConnection server) {
final EventManager em = EventManager.getEventManager();
EventManager.EventListener f = new EventManager.EventListener() {
@Override
public synchronized void reactToEvent() {
JSONObject o = server.o;
parseJSONStatusGames(o);
// also connects now to the client server to give it time to connect correctly
if (games2play.size()>0) WSclient.init(games2play.get(0).myid);
if (GoJsActivity.main.getGamesFromDGS) {
final EventManager em = EventManager.getEventManager();
EventManager.EventListener f = new EventManager.EventListener() {
@Override
public synchronized void reactToEvent() {
JSONObject o = server.o;
parseJSONStatusGames(o);
// also connects now to the client server to give it time to connect correctly
if (games2play.size()>0) WSclient.init(games2play.get(0).myid);

System.out.println("end of loadstatusgame, unregistering listener "+games2play.size());
em.unregisterListener(eventType.downloadListEnd, this);
em.sendEvent(eventType.downloadListGamesEnd);
}
@Override
public String getName() {return "loadStatusGame";}
};
em.registerListener(eventType.downloadListEnd, f);
server.sendCmdToServer(cmdGetListOfGames,eventType.downloadListStarted,eventType.downloadListEnd);

// TODO
System.out.println("OGS login:");
OGSConnection.login();
System.out.println("end of loadstatusgame, unregistering listener "+games2play.size());
em.unregisterListener(eventType.downloadListEnd, this);
em.sendEvent(eventType.downloadListGamesEnd);
}
@Override
public String getName() {return "loadStatusGame";}
};
em.registerListener(eventType.downloadListEnd, f);
server.sendCmdToServer(cmdGetListOfGames, eventType.downloadListStarted, eventType.downloadListEnd);
}
if (GoJsActivity.main.getGamesFromOGS) {
// TODO: disable the game button while downloading is not done
System.out.println("OGS login:");
// TODO: this will be made after the waiting window has closed: do it before without events !
OGSConnection.login();
}
}

public static List<Game> getGames() {return games2play;}
Expand Down Expand Up @@ -753,9 +757,10 @@ private int getOppID() {
if (myid==whiteid) return blackid;
else return whiteid;
}


// this function is called by the "weblistener" that captures the last move from the embedded javascript
public void sendMove2server(String move, final ServerConnection server) {
if (server==null) {
if (gid>=0&&server==null) {
if (!GoJsActivity.main.initServer()) return;
sendMove2server(move, GoJsActivity.main.server);
return;
Expand Down Expand Up @@ -792,16 +797,22 @@ public void onClick(DialogInterface dialog, int id) {
.setPositiveButton("OK, send !", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
addMoveToSGF(finmove);
String cmd = "quick_do.php?obj=game&cmd=move&gid="+getGameID()+"&move_id="+newMoveId+"&move="+finmove;
if (msg!=null) {
addMessageToSGF(msg.toString());
cmd+="&msg="+URLEncoder.encode(msg.toString());
}
// TODO: check that server exists
// this first command sends the move to the DGS server
server.sendCmdToServer(cmd,eventType.moveSentStart,eventType.moveSentEnd);
// this second command sends the move to the push server for real-time playing
WSclient.sendMove(getGameID(), newMoveId, finmove, getOppID());
if (gid>=0) {
// this is a DGS game
String cmd = "quick_do.php?obj=game&cmd=move&gid=" + getGameID() + "&move_id=" + newMoveId + "&move=" + finmove;
if (msg != null) {
addMessageToSGF(msg.toString());
cmd += "&msg=" + URLEncoder.encode(msg.toString());
}
// TODO: check that server exists
// this first command sends the move to the DGS server
server.sendCmdToServer(cmd, eventType.moveSentStart, eventType.moveSentEnd);
// this second command sends the move to the push server for real-time playing
WSclient.sendMove(getGameID(), newMoveId, finmove, getOppID());
} else {
// OGS move
OGSConnection.sendMove(-gid,finmove);
}
ConfirmDialogFragment.this.getDialog().cancel();
}
});
Expand Down
84 changes: 67 additions & 17 deletions src/fr/xtof54/jsgo/GoJsActivity.java
Expand Up @@ -91,7 +91,9 @@ enum guistate {nogame, play, markDeadStones, checkScore, message, review, forums
public static GoJsActivity main;
private int numEventsReceived = 0;

// private static void copyFile(InputStream in, OutputStream out) throws IOException {
public boolean getGamesFromDGS=true, getGamesFromOGS=true;

// private static void copyFile(InputStream in, OutputStream out) throws IOException {
// byte[] buffer = new byte[1024];
// int read;
// while((read = in.read(buffer)) != -1){
Expand Down Expand Up @@ -950,6 +952,13 @@ public void reactToEvent() {
int valInConfig = PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_PUSHSERVER,1);
WSclient.setConnect(valInConfig==1?true:false);
}
{
// initialize active Go servers
int valInConfig = PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_DGSON,1);
getGamesFromDGS = (valInConfig==1?true:false);
valInConfig = PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_OGSON,1);
getGamesFromOGS = (valInConfig==1?true:false);
}

// initialize traffic stats
ActivityManager mgr = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
Expand Down Expand Up @@ -1289,14 +1298,25 @@ public void onClick(DialogInterface dialogInterface, int i) {
dialog.show(getSupportFragmentManager(),"dgs signin");
}

// TODO: fix to play first all DGS games, and then all OGS games
private void skipGame() {
if (Game.getGames().size()<=1) {
showMessage("No more games downloaded; retry GetGames ?");
changeState(guistate.nogame);
return;
}
if (++curgidx2play>=Game.getGames().size()) curgidx2play=0;
downloadAndShowGame();
if (Game.gameShown!=null) {
if (Game.gameShown.getGameID() >= 0) {
// DGS game
if (Game.getGames().size() <= 1) {
// go to OGS games
showMessage("No more games downloaded; retry GetGames ?");
changeState(guistate.nogame);
return;
}
if (++curgidx2play >= Game.getGames().size()) curgidx2play = 0;
downloadAndShowGame();
} else {
// OGS game
// OGS games are played last
OGSConnection.nextGame2play();
}
}
}
// TODO: move this method into Game !
private void resignGame() {
Expand Down Expand Up @@ -1637,23 +1657,53 @@ public void onClick(View vv) {
dialog.dismiss();
}
});
final CheckBox connectClientServer = (CheckBox)v.findViewById(R.id.checkBoxClientServer);

final CheckBox connectClientServer = (CheckBox)v.findViewById(R.id.checkBoxClientServer);
{
int prefval=PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_PUSHSERVER,1);
connectClientServer.setChecked(prefval==1?true:false);
}
connectClientServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
connectClientServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
boolean curval = connectClientServer.isChecked();
WSclient.setConnect(curval);
WSclient.setConnect(curval);
int curvali = curval?1:0;
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.PREFS_PUSHSERVER,curvali);
}
});
}
});

final CheckBox connectDGS = (CheckBox)v.findViewById(R.id.checkBoxDGS);
{
int prefval=PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_DGSON,1);
connectDGS.setChecked(prefval==1?true:false);
}
connectDGS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
boolean curval = connectDGS.isChecked();
getGamesFromDGS=curval;
int curvali = curval?1:0;
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.PREFS_DGSON,curvali);
}
});
final CheckBox connectOGS = (CheckBox)v.findViewById(R.id.checkBoxOGS);
{
int prefval=PrefUtils.getFromPrefs(getApplicationContext(),PrefUtils.PREFS_OGSON,1);
connectOGS.setChecked(prefval==1?true:false);
}
connectOGS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vv) {
boolean curval = connectOGS.isChecked();
getGamesFromOGS=curval;
int curvali = curval?1:0;
PrefUtils.saveToPrefs(getApplicationContext(),PrefUtils.PREFS_OGSON,curvali);
}
});


builder.setView(v);
builder.setView(v);
return builder.create();
}
}
Expand Down

0 comments on commit 9bfc493

Please sign in to comment.