Skip to content

Commit

Permalink
Merge pull request #2 from harjot-oberai/p2p
Browse files Browse the repository at this point in the history
Fixes : Database and related issues
  • Loading branch information
harjot-oberai committed Feb 18, 2016
2 parents 2ef0db0 + 2785b71 commit 461148f
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
android:name=".MainActivity"
android:configChanges="orientation"
android:launchMode="singleInstance"
android:screenOrientation="portrait">

</activity>
android:screenOrientation="portrait"></activity>
<activity
android:name=".SplashScreen"
android:configChanges="orientation"
Expand Down Expand Up @@ -79,6 +77,8 @@
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity android:name=".TwoDevice2P_names"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,50 @@ public boolean onTouchEvent(MotionEvent event) {
status = status.concat(";"+turn);
byte[] ByteArray = status.getBytes();
connectedThread.write(ByteArray);
if(turn == 1){
two_player_names.p1Name = TwoDevice2P_names.MyName.trim().toUpperCase();
two_player_names.p2Name = TwoDevice2P_names.OpponentName.trim().toUpperCase();
String tmp = dbh.checkUser(two_player_names.p1Name);
if (tmp.equals("FOUND")) {
//Toast.makeText(two_player_names.this,"found 1",Toast.LENGTH_SHORT).show();
} else {
scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p1Name, 0);
//Toast.makeText(two_player_names.this,"adding 1",Toast.LENGTH_SHORT).show();
dbh.addScore(sb);
}

String tmp2 = dbh.checkUser(two_player_names.p2Name);
if (tmp2.equals("FOUND")) {
//Toast.makeText(two_player_names.this,"found 2",Toast.LENGTH_SHORT).show();

} else {
scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p2Name, 0);
//Toast.makeText(two_player_names.this,"adding 2",Toast.LENGTH_SHORT).show();
dbh.addScore(sb);
}
}
else if(turn == 2){
two_player_names.p2Name = TwoDevice2P_names.MyName.trim().toUpperCase();
two_player_names.p1Name = TwoDevice2P_names.OpponentName.trim().toUpperCase();
String tmp = dbh.checkUser(two_player_names.p1Name);
if (tmp.equals("FOUND")) {
//Toast.makeText(two_player_names.this,"found 1",Toast.LENGTH_SHORT).show();
} else {
scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p1Name, 0);
//Toast.makeText(two_player_names.this,"adding 1",Toast.LENGTH_SHORT).show();
dbh.addScore(sb);
}

String tmp2 = dbh.checkUser(two_player_names.p2Name);
if (tmp2.equals("FOUND")) {
//Toast.makeText(two_player_names.this,"found 2",Toast.LENGTH_SHORT).show();

} else {
scoreboard sb = new scoreboard(dbh.getPlayerCount(), two_player_names.p2Name, 0);
//Toast.makeText(two_player_names.this,"adding 2",Toast.LENGTH_SHORT).show();
dbh.addScore(sb);
}
}
touchEnabled = false;
}

Expand Down Expand Up @@ -178,7 +222,8 @@ public void onClick(DialogInterface dialog, int which) {
case DialogInterface.BUTTON_POSITIVE:
{
try {
connectedThread=null;
connectedThread.cancel();
connectedThread = null;
bluetoothSocket.close();
} catch (Exception e) {
Log.d(TAG, "exception " + e.getMessage());
Expand All @@ -198,7 +243,7 @@ public void onClick(DialogInterface dialog, int which) {
}

public void updateWin(int i){
/*if(i==1){
if(i==1){
String tmp="FOUND";
try{
tmp=dbh.checkUser(two_player_names.p1Name);
Expand Down Expand Up @@ -235,7 +280,7 @@ else if (i==2){
scoreboard sb=new scoreboard(dbh.getPlayerCount(),two_player_names.p2Name,1);
dbh.addScore(sb);
}
}*/
}
}

public void check()
Expand Down Expand Up @@ -400,6 +445,7 @@ public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private int cnt = 0;

public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
Expand All @@ -415,40 +461,72 @@ public ConnectedThread(BluetoothSocket socket) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;

}

public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
if(cnt == 0){
try{
byte[] ByteArray = TwoDevice2P_names.MyName.getBytes();
connectedThread.write(ByteArray);
cnt++;
}
catch(Exception e){
Log.d(TAG, e.getMessage());
}
}
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
Log.i(TAG, "BEGIN Listening");
// Read from the InputStream
String readMessage = "";
bytes = mmInStream.read(buffer);
readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
Log.i(TAG, "Listening : " + readMessage);
a[0][0] = (int)(readMessage.charAt(0)-48);
a[0][1] = (int)(readMessage.charAt(1)-48);
a[0][2] = (int)(readMessage.charAt(2)-48);
a[1][0] = (int)(readMessage.charAt(3)-48);
a[1][1] = (int)(readMessage.charAt(4)-48);
a[1][2] = (int)(readMessage.charAt(5)-48);
a[2][0] = (int)(readMessage.charAt(6)-48);
a[2][1] = (int)(readMessage.charAt(7)-48);
a[2][2] = (int)(readMessage.charAt(8)-48);
turn = (int)(readMessage.charAt(10)-48);
String str= ""+a[0][0]+a[0][1]+a[0][2]+a[1][0]+a[1][1]+a[1][2]+a[2][0]+a[2][1]+a[2][2]+";"+turn;

Log.i(TAG, "GOT : " + str);

touchEnabled = true;

if(!oncewin && !oncedrawen){
postInvalidate();
check();
if(readMessage.contains(";")){
// Send the obtained bytes to the UI Activity
Log.i(TAG, "Listening : " + readMessage);
a[0][0] = (int)(readMessage.charAt(0)-48);
a[0][1] = (int)(readMessage.charAt(1)-48);
a[0][2] = (int)(readMessage.charAt(2)-48);
a[1][0] = (int)(readMessage.charAt(3)-48);
a[1][1] = (int)(readMessage.charAt(4)-48);
a[1][2] = (int)(readMessage.charAt(5)-48);
a[2][0] = (int)(readMessage.charAt(6)-48);
a[2][1] = (int)(readMessage.charAt(7)-48);
a[2][2] = (int)(readMessage.charAt(8)-48);
turn = (int)(readMessage.charAt(10)-48);
String str= ""+a[0][0]+a[0][1]+a[0][2]+a[1][0]+a[1][1]+a[1][2]+a[2][0]+a[2][1]+a[2][2]+";"+turn;

Log.i(TAG, "GOT : " + str);

touchEnabled = true;

if(!oncewin && !oncedrawen){
postInvalidate();
check();
}
}
else{
try{
Log.i(TAG,"Hello");
TwoDevice2P_names.OpponentName = readMessage;
Log.i(TAG,TwoDevice2P_names.MyName + " vs "+TwoDevice2P_names.OpponentName);
TwoDevice2P.act_2p.runOnUiThread(
new Runnable() {
@Override
public void run() {
Toast.makeText(TwoDevice2P.act_2p,TwoDevice2P_names.MyName + " vs "+TwoDevice2P_names.OpponentName,Toast.LENGTH_SHORT).show();
}
}
);
}
catch(Exception e){
Log.d(TAG,e.getMessage());
}
}
} catch (Exception e) {
//Log.e(TAG, "disconnected", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.haresh.tic_tac_toe_app;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TwoDevice2P_names extends AppCompatActivity {

public static String MyName;
public static String OpponentName;
Button btn;
EditText name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two_device2_p_names);
btn = (Button) findViewById(R.id.btn_find);
name = (EditText) findViewById(R.id.myName);
name.setText("");
btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
MyName = name.getText().toString();
if(MyName.trim().equals("")){
name.setError("Enter Name");
}
else{
Intent intent = new Intent(TwoDevice2P_names.this,BluetoothActivity.class);
startActivity(intent);
}
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onClick(View v) {
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(two_player_selector.this,BluetoothActivity.class);
Intent intent=new Intent(two_player_selector.this,TwoDevice2P_names.class);
startActivity(intent);
}
}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/activity_two_device2_p_names.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.haresh.tic_tac_toe_app.TwoDevice2P_names">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myName"
android:layout_marginTop="40dp"
android:hint="Your Name"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="Find Opponent"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:id="@+id/btn_find"
android:layout_alignParentBottom="true"/>
</RelativeLayout>

0 comments on commit 461148f

Please sign in to comment.