2026-05-19 13:26:31 +00:00

256 lines
3.7 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const matches = sequelize.define(
'matches',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
room_code: {
type: DataTypes.TEXT,
},
status: {
type: DataTypes.ENUM,
values: [
"waiting",
"in_progress",
"completed",
"cancelled"
],
},
started_at: {
type: DataTypes.DATE,
},
ended_at: {
type: DataTypes.DATE,
},
player_one_round_wins: {
type: DataTypes.INTEGER,
},
player_two_round_wins: {
type: DataTypes.INTEGER,
},
best_of_rounds: {
type: DataTypes.INTEGER,
},
rematch_requested: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
server_region: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
matches.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.matches.hasMany(db.match_rounds, {
as: 'match_rounds_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.match_events, {
as: 'match_events_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.chat_messages, {
as: 'chat_messages_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.spectator_sessions, {
as: 'spectator_sessions_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.points_transactions, {
as: 'points_transactions_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.shop_purchases, {
as: 'shop_purchases_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
db.matches.hasMany(db.replays, {
as: 'replays_match',
foreignKey: {
name: 'matchId',
},
constraints: false,
});
//end loop
db.matches.belongsTo(db.matchmaking_queues, {
as: 'queue',
foreignKey: {
name: 'queueId',
},
constraints: false,
});
db.matches.belongsTo(db.users, {
as: 'player_one',
foreignKey: {
name: 'player_oneId',
},
constraints: false,
});
db.matches.belongsTo(db.users, {
as: 'player_two',
foreignKey: {
name: 'player_twoId',
},
constraints: false,
});
db.matches.belongsTo(db.users, {
as: 'winner',
foreignKey: {
name: 'winnerId',
},
constraints: false,
});
db.matches.belongsTo(db.users, {
as: 'rematch_requested_by',
foreignKey: {
name: 'rematch_requested_byId',
},
constraints: false,
});
db.matches.belongsTo(db.users, {
as: 'createdBy',
});
db.matches.belongsTo(db.users, {
as: 'updatedBy',
});
};
return matches;
};