---
# System prepended metadata

title: Levy's Terraria Server!

---

# Levy's Terraria Server!


Levy wanted to set up to play Terraria with a lot of friends, but his friends has something else in mind! His friends keeps on blowing up his spawn. Levy knew this, so he wants to protect his server! He only has PintOS Lists, however, so he enlists your help to create a structure to keep track of who hops on and off his server and at what time!


## Code
```
typedef struct entry_list {
    int ID;
    int time;
    int type;
    struct list_elem elem;
} entry_t;

struct list log;
pthread_mutex_t lock;

/* this function sets up the server */
void setup() {
    list_init(&log);
    pthread_mutex_init(&lock);
}

/* This function is ran when a user joins the server; type 0 for entry; type 1 for exit */
void join_exit(int ID, int time, int type) {
    pthread_mutex_lock(&lock);
    entry_t *e = malloc(sizeof(entry_t));
    e->ID = ID;
    e->time = time;
    e->type = type;
    list_push_back(&log, e);
    pthread_mutex_unlock(&lock);
}


/* This function is ran by Levy to find out how many people keeps on blowing up his spawn.*/

int accountability(int tragedy){
    struct list_elem *iter;
    int count = 0;
    pthread_mutex_lock(&lock);
    for (iter=list_begin(&log); iter != list_end(&log); iter = list_next(iter)) {
        entry_t *entry = list_entry(iter, entry_t, elem);
        if (entry->time > tragedy) {
            pthread_mutex_lock(&lock);
            return count;
        }
        if (entry->type==0) { #entry
            count += 1;
        }
        else { #exit
            count -= 1;
        }
    }
    pthread_mutex_lock(&lock);
    return count;
}
```

