aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/jopek/pai_fillo_nai/OnlineGameActivity.java
blob: d389833f2f3afe8bcb1253bc6e7f374683a0393c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package com.jopek.pai_fillo_nai;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class OnlineGameActivity extends AppCompatActivity {
    protected boolean gameWasEnd = false;
    // 0player - O, 1player - X
    long whoseTurn = ThreadLocalRandom.current().nextInt(0, 1 + 1);
    List<Long> gameBoard = new ArrayList<>(
            Arrays.asList(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L)
    );
    List<ImageView> squares = new ArrayList<>();
    DatabaseReference rooms;
    DatabaseReference room;
    // FinishGameAlert
    AlertDialog fgAlert = null;
    long whoami = 0;
    boolean myTurn = false;
    boolean revengePossible = true;
    boolean revengeRequested = false;
    String TAG = "maks";
    ImageView p0Img;
    ImageView p1Img;
    ImageView square0, square1, square2, square3, square4, square5, square6, square7, square8;
    TextView tvName;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_online_game);

        p0Img = findViewById(R.id.p0Img);
        p1Img = findViewById(R.id.p1Img);
        tvName = findViewById(R.id.onPlayerName);

        square0 = findViewById(R.id.square0);
        square1 = findViewById(R.id.square1);
        square2 = findViewById(R.id.square2);
        square3 = findViewById(R.id.square3);
        square4 = findViewById(R.id.square4);
        square5 = findViewById(R.id.square5);
        square6 = findViewById(R.id.square6);
        square7 = findViewById(R.id.square7);
        square8 = findViewById(R.id.square8);
        squares.addAll(Arrays.asList(square0, square1, square2, square3, square4, square5, square6, square7, square8));
        int i = 0;
        for (ImageView square : squares) {
            int finalI = i;
            square.setOnClickListener(view -> onSquareClick((ImageView) view, finalI));
            i++;
        }
        rooms = FirebaseDatabase.getInstance("https://pai-fillo-nai-default-rtdb.europe-west1.firebasedatabase.app/").getReference("rooms");

        askForRoomId(false);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    protected void askForRoomId(boolean errorOccured) {
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);

        new AlertDialog.Builder(this)
                .setTitle(errorOccured ? "Error occured, choose room again" : "Choose room")
                .setMessage("(blank to create new)")
                .setCancelable(false)
                .setView(input)
                .setPositiveButton("Join room", (dialog, which) -> {
                    getRoom(String.valueOf(input.getText()));
                })
                .setIcon(R.drawable.ic_baseline_gamepad_24)
                .show();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @SuppressLint("DefaultLocale")
    protected void getRoom(String roomId) {
        if (roomId.equals("")) {
            Random rand = new Random();

            int n = new Random().nextInt(900000) + 100000;

            String rid = String.valueOf(n);
            whoami = rand.nextInt(2);
            tvName.setText("You're player 1 (playing as " + (whoami == 0 ? "O" : "X") + ")");

            room = rooms.child(rid);
            Map<String, Object> data = new HashMap<>();
            data.put("started", 0);
            data.put("player1", whoami);
            data.put("player2", whoami == 0 ? 1 : 0);
            data.put("whoseTurn", rand.nextInt(2));
            data.put("board", gameBoard);
            data.put("winner", -1);
            data.put("revenge0", -1);
            data.put("revenge1", -1);
            room.setValue(data);

            waitFor2Player(rid);
        } else {
            rooms.child(roomId).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (!(dataSnapshot.getValue() instanceof HashMap)) {
                        askForRoomId(true);
                    }
                    HashMap data = (HashMap) dataSnapshot.getValue();
                    if ((long) data.get("started") == 1) {
                        askForRoomId(true);
                    }
                    whoami = (long) data.get("player2");
                    whoseTurn = (long) data.get("whoseTurn");
                    tvName.setText("You're player 1 (playing as " + (whoami == 0 ? "O" : "X") + ")");
                    room = rooms.child(roomId);
                    startGame(true);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    askForRoomId(true);
                }
            });
        }
    }

    protected void waitFor2Player(String roomId) {
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
                .setTitle("Waiting for 2nd player")
                .setMessage("Your room id = " + roomId)
                .setCancelable(false)
                .setIcon(R.drawable.ic_baseline_gamepad_24);
        AlertDialog alert = alertBuilder.create();
        alert.show();
        alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        room.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                HashMap data = (HashMap) dataSnapshot.getValue();
                if ((long) data.get("started") == 1) {
                    room.removeEventListener(this);
                    alert.dismiss();
                    startGame(false);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled: Waiter Value Event Listener");
            }
        });
    }

    protected void startGame(boolean markStarted) {
        if (markStarted) {
            room.child("started").setValue(1);
        }
        room.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                HashMap data = (HashMap) dataSnapshot.getValue();
                if (data == null) {
                    room.removeEventListener(this);
                    return;
                }

                Log.d(TAG, "onDataChange: " + Arrays.toString(data.entrySet().toArray()));
                gameBoard = (List) data.get("board");
                Log.d(TAG, "onDataChange: " + Arrays.toString(gameBoard.toArray()));
                whoseTurn = (long) data.get("whoseTurn");
                myTurn = whoseTurn == whoami;
                playTurn();
                long winner = (long) data.get("winner");
                long opRevenge = (long) data.get("revenge" + (whoami == 0 ? 1 : 0));
                revengePossible = opRevenge != 0;
                if (revengeRequested && winner == -1) {
                    myTurn = whoami == (long) data.get("whoseTurn");
                    revengePossible = true;
                    revengeRequested = false;
                    gameWasEnd = false;
                    playTurn();
                } else if (!revengePossible && revengeRequested) {
                    fgAlert.dismiss();
                    new AlertDialog.Builder(OnlineGameActivity.this)
                            .setTitle("Sad info")
                            .setMessage("Second player don't want to play with ya :(")
                            .setCancelable(false)
                            .setNegativeButton("Go to menu without friend", (dialog, which) -> {
                                room.removeValue();
                                Intent intent = new Intent(OnlineGameActivity.this, MainActivity.class);
                                startActivity(intent);
                            })
                            .setIcon(R.drawable.ic_baseline_gamepad_24)
                            .show();
                } else if (opRevenge == 1 && revengeRequested) {
                    Map<String, Object> data2 = new HashMap<>();
                    gameBoard = new ArrayList<>(Arrays.asList(-1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L));
                    data2.put("started", 1);
                    data2.put("player1", whoami);
                    data2.put("player2", whoami == 0 ? 1L : 0L);
                    data2.put("whoseTurn", new Long(ThreadLocalRandom.current().nextInt(0, 1 + 1)));
                    data2.put("board", gameBoard);
                    data2.put("winner", -1L);
                    data2.put("revenge0", -1L);
                    data2.put("revenge1", -1L);
                    myTurn = whoami == (long) data2.get("whoseTurn");
                    room.setValue(data2);
                    playTurn();
                }

                if (winner != -1) {
                    endGame(winner);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled: Main Value Event Listener");
            }
        });
    }

    protected void playTurn() {
        int i = 0;
        for (ImageView square : squares) {
            if (gameBoard.get(i) == -1) {
                square.setImageResource(R.mipmap.emptiness_foreground);
            } else if (gameBoard.get(i) == 0) {
                square.setImageResource(R.mipmap.bg_o_foreground);
            } else if (gameBoard.get(i) == 1) {
                square.setImageResource(R.mipmap.bg_x_foreground);
            }
            i++;
        }
        if (myTurn) {
            p0Img.setImageResource(R.mipmap.bg_you_in_army_foreground);
        } else {
            p0Img.setImageResource(R.mipmap.bg_no_no_foreground);
        }
    }

    protected void finishTurn() {
        room.child("whoseTurn").setValue(whoseTurn);
        room.child("board").setValue(gameBoard);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    protected void onSquareClick(ImageView square, int id) {
        if (gameBoard.get(id) != -1 || !myTurn) return;
        gameBoard.set(id, whoseTurn);
        square.setImageResource(whoseTurn == 0 ? R.mipmap.bg_o_foreground : R.mipmap.bg_x_foreground);
        if (checkWon()) {
            endGame(whoseTurn);
            return;
        } else if (checkDraw()) {
            endGame(2);
            return;
        }
        whoseTurn = whoseTurn == 0 ? 1 : 0;
        if (whoseTurn == whoami) {
            p0Img.setImageResource(R.mipmap.bg_you_in_army_foreground);
        } else {
            p0Img.setImageResource(R.mipmap.bg_no_no_foreground);
        }
        finishTurn();
    }

    protected boolean checkWon() {
        return chkTriEqOnGB(0, 1, 2) || chkTriEqOnGB(3, 4, 5) || chkTriEqOnGB(6, 7, 8) ||
                chkTriEqOnGB(0, 3, 6) || chkTriEqOnGB(1, 4, 7) || chkTriEqOnGB(2, 5, 8) ||
                chkTriEqOnGB(0, 4, 8) || chkTriEqOnGB(2, 4, 6);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    protected boolean checkDraw() {
        return gameBoard.get(0) != -1 &&
                gameBoard.get(1) != -1 &&
                gameBoard.get(2) != -1 &&
                gameBoard.get(3) != -1 &&
                gameBoard.get(4) != -1 &&
                gameBoard.get(5) != -1 &&
                gameBoard.get(6) != -1 &&
                gameBoard.get(7) != -1 &&
                gameBoard.get(8) != -1;
    }

    protected void endGame(long whoWon) {
        if (gameWasEnd) return;
        gameWasEnd = true;

        finishTurn();
        room.child("winner").setValue(whoWon);
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
                .setTitle(whoWon == 2 ? "DRAW" : "Player " + (whoWon + 1) + " WON")
                .setMessage(whoWon == 2 ? "Maybe next time" : "Congratulations to Player " + (whoWon + 1))
                .setCancelable(false)
                .setPositiveButton("Request revenge", (dialog, which) -> {
                    revengeRequested = true;
                    room.child("revenge" + whoami).setValue(1);
                })
                .setNegativeButton("Go to menu", (dialog, which) -> {
                    room.child("revenge" + whoami).setValue(0);
                    if (!revengePossible) {
                        room.removeValue();
                    }
                    Intent intent = new Intent(OnlineGameActivity.this, MainActivity.class);
                    startActivity(intent);
                })
                .setIcon(R.drawable.ic_baseline_gamepad_24);
        fgAlert = alertBuilder.create();
        fgAlert.show();
    }

    // CheckTripleEqualityOnGameBoard
    protected boolean chkTriEqOnGB(int a, int b, int c) {
        return Objects.equals(gameBoard.get(a), gameBoard.get(b)) && gameBoard.get(b).equals(gameBoard.get(c)) && gameBoard.get(a) != -1;
    }
}