slashinfty / tournament-organizer

JavaScript library for running tournaments
https://slashinfty.github.io/tournament-organizer/
MIT License
51 stars 18 forks source link

Handling tournament with database #6

Closed pedrohba1 closed 2 years ago

pedrohba1 commented 3 years ago

I have a use case where I want to handle a tournament and save it's state on a database, be it mongo or sql. Is there a possible approach for it using this module or would it require a lot of additional features?

I'm thinking about instantiating an object of a tournament and saving it as a blob on the database, but there could also be something better.

slashinfty commented 3 years ago

I am not super familiar with databases. Tournaments are classes, which are just objects, so if you can work with objects, it might be okay.

pedrohba1 commented 3 years ago

It can work with objects, however I would like to make it work in a back-end. Thing is, the back-end might go down and the class instance would be gone with it. I would like to have a way to save it somehow. Maybe in a Mongo database, or either the entire object on the back-end, just to have the ability to keep it in a storage would help.

I would gladly add this feature if you are ok. Do you have any development guide on this project? Giving a look, the code pretty much documents itself. But anything else would be useful.

pedrohba1 commented 3 years ago

Hello again, I found a way to store it on a database using redis. It is not a database "per se", it actually stores in memory, but it can persist so it works for me.

But I found another problem. the torunament-organizer does not have @types. I started typing the functions in my project in order to have auto-completion and to be able to retrieve and cast the object from the database. Take a look:

image

slashinfty commented 3 years ago

I don't have a development guide, but you're more than welcome to contribute (I welcome contribution!).

I don't know if I'd call it a problem, but I think it's definitely an enhancement to have types.

I am just a hobbyist, not a professional. Thus why I'm happy if anyone is interested in contributing. I know there are a couple others who are interested in rewriting some/most of it.

pedrohba1 commented 2 years ago

Hey, I've started making the types for the classes. Sorry, I don't have much time, but thanks a lot for developing this code! Maybe I can fully contribute if I have more time in 2 weeks.

declare module 'tournament-organizer' {
  export class Match {
    constructor(id, round, matchNumber, players = null);
    id: string;
    round: number;
    matchNumber: number;
    resultForPlayers(wv, lv, dv);

    playerOne: Player;
    playerTwo: Player;
  }

  export class Player {
    constructor(alias, id, seed);
    alias: string;
    id: string;
    seed: number;
    matchPoints: number;

    matches: number;
    gamePoints: number;
    games: number;
    initialByes: number;
    byes: number;

    //TODO  type the object contents:
    /**
     * Array of results. Objects include match ID, opponent ID, and result ('w', 'l', or 'd').
     * @type {Object[]}
     */
    results: any[];
    colorPref: number;
    colors: string[];
    active: boolean;

    tiebreakers: {
      matchWinPctM: number;
      matchWinPctP: number;
      oppMatchWinPctM: number;
      oppMatchWinPctP: number;
      gameWinPct: number;
      oppGameWinPct: number;
      oppOppMatchWinPct: number;
      solkoff: number;
      cutOne: number;
      median: number;
      neustadtl: number;
      cumulative: number;
      oppCumulative: number;
    };
  }
  export class Tournament {
    static Swiss: any;
    constructor(id, options): Tournament;

    matches: Match[];
    players: Player[];
    addPlayer(alias, id = null, seed = null);
    startEvent();
    activeMatches(): Match[];
    standings(): Player[];
    result(match: Match, playerOneWins: number, playerTwoWins: number): Match[];
  }

  export class EventManager {
    constructor(): EventManager;
    createTournament(id: string = null, options = {}): Tournament;
    reloadTournament(tournament: Tournament): Tournament;
  }
}

So, inside my project when I use the tournament-organizer I have to make an tournament-organizer.d.ts and add the code above. That enables me to have types. It's not complete, but this should get you going if you are going to write it. There is a guide on how to add types: https://www.freecodecamp.org/news/how-to-add-typescript-to-a-javascript-project/

slashinfty commented 2 years ago

Sorry I haven't responded. If you want to officially contribute, please feel free to make a pull request. No worries if you aren't able to.

pedrohba1 commented 2 years ago

All right. I will, in some time. I have no time right now to add the types the correct way. The original database issue was solved. I will mark this as solved.