[refactor] move models to core package

This commit is contained in:
Halit Aksoy 2024-06-17 15:37:17 +03:00
parent 6c2b15070d
commit 597bb708f1
36 changed files with 62 additions and 145 deletions

View File

@ -1,5 +1,6 @@
import fs from "fs";
import { Game } from "../models/Game";
import { Game } from "@mancala/core";
import { BackendGame } from "../models/Game";
const crashFolder = "./crashes";
export class GameCrashManager {
constructor() {}
@ -22,7 +23,7 @@ export class GameCrashManager {
return `${year}-${month}-${date}-${hours}:${minutes}:${seconds}:${miliSeconds}`;
}
public static logGameCrash(error: Error, game: Game): string {
public static logGameCrash(error: Error, game: BackendGame): string {
this.createCrashFolderIfNotExist();
const crash = {
message: error.message,

View File

@ -15,10 +15,11 @@ import {
channel_on_user_connection_change,
channel_unlisten_game_events
} from "@mancala/core";
import { GameMove } from "../models/GameMove";
import { GameMove } from "@mancala/core";
import { GameCrashManager } from "./GameCrashManager";
import { MatchMaker } from "../matchmaker/MatchMaker";
import { Game, GameUsersConnectionInfo } from "../models/Game";
import { GameUsersConnectionInfo } from "@mancala/core";
import { BackendGame } from "../models/Game";
export class GameManager {
gameStore: GameStore;
@ -82,7 +83,7 @@ export class GameManager {
}
}
private onGameError(game: Game, error: any) {
private onGameError(game: BackendGame, error: any) {
console.error(error);
const crashFileName = GameCrashManager.logGameCrash(error, game);
console.info(`Game crash saved to file : ${crashFileName}`);
@ -118,18 +119,18 @@ export class GameManager {
}
}
private checkUserIdisPlayer(game: Game, userId: string): boolean {
private checkUserIdisPlayer(game: BackendGame, userId: string): boolean {
return game.mancalaGame.player1Id === userId || game.mancalaGame.player2Id === userId;
}
private sendMessageToPlayersAndSpectators(game: Game, channel: string, message: Object) {
private sendMessageToPlayersAndSpectators(game: BackendGame, channel: string, message: Object) {
const sendMessage = (userId: string) => this.rtmt.sendMessage(userId, channel, message);
sendMessage(game.mancalaGame.player1Id);
sendMessage(game.mancalaGame.player2Id);
game.spectatorIds.forEach((spectatorId) => sendMessage(spectatorId));
}
private onGameEnd(game: Game) {
private onGameEnd(game: BackendGame) {
this.deleteGame(game);
game.spectatorIds = [];
}
@ -143,12 +144,12 @@ export class GameManager {
public fireOnPlayerConnected(playerId: string) { }
public createMancalaGame(userKey1: string, userKey2: string): Game {
public createMancalaGame(userKey1: string, userKey2: string): BackendGame {
const mancalaGame = new CommonMancalaGame(generateKey(), userKey1, userKey2);
const random = Math.random();
mancalaGame.turnPlayerId = random > 0.5 ? userKey1 : userKey2;
const game: Game = {
const game: BackendGame = {
id: mancalaGame.id,
mancalaGame,
gameUsersConnectionInfo: this.getGameUsersConnectionInfoFromUsers(mancalaGame.player1Id, mancalaGame.player2Id),
@ -161,7 +162,7 @@ export class GameManager {
return game;
}
public getGameUsersConnectionInfo(game: Game): GameUsersConnectionInfo {
public getGameUsersConnectionInfo(game: BackendGame): GameUsersConnectionInfo {
return this.getGameUsersConnectionInfoFromUsers(game.mancalaGame.player1Id, game.mancalaGame.player2Id);
}
@ -174,18 +175,18 @@ export class GameManager {
};
}
public startGame(game: Game) {
public startGame(game: BackendGame) {
this.sendMessageToPlayersAndSpectators(game, channel_on_game_start, game);
this.sendUserConnectionInfo(game);
this.sendUserConnectionInfo(game);
}
public sendUserConnectionInfo(game: Game) {
public sendUserConnectionInfo(game: BackendGame) {
const gameUsersConnectionInfo = this.getGameUsersConnectionInfo(game);
this.sendMessageToPlayersAndSpectators(game, channel_on_user_connection_change, gameUsersConnectionInfo);
}
public deleteGame(game: Game) {
public deleteGame(game: BackendGame) {
if (game) {
this.gameStore.removeGameByPlayer(game.mancalaGame.player1Id);
this.gameStore.removeGameByPlayer(game.mancalaGame.player2Id);

View File

@ -1,16 +1,17 @@
import { Game } from "../../models/Game";
import { Game } from "@mancala/core";
import { BackendGame } from "../../models/Game";
export interface GameStore {
get(id: string): Game | undefined;
set(id: string, game: Game): void;
get(id: string): BackendGame | undefined;
set(id: string, game: BackendGame): void;
remove(id: string): void;
setGameIdByUser(userId: string, gameId: string): void;
getGameIdByUser(userId: string): string | undefined;
removeGameIdByPlayer(userId: string): void;
setGameByUser(userId: string, game: Game): void;
getGameByUser(userId: string): Game | undefined;
setGameByUser(userId: string, game: BackendGame): void;
getGameByUser(userId: string): BackendGame | undefined;
removeGameByPlayer(userId: string): void;
getSpeactatingGameIdsByUser(userId: string): string[]

View File

@ -1,14 +1,14 @@
import { Game } from "../../models/Game";
import { GameStore } from "./GameStore";
import { BackendGame } from "../../models/Game";
export class GameStoreImpl implements GameStore {
gameStore: Map<string, Game> = new Map<string, Game>()
gameStore: Map<string, BackendGame> = new Map<string, BackendGame>()
userGameMap: Map<string, string> = new Map<string, string>()
get(id: string): Game | undefined {
get(id: string): BackendGame | undefined {
return this.gameStore.get(id);
}
set(id: string, game: Game): void {
set(id: string, game: BackendGame): void {
this.gameStore.set(id, game);
}
remove(id: string): void {
@ -25,10 +25,10 @@ export class GameStoreImpl implements GameStore {
this.userGameMap.delete(userId);
}
setGameByUser(userId: string, game: Game): void {
setGameByUser(userId: string, game: BackendGame): void {
this.setGameIdByUser(userId, game.id);
}
getGameByUser(userId: string): Game | undefined {
getGameByUser(userId: string): BackendGame | undefined {
const gameId = this.getGameIdByUser(userId);
return gameId && this.gameStore.get(gameId) || undefined;
}
@ -39,7 +39,7 @@ export class GameStoreImpl implements GameStore {
getSpeactatingGameIdsByUser(userId: string): string[] {
const speactatingGameIds = [];
for (const gameId in this.gameStore.keys()) {
const game = this.gameStore.get(gameId) as Game;
const game = this.gameStore.get(gameId) as BackendGame;
const isSpectator = game.spectatorIds.find((value) => value === userId);
isSpectator && speactatingGameIds.push(game.id);
}

View File

@ -1,14 +1,5 @@
import { MancalaGame } from "mancala.js";
import { UserConnectionInfo } from "./UserConnectionInfo";
import { Game } from "@mancala/core";
export interface Game {
id: string;
mancalaGame: MancalaGame;
gameUsersConnectionInfo: GameUsersConnectionInfo;
export interface BackendGame extends Game {
spectatorIds: string[];
}
export interface GameUsersConnectionInfo {
user1ConnectionInfo: UserConnectionInfo;
user2ConnectionInfo: UserConnectionInfo;
}

View File

@ -14,6 +14,9 @@
},
"author": "Halit Aksoy",
"license": "MIT",
"dependencies": {
"mancala.js": "^0.0.2-beta.3"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@typescript-eslint/eslint-plugin": "^5.21.0",

View File

@ -1 +1,2 @@
export * from './rtmt/channel_names'
export * from './models/index'

5
core/src/models/index.ts Normal file
View File

@ -0,0 +1,5 @@
export * from './Game'
export * from './GameMove'
export * from './LoadingState'
export * from './User'
export * from './UserConnectionInfo'

View File

@ -2329,6 +2329,11 @@ makeerror@1.0.12:
dependencies:
tmpl "1.0.5"
mancala.js@^0.0.2-beta.3:
version "0.0.2-beta.3"
resolved "https://registry.yarnpkg.com/mancala.js/-/mancala.js-0.0.2-beta.3.tgz#78edfa220e1a7172351a07f255eb81180845226a"
integrity sha512-LPmQ/VT4/JWFdp/YSB7k63zK7GyflApyh4M26t23a9uXFRSpBcWSePtNFpHU/xY2+1gVjlbOwQjup2QW3Tue7w==
merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz"

View File

@ -12,7 +12,7 @@ import { v4 } from "uuid";
import { Context } from "../context/context";
import BoardViewModelFactory from "../factory/BoardViewModelFactory";
import { PitViewModelFactory } from "../factory/PitViewModelFactory";
import { Game } from "../models/Game";
import { Game } from "@mancala/core";
import { Theme } from "../theme/Theme";
import { getColorByBrightness } from "../util/ColorUtil";
import BoardViewModel from "../viewmodel/BoardViewModel";

View File

@ -1,8 +1,7 @@
import * as React from "react";
import { FunctionComponent } from "react";
import { Context } from "../context/context";
import { Game } from "../models/Game";
import { User } from "../models/User";
import { Game, User } from "@mancala/core";
import { getColorByBrightness } from "../util/ColorUtil";
import CircularPanel from "./CircularPanel";

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { FunctionComponent } from 'react';
import { Context } from '../context/context';
import { LoadingState } from '../models/LoadingState';
import { LoadingState } from "@mancala/core";
import { getColorByBrightness } from '../util/ColorUtil';
import CircularPanel from './CircularPanel';

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { FunctionComponent } from 'react';
import { Context } from '../context/context';
import { User } from '../models/User';
import { User } from "@mancala/core";
import { getColorByBrightness } from '../util/ColorUtil';
import Space from './Space';

View File

@ -5,7 +5,7 @@ import BoardViewModel from "../../viewmodel/BoardViewModel";
import PitViewModel from "../../viewmodel/PitViewModel";
import PitView from "./PitView";
import StoreView from "./StoreView";
import { Game } from "../../models/Game";
import { Game } from "@mancala/core";
import { Pit } from "mancala.js";
const BoardView: FunctionComponent<{

View File

@ -1,3 +0,0 @@
export interface GameMove {
index: number;
}

View File

@ -1,6 +0,0 @@
export interface User {
id: string;
name: string;
isOnline: boolean;
isAnonymous: boolean;
}

View File

@ -1,4 +0,0 @@
export interface UserConnectionInfo {
userId: string;
isOnline: boolean;
}

View File

@ -20,13 +20,11 @@ import UserStatus from '../components/UserStatus';
import { channel_on_game_update, channel_on_game_crashed, channel_on_game_user_leave, channel_on_user_connection_change, channel_leave_game, channel_game_move, channel_listen_game_events, channel_unlisten_game_events } from '@mancala/core';
import { Context } from '../context/context';
import useWindowDimensions from '../hooks/useWindowDimensions';
import { GameMove } from '../models/GameMove';
import { LoadingState } from '../models/LoadingState';
import { GameMove, LoadingState, Game, GameUsersConnectionInfo } from "@mancala/core";
import { Theme } from '../theme/Theme';
import { getColorByBrightness } from '../util/ColorUtil';
import BoardViewModel from '../viewmodel/BoardViewModel';
import Center from '../components/Center';
import { Game, GameUsersConnectionInfo } from '../models/Game';
import notyf from '../util/Notyf';
import swal from 'sweetalert';
import Util from '../util/Util';

View File

@ -1,5 +1,5 @@
import { CommonMancalaGame, MancalaGame } from "mancala.js";
import { Game } from "../models/Game";
import { MancalaGame } from "mancala.js";
import { Game } from "@mancala/core";
import { HttpService } from "../service/HttpService";
export interface GameStore {

View File

@ -12,7 +12,7 @@ import { v4 } from "uuid";
import { Context } from "../context/context";
import BoardViewModelFactory from "../factory/BoardViewModelFactory";
import { PitViewModelFactory } from "../factory/PitViewModelFactory";
import { Game } from "../models/Game";
import { Game } from "@mancala/core";
import { Theme } from "../theme/Theme";
import { getColorByBrightness } from "../util/ColorUtil";
import BoardViewModel from "../viewmodel/BoardViewModel";

View File

@ -1,8 +1,7 @@
import * as React from "react";
import { FunctionComponent } from "react";
import { Context } from "../context/context";
import { Game } from "../models/Game";
import { User } from "../models/User";
import { Game, User} from "@mancala/core";
import { getColorByBrightness } from "../util/ColorUtil";
import CircularPanel from "./CircularPanel";
import { useTranslation } from "react-i18next";

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { FunctionComponent } from 'react';
import { Context } from '../context/context';
import { LoadingState } from '../models/LoadingState';
import { LoadingState } from "@mancala/core";
import { getColorByBrightness } from '../util/ColorUtil';
import CircularPanel from './CircularPanel';
import { useTranslation } from 'react-i18next';

View File

@ -1,7 +1,7 @@
import * as React from 'react';
import { FunctionComponent } from 'react';
import { Context } from '../context/context';
import { User } from '../models/User';
import { User } from "@mancala/core";
import { getColorByBrightness } from '../util/ColorUtil';
import Space from './Space';
import { Theme } from '../theme/Theme';

View File

@ -5,7 +5,7 @@ import BoardViewModel from "../../viewmodel/BoardViewModel";
import PitViewModel from "../../viewmodel/PitViewModel";
import PitView from "./PitView";
import StoreView from "./StoreView";
import { Game } from "../../models/Game";
import { Game } from "@mancala/core";
import { Pit } from "mancala.js";
import { View, Dimensions } from "react-native";
import StoneView from "./StoneView";

View File

@ -1,13 +0,0 @@
import { MancalaGame } from "mancala.js";
import { UserConnectionInfo } from "./UserConnectionInfo";
export interface Game {
id: string;
mancalaGame: MancalaGame;
gameUsersConnectionInfo: GameUsersConnectionInfo;
}
export interface GameUsersConnectionInfo {
user1ConnectionInfo: UserConnectionInfo;
user2ConnectionInfo: UserConnectionInfo;
}

View File

@ -1,3 +0,0 @@
export interface GameMove {
index: number;
}

View File

@ -1,46 +0,0 @@
export type LoadingStateType = "unset" | "loading" | "loaded" | "error";
export class LoadingState<T> {
state: LoadingStateType;
errorMessage?: string;
value?: T;
constructor(props: { state?: LoadingStateType, errorMessage?: string, value?: T }) {
this.state = props.state ? props.state : "unset";
this.errorMessage = props.errorMessage;
this.value = props.value;
}
public static Unset<T>() {
return new LoadingState<T>({ state: "unset" });
}
public static Loading<T>() {
return new LoadingState<T>({ state: "loading" });
}
public static Error<T>(props: { errorMessage: string }) {
const { errorMessage } = props;
return new LoadingState<T>({ state: "error", errorMessage });
}
public static Loaded<T>(props: { value?: T }) {
const { value } = props;
return new LoadingState<T>({ state: "loaded", value });
}
public isUnset() : boolean {
return this.state === "unset";
}
public isLoading() : boolean {
return this.state === "loading";
}
public isError() : boolean {
return this.state === "error";
}
public isLoaded() : boolean {
return this.state === "loaded";
}
}

View File

@ -1,6 +0,0 @@
export interface User {
id: string;
name: string;
isOnline: boolean;
isAnonymous: boolean;
}

View File

@ -1,4 +0,0 @@
export interface UserConnectionInfo {
userId: string;
isOnline: boolean;
}

View File

@ -3,14 +3,12 @@ import { View, Text, useWindowDimensions, Alert, Pressable } from 'react-native'
import { useTranslation } from 'react-i18next';
import { GameScreenProps } from '../types';
import { useState } from 'react';
import { Game, GameUsersConnectionInfo } from '../models/Game';
import { Game, GameUsersConnectionInfo, GameMove, LoadingState } from "@mancala/core";
import BoardViewModel from '../viewmodel/BoardViewModel';
import { MancalaGame, Pit } from 'mancala.js';
import { v4 } from 'uuid';
import PitAnimator from '../animation/PitAnimator';
import { channel_on_game_update, channel_on_game_crashed, channel_on_game_user_leave, channel_on_user_connection_change, channel_listen_game_events, channel_unlisten_game_events, channel_leave_game, channel_game_move } from '@mancala/core';
import { GameMove } from '../models/GameMove';
import { LoadingState } from '../models/LoadingState';
import { getColorByBrightness } from '../util/ColorUtil';
import Util from '../util/Util';
import Snackbar from 'react-native-snackbar';

View File

@ -1,5 +1,5 @@
import { CommonMancalaGame, MancalaGame } from "mancala.js";
import { Game } from "../models/Game";
import { MancalaGame } from "mancala.js";
import { Game } from "@mancala/core";
import { HttpService } from "../service/HttpService";
export interface GameStore {