Skip to content

Commit

Permalink
result display implented for native app
Browse files Browse the repository at this point in the history
  • Loading branch information
KartikWatts committed Mar 17, 2022
1 parent d5d4926 commit c152640
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Native_Application/assets/data/Interfaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ export interface LayoutData {

export interface GameState {
isGameOn: boolean;
gameArray: Data[];
wordsList: WordsData[];
score: number;
}

export enum GameActionType {
TOGGLE = "toggle",
UPDATE_WORDS_LIST = "update-words-list",
UPDATE_GAME_ARRAY = "update-game-array",
UPDATE_SCORE = "update-score",
}
export interface GameAction {
type: GameActionType;
payload: any;
}

export interface FoundWords {
Expand Down
23 changes: 17 additions & 6 deletions Native_Application/assets/data/Types.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FoundWords } from "./Interfaces";
import { Data, FoundWords, WordsData } from "./Interfaces";

export type DataProps = {
id: number;
Expand All @@ -15,19 +15,30 @@ export type ScoreProps = {
foundWords: number;
};

let DEFAULT = 1,
SELECTED = 2,
CORRECT = 3,
ALREADY = 4,
WRONG = 5;
export type ResultProps = {
gameArray: Data[];
wordsList: WordsData[];
score: number;
};

export type GameContextType = {
isGameOn: boolean;
gameArray: Data[];
wordsList: WordsData[];
score: number;
toggleGameState: () => void;
updateWordsList: (wordsList: WordsData[]) => void;
updateGameArray: (gameArray: Data[]) => void;
updateScore: (score: number) => void;
};

export type WordProps = {
foundWords: FoundWords[];
};

let DEFAULT = 1,
SELECTED = 2,
CORRECT = 3,
ALREADY = 4,
WRONG = 5;
export { DEFAULT, SELECTED, CORRECT, ALREADY, WRONG };
57 changes: 57 additions & 0 deletions Native_Application/components/GameBoxDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useEffect, useState } from "react";
import tw from "twrnc";
import { View, Text, Dimensions, StyleSheet } from "react-native";
import { DEFAULT } from "../assets/data/Types";
import { Data } from "../assets/data/Interfaces";

const screen = Dimensions.get("screen");

function GameBoxDisplay(props: Data) {
const getBoxColorFromState = (state: number) => {
switch (state) {
default:
return `bg-blue-400`;
}
};

const [tileColor, setTileColor] = useState(getBoxColorFromState(DEFAULT));

useEffect(() => {
setTileColor(getBoxColorFromState(props.tileState));
}, [props.tileState]);

return (
<View
style={[
styles.boxDimensions,
tw`rounded-sm flex justify-center items-center`,
tw`${tileColor}`,
]}
>
<Text style={[styles.boxValue, tw`absolute top-0 left-1`]}>
{props.value}
</Text>
<Text style={[styles.boxText]}>{props.letter}</Text>
</View>
);
}

const styles = StyleSheet.create({
boxDimensions: {
width: (screen.width - 25 - 5 * 2.5) / 8,
height: (screen.width - 25 - 5 * 2.5) / 8,
margin: 0.8,
elevation: 8,
shadowColor: "black",
borderColor: "black",
borderWidth: 1,
},
boxText: {
fontSize: 15,
},
boxValue: {
fontSize: 6,
},
});

export default GameBoxDisplay;
7 changes: 7 additions & 0 deletions Native_Application/components/GameWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tw from "twrnc";
import { Text, TouchableOpacity, View } from "react-native";
import Game from "../containers/Game";
import { GameContext } from "../contexts/GameContext";
import ResultDisplay from "../containers/ResultDisplay";

export default function GameWrapper() {
const gameContext = useContext(GameContext);
Expand All @@ -13,6 +14,12 @@ export default function GameWrapper() {
>
{gameContext && gameContext.isGameOn ? (
<Game />
) : gameContext && gameContext.wordsList.length > 0 ? (
<ResultDisplay
gameArray={gameContext.gameArray}
wordsList={gameContext.wordsList}
score={gameContext.score}
/>
) : (
<View>
<TouchableOpacity
Expand Down
10 changes: 9 additions & 1 deletion Native_Application/containers/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useContext, useEffect, useState } from "react";
import tw from "twrnc";
import { View, Text, GestureResponderEvent, StyleSheet } from "react-native";
import {
Expand All @@ -13,6 +13,7 @@ import { ALREADY, CORRECT, SELECTED, WRONG } from "../assets/data/Types";
import { Audio } from "expo-av";
import ScoreDisplay from "../components/ScoreDisplay";
import WordDisplay from "../components/WordDisplay";
import { GameContext } from "../contexts/GameContext";

function Game() {
let validWordsList: Array<WordsData> = [];
Expand All @@ -30,6 +31,7 @@ function Game() {
const [isItAwesome, setIsItAwesome] = useState<boolean>(false);
const [selectedBoxId, setSelectedBoxId] = useState<number>(-1);
const [foundWords, setFoundWords] = useState<FoundWords[]>([]);
const gameContext = useContext(GameContext);

solutionData.map((solution) => {
if (solution.length > 2) {
Expand All @@ -40,6 +42,10 @@ function Game() {
}
});

useEffect(() => {
if (gameContext) gameContext.updateGameArray(gameData);
}, []);

const [sound, setSound] = useState<Audio.Sound>();

const playLetterSelectorSound = async () => {
Expand Down Expand Up @@ -140,6 +146,7 @@ function Game() {
if (!tempWordsList[foundIndex].isIncluded) {
tempWordsList[foundIndex].isIncluded = true;
setValidWordsData(tempWordsList);
if (gameContext) gameContext.updateWordsList(tempWordsList);
return 1;
} else return 2;
}
Expand Down Expand Up @@ -242,6 +249,7 @@ function Game() {
console.log("Score: ", currentWordScore);

setGameScore(tempScore);
if (gameContext) gameContext.updateScore(tempScore);
setCurrentWordScore(0);
setCurrentWord("");
setSelectedBoxId(-1);
Expand Down
172 changes: 172 additions & 0 deletions Native_Application/containers/ResultDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import React, { useEffect, useState } from "react";
import { ScrollView, Text, View } from "react-native";
import tw from "twrnc";
import { WordsData } from "../assets/data/Interfaces";
import { ResultProps } from "../assets/data/Types";
import GameBoxDisplay from "../components/GameBoxDisplay";

const PerformaceStats = (props: {
name: string;
value: any;
altColor?: boolean;
}) => {
return (
<View
style={[
props.altColor && tw`bg-slate-600`,
tw`flex flex-row items-center justify-center p-1 mt-0.5`,
]}
>
<View style={tw`w-3/5`}>
<Text style={tw`text-slate-200 text-sm`}>{props.name}:</Text>
</View>
<View style={tw`w-2/5 pl-0.5`}>
<Text style={tw`text-white text-sm`}>{props.value}</Text>
</View>
</View>
);
};

const ResultDisplay = (props: ResultProps) => {
const [foundWords, setFoundWords] = useState<WordsData[]>([]);
const [unFoundWords, setUnFoundWords] = useState<WordsData[]>([]);
const [score, setScore] = useState<number>(0);

const sortByLength = (arr: WordsData[]) => {
return arr.sort((x, y) => y.value.length - x.value.length);
};

useEffect(() => {
const { wordsList } = props;
let foundWordsTemp: WordsData[] = [],
unFoundWordsTemp: WordsData[] = [];
wordsList.map((word) => {
if (word.isIncluded) {
foundWordsTemp.push(word);
} else {
unFoundWordsTemp.push(word);
}
});

sortByLength(foundWordsTemp);
sortByLength(unFoundWordsTemp);

setFoundWords(foundWordsTemp);
setUnFoundWords(unFoundWordsTemp);
setScore(props.score);
}, [props]);

const getAvgLength = () => {
let totalLength = 0;
foundWords.map((word) => {
totalLength += word.value.length;
});
let avgLength = totalLength / foundWords.length;
return parseFloat(avgLength.toString()).toFixed(2);
};

return (
<>
<Text style={tw`text-3xl p-3 text-amber-600 font-bold underline`}>
Result
</Text>
<View style={tw`flex-1 flex flex-row w-full flex-wrap`}>
<View style={tw`w-1/2 flex justify-center items-center h-70`}>
<View
style={tw`flex flex-row justify-center items-center w-full flex-wrap`}
>
{props.gameArray.map((data) => {
return <GameBoxDisplay key={data.id} {...data} />;
})}
</View>
</View>
<View style={tw`w-1/2 flex justify-center items-center pl-2`}>
<PerformaceStats name="Points" value={score} altColor />
<PerformaceStats name="Words" value={foundWords.length} />
<PerformaceStats
name="Seconds per word"
value={parseFloat(
(120 / foundWords.length).toString()
).toFixed(2)}
altColor
/>
<PerformaceStats
name="Average length"
value={getAvgLength()}
/>
<PerformaceStats
name="Average points"
value={parseFloat(
(score / foundWords.length).toString()
).toFixed(2)}
altColor
/>
</View>
<View style={tw`w-1/3 p-2`}>
<View style={tw`flex flex-row items-center`}>
<Text style={tw`text-amber-500 text-sm`}>
Words Found
</Text>
<Text style={tw`text-amber-300 text-xs ml-1`}>
({foundWords.length})
</Text>
</View>
<ScrollView
persistentScrollbar
style={tw`w-8/8 h-60 border rounded-sm border-white p-2 `}
>
<View style={tw`flex-row flex-wrap`}>
{foundWords.map((word, index) => {
return (
<View
key={index}
style={tw`flex flex-row mr-3 mb-1`}
>
<Text
style={tw`text-sm text-amber-300`}
>
{word.value}
</Text>
</View>
);
})}
</View>
</ScrollView>
</View>
<View style={tw`w-2/3 p-2`}>
<View style={tw`flex flex-row items-center`}>
<Text style={tw`text-amber-500 text-sm`}>
Not Found
</Text>
<Text style={tw`text-amber-300 text-xs ml-1`}>
({unFoundWords.length})
</Text>
</View>
<ScrollView
persistentScrollbar
style={tw`w-8/8 h-60 border rounded-sm border-white p-2 `}
>
<View style={tw`flex-row flex-wrap`}>
{unFoundWords.map((word, index) => {
return (
<View
key={index}
style={tw`flex flex-row mr-3 mb-1`}
>
<Text
style={tw`text-sm text-amber-300`}
>
{word.value}
</Text>
</View>
);
})}
</View>
</ScrollView>
</View>
</View>
</>
);
};

export default ResultDisplay;
Loading

0 comments on commit c152640

Please sign in to comment.