Exemple #1
0
def _create_new_user(rm, device_id):
    """
    Creates a new user and returns its instance
    :param rm:
    :param device_id:
    :return: user object (django model),
    """

    while True:
        try:

            with transaction.atomic():

                temp_room = Room.objects.select_for_update().filter(
                    id=rm.id).first()

                tablet_id = ConnectedTablet.objects.filter(
                    device_id=device_id).first().tablet_id

                player_id = temp_room.counter

                pseudo = parameters.pseudos[player_id]

                u = User(player_id=player_id,
                         pseudo=pseudo,
                         room_id=rm.id,
                         training_done=False,
                         score=0,
                         training_score=0,
                         state=game.room.state.states.WELCOME,
                         tablet_id=tablet_id)

                u.save()

                u.production_good = _get_user_production_good(rm, u)

                # prod i - 1 rule
                u.consumption_good = (u.production_good + 1) % rm.n_type

                u.save(update_fields=["production_good", "consumption_good"])

                temp_room.counter += 1
                temp_room.save(update_fields=['counter'])

                return u

        except (django.db.IntegrityError, django.db.OperationalError,
                django.db.utils.OperationalError, psycopg2.IntegrityError,
                psycopg2.OperationalError) as e:
            print("*" * 50)
            print("INTEGRITY ERROR" + "!" * 10)
            print(str(e))
            print("*" * 50)
            threading.Event().wait(1 + np.random.random() * 4)
            continue