ADD: SaverMatchApi + persistance pour match
continuous-integration/drone/push Build is passing Details

peristanceBDD
Thomas Chazot 2 years ago
parent 48dbae30d9
commit 0dd98f20f6

@ -41,7 +41,7 @@ class MatchGateway{
}
/// Brief : Adding a NEW match in database
public function postMatch(int $idGame, int $idCreator){
public function postMatch(int $idGame, int $idCreator): ?Matchs{
$insertMatchQuery="INSERT INTO T_E_MATCH_MTC VALUES(NULL,0,:idGame)";
$insertPlayQuery = "INSERT INTO T_J_PLAY_MATCH_PLM VALUES(:idCreator,:id);";
$argInsertMatch=array('idGame'=>array($idGame, PDO::PARAM_INT));
@ -57,7 +57,7 @@ class MatchGateway{
$argInsertPlay= array('idCreator'=>array($idCreator,PDO::PARAM_INT),
'id'=>array($id,PDO::PARAM_INT));
$this->connection->execQuery($insertPlayQuery,$argInsertPlay);
return;
return new Matchs($id, 0, $idGame, [$idCreator]);
}
/// Brief : Modifying an EXISTING match in database

@ -164,6 +164,7 @@
$idCreator = !empty($url[5]) ? (int) $url[5] : null;
if ($idGame != null || $idCreator != null){
$match =$matchgw->postMatch($idGame,$idCreator);
echo json_encode($match, JSON_PRETTY_PRINT);
http_response_code(200);
} else{
header("HTTP/1.0 400 idGame or idCreator not given");
@ -203,7 +204,6 @@
$totalnbCoins = (int) $url[10];
$nbGames = (int) $url[11];
$currentSkin = !empty($url[12]) ? (int) $url[12] : null;
echo ($nbCurrentCoins . ' ' . $totalnbCoins . " ". $nbGames);
$usergw->putUser($id,$username,$password,$sexe, $nationality, $nbCurrentCoins,$totalnbCoins,$nbGames,$currentSkin);
http_response_code(200);
}

@ -17,6 +17,10 @@ io.on('connection', (socket) => {
socket.join("C" + conv.id);
});
socket.on('quitConv', (conv) => {
socket.off("C" + conv);
});
socket.on("messageSent", (conv) =>{
socket.to("C"+conv.id).emit("messageReceived");
console.log("Message envoyé");
@ -28,10 +32,15 @@ io.on('connection', (socket) => {
});
});
socket.on('inMatch', (match) => {
socket.on('joinMatch', (match) => {
socket.join("M" + match);
});
socket.on('quitMatch', (match) => {
socket.off("M" + match);
});
socket.on("playTicTacToe", (match, rowIndex, columnIndex, turn) =>{
socket.to("M"+match).emit("oppPlayTicTacToe", rowIndex, columnIndex, turn);
});

@ -10,9 +10,11 @@ import { Game } from "../core/game"
import styles from './style/Game.style';
import Lobby from "../screens/Lobby"
import ManagerMatch from "../services/matchServices/managerMatch"
import MatchCreator from "../core/Match/matchCreator"
import MatchModifier from "../core/Match/matchModifier"
import { useMatchStore } from "../context/matchContext"
import { MANAGER_MATCH, MANAGER_USER } from "../../appManagers"
import { MANAGER_GAME, MANAGER_MATCH, MANAGER_USER } from "../../appManagers"
import { GameSolo } from "../core/gameSolo"
import { socket } from "../../socketConfig"
export const GameComponent :
@ -27,12 +29,13 @@ FC<{game: Game, nav: any}> =
const setMatch = useMatchStore((state) => state.setMatch);
const createNewMatchSolo = useCallback(async (game : Game, nav: any) => {
const createNewMatch = useCallback(async (game : Game, nav: any) => {
const m=new MatchCreator();
const m=new MatchModifier();
const tmp=MANAGER_USER.getCurrentUser();
if (tmp!==null){
let match=await m.createMatch(tmp, game);
socket.emit("joinMatch", match);
MANAGER_MATCH.setCurrentMatch(match);
setMatch(match);
nav.navigate("GameSolo");
@ -42,7 +45,7 @@ FC<{game: Game, nav: any}> =
return (
<View>
<Pressable onPress={() => createNewMatchSolo(game, nav)}>
<Pressable onPress={() => createNewMatch(game, nav)}>
<Image
style={styles.image}
source={{uri: game.getImageSource()}}

@ -22,7 +22,6 @@ FC<{nav: any}> =
if(MANAGER_GAME.currentGameType === "solo" ){
gameList = MANAGER_GAME.getTabGameSolo();
console.log(gameList);
}
else if(MANAGER_GAME.currentGameType === "multi"){
gameList = MANAGER_GAME.getTabGameMulti();

@ -1,5 +1,5 @@
import { FC} from "react"
import { FlatList } from "react-native"
import { FC, useState} from "react"
import { Button, FlatList } from "react-native"
import React from "react"
import { Game } from "../core/game"
@ -22,8 +22,28 @@ export const LobbyComponent :
FC<{nav: any}> =
({nav}) =>
{
const setTabUser = useMatchStore((state) => state.setTabUser);
const [initUsers, setInitUsers] = useState(0);
function getUsers(){
if (initUsers===0){
setInitUsers(1);
const tmp:any=[];
MANAGER_MATCH.getCurrentMatch()?.getTabUsers().forEach(user => {
tmp.push(user);
});
const tmpGame=MANAGER_MATCH.getCurrentMatch()?.getGame();
if (tmpGame!=undefined){
for (let i=tmp.length; i<tmpGame.getNbPlayerMax(); i++){
tmp.push(null);
}
}
setTabUser(tmp);
}
}
const match = useMatchStore().match;
if(MANAGER_MATCH.getCurrentMatch()?.getGame().getNbPlayerMax()==1){
return (
<View style={stylesScreen.bodyStartCenter}>
@ -35,16 +55,32 @@ FC<{nav: any}> =
/>
<Image
style={{height: '30%', width: '70%', alignSelf:'center', borderRadius: 25, marginTop: "15%"}}
source={{uri: match?.getGame().getImageSource()}}
source={{uri: MANAGER_MATCH.getCurrentMatch()?.getGame().getImageSource()}}
/>
</View>
</View>
);
}
else{
getUsers();
return(
<View style={stylesScreen.bodyStartCenter}>
<UserPreview user={MANAGER_MATCH.getCurrentMatch()?.getTabUsers()[0]}></UserPreview>
<FlatList
data={useMatchStore().tabUser}
keyExtractor={usr =>usr?.getUsername() || usr}
renderItem={({item}) => <UserPreview user={item}/>}
/>
<View style={stylesScreen.bodyCenter}>
<Button
title='Lancer la partie'
onPress={() => nav.navigate(MANAGER_MATCH.getCurrentMatch()?.getGame().getName().replace(/\s/g, ''))}
/>
</View>
<Image
style={{width:100, height:100}}
source={{uri: MANAGER_MATCH.getCurrentMatch()?.getGame().getImageSource()}}
/>
</View>
);
}

@ -10,11 +10,12 @@ import { User } from "../core/User/user"
*/
import styles from './style/TopBar.style';
import { useMatchStore } from "../context/matchContext"
import { MANAGER_CONVERSATION, MANAGER_USER } from "../../appManagers"
import { MANAGER_CONVERSATION, MANAGER_MATCH, MANAGER_USER } from "../../appManagers"
import { useUserStore } from "../context/userContext"
import { useConversationStore } from "../context/conversationContext"
import { socket } from "../../socketConfig"
import { Conversation } from "../core/conversation"
import MatchModifier from "../core/Match/matchModifier"
/*
Images required
@ -36,6 +37,8 @@ FC<{nav: any, state?: string}> =
{
const resetMatch = useMatchStore((state) => state.resetMatch);
const resetTabUserMatch = useMatchStore((state) => state.resetTabUser);
const resetCurrentConv = useConversationStore((state) => state.resetCurrentConv);
const setTabConv = useConversationStore((state) => state.setTabConv);
@ -56,10 +59,24 @@ FC<{nav: any, state?: string}> =
MANAGER_CONVERSATION.setCurrentConv(null);
setTabConv(MANAGER_CONVERSATION.getTabConv());
socket.emit("messageSent", tmpConv);
socket.emit("quitConv", tmpConv);
nav.goBack();
}
}
async function clickQuitMatch(){
const tmp=MANAGER_USER.getCurrentUser();
const tmpMatch=MANAGER_MATCH.getCurrentMatch();
const m=new MatchModifier();
if (tmp!==null && tmpMatch!==null){
socket.emit("quitMatch", tmpMatch);
await m.quitMatch(tmp, tmpMatch);
resetMatch();
resetTabUserMatch();
MANAGER_MATCH.setCurrentMatch(null);
}
}
/* The display of this component depends of the screen from where it has been called:
* From the Settings (icon) : Name of the page + cross button
* From other : skin + Title + parameters icon
@ -85,7 +102,7 @@ FC<{nav: any, state?: string}> =
<Image source={msc} style={styles.icon}/>
</Pressable>
<Text style={styles.titre}>BOB PARTY</Text>
<Pressable onPress={() => { resetMatch(); nav.goBack()}}>
<Pressable onPress={() => { clickQuitMatch(); nav.goBack()}}>
<Image source={cross} style={styles.icon}/>
</Pressable>
</View>

@ -8,12 +8,6 @@ import { Game } from "../core/game"
Importing the correct stylesheet
*/
import styles from './style/UserPreview.style';
import Lobby from "../screens/Lobby"
import ManagerMatch from "../services/matchServices/managerMatch"
import MatchCreator from "../core/Match/matchCreator"
import { useMatchStore } from "../context/matchContext"
import { MANAGER_MATCH, MANAGER_USER } from "../../appManagers"
import { Match } from "../core/Match/match"
import { User } from "../core/User/user"
export const UserPreview :
@ -22,10 +16,11 @@ export const UserPreview :
* match : match that must be displayed
* nav : tool needed to allow the navigation between the screens
*/
FC<{user: User | undefined}> =
FC<{user: User | null}> =
({user}) =>
{
if(user != undefined){
console.log(user);
if(user !== null){
return (
<View style= {styles.view}>
<Image

@ -7,10 +7,10 @@ import { User } from "../core/User/user";
// Define store types
interface MatchState {
match: Match | null;
tabUser: User[] | null[];
tabUser: any[];
setMatch: (match: Match|null) => void;
resetMatch: () => void;
setTabUser: (tabUser: User[] | null[]) => void;
setTabUser: (tabUser: any[]) => void;
resetTabUser: () => void;
}

@ -1,11 +0,0 @@
import { MANAGER_MATCH } from "../../../appManagers";
import { Game } from "../game";
import { User } from "../User/user";
import { Match } from "./match";
export default class MatchCreator{
async createMatch(u:User, g:Game): Promise<Match>{
return await MANAGER_MATCH.getsaverMatch().saveMatch(u, g);
}
}

@ -0,0 +1,26 @@
import { MANAGER_MATCH } from "../../../appManagers";
import { Game } from "../game";
import { GameSolo } from "../gameSolo";
import { User } from "../User/user";
import { Match } from "./match";
import MatchSolo from "./matchSolo";
export default class MatchModifier{
async createMatch(u:User, g:Game): Promise<Match>{
if (g instanceof GameSolo){
return new MatchSolo(0, false, [u], g);
}
return await MANAGER_MATCH.getsaverMatch().saveMatch(u, g);
}
async quitMatch(u:User, m:Match): Promise<void>{
const saver=MANAGER_MATCH.getsaverMatch();
if (m.getTabUsers().length===1){
saver.deleteMatch(m);
}
else{
saver.deleteUserFromMatch(u);
}
}
}

@ -10,6 +10,8 @@ import { BigBlueButton } from '../components/BigBlueButton';
import { useMatchStore } from '../context/matchContext';
import { FlatList, TextInput } from 'react-native-gesture-handler';
import { PlayerBox } from '../components/PlayerBox';
import { MANAGER_MATCH } from '../../appManagers';
import { User } from '../core/User/user';
function MatchMaking(props: { navigation: any; }) {

@ -63,8 +63,6 @@ function SignIn(props: { navigation: any; }) {
socket.on("messageReceived", async () =>{
await handleConversationLoad();
});
const match=await MANAGER_MATCH.getLoaderMatch().loadByID(1);
console.log(match);
navigation.navigate('HomeTab');
}
else{

@ -18,8 +18,15 @@ export default interface ISaverMatch{
deleteMatch(m:Match): Promise<void>;
/**
* updateMatch methode that update a Match in the data management system
* m the Match we want to update
* deleteUserFromMatch methode that delete a User from a Match in the data management system
* u the User we want to delete
*/
updateMatch(m:Match): Promise<void>;
deleteUserFromMatch(u:User): Promise<void>;
/**
* joinMatch methode that add a User to a Match in the data management system
* u the User we want to add
* id the id of the Match
*/
joinMatch(u:User, id:number): Promise<Match | null>;
}

@ -8,25 +8,71 @@ import { GameMulti } from "../../core/gameMulti";
import MatchSolo from "../../core/Match/matchSolo";
import MatchMulti from "../../core/Match/matchMulti";
import MatchCasino from "../../core/Match/matchCasino";
import { MANAGER_MATCH } from "../../../appManagers";
export default class SaverMatchApi implements ISaverMatch{
private axios = require('axios').default;
async saveMatch(u:User, g:Game): Promise<Match> {
//match = mettre dans bdd
if (g instanceof GameSolo){
return new MatchSolo(12, false, [u], g);
}
else if(g instanceof GameMulti){
return new MatchMulti(12, false, [u], g);
let id=0;
let url='http://localhost:8888/api-rest/index.php/postMatch/' + g.id + "/" + u.id;
await this.axios({
method: 'post',
url: url,
})
.then(async (response: any) => {
id=response.data.id;
});
if(g instanceof GameMulti){
return new MatchMulti(id, false, [u], g);
}
return new MatchCasino(12, false, [u], g);
return new MatchCasino(id, false, [u], g);
}
async deleteMatch(m: Match): Promise<void> {
throw new Error("Method not implemented.");
let url='http://localhost:8888/api-rest/index.php/deleteMatch/' + m.getCode();
await this.axios({
method: 'delete',
url: url,
});
}
async updateMatch(m: Match): Promise<void> {
throw new Error("Method not implemented.");
async deleteUserFromMatch(u: User): Promise<void> {
let url='http://localhost:8888/api-rest/deleteUserFromMatch.php/' + u.id;
await this.axios({
method: 'put',
url: url,
});
}
async joinMatch(u:User, id:number): Promise<Match | null>{
let match:Match|null=null;
let url='http://localhost:8888/api-rest/index.php/addUserToMatch/' + id + "/" + u.id;
await MANAGER_MATCH.getLoaderMatch().loadByID(id).then(async (response)=>{
if (response!==undefined && response !== null){
if (response.getTabUsers().length<response.getGame().getNbPlayerMax() || response.getInGame()===false){
response.getTabUsers().push(u);
match=response;
await this.axios({
method: 'put',
url: url,
});
}
else{
return null;
}
}
else{
return null;
}
});
return match;
}
}
Loading…
Cancel
Save