Compare commits
119 Commits
Api_Game_E
...
master
@ -1,27 +0,0 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Dart SDK">
|
||||
<CLASSES>
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/async" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/cli" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/collection" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/convert" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/core" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/developer" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/ffi" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/html" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/indexed_db" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/io" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/isolate" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/js" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/js_util" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/math" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/mirrors" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/svg" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/typed_data" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/web_audio" />
|
||||
<root url="file://$PROJECT_DIR$/../../../../src/flutter/bin/cache/dart-sdk/lib/web_gl" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</component>
|
After Width: | Height: | Size: 55 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 128 KiB |
After Width: | Height: | Size: 462 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 215 KiB |
After Width: | Height: | Size: 5.9 MiB |
After Width: | Height: | Size: 56 KiB |
After Width: | Height: | Size: 275 KiB |
After Width: | Height: | Size: 198 KiB |
After Width: | Height: | Size: 688 KiB |
@ -0,0 +1,32 @@
|
||||
package org.acme.Api.controllers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
@ApplicationScoped
|
||||
@Path("/test")
|
||||
public class TestController {
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
@Path("/po")
|
||||
public String hello() {
|
||||
return "Hello from RESTEasy Reactive";
|
||||
}
|
||||
|
||||
}
|
After Width: | Height: | Size: 206 KiB |
Before Width: | Height: | Size: 544 B After Width: | Height: | Size: 8.1 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 9.8 KiB |
Before Width: | Height: | Size: 442 B After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 10 KiB |
After Width: | Height: | Size: 5.3 KiB |
Before Width: | Height: | Size: 721 B After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 27 KiB |
After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 49 KiB |
After Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 75 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 543 KiB |
Before Width: | Height: | Size: 295 B After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 450 B After Width: | Height: | Size: 7.4 KiB |
Before Width: | Height: | Size: 282 B After Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 462 B After Width: | Height: | Size: 7.0 KiB |
Before Width: | Height: | Size: 704 B After Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 4.1 KiB |
Before Width: | Height: | Size: 586 B After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 862 B After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 862 B After Width: | Height: | Size: 20 KiB |
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 762 B After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 32 KiB |
@ -0,0 +1,10 @@
|
||||
class GameFields {
|
||||
static final List<String> values = [
|
||||
id, date, pointsCurrentUser, userId
|
||||
];
|
||||
|
||||
static final String id = '_id';
|
||||
static final String date = '_date';
|
||||
static final String pointsCurrentUser = '_points_current_user';
|
||||
static final String userId = '_user_id';
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
class GameDetailFields {
|
||||
static final List<String> values = [
|
||||
id, date, nameWinner, host
|
||||
];
|
||||
|
||||
static final String id = '_id';
|
||||
static final String date = '_date';
|
||||
static final String nameWinner = '_winner_id';
|
||||
static final String host = '_host';
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
class GameFields {
|
||||
static final List<String> values = [
|
||||
id, date, pointsCurrentUser, userId
|
||||
];
|
||||
|
||||
static final String id = '_id';
|
||||
static final String date = '_date';
|
||||
static final String pointsCurrentUser = '_points_current_user';
|
||||
static final String userId = '_user_id';
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
class PlayerFields {
|
||||
static final List<String> values = [
|
||||
idGame, name, image
|
||||
];
|
||||
|
||||
static final String idGame = '_idGame';
|
||||
static final String name = '_name';
|
||||
static final String image = '_image';
|
||||
}
|
||||
|
@ -0,0 +1,16 @@
|
||||
class StatFields {
|
||||
static final List<String> values = [
|
||||
idUser, nbVictory, nbGames, highscore, nbStrikes, nbSpares, nbScore, avgScore, avgPinsPerRound
|
||||
];
|
||||
|
||||
static final String nbVictory = '_nbVictory';
|
||||
static final String nbGames = '_nbGames';
|
||||
static final String highscore = '_highscore';
|
||||
static final String nbStrikes = '_nbStrikes';
|
||||
static final String nbSpares = '_nbSpares';
|
||||
static final String nbScore = '_nbScore';
|
||||
static final String avgScore = '_avgScore';
|
||||
static final String avgPinsPerRound = '_avgPinsPerRound';
|
||||
static final String idUser = '_idUser';
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
class UserFields {
|
||||
static final List<String> values = [
|
||||
id, name, image, mail
|
||||
];
|
||||
|
||||
static final String id = '_id';
|
||||
static final String name = '_name';
|
||||
static final String image = '_image';
|
||||
static final String mail = '_mail';
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
import 'package:bowl_in/model/GameDetail.dart';
|
||||
|
||||
import '../../model/Player.dart';
|
||||
import '../fields/GameDetailFields.dart';
|
||||
|
||||
class GameDetailMapper {
|
||||
static Map<String, dynamic> toJson(GameDetail gameDetail) {
|
||||
return {
|
||||
GameDetailFields.id: gameDetail.id,
|
||||
GameDetailFields.date: gameDetail.time.toIso8601String(),
|
||||
GameDetailFields.nameWinner: gameDetail.winner?.name,
|
||||
GameDetailFields.host: gameDetail.host
|
||||
};
|
||||
}
|
||||
|
||||
static GameDetail toModel(
|
||||
Map<String, dynamic> json, Player? winner, List<Player> players) {
|
||||
String dateString = json[GameDetailFields.date];
|
||||
return GameDetail(
|
||||
json[GameDetailFields.id],
|
||||
DateTime.parse(dateString),
|
||||
winner,
|
||||
json[GameDetailFields.host],
|
||||
players);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
import 'package:bowl_in/model/Game.dart';
|
||||
import '../../model/Player.dart';
|
||||
import '../../model/User.dart';
|
||||
import '../fields/GameFields.dart';
|
||||
|
||||
class GameMapper {
|
||||
static Map<String, dynamic> toJson(Game game, User user) {
|
||||
return {
|
||||
GameFields.id: game.id,
|
||||
GameFields.date: game.date.toIso8601String(),
|
||||
GameFields.pointsCurrentUser: game.pointsCurrentUser,
|
||||
GameFields.userId: user.id,
|
||||
};
|
||||
}
|
||||
|
||||
static Game toModel(Map<String, dynamic> json, List<Player> players) {
|
||||
return Game(
|
||||
json[GameFields.id],
|
||||
DateTime.parse(json[GameFields.date]),
|
||||
json[GameFields.pointsCurrentUser],
|
||||
players
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
import 'package:bowl_in/database/fields/PlayerFields.dart';
|
||||
|
||||
import '../../model/Game.dart';
|
||||
import '../../model/Player.dart';
|
||||
|
||||
class PlayerMapper {
|
||||
static Map<String, dynamic> toJson(Player player, Game game) {
|
||||
return {
|
||||
PlayerFields.idGame: game.id,
|
||||
PlayerFields.name: player.name,
|
||||
PlayerFields.image: player.image,
|
||||
};
|
||||
}
|
||||
|
||||
static Player toModel(Map<String, dynamic> json) {
|
||||
return Player(
|
||||
json[PlayerFields.name],
|
||||
json[PlayerFields.image]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
import 'package:bowl_in/database/fields/StatFields.dart';
|
||||
import '../../model/Stat.dart';
|
||||
import '../../model/User.dart';
|
||||
|
||||
class StatMapper {
|
||||
static Map<String, dynamic> toJson(Stat stat, User user) {
|
||||
return {
|
||||
StatFields.idUser: user.id,
|
||||
StatFields.nbVictory: stat.nbVictory,
|
||||
StatFields.nbGames: stat.nbGames,
|
||||
StatFields.highscore: stat.highscore,
|
||||
StatFields.nbStrikes: stat.nbStrikes,
|
||||
StatFields.nbSpares: stat.nbSpares,
|
||||
StatFields.nbScore: stat.nbScore,
|
||||
StatFields.avgScore: stat.avgScore,
|
||||
StatFields.avgPinsPerRound: stat.avgPinsPerRound
|
||||
};
|
||||
}
|
||||
|
||||
static Stat toModel(Map<String, dynamic> json) {
|
||||
return Stat(
|
||||
json[StatFields.nbVictory],
|
||||
json[StatFields.nbGames],
|
||||
json[StatFields.highscore],
|
||||
json[StatFields.nbStrikes],
|
||||
json[StatFields.nbSpares],
|
||||
json[StatFields.nbScore],
|
||||
json[StatFields.avgScore],
|
||||
json[StatFields.avgPinsPerRound]
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
import '../../model/Stat.dart';
|
||||
import '../../model/User.dart';
|
||||
import '../fields/UserFields.dart';
|
||||
|
||||
class UserMapper {
|
||||
static Map<String, dynamic> toJson(User user) {
|
||||
return {
|
||||
UserFields.id: user.id,
|
||||
UserFields.name: user.name,
|
||||
UserFields.image: user.image,
|
||||
UserFields.mail: user.mail,
|
||||
};
|
||||
}
|
||||
|
||||
static User toModel(Map<String, dynamic> json, Stat stat) {
|
||||
return User.withStat(
|
||||
json[UserFields.id],
|
||||
json[UserFields.name],
|
||||
json[UserFields.image],
|
||||
json[UserFields.mail],
|
||||
[],
|
||||
[],
|
||||
stat
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,336 @@
|
||||
import 'package:bowl_in/database/fields/PlayerFields.dart';
|
||||
import 'package:bowl_in/database/mappers/GameMapper.dart';
|
||||
import 'package:bowl_in/database/mappers/PlayerMapper.dart';
|
||||
import 'package:bowl_in/database/mappers/StatMapper.dart';
|
||||
import 'package:bowl_in/model/Game.dart';
|
||||
import 'package:bowl_in/model/GameDetail.dart';
|
||||
import 'package:bowl_in/model/User.dart';
|
||||
import 'package:path/path.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import '../../model/Player.dart';
|
||||
import '../../model/Stat.dart';
|
||||
import '../fields/GameDetailFields.dart';
|
||||
import '../fields/GameFields.dart';
|
||||
import '../fields/StatFields.dart';
|
||||
import '../fields/UserFields.dart';
|
||||
import '../mappers/GameDetailMapper.dart';
|
||||
import '../mappers/UserMapper.dart';
|
||||
|
||||
class BowlInDatabase {
|
||||
BowlInDatabase();
|
||||
|
||||
static final BowlInDatabase instance = BowlInDatabase._init();
|
||||
|
||||
static Database? _database;
|
||||
|
||||
BowlInDatabase._init();
|
||||
|
||||
static const String tableUser = 'users';
|
||||
static const String tableGame = 'games';
|
||||
static const String tableGameDetail = 'gameDetails';
|
||||
static const String tableStat = 'stats';
|
||||
static const String tablePlayer = 'players';
|
||||
|
||||
Future<Database> get database async {
|
||||
if (_database != null) return _database!;
|
||||
|
||||
_database = await _initDB('user.db');
|
||||
return _database!;
|
||||
}
|
||||
|
||||
Future<Database> _initDB(String filePath) async {
|
||||
final dbPath = await getDatabasesPath();
|
||||
final path = join(dbPath, filePath);
|
||||
|
||||
return await openDatabase(path,
|
||||
version: 6, onCreate: _createDB, onUpgrade: _upgradeDB);
|
||||
}
|
||||
|
||||
Future _createDB(Database db, int version) async {
|
||||
const idType = 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
||||
const textType = 'TEXT NOT NULL';
|
||||
const boolType = 'BOOLEAN NOT NULL';
|
||||
const integerType = 'INTEGER NOT NULL';
|
||||
const realType = 'REAL NOT NULL';
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE $tableUser (
|
||||
${UserFields.id} $idType,
|
||||
${UserFields.name} $boolType,
|
||||
${UserFields.image} $textType,
|
||||
${UserFields.mail} $textType
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE $tableGame (
|
||||
${GameFields.id} $idType,
|
||||
${GameFields.date} $textType,
|
||||
${GameFields.pointsCurrentUser} $integerType,
|
||||
${GameFields.userId} $integerType,
|
||||
FOREIGN KEY(${GameFields.userId}) REFERENCES $tableUser(${UserFields.id})
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE $tableGameDetail (
|
||||
${GameDetailFields.id} $idType,
|
||||
${GameDetailFields.date} $textType,
|
||||
${GameDetailFields.nameWinner} $textType,
|
||||
${GameDetailFields.host} $integerType
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE $tableStat (
|
||||
${StatFields.idUser} $integerType,
|
||||
${StatFields.nbVictory} $integerType,
|
||||
${StatFields.nbGames} $integerType,
|
||||
${StatFields.highscore} $integerType,
|
||||
${StatFields.nbStrikes} $integerType,
|
||||
${StatFields.nbSpares} $integerType,
|
||||
${StatFields.nbScore} $integerType,
|
||||
${StatFields.avgScore} $realType,
|
||||
${StatFields.avgPinsPerRound} $realType,
|
||||
FOREIGN KEY(${StatFields.idUser}) REFERENCES $tableUser(${UserFields.id})
|
||||
)
|
||||
''');
|
||||
|
||||
await db.execute('''
|
||||
CREATE TABLE $tablePlayer (
|
||||
id $idType,
|
||||
${PlayerFields.name} $textType,
|
||||
${PlayerFields.image} $textType,
|
||||
${PlayerFields.idGame} $integerType,
|
||||
FOREIGN KEY(${PlayerFields.idGame}) REFERENCES $tableGame(${GameFields.id})
|
||||
)
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> _upgradeDB(Database db, int oldVersion, int newVersion) async {
|
||||
if (oldVersion < 6 ) {
|
||||
await db.execute('DROP TABLE IF EXISTS $tableUser');
|
||||
await db.execute('DROP TABLE IF EXISTS $tableGame');
|
||||
await db.execute('DROP TABLE IF EXISTS $tableGameDetail');
|
||||
await db.execute('DROP TABLE IF EXISTS $tableStat');
|
||||
await db.execute('DROP TABLE IF EXISTS $tablePlayer');
|
||||
await _createDB(db, newVersion);
|
||||
}
|
||||
}
|
||||
|
||||
// User
|
||||
|
||||
Future<void> createUser(User user) async {
|
||||
final db = await instance.database;
|
||||
await db.insert(tableUser, UserMapper.toJson(user));
|
||||
await createStat(user);
|
||||
}
|
||||
|
||||
Future<User?> readUser(int id) async {
|
||||
final db = await instance.database;
|
||||
final result = await db
|
||||
.query(tableUser, where: '${UserFields.id} = ?', whereArgs: [id]);
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
final stat = await readStat(id);
|
||||
|
||||
User user;
|
||||
if (stat != null) {
|
||||
user = UserMapper.toModel(result.first, stat);
|
||||
} else {
|
||||
user = UserMapper.toModel(result.first, Stat.empty());
|
||||
}
|
||||
final games = await readGame(id);
|
||||
for (var game in games) {
|
||||
user.games.add(game);
|
||||
}
|
||||
return user;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateUser(User user) async {
|
||||
final db = await instance.database;
|
||||
|
||||
await db.transaction((txn) async {
|
||||
await txn.update(tableUser, UserMapper.toJson(user),
|
||||
where: '${UserFields.id} = ?', whereArgs: [user.id]);
|
||||
|
||||
await txn.update(tableStat, StatMapper.toJson(user.stat, user),
|
||||
where: '${StatFields.idUser} = ?', whereArgs: [user.id]);
|
||||
|
||||
// Insert new games for the user
|
||||
for (var game in user.games) {
|
||||
var result = await txn.query(tableGame,
|
||||
where: '${GameFields.id} = ? AND ${GameFields.userId} = ?',
|
||||
whereArgs: [game.id, user.id]);
|
||||
if (result.isNotEmpty) {
|
||||
await txn.update(tableGame, GameMapper.toJson(game, user),
|
||||
where: '${GameFields.id} = ? AND ${GameFields.userId} = ?',
|
||||
whereArgs: [game.id, user.id]);
|
||||
} else {
|
||||
await txn.insert(tableGame, GameMapper.toJson(game, user));
|
||||
for (var player in game.players) {
|
||||
await txn.insert(tablePlayer, PlayerMapper.toJson(player, game));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<int> deleteUser(int id) async {
|
||||
final db = await instance.database;
|
||||
|
||||
await deleteGame(id);
|
||||
|
||||
await deleteStat(id);
|
||||
|
||||
return await db.delete(
|
||||
tableUser,
|
||||
where: '${UserFields.id} = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
// GameDetail
|
||||
|
||||
Future<void> createGameDetail(GameDetail gameDetail) async {
|
||||
final db = await instance.database;
|
||||
|
||||
await db.transaction((txn) async {
|
||||
await txn.insert(tableGameDetail, GameDetailMapper.toJson(gameDetail));
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<GameDetail>> readGameDetail() async {
|
||||
final db = await instance.database;
|
||||
final result = await db.query(tableGameDetail);
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
List<GameDetail> gameDetails = [];
|
||||
for (var gameDetail in result) {
|
||||
List<Player> players = [];
|
||||
Player? winner = null;
|
||||
final resultPlayer = await db.query(tablePlayer,
|
||||
where: '${PlayerFields.idGame} = ?',
|
||||
whereArgs: [gameDetail[GameDetailFields.id]]);
|
||||
for (var player in resultPlayer) {
|
||||
var rPlayer = PlayerMapper.toModel(player);
|
||||
players.add(rPlayer);
|
||||
if (rPlayer.name == gameDetail[GameDetailFields.nameWinner]) {
|
||||
winner = rPlayer;
|
||||
}
|
||||
}
|
||||
gameDetails.add(GameDetailMapper.toModel(gameDetail, winner, players));
|
||||
}
|
||||
return gameDetails;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> deleteGameDetail() async {
|
||||
final db = await instance.database;
|
||||
|
||||
return await db.delete(tableGameDetail);
|
||||
}
|
||||
|
||||
// Game
|
||||
|
||||
Future<void> createGame(User user) async {
|
||||
final db = await instance.database;
|
||||
|
||||
await db.transaction((txn) async {
|
||||
for (var game in user.games) {
|
||||
await txn.insert(tableGame, GameMapper.toJson(game, user));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Game>> readGame(int id) async {
|
||||
final db = await instance.database;
|
||||
final result = await db
|
||||
.query(tableGame, where: '${GameFields.userId} = ?', whereArgs: [id]);
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
List<Game> games = [];
|
||||
for (var game in result) {
|
||||
List<Player> players = [];
|
||||
final resultPlayer = await db.query(tablePlayer,
|
||||
where: '${PlayerFields.idGame} = ?', whereArgs: [game[GameFields.id]]);
|
||||
for (var player in resultPlayer) {
|
||||
players.add(PlayerMapper.toModel(player));
|
||||
}
|
||||
games.add(GameMapper.toModel(game, players));
|
||||
players = [];
|
||||
}
|
||||
return games;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Future<int> deleteGame(int id) async {
|
||||
final db = await instance.database;
|
||||
|
||||
return await db.transaction((txn) async {
|
||||
await txn.delete(
|
||||
tablePlayer,
|
||||
where: '${PlayerFields.idGame} IN (SELECT ${GameFields.id} FROM $tableGame WHERE ${GameFields.userId} = ?)',
|
||||
whereArgs: [id],
|
||||
);
|
||||
|
||||
return await txn.delete(
|
||||
tableGame,
|
||||
where: '${GameFields.userId} = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// Stat
|
||||
|
||||
Future<void> createStat(User user) async {
|
||||
final db = await instance.database;
|
||||
await db.insert(tableStat, StatMapper.toJson(Stat.empty(), user));
|
||||
}
|
||||
|
||||
Future<Stat?> readStat(int id) async {
|
||||
final db = await instance.database;
|
||||
final result = await db
|
||||
.query(tableStat, where: '${StatFields.idUser} = ?', whereArgs: [id]);
|
||||
|
||||
if (result.isNotEmpty) {
|
||||
Stat stat = StatMapper.toModel(result.first);
|
||||
return stat;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateStat(User user) async {
|
||||
final db = await instance.database;
|
||||
await db.transaction((txn) async {
|
||||
await txn.update(tableStat, StatMapper.toJson(user.stat, user),
|
||||
where: '${StatFields.idUser} = ?', whereArgs: [user.id]);
|
||||
});
|
||||
}
|
||||
|
||||
Future<int> deleteStat(int id) async {
|
||||
final db = await instance.database;
|
||||
|
||||
return await db.delete(
|
||||
tableStat,
|
||||
where: '${StatFields.idUser} = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
Future close() async {
|
||||
final db = await instance.database;
|
||||
|
||||
db.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
import 'Player.dart';
|
||||
|
||||
final maxScoreInFrame = 30;
|
||||
|
||||
abstract class AbstractRound {
|
||||
int? _firstThrow;
|
||||
int? _secondThrow;
|
||||
int? _points;
|
||||
int _number;
|
||||
Player _player;
|
||||
|
||||
AbstractRound? _previousRound;
|
||||
|
||||
// Constructor
|
||||
AbstractRound(this._firstThrow, this._secondThrow, this._points, this._player, this._number);
|
||||
|
||||
// Getters and setters
|
||||
int? get firstThrow => _firstThrow;
|
||||
|
||||
set firstThrow(int? value) {
|
||||
_firstThrow = value;
|
||||
}
|
||||
|
||||
int? get secondThrow => _secondThrow;
|
||||
|
||||
set secondThrow(int? value) {
|
||||
_secondThrow = value;
|
||||
}
|
||||
|
||||
int? get points => _points;
|
||||
|
||||
set points(int? value) {
|
||||
if((value??0)>=maxScoreInFrame){
|
||||
_points=maxScoreInFrame;
|
||||
}else {
|
||||
_points = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int get number => _number;
|
||||
|
||||
set number(int value) {
|
||||
_number = value;
|
||||
}
|
||||
|
||||
Player get player => _player;
|
||||
|
||||
set player(Player value) {
|
||||
_player = value;
|
||||
}
|
||||
|
||||
AbstractRound? get previousRound => _previousRound;
|
||||
|
||||
set previousRound(AbstractRound? value) {
|
||||
_previousRound = value;
|
||||
}
|
||||
|
||||
bool computeNext(int val);
|
||||
void computePoints();
|
||||
bool shotIsStrike();
|
||||
int getMaxPinsThisShot();
|
||||
|
||||
bool isStrike() {
|
||||
return firstThrow==10;
|
||||
}
|
||||
|
||||
bool isSpare(){
|
||||
return firstThrow!=10 && (firstThrow ?? 0)+(secondThrow ?? 0)==10;
|
||||
}
|
||||
|
||||
bool isSpareOrStrike() {
|
||||
return (firstThrow ?? 0)+(secondThrow ?? 0)==10;
|
||||
}
|
||||
|
||||
int getNbStrike();
|
||||
int getNbSpares();
|
||||
int getPinsKnockedDown();
|
||||
|
||||
void subscribe(AbstractRound nextRound){
|
||||
nextRound.previousRound=this;
|
||||
}
|
||||
|
||||
void update(int val){
|
||||
print(" ROUND " + number.toString() + "UPDATE : " + val.toString());
|
||||
|
||||
points = (points ?? 0) + val;
|
||||
previousRound?.update(val);
|
||||
}
|
||||
|
||||
void unsubscribePreviousRound(){
|
||||
print("UNSUBSCRIBE");
|
||||
previousRound?.unsubscribePreviousRound();
|
||||
previousRound=null;
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +1,17 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'Player.dart';
|
||||
|
||||
class Game {
|
||||
Uuid _id;
|
||||
DateTime _time;
|
||||
int _pointsCurrentUser;
|
||||
bool _isFinished;
|
||||
List<String> _playerImages = [];
|
||||
final int _id;
|
||||
final DateTime _date;
|
||||
final int _pointsCurrentUser;
|
||||
final List<Player> _players;
|
||||
|
||||
// Constructor
|
||||
Game(this._id, this._time, this._pointsCurrentUser, this._isFinished, this._playerImages);
|
||||
|
||||
// Getters and setters
|
||||
Uuid get id => _id;
|
||||
|
||||
set id(Uuid value) {
|
||||
_id = value;
|
||||
}
|
||||
|
||||
DateTime get time => _time;
|
||||
|
||||
set time(DateTime value) {
|
||||
_time = value;
|
||||
}
|
||||
Game(this._id, this._date, this._pointsCurrentUser,
|
||||
this._players);
|
||||
|
||||
int get id => _id;
|
||||
DateTime get date => _date;
|
||||
int get pointsCurrentUser => _pointsCurrentUser;
|
||||
|
||||
set pointsCurrentUser(int value) {
|
||||
_pointsCurrentUser = value;
|
||||
}
|
||||
|
||||
bool get isFinished => _isFinished;
|
||||
|
||||
set isFinished(bool value) {
|
||||
_isFinished = value;
|
||||
}
|
||||
|
||||
List<String> get playerImages => _playerImages;
|
||||
|
||||
set playerImages(List<String> value) {
|
||||
_playerImages = value;
|
||||
}
|
||||
List<Player> get players => _players;
|
||||
}
|
||||
|
@ -1,82 +1,82 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:bowl_in/model/AbstractRound.dart';
|
||||
import 'package:bowl_in/model/Game.dart';
|
||||
import 'package:bowl_in/model/LastRound.dart';
|
||||
import 'Player.dart';
|
||||
import 'Round.dart';
|
||||
import 'User.dart';
|
||||
|
||||
class GameDetail {
|
||||
Uuid _id;
|
||||
DateTime _time;
|
||||
Uuid _winner;
|
||||
int _nbPoints;
|
||||
bool _isFinished;
|
||||
Round _currentRound;
|
||||
Player _host;
|
||||
List<Round> _rounds = [];
|
||||
List<Player> _players = [];
|
||||
int _id;
|
||||
final DateTime _time;
|
||||
Player? _winner;
|
||||
final int _host;
|
||||
final List<AbstractRound> _rounds = [];
|
||||
final List<Player> _players;
|
||||
final Map<Player, int> _points = {};
|
||||
|
||||
// Constructor
|
||||
GameDetail(
|
||||
this._id,
|
||||
this._time,
|
||||
this._winner,
|
||||
this._nbPoints,
|
||||
this._isFinished,
|
||||
this._currentRound,
|
||||
this._host,
|
||||
this._rounds,
|
||||
this._players);
|
||||
GameDetail(this._id, this._time, this._winner, this._host, this._players) {
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
players.forEach((element) {
|
||||
this.rounds.add(Round(null, null, 0, element, i));
|
||||
});
|
||||
}
|
||||
players.forEach((element) {
|
||||
this.rounds.add(LastRound(null, null, 0, element, 10, null));
|
||||
});
|
||||
}
|
||||
|
||||
// Getters and setters
|
||||
Uuid get id => _id;
|
||||
int get id => _id;
|
||||
|
||||
set id(Uuid value) {
|
||||
set id(int value) {
|
||||
_id = value;
|
||||
}
|
||||
|
||||
DateTime get time => _time;
|
||||
|
||||
set time(DateTime value) {
|
||||
_time = value;
|
||||
}
|
||||
|
||||
Uuid get winner => _winner;
|
||||
Player? get winner => _winner;
|
||||
|
||||
set winner(Uuid value) {
|
||||
set winner(Player? value) {
|
||||
_winner = value;
|
||||
}
|
||||
|
||||
int get nbPoints => _nbPoints;
|
||||
int get host => _host;
|
||||
|
||||
set nbPoints(int value) {
|
||||
_nbPoints = value;
|
||||
}
|
||||
|
||||
bool get isFinished => _isFinished;
|
||||
List<AbstractRound> get rounds => _rounds;
|
||||
|
||||
set isFinished(bool value) {
|
||||
_isFinished = value;
|
||||
}
|
||||
List<Player> get players => _players;
|
||||
|
||||
Round get currentRound => _currentRound;
|
||||
Map<Player, int> get points => _points;
|
||||
|
||||
set currentRound(Round value) {
|
||||
_currentRound = value;
|
||||
void addGameToUsers() {
|
||||
for (var p in players) {
|
||||
if (p is User) {
|
||||
p.games.add(Game(this.id, this.time, points[p] ?? 0, players));
|
||||
p.stat.updateStats(this, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Player get host => _host;
|
||||
|
||||
set host(Player value) {
|
||||
_host = value;
|
||||
void computeWinner() {
|
||||
print(getRank().entries.first.key.name);
|
||||
this.winner = getRank().entries.first.key;
|
||||
}
|
||||
|
||||
List<Round> get rounds => _rounds;
|
||||
|
||||
set rounds(List<Round> value) {
|
||||
_rounds = value;
|
||||
void computeScores() {
|
||||
print("====COMPUTE POINTS====");
|
||||
for (var element in rounds) {
|
||||
points[element.player] =
|
||||
(points[element.player] ?? 0) + (element.points ?? 0);
|
||||
print(element.points);
|
||||
}
|
||||
computeWinner();
|
||||
addGameToUsers();
|
||||
}
|
||||
|
||||
List<Player> get players => _players;
|
||||
|
||||
set players(List<Player> value) {
|
||||
_players = value;
|
||||
Map<Player, int> getRank() {
|
||||
var sortedByValueMap = Map.fromEntries(points.entries.toList()
|
||||
..sort((e1, e2) => e2.value.compareTo(e1.value)));
|
||||
return sortedByValueMap;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
import 'package:bowl_in/model/AbstractRound.dart';
|
||||
import 'package:bowl_in/model/IManager.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import 'GameDetail.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'Round.dart';
|
||||
|
||||
class GamePlayer {
|
||||
late GameDetail _game;
|
||||
final IManager _parent;
|
||||
int currentRoundIndex = 0;
|
||||
|
||||
GamePlayer(this._parent);
|
||||
|
||||
GameDetail get game => _game;
|
||||
|
||||
set game(GameDetail value) {
|
||||
currentRoundIndex = 0;
|
||||
_game = value;
|
||||
}
|
||||
|
||||
AbstractRound? get currentRound {
|
||||
if (currentRoundIndex < game.rounds.length){
|
||||
return game.rounds[currentRoundIndex];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void onNext(bool isRoundFinished, BuildContext? context) {
|
||||
if (isRoundFinished) {
|
||||
print("++");
|
||||
currentRoundIndex++;
|
||||
}
|
||||
if (currentRoundIndex >= game.rounds.length) {
|
||||
print("FINISHED");
|
||||
|
||||
game.id=_parent.gameMgr.getNextId();
|
||||
_parent.gameMgr.addGame(game);
|
||||
|
||||
game.computeScores();
|
||||
_parent.userMgr.saveUser(_parent.userCurrent);
|
||||
_parent.gameMgr.saveGame(_game);
|
||||
context?.go("/scoreboard", extra: game);
|
||||
} else {
|
||||
print("IN GAME : " + currentRoundIndex.toString());
|
||||
if (game.rounds[currentRoundIndex] is Round) {
|
||||
print("ROUND");
|
||||
} else {
|
||||
print("LAST ROUND");
|
||||
}
|
||||
context?.pushReplacement("/in-game",
|
||||
extra: game.rounds[currentRoundIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
void onSpareOrStrike() {
|
||||
if (currentRoundIndex < game.rounds.length - game.players.length) {
|
||||
print("ON SPAREORSTRIKE");
|
||||
game.rounds[currentRoundIndex]
|
||||
.subscribe(game.rounds[currentRoundIndex + game.players.length]);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,18 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:uuid/uuid_util.dart';
|
||||
import 'dart:math';
|
||||
|
||||
import 'Player.dart';
|
||||
|
||||
const _images = [
|
||||
"./assets/images/image_user_cyan.png",
|
||||
"./assets/images/image_user_red.png",
|
||||
"./assets/images/image_user_yellow.png",
|
||||
"./assets/images/image_user_pink.png",
|
||||
"./assets/images/image_user_orange.png",
|
||||
"./assets/images/image_user_green.png",
|
||||
"./assets/images/image_user_purple.png",
|
||||
];
|
||||
|
||||
class Guest extends Player {
|
||||
// Constructor
|
||||
Guest(Uuid id, String image, String name) : super(id, image, name);
|
||||
Guest(String name) : super(name, _images[Random().nextInt(_images.length)]);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
abstract class IAuthManager {
|
||||
|
||||
// Methods
|
||||
bool verifiedUser(String name, String password);
|
||||
bool verifiedUser(String mail, String password);
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:uuid/uuid_util.dart';
|
||||
import 'GameDetail.dart';
|
||||
import 'Player.dart';
|
||||
|
||||
abstract class IGameManager {
|
||||
// Methods
|
||||
GameDetail getGameById(Uuid id);
|
||||
List<GameDetail> getGamesByPlayerId(Uuid id);
|
||||
GameDetail getGameById(int id);
|
||||
saveGame(GameDetail gameDetail);
|
||||
List<GameDetail> getGamesByPlayerId(int id);
|
||||
List<GameDetail> getGamesByPlayer(Player user);
|
||||
List<GameDetail> getGamesByPlayers(List<Player> users);
|
||||
List<Player> getPlayersByIdGame(int id);
|
||||
Map<Player, int> getRankByIdGame(int id);
|
||||
int getNextId();
|
||||
addGame(GameDetail gd);
|
||||
}
|
||||
|
@ -1,21 +1,40 @@
|
||||
import 'package:bowl_in/model/GameDetail.dart';
|
||||
import 'package:bowl_in/model/GamePlayer.dart';
|
||||
import 'User.dart';
|
||||
import 'Game.dart';
|
||||
import 'IUserManager.dart';
|
||||
import 'IGameManager.dart';
|
||||
import 'Game.dart';
|
||||
|
||||
abstract class IManager {
|
||||
late User _userCurrent;
|
||||
late Game _gameCurrent;
|
||||
IUserManager _userMgr;
|
||||
IGameManager _gameMgr;
|
||||
late GameDetail _gameCurrent;
|
||||
late final GamePlayer _gamePlayer = GamePlayer(this);
|
||||
late final IUserManager _userMgr;
|
||||
late final IGameManager _gameMgr;
|
||||
|
||||
// Constructor
|
||||
IManager(this._userCurrent, this._gameCurrent);
|
||||
|
||||
// Getters and setters
|
||||
// Getters and settersz
|
||||
User get userCurrent => _userCurrent;
|
||||
Game get gameCurrent => _gameCurrent;
|
||||
|
||||
set userCurrent(User user) {
|
||||
_userCurrent = user;
|
||||
}
|
||||
|
||||
GamePlayer get gamePlayer => _gamePlayer;
|
||||
|
||||
GameDetail get gameCurrent => _gameCurrent;
|
||||
|
||||
set gameCurrent(GameDetail value) {
|
||||
_gameCurrent = value;
|
||||
}
|
||||
|
||||
IUserManager get userMgr => _userMgr;
|
||||
|
||||
IGameManager get gameMgr => _gameMgr;
|
||||
|
||||
set gameMgr(IGameManager value) {
|
||||
_gameMgr = value;
|
||||
}
|
||||
|
||||
set userMgr(IUserManager value) {
|
||||
_userMgr = value;
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,17 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'Player.dart';
|
||||
import 'User.dart';
|
||||
import 'IAuthManager.dart';
|
||||
|
||||
abstract class IUserManager {
|
||||
IAuthManager _authMgr;
|
||||
|
||||
// Constructor
|
||||
IUserManager(this._authMgr);
|
||||
|
||||
// Methods
|
||||
IAuthManager get authMgr => _authMgr;
|
||||
List<User> getUsersByName(String name);
|
||||
User getUserById(Uuid id);
|
||||
List<Player> getUsersByName(String name);
|
||||
Player getUserById(int id);
|
||||
saveUser(User user);
|
||||
List<User> getRankingWithFriends();
|
||||
}
|
||||
|
@ -0,0 +1,112 @@
|
||||
import 'package:bowl_in/model/AbstractRound.dart';
|
||||
|
||||
class LastRound extends AbstractRound {
|
||||
int? _thirdThrow;
|
||||
|
||||
LastRound(super.firstThrow, super.secondThrow, super.points, super.player,
|
||||
super.number, this._thirdThrow);
|
||||
|
||||
int? get thirdThrow => _thirdThrow;
|
||||
|
||||
set thirdThrow(int? value) {
|
||||
_thirdThrow = value;
|
||||
}
|
||||
|
||||
@override
|
||||
bool computeNext(int val) {
|
||||
if (firstThrow == null) {
|
||||
firstThrow = val;
|
||||
if (previousRound?.isSpare() ?? false) {
|
||||
previousRound?.update(val);
|
||||
}
|
||||
return false;
|
||||
} else if (secondThrow == null) {
|
||||
secondThrow = val;
|
||||
if ((firstThrow ?? 0) + (secondThrow ?? 0) < 10) {
|
||||
computePoints();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} else if ((firstThrow ?? 0) + (secondThrow ?? 0) >= 10) {
|
||||
thirdThrow = val;
|
||||
}
|
||||
computePoints();
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
void computePoints() {
|
||||
points = (firstThrow ?? 0) + (secondThrow ?? 0) + (thirdThrow ?? 0);
|
||||
print("Compute points : " + points.toString());
|
||||
|
||||
if (previousRound?.isStrike() ?? false) {
|
||||
update(points ?? 0);
|
||||
}
|
||||
unsubscribePreviousRound();
|
||||
}
|
||||
|
||||
@override
|
||||
bool shotIsStrike() {
|
||||
if (firstThrow == null) {
|
||||
return true;
|
||||
} else if (secondThrow == null) {
|
||||
return firstThrow == 10;
|
||||
} else {
|
||||
return secondThrow == 10;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
int getNbSpares() {
|
||||
int nb = 0;
|
||||
if (firstThrow != 10) {
|
||||
if ((firstThrow ?? 0) + (secondThrow ?? 0) == 10) {
|
||||
nb += 1;
|
||||
}
|
||||
} else {
|
||||
if ((thirdThrow ?? 0) + (secondThrow ?? 0) == 10) {
|
||||
nb += 1;
|
||||
}
|
||||
}
|
||||
return nb;
|
||||
}
|
||||
|
||||
@override
|
||||
int getNbStrike() {
|
||||
int nb = 0;
|
||||
if (firstThrow == 10) {
|
||||
nb += 1;
|
||||
if (secondThrow == 10) {
|
||||
nb += 1;
|
||||
if (thirdThrow == 10) {
|
||||
nb += 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (thirdThrow == 10) {
|
||||
nb += 1;
|
||||
}
|
||||
}
|
||||
return nb;
|
||||
}
|
||||
|
||||
@override
|
||||
int getPinsKnockedDown() {
|
||||
return (firstThrow ?? 0) + (secondThrow ?? 0) + (thirdThrow ?? 0);
|
||||
}
|
||||
|
||||
@override
|
||||
int getMaxPinsThisShot() {
|
||||
if (firstThrow == null) {
|
||||
return 10;
|
||||
} else if (firstThrow == 10 && secondThrow == null) {
|
||||
return 10;
|
||||
} else if ( secondThrow == null ){
|
||||
return 10 - ( firstThrow ?? 0 );
|
||||
} else if ( ( firstThrow ?? 0 ) + ( secondThrow ?? 0 ) == 10 || secondThrow == 10 ){
|
||||
return 10;
|
||||
} else {
|
||||
return 10 - ( secondThrow ?? 0 );
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
import 'package:bowl_in/model/IAuthManager.dart';
|
||||
import 'package:bowl_in/model/LocalManager/LocalData.dart';
|
||||
|
||||
class AuthManager extends IAuthManager {
|
||||
final LocalData parent;
|
||||
|
||||
// Constructor
|
||||
AuthManager(this.parent);
|
||||
|
||||
@override
|
||||
bool verifiedUser(String mail, String password) {
|
||||
// TODO: implement verifiedUser
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
import 'package:bowl_in/model/GameDetail.dart';
|
||||
import 'package:bowl_in/model/IGameManager.dart';
|
||||
import 'package:bowl_in/model/LocalManager/LocalData.dart';
|
||||
import 'package:bowl_in/model/Player.dart';
|
||||
|
||||
import '../User.dart';
|
||||
|
||||
class GameManager extends IGameManager {
|
||||
final LocalData parent;
|
||||
|
||||
// Constructor
|
||||
GameManager(this.parent){
|
||||
_initGame();
|
||||
}
|
||||
|
||||
// Methods
|
||||
_initGame() async {
|
||||
parent.gameDetails = await parent.database.readGameDetail();
|
||||
}
|
||||
|
||||
GameDetail getGameById(int id) {
|
||||
for (var element in parent.gameDetails) {
|
||||
if (element.id == id) {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
throw Exception("Game not found.");
|
||||
}
|
||||
|
||||
saveGame(GameDetail gameDetail) async {
|
||||
await parent.database.createGameDetail(gameDetail);
|
||||
}
|
||||
|
||||
@override
|
||||
List<GameDetail> getGamesByPlayerId(int id) {
|
||||
return parent.gameDetails.where((element) =>
|
||||
element.players.any((player) => player is User && player.id == id)
|
||||
).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
List<GameDetail> getGamesByPlayer(Player user) {
|
||||
return parent.gameDetails
|
||||
.where((element) =>
|
||||
element.players.any((player) => player is User && user is User && player.id == user.id))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
List<GameDetail> getGamesByPlayers(List<Player> users) {
|
||||
List<GameDetail> games = [];
|
||||
for (var element in parent.gameDetails) {
|
||||
if (element.players.toSet().containsAll(users.toSet())) {
|
||||
games.add(element);
|
||||
}
|
||||
}
|
||||
return games;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Player> getPlayersByIdGame(int id) {
|
||||
final gameDetails = parent.gameDetails.firstWhere(
|
||||
(element) => element.id == id,
|
||||
orElse: () => throw Exception("Game not found.")
|
||||
);
|
||||
return gameDetails.players;
|
||||
}
|
||||
|
||||
@override
|
||||
Map<Player, int> getRankByIdGame(int id) {
|
||||
for (var game in parent.gameDetails) {
|
||||
if (game.id == id) {
|
||||
return game.getRank();
|
||||
}
|
||||
}
|
||||
throw Exception("Game not found.");
|
||||
}
|
||||
|
||||
@override
|
||||
addGame(GameDetail gd) {
|
||||
parent.gameDetails.add(gd);
|
||||
}
|
||||
|
||||
@override
|
||||
int getNextId() {
|
||||
return parent.gameDetails.length;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
import 'package:bowl_in/database/sqlflite/BowlInDatabase.dart';
|
||||
import 'package:bowl_in/model/IManager.dart';
|
||||
import '../GameDetail.dart';
|
||||
import 'GameManager.dart';
|
||||
import 'UserManager.dart';
|
||||
|
||||
class LocalData extends IManager {
|
||||
final BowlInDatabase database = BowlInDatabase();
|
||||
|
||||
LocalData() {
|
||||
userMgr = UserManager(this);
|
||||
gameMgr = GameManager(this);
|
||||
}
|
||||
|
||||
List<GameDetail> _gameDetails = [];
|
||||
|
||||
List<GameDetail> get gameDetails => _gameDetails;
|
||||
|
||||
set gameDetails(List<GameDetail> gameDetails) {
|
||||
_gameDetails = gameDetails;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
import 'package:bowl_in/model/IUserManager.dart';
|
||||
import 'package:bowl_in/model/LocalManager/LocalData.dart';
|
||||
import 'package:bowl_in/model/Player.dart';
|
||||
import 'package:bowl_in/model/User.dart';
|
||||
import 'AuthManager.dart';
|
||||
|
||||
class UserManager extends IUserManager {
|
||||
final LocalData parent;
|
||||
|
||||
// Constructor
|
||||
UserManager(this.parent) : super(AuthManager(parent)) {
|
||||
_initUser();
|
||||
}
|
||||
|
||||
_initUser() async {
|
||||
//await parent.database.deleteGameDetail();
|
||||
//await parent.database.deleteUser(0);
|
||||
//User crUser = User(0, "Lucas", "./assets/images/image_user_red.png", "", [], []);
|
||||
//await saveUser(crUser);
|
||||
var user = await parent.database.readUser(0);
|
||||
if (user == null) {
|
||||
User user2 =
|
||||
User(0, "Unknown", "./assets/images/image_user_pink.png", "", [], []);
|
||||
parent.userCurrent = user2;
|
||||
} else {
|
||||
parent.userCurrent = user;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
saveUser(User user) async {
|
||||
var result = await parent.database.readUser(0);
|
||||
if (result == null) {
|
||||
await parent.database.createUser(user);
|
||||
} else {
|
||||
await parent.database.updateUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
List<User> getRankingWithFriends() {
|
||||
List<User> sortedPlayers = List.from(parent.userCurrent.friends);
|
||||
sortedPlayers.sort((a, b) => b.stat.highscore.compareTo(a.stat.highscore));
|
||||
return sortedPlayers;
|
||||
}
|
||||
|
||||
@override
|
||||
Player getUserById(int id) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
List<Player> getUsersByName(String name) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
@ -1,36 +1,69 @@
|
||||
import 'Player.dart';
|
||||
import 'package:bowl_in/model/AbstractRound.dart';
|
||||
|
||||
class Round {
|
||||
int _firstThrow;
|
||||
int _secondThrow;
|
||||
int _points;
|
||||
Player _player;
|
||||
class Round extends AbstractRound {
|
||||
Round(super.firstThrow, super.secondThrow, super.points, super.player,
|
||||
super.number);
|
||||
|
||||
// Constructor
|
||||
Round(this._firstThrow, this._secondThrow, this._points, this._player);
|
||||
|
||||
// Getters and setters
|
||||
int get firstThrow => _firstThrow;
|
||||
|
||||
set firstThrow(int value) {
|
||||
_firstThrow = value;
|
||||
@override
|
||||
bool computeNext(int val) {
|
||||
if (firstThrow == null) {
|
||||
firstThrow = val;
|
||||
if (previousRound?.isSpare() ?? false) {
|
||||
previousRound?.update(val);
|
||||
unsubscribePreviousRound();
|
||||
}
|
||||
if (val == 10) {
|
||||
computePoints();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} else if (firstThrow != 10 && secondThrow == null) {
|
||||
secondThrow = val;
|
||||
}
|
||||
computePoints();
|
||||
return true;
|
||||
}
|
||||
|
||||
int get secondThrow => _secondThrow;
|
||||
@override
|
||||
void computePoints() {
|
||||
points = (firstThrow ?? 0) + (secondThrow ?? 0);
|
||||
if (previousRound?.isStrike() ?? false) {
|
||||
previousRound?.update(points ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
set secondThrow(int value) {
|
||||
_secondThrow = value;
|
||||
@override
|
||||
bool shotIsStrike() {
|
||||
return firstThrow == null;
|
||||
}
|
||||
|
||||
int get points => _points;
|
||||
@override
|
||||
int getNbSpares() {
|
||||
if (isSpare()) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
set points(int value) {
|
||||
_points = value;
|
||||
@override
|
||||
int getNbStrike() {
|
||||
if (isStrike()) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Player get player => _player;
|
||||
@override
|
||||
int getPinsKnockedDown() {
|
||||
return (firstThrow ?? 0) + (secondThrow ?? 0);
|
||||
}
|
||||
|
||||
set player(Player value) {
|
||||
_player = value;
|
||||
@override
|
||||
int getMaxPinsThisShot() {
|
||||
if (firstThrow == null) {
|
||||
return 10;
|
||||
} else {
|
||||
return 10 - (firstThrow ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|