MemberEntity.ts 5.34 KB
Newer Older
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
1 2
import { ObjectId } from 'bson';
import CasDAO from '../../db/CasDAO';
3
import DbDAO from '../../db/DbDAO';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
4
import QuizDAO from '../../db/quiz/QuizDAO';
5
import { DbCollection } from '../../enums/DbOperation';
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { IMemberEntity } from '../../interfaces/entities/Member/IMemberEntity';
import { IMemberSerialized } from '../../interfaces/entities/Member/IMemberSerialized';
import { IQuizEntity } from '../../interfaces/quizzes/IQuizEntity';
import { IQuizResponse } from '../../interfaces/quizzes/IQuizResponse';
import { ICasData } from '../../interfaces/users/ICasData';
import { AbstractEntity } from '../AbstractEntity';

export class MemberEntity extends AbstractEntity implements IMemberEntity {
  private _currentQuizName: string;

  get currentQuizName(): string {
    return this._currentQuizName;
  }

  set currentQuizName(value: string) {
    this._currentQuizName = value;

    if (value) {
24
      this._responses = this.generateResponseForQuiz(QuizDAO.getQuizByName(value).questionList.length);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }
  }

  private _token: string;

  get token(): string {
    return this._token;
  }

  set token(value: string) {
    this._token = value;
  }

  private _name: string;

  get name(): string {
    return this._name;
  }

  set name(value: string) {
    this._name = value;
  }

  private _groupName: string;

  get groupName(): string {
    return this._groupName;
  }

  set groupName(value: string) {
    this._groupName = value;
  }

  private _colorCode: string;

  get colorCode(): string {
    return this._colorCode;
  }

  set colorCode(value: string) {
    this._colorCode = value;
  }

  private _responses: Array<IQuizResponse>;

  get responses(): Array<IQuizResponse> {
    return this._responses;
  }

  set responses(value: Array<IQuizResponse>) {
    this._responses = value;
  }

  private _casProfile: ICasData;

  get casProfile(): ICasData {
    return this._casProfile;
  }

  set casProfile(value: ICasData) {
    this._casProfile = value;
  }

  private _ticket: string;

  get ticket(): string {
    return this._ticket;
  }

  set ticket(value: string) {
    this._ticket = value;
  }

  constructor(data: IMemberSerialized) {
    super();

    this._id = new ObjectId(data.id || data._id);
    this._name = data.name;
    this._groupName = data.groupName;
    this._colorCode = data.colorCode || this.generateRandomColorCode();
    this._responses = data.responses || [];
    this._token = data.token;
    this._ticket = data.ticket;
    this._casProfile = CasDAO.match(data.ticket);
    this._currentQuizName = data.currentQuizName;

    if (!data.responses) {
112
      this._responses = this.generateResponseForQuiz(QuizDAO.getQuizByName(data.currentQuizName).questionList.length);
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    CasDAO.remove(data.ticket);
  }

  public serialize(): IMemberSerialized {
    return {
      id: this.id.toHexString(),
      name: this.name,
      groupName: this.groupName,
      colorCode: this.colorCode,
      responses: this.responses,
      token: this.token,
      currentQuizName: this.currentQuizName,
    };
  }

  public addResponseValue(data: Array<number> | string | number): void {
131 132
    const quiz = this.getCurrentQuiz();
    const responseTime = new Date().getTime() - quiz.currentStartTimestamp;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
133

134 135
    this.responses[quiz.currentQuestionIndex].value = data;
    this.responses[quiz.currentQuestionIndex].responseTime = responseTime;
136 137 138
    DbDAO.updateOne(DbCollection.Members, {
      _id: this.id,
    }, { responses: this.responses });
139
    quiz.updatedMemberResponse({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
140
      nickname: this.name,
141
      questionIndex: quiz.currentQuestionIndex,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
142 143 144 145 146 147 148 149
      update: {
        value: data,
        responseTime,
      },
    });
  }

  public setConfidenceValue(confidence: number): void {
150 151
    const quiz = this.getCurrentQuiz();
    this.responses[quiz.currentQuestionIndex].confidence = confidence;
152 153 154
    DbDAO.updateOne(DbCollection.Members, {
      _id: this.id,
    }, { responses: this.responses });
155
    quiz.updatedMemberResponse({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
156
      nickname: this.name,
157
      questionIndex: quiz.currentQuestionIndex,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
158 159 160 161 162
      update: { confidence: confidence },
    });
  }

  public setReadingConfirmation(): void {
163 164
    const quiz = this.getCurrentQuiz();
    this.responses[quiz.currentQuestionIndex].readingConfirmation = true;
165 166 167
    DbDAO.updateOne(DbCollection.Members, {
      _id: this.id,
    }, { responses: this.responses });
168
    quiz.updatedMemberResponse({
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
169
      nickname: this.name,
170
      questionIndex: quiz.currentQuestionIndex,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
171 172 173 174 175
      update: { readingConfirmation: true },
    });
  }

  public generateResponseForQuiz(questionAmount: number): Array<IQuizResponse> {
176
    const responses: Array<IQuizResponse> = [];
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
177
    for (let i = 0; i < questionAmount; i++) {
178
      responses[i] = {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
179 180
        value: [],
        responseTime: -1,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
181
        confidence: -1,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
182 183 184
        readingConfirmation: false,
      };
    }
185
    return responses;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  }

  private getCurrentQuiz(): IQuizEntity {
    return QuizDAO.getQuizByName(this.currentQuizName);
  }

  private hashCode(str: string): number { // java String#hashCode
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
      hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }
    return hash;
  }

  private intToRGB(i: number): string {
    const c: string = (i & 0x00FFFFFF)
    .toString(16)
    .toUpperCase();

    return '00000'.substring(0, 6 - c.length) + c;
  }

  private generateRandomColorCode(): string {
    return this.intToRGB(this.hashCode(this.name));
  }
}