Add: Réseau:
continuous-integration/drone/push Build is passing Details

-Match
                -Conversation
                -TicTacToeOnline
peristanceBDD
Thomas Chazot 2 years ago
parent 0f1439c8d2
commit f7970d35b2

@ -230,15 +230,15 @@
//header("HTTP/1.0 400 Invalid number of arguments");
http_response_code(400);
}
$id = !empty($url[4]) ? (int) $url[4] : null;
$username = !empty($url[5]) ? (string) $url[5] : null;
$password = !empty($url[6]) ? (string) $url[6] : null;
$sexe = !empty($url[7]) ? (string) $url[7] : null;
$nationality = !empty($url[8]) ? (string) $url[8] : null;
$nbCurrentCoins = (int) $url[9];
$totalnbCoins = (int) $url[10];
$nbGames = (int) $url[11];
$currentSkin = !empty($url[12]) ? (int) $url[12] : null;
$id = !empty($url[3]) ? (int) $url[3] : null;
$username = !empty($url[4]) ? (string) $url[4] : null;
$password = !empty($url[5]) ? (string) $url[5] : null;
$sexe = !empty($url[6]) ? (string) $url[6] : null;
$nationality = !empty($url[7]) ? (string) $url[7] : null;
$nbCurrentCoins = (int) $url[8];
$totalnbCoins = (int) $url[9];
$nbGames = (int) $url[10];
$currentSkin = !empty($url[11]) ? (int) $url[11] : null;
$usergw->putUser($id,$username,$password,$sexe, $nationality, $nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin);
http_response_code(200);
}

@ -7,7 +7,7 @@ const { Server } = require("socket.io");
const io = new Server(server);
io.on('connection', (socket) => {
console.log(socket.id)
console.log(socket.id);
socket.on('signIn', (id) => {
socket.join("U"+id);
@ -26,23 +26,28 @@ io.on('connection', (socket) => {
console.log("Message envoyé");
});
socket.on("createConversation", (tabId) =>{
socket.on("createConversation", (tabId, conv) =>{
tabId.forEach(id => {
socket.to("U"+id).emit("messageReceived");
socket.to("U"+id).emit("addedToConv", conv);
});
});
socket.on('joinMatch', (match) => {
socket.join("M" + match);
socket.join("M" + match.code);
socket.to("M"+ match.code).emit("matchUsersChanged");
});
socket.on('launchMatch', (match) => {
socket.to("M"+ match.code).emit("matchLaunched");
});
socket.on('quitMatch', (match) => {
socket.off("M" + match);
socket.to("M"+ match.code).emit("matchUsersChanged")
});
socket.on("playTicTacToe", (match, rowIndex, columnIndex, turn) =>{
socket.to("M"+match).emit("oppPlayTicTacToe", rowIndex, columnIndex, turn);
socket.to("M"+match.code).emit("oppPlayTicTacToe", rowIndex, columnIndex, turn);
});
});

@ -7,11 +7,15 @@ import { ScreenIndicator } from "../../components/ScreenIndicator";
import { TopBar } from "../../components/TopBar";
import { socket } from "../../../socketConfig";
import { MANAGER_MATCH, MANAGER_USER } from "../../../appManagers";
import { useUserStore } from "../../context/userContext";
export default function TicTacToeOnline(props: { navigation: any }){
const [init, setInit]=useState(0);
const [initTic, setInitTic]=useState(0);
const setUser = useUserStore((state) => state.setUser);
setUpTicTacToeOnline();
@ -29,7 +33,7 @@ export default function TicTacToeOnline(props: { navigation: any }){
const [currentTurn,setCurrentTurn] = useState("x");
const onPressCell = (rowIndex:number,columnIndex:number) => {
const onPressCell = async (rowIndex:number,columnIndex:number) => {
if (turnUser!==currentTurn){
Alert.alert("ce n'est pas à votre tour de jouer");
return;
@ -40,9 +44,9 @@ export default function TicTacToeOnline(props: { navigation: any }){
updateMap[rowIndex][columnIndex]=currentTurn;
return updateMap;
});
socket.emit("playTicTacToe", 1, rowIndex, columnIndex, currentTurn);
socket.emit("playTicTacToe", MANAGER_MATCH.getCurrentMatch(), rowIndex, columnIndex, currentTurn);
setCurrentTurn(currentTurn === "x"? "o" : "x");
const retour=checkWinning();
const retour= await checkWinning();
if(retour!=true){
checkComplete();
}
@ -54,9 +58,8 @@ export default function TicTacToeOnline(props: { navigation: any }){
};
function setUpTicTacToeOnline() {
if (init===0){
setInit(1);
socket.emit("inMatch", 1);
if (initTic===0){
setInitTic(1);
socket.on("oppPlayTicTacToe", (rowIndex, columnIndex, turn) =>{
setMap((existingMap) =>{
@ -80,36 +83,42 @@ export default function TicTacToeOnline(props: { navigation: any }){
}
}
const checkWinning = () =>{
async function endGame(win: number){
socket.off("oppPlayTicTacToe");
navigation.goBack();
const tmp=MANAGER_USER.getCurrentUser();
if (tmp!==null){
await MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, win);
MANAGER_USER.setCurrentUser(tmp);
setUser(tmp);
}
}
const checkWinning = async () =>{
const tmp=MANAGER_USER.getCurrentUser()
// Checks rows
for (let i=0; i<3; i++){
const isRowXWinning = map[i].every((cell)=> cell==="x");
const isRowOWinning = map[i] .every((cell)=>cell==="o");
if(isRowXWinning==true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
}
Alert.alert("X won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(2);
}
else{
await endGame(0);
}
return true;
}
else if(isRowOWinning==true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
}
Alert.alert("O won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(0);
}
else{
await endGame(2);
}
return true;
}
}
@ -127,29 +136,23 @@ export default function TicTacToeOnline(props: { navigation: any }){
}
}
if (isColumnXWinning == true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
}
Alert.alert("X won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(2);
}
else{
await endGame(0);
}
return true;
}
if(isColumnOWinning==true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
}
Alert.alert("O won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(0);
}
else{
await endGame(2);
}
return true;
}
@ -174,44 +177,34 @@ export default function TicTacToeOnline(props: { navigation: any }){
}
}
if(isDiag1OWinning==true || isDiag2OWinning==true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
}
Alert.alert("O won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(0);
}
else{
await endGame(2);
}
return true;
}
if(isDiag1XWinning==true || isDiag2XWinning==true){
if (tmp!==null){
if (turnUser==="x"){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 2);
}
else{
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 0);
}
}
Alert.alert("X won !");
navigation.goBack();
if (turnUser==="x"){
await endGame(2);
}
else{
await endGame(0);
}
return true;
}
};
const checkComplete = () =>{
const checkComplete = async () =>{
const isRow0Full = map[0].every((cell)=> cell!=="");
const isRow1Full = map[1] .every((cell)=>cell!=="");
const isRow2Full = map[2] .every((cell)=>cell!=="");
if(isRow0Full==true && isRow1Full==true && isRow2Full==true){
const tmp=MANAGER_USER.getCurrentUser();
if (tmp!==null){
MANAGER_MATCH.getCurrentMatch()?.updatePostMatch(tmp, 1);
}
Alert.alert("Draw !");
navigation.goBack();
await endGame(1);
return false;
}
};

@ -1,4 +1,4 @@
import { FC, ReactNode } from "react"
import { FC, ReactNode, useState } from "react"
import { Pressable, Image, ImageStyle, Text, View, Alert, ImageSourcePropType, TextStyle } from "react-native"
import React from "react"
import { Skin } from "../core/skin"
@ -22,24 +22,33 @@ FC<{conv: Conversation, navigation: any}> =
{
const setCurrentConv = useConversationStore((state) => state.setCurrentConv);
const [user,setUser]=useState(MANAGER_USER.getCurrentUser());
const [initVar,setInitVar]=useState(0);
const user1 = MANAGER_USER.getCurrentUser();
let tmp;
if (conv.getTabMessage().length<2){
tmp=conv.getTabUser()[0];
}
else if (user1?.isEqual(conv.getTabUser()[0])) tmp = conv.getTabUser()[1];
else tmp = conv.getTabUser()[0];
const user2 = tmp;
init();
function init(){
if (initVar===0){
setInitVar(1);
if (conv.getTabUser().length<2){
setUser(MANAGER_USER.getCurrentUser());
}
else if (MANAGER_USER.getCurrentUser()?.isEqual(conv.getLastMessage().getMessageSender())){
setUser(conv.getLastMessage().getMessageSender());
}
else{
setUser(conv.getTabUser()[1]);
}
}
}
return(
<Pressable onPress={() => {MANAGER_CONVERSATION.setCurrentConv(conv); setCurrentConv(conv); navigation.navigate(Conversation)}}>
<View style={styles.conv}>
<View>
<SkinComponent skin={user2.getCurrentSkin()} state='icon' nav={navigation}/>
<SkinComponent skin={user?.getCurrentSkin()} state='icon' nav={navigation}/>
</View>
<View style={{marginLeft: '5%', justifyContent: 'space-evenly'}}>
<Text style={styles.textNom}>{conv.getName()}</Text>

@ -41,6 +41,7 @@ FC<{game: Game, nav: any}> =
nav.navigate("GameSolo");
}
}, []);
return (

@ -17,6 +17,7 @@ import { useMatchStore } from '../context/matchContext';
import { MANAGER_MATCH } from '../../appManagers';
import { ScreenIndicator } from '../components/ScreenIndicator';
import { UserPreview } from "./UserPreview"
import { socket } from "../../socketConfig"
export const LobbyComponent :
@ -24,8 +25,12 @@ FC<{nav: any}> =
({nav}) =>
{
const setTabUser = useMatchStore((state) => state.setTabUser);
const setMatch = useMatchStore((state) => state.setMatch);
const [initUsers, setInitUsers] = useState(0);
const [init, setInit] = useState(0);
function getUsers(){
if (initUsers===0){
@ -45,6 +50,36 @@ FC<{nav: any}> =
}
async function launchMatch(){
}
function initMatchSocket(){
if (init===0){
setInit(1);
socket.on("matchUsersChanged", async () =>{
const match=MANAGER_MATCH.getCurrentMatch();
if (match !==null){
await MANAGER_MATCH.getLoaderMatch().loadByID(match.code).then((res) =>{
MANAGER_MATCH.setCurrentMatch(res);
setMatch(res);
setInitUsers(0);
getUsers();
});
}
});
socket.on("matchLaunched", async () =>{
nav.navigate(MANAGER_MATCH.getCurrentMatch()?.getGame().getName().replace(/\s/g, ''));
});
}
}
initMatchSocket();
if(MANAGER_MATCH.getCurrentMatch()?.getGame().getNbPlayerMax()==1){
return (
<View style={stylesScreen.bodyStartCenter}>
@ -75,7 +110,7 @@ FC<{nav: any}> =
/>
<Pressable
style={style.pressable}
onPress={() => nav.navigate(MANAGER_MATCH.getCurrentMatch()?.getGame().getName().replace(/\s/g, ''))}
onPress={() => {socket.emit("launchMatch", MANAGER_MATCH.getCurrentMatch()); nav.navigate(MANAGER_MATCH.getCurrentMatch()?.getGame().getName().replace(/\s/g, ''))}}
>
<Text style={style.text}>Lancer la partie</Text>
</Pressable>

@ -52,7 +52,7 @@ FC<{nav: any, state?: string}> =
await MANAGER_CONVERSATION.getsaverConversation().deleteUserToConversation(tmpConv, tmp);
const trouveIndex = (element: Conversation) => element.getId()===tmpConv.getId();
const index=MANAGER_CONVERSATION.getTabConv().findIndex(trouveIndex);
MANAGER_CONVERSATION.getTabConv().splice(index);
MANAGER_CONVERSATION.getTabConv().splice(index, 1);
if (tmpConv.getTabUser().length===1){
await MANAGER_CONVERSATION.getsaverConversation().deleteConversation(tmpConv);
}
@ -70,6 +70,7 @@ FC<{nav: any, state?: string}> =
const m=new MatchModifier();
if (tmp!==null && tmpMatch!==null){
socket.emit("quitMatch", tmpMatch);
socket.off("M" + tmpMatch.code);
await m.quitMatch(tmp, tmpMatch);
resetMatch();
resetTabUserMatch();
@ -111,7 +112,7 @@ FC<{nav: any, state?: string}> =
case 'conversation':
return (
<View style={styles.header}>
<Pressable onPress={() => { resetCurrentConv; MANAGER_CONVERSATION.setCurrentConv(null); nav.goBack()}}>
<Pressable onPress={() => { resetCurrentConv(); MANAGER_CONVERSATION.setCurrentConv(null); nav.goBack()}}>
<Image source={cross} style={styles.icon}/>
</Pressable>
<Text style={styles.titre}>{useConversationStore().currentConv?.getName()}</Text>

@ -67,6 +67,6 @@ export abstract class Match{
}
abstract updatePostMatch(user:User, points:number):void;
abstract updatePostMatch(user:User, points:number):Promise<void>;
}

@ -10,9 +10,9 @@ export default class MatchCasino extends Match{
super(code, inGame, tabUser, game);
}
updatePostMatch(user:User, points: number): void {
async updatePostMatch(user:User, points: number): Promise<void> {
const manage= new UserCoinsModifier();
manage.addCoins(user, this.getGame().coinsCalculator(points));
await manage.addCoins(user, this.getGame().coinsCalculator(points));
}
}

@ -10,8 +10,8 @@ export default class MatchMulti extends Match{
super(code, inGame, tabUser, game);
}
updatePostMatch(user:User, points: number): void {
async updatePostMatch(user:User, points: number): void {
const manage= new UserCoinsModifier();
manage.addCoins(user, this.getGame().coinsCalculator(points));
await manage.addCoins(user, this.getGame().coinsCalculator(points));
}
}

@ -10,8 +10,8 @@ export default class MatchSolo extends Match{
super(code, inGame, tabUser, game);
}
updatePostMatch(user:User, points: number): void {
async updatePostMatch(user:User, points: number): void {
const manage= new UserCoinsModifier();
manage.addCoins(user, this.getGame().coinsCalculator(points));
await manage.addCoins(user, this.getGame().coinsCalculator(points));
}
}

@ -66,7 +66,8 @@ export default function AddConversation(props: {navigation:any}){
(objA, objB) => objB.getLastMessage().getMessageDate().getTime() - objA.getLastMessage().getMessageDate().getTime(),
);
setTabConv(MANAGER_CONVERSATION.getTabConv());
socket.emit("createConversation", tabId);
socket.emit("createConversation", tabId, res);
socket.emit("inConv", res);
navigation.goBack();
}
});

@ -26,7 +26,7 @@ function ConversationScreen(props: { navigation: any; }) {
await MANAGER_CONVERSATION.getsaverConversation().addMessage(tmpConv.getId(), e.nativeEvent.text, new Date(), tmpUs).then((res) => {
if (res!==null){
const trouveIndex = (element: Conversation) => element.getId()===tmpConv.getId();
MANAGER_CONVERSATION.getCurrentConv()?.getTabMessage().push(res);
MANAGER_CONVERSATION.getCurrentConv()?.ajouterMessage(res);
const index=MANAGER_CONVERSATION.getTabConv().findIndex(trouveIndex);
const tmp=MANAGER_CONVERSATION.getCurrentConv();
if (tmp!==null){

@ -1,5 +1,5 @@
import { StatusBar } from 'expo-status-bar'
import {View, FlatList, Text, Alert} from 'react-native'
import {View, FlatList, Text, Alert, NativeSyntheticEvent, TextInputSubmitEditingEventData} from 'react-native'
import React, { useState } from 'react';
import { TopBar } from '../components/TopBar';
import { BotBar } from '../components/BotBar';
@ -10,23 +10,35 @@ import stylesScreen from './style/screens.style'
import styles from './style/GameChoice.style'
import { MANAGER_GAME, MANAGER_MATCH, MANAGER_USER } from '../../appManagers';
import { GameList } from '../components/GameList';
import { useMatchStore } from '../context/matchContext';
import { socket } from '../../socketConfig';
function GameChoice(props: { navigation: any}) {
const { navigation} = props
const [matchId, setMatchId] = useState('');
async function joinMatch(id:string){
const newId = parseInt(id);
const setMatch = useMatchStore((state) => state.setMatch);
async function joinMatch(id:NativeSyntheticEvent<TextInputSubmitEditingEventData>){
const newId = parseInt(id.nativeEvent.text);
const tmp=MANAGER_USER.getCurrentUser();
if (tmp !== null){
await MANAGER_MATCH.getsaverMatch().joinMatch(tmp, newId).then((res) =>{
if (res===null){
Alert.alert()
Alert.alert("L'id du match n'existe pas ou un jeu est déjà lancé ou il y a trop de joueurs");//changer ça avec d'autre codes de retour
}
else{
MANAGER_MATCH.setCurrentMatch(res);
setMatch(res);
socket.emit("joinMatch", res);
navigation.navigate("GameSolo");
}
});
}
}
if(MANAGER_GAME.currentGameType === "solo" ){
return (

@ -32,6 +32,8 @@ function SignIn(props: { navigation: any; }) {
const setTabSkin = useSkinStore((state) => state.setTabSkin);
const [waitConnect, setWaitConnect] = useState(0);
const errorList = useSelector((state: RootState) => state.credentialErrors.loginErrorList);
@ -44,12 +46,11 @@ function SignIn(props: { navigation: any; }) {
dispatch(updateIncorrectCredentials(true));
}
let waitConnect=0;
async function handleUserConnect(username: string, password: string){
if (waitConnect==0){
waitConnect=1;
setWaitConnect(-1);
await MANAGER_USER.getLoaderUser().loadByUsernamePassword(username, password).then(async (res) => {
if (res!=null){
@ -67,10 +68,10 @@ function SignIn(props: { navigation: any; }) {
}
else{
Alert.alert("Incorrect Username or Password");
setWaitConnect(0);
}
});
waitConnect=0;
}
return;
}
@ -83,6 +84,10 @@ function SignIn(props: { navigation: any; }) {
socket.on("messageReceived", async () =>{
await handleConversationLoad();
});
socket.on("addedToConv", async (conv) =>{
socket.emit("inConv", conv);
await handleConversationLoad();
});
}
async function handleConversationLoad(){
@ -96,9 +101,6 @@ function SignIn(props: { navigation: any; }) {
if (tmpConv!==null){
const trouveIndex = (element: Conversation) => element.getId()===tmpConv.getId();
const index=MANAGER_CONVERSATION.getTabConv().findIndex(trouveIndex);
MANAGER_CONVERSATION.getTabConv()?.sort(
(objA, objB) => objB.getLastMessage().getMessageDate().getTime() - objA.getLastMessage().getMessageDate().getTime(),
);
MANAGER_CONVERSATION.setCurrentConv(MANAGER_CONVERSATION.getTabConv()[index]);
setCurrentConv(MANAGER_CONVERSATION.getCurrentConv());
}

@ -63,7 +63,10 @@ async function jsonToConversation(response:any) {
const sender:User | null= await MANAGER_USER.getLoaderUser().loadByID(message.idSender);
if (sender!=null){
tabMessage.push(new Message(message.id, message.content, sender, new Date(message.dateEnvoie)));
const tab=message.dateEnvoie.split(' ');
const tabDate=tab[0].split('-');
const tabPrecis=tab[1].split(":");
tabMessage.push(new Message(message.id, message.content, sender, new Date(tabDate[0],parseInt(tabDate[1])-1,tabDate[2],tabPrecis[0], tabPrecis[1], tabPrecis[2])));
}
if(conv.tabMessages.length===tabMessage.length){
resolve();

@ -21,7 +21,6 @@ export default class LoaderGameApi implements ILoaderGame{
.then(function (response: any) {
if (response.data != null && response.data != undefined){
response.data.forEach(game => {
switch(game.type){
case "GameSolo":
let mapSolo = new Map();
@ -54,7 +53,7 @@ export default class LoaderGameApi implements ILoaderGame{
method: 'get',
url: url,
}).then(function (response: any){
if (response.data!=undefined || response.data!==null){
if (response.data!=undefined && response.data!==null){
switch(response.data.type){
case "GameSolo":
let mapSolo = new Map();
@ -62,14 +61,17 @@ export default class LoaderGameApi implements ILoaderGame{
mapSolo.set(new Number(response.data.keys[i]), new Number(response.data.values[i]))
}
game = new GameSolo(response.data.id, response.data.name, response.data.image, response.data.nbPlayerMin, response.data.nbPlayerMax, mapSolo);
break;
case "GameMulti":
const mapMulti = new Map();
for (let i=0; i<response.data.keys.length; i++){
mapMulti.set(new Number(response.data.keys[i]), new Number(response.data.values[i]));
}
game = new GameMulti(response.data.id, response.data.name, response.data.image, response.data.nbPlayerMin, response.data.nbPlayerMax, mapMulti);
break;
case "GameCasino":
game = new GameCasino(response.data.id, response.data.name, response.data.image, response.data.nbPlayerMin, response.data.nbPlayerMax);
break;
}
}
});

@ -27,6 +27,7 @@ export default class SaverUserApi implements ISaverUser{
async updateUser(u: User): Promise<void> {
let us:User|null=null;
const url=this.baseUrl + 'putUser/'+ u.getId() + "/" + u.getUsername() + "/" + u.getPassword() + "/" + u.getSexe() + "/" + u.getNationality() + "/" + u.getCurrentCoins() + "/" + u.getTotalCoins() + "/" + u.getGamesPlayed() + "/" + u.getCurrentSkin().getSkinId();
console.log(url);
await this.axios({
method: 'put',
url: url,

Loading…
Cancel
Save