notify user if is necessary

This commit is contained in:
Halit Aksoy 2022-09-04 00:04:32 +03:00
parent 9ab260a0a7
commit 9ea7ecfe93
3 changed files with 31 additions and 10 deletions

View File

@ -6,7 +6,7 @@ export type Texts = {
YourTurn: string, YourTurn: string,
OpponentTurn: string, OpponentTurn: string,
GameEnded: string, GameEnded: string,
GameCrashed: string, InternalErrorOccurred: string,
YouWon: string, YouWon: string,
Won: string, Won: string,
YouLost: string, YouLost: string,
@ -28,7 +28,10 @@ export type Texts = {
Loading: string, Loading: string,
Playing: string, Playing: string,
Error: string, Error: string,
ErrorWhenRetrievingInformation: string; ErrorWhenRetrievingInformation: string,
UCanOnlyPlayYourOwnPits: string,
UMustWaitUntilCurrentMoveComplete: string,
UCanNotPlayEmptyPit: string,
} }
export const EnUs: Texts = { export const EnUs: Texts = {
@ -38,7 +41,7 @@ export const EnUs: Texts = {
YourTurn: "Your Turn", YourTurn: "Your Turn",
OpponentTurn: "Opponent Turn", OpponentTurn: "Opponent Turn",
GameEnded: "Game Ended", GameEnded: "Game Ended",
GameCrashed: "Game Crashed", InternalErrorOccurred: "An internal error has occurred",
YouWon: "You Won", YouWon: "You Won",
Won: "Won", Won: "Won",
YouLost: "You Lost", YouLost: "You Lost",
@ -60,7 +63,11 @@ export const EnUs: Texts = {
Loading: "Loading", Loading: "Loading",
Playing: "Playing", Playing: "Playing",
Error: "Error", Error: "Error",
ErrorWhenRetrievingInformation: "An error occured when retrieving information!" ErrorWhenRetrievingInformation: "An error occured when retrieving information!",
UCanOnlyPlayYourOwnPits: "You can only play your own pits",
UMustWaitUntilCurrentMoveComplete: "You must wait until the current move is complete",
UCanNotPlayEmptyPit: "You can not play empty pit",
} }
export const TrTr: Texts = { export const TrTr: Texts = {
@ -70,7 +77,7 @@ export const TrTr: Texts = {
YourTurn: "Sıra Sende", YourTurn: "Sıra Sende",
OpponentTurn: "Sıra Rakipte", OpponentTurn: "Sıra Rakipte",
GameEnded: "Oyun Bitti", GameEnded: "Oyun Bitti",
GameCrashed: "Oyunda Hata Oluştu", InternalErrorOccurred: "Dahili bir hata oluştu",
YouWon: "Kazandın", YouWon: "Kazandın",
Won: "Kazandı", Won: "Kazandı",
YouLost: "Kaybettin", YouLost: "Kaybettin",
@ -92,5 +99,8 @@ export const TrTr: Texts = {
Loading: "Yükleniyor", Loading: "Yükleniyor",
Playing: "Oynuyor", Playing: "Oynuyor",
Error: "Hata", Error: "Hata",
ErrorWhenRetrievingInformation: "Bilgiler toplanırken bir hata oluştu!" ErrorWhenRetrievingInformation: "Bilgiler toplanırken bir hata oluştu!",
UCanOnlyPlayYourOwnPits: "Sadece sana ait olan kuyular ile oynayabilirsin",
UMustWaitUntilCurrentMoveComplete: "Devam eden haraketin bitmesini beklemelisin",
UCanNotPlayEmptyPit: "Boş kuyu ile oynayamazsın",
} }

View File

@ -28,6 +28,7 @@ import { getColorByBrightness } from '../util/ColorUtil';
import BoardViewModel from '../viewmodel/BoardViewModel'; import BoardViewModel from '../viewmodel/BoardViewModel';
import Center from '../components/Center'; import Center from '../components/Center';
import { Game, GameUsersConnectionInfo } from '../models/Game'; import { Game, GameUsersConnectionInfo } from '../models/Game';
import notyf from '../util/Notyf';
const GamePage: FunctionComponent<{ const GamePage: FunctionComponent<{
context: Context, context: Context,
@ -89,6 +90,7 @@ const GamePage: FunctionComponent<{
} }
const onGameCrashed = (message: any) => { const onGameCrashed = (message: any) => {
const newCrashMessage = message as string; const newCrashMessage = message as string;
notyf.error(context.texts.InternalErrorOccurred);
console.error("on_game_crash"); console.error("on_game_crash");
console.error(newCrashMessage); console.error(newCrashMessage);
} }
@ -147,21 +149,26 @@ const GamePage: FunctionComponent<{
if (!game || isSpectator || !userKey) { if (!game || isSpectator || !userKey) {
return; return;
} }
if(game.mancalaGame.getPlayerIdByIndex(index) !== userKey) {
notyf.error(context.texts.UCanOnlyPlayYourOwnPits);
return;
}
const pitIndexForUser = index % (game.mancalaGame.board.totalPitCount() / 2); const pitIndexForUser = index % (game.mancalaGame.board.totalPitCount() / 2);
if (game.mancalaGame.getPlayerIdByIndex(index) !== userKey || if (!game.mancalaGame.canPlayerMove(userKey, pitIndexForUser)) {
!game.mancalaGame.canPlayerMove(userKey, pitIndexForUser)) { notyf.error(context.texts.OpponentTurn);
return; return;
} }
if (checkHasAnOngoingAction()) { if (checkHasAnOngoingAction()) {
notyf.error(context.texts.UMustWaitUntilCurrentMoveComplete);
return; return;
} }
setHasOngoingAction(true);
if (!boardViewModel) return; if (!boardViewModel) return;
//TODO: this check should be in mancala.js //TODO: this check should be in mancala.js
if (pit.stoneCount === 0) { if (pit.stoneCount === 0) {
//TODO : warn user notyf.error(context.texts.UCanNotPlayEmptyPit);
return; return;
} }
setHasOngoingAction(true);
boardViewModel.pits[getBoardIndex(pitIndexForUser)].pitColor = boardViewModel.pits[getBoardIndex(pitIndexForUser)].pitColor =
context.themeManager.theme.pitSelectedColor; context.themeManager.theme.pitSelectedColor;
updateBoardViewModel(boardViewModel); updateBoardViewModel(boardViewModel);

4
src/util/Notyf.ts Normal file
View File

@ -0,0 +1,4 @@
import { Notyf } from 'notyf';
import 'notyf/notyf.min.css';
const notyf = new Notyf();
export default notyf;