pile_js / ByronHsu__Never-Blink.jsonl
Hamhams's picture
commit files to HF hub
c7f4bd0
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"find_by_id","parameters":"(_id)","argument_list":"","return_statement":"return index","docstring":"Find element by id in the player_list.\n Args:\n _id: ID of this player.\n Return:\n index: The index of this player in the player list.","docstring_summary":"Find element by id in the player_list.\n Args:\n _id: ID of this player.\n Return:\n index: The index of this player in the player list.","docstring_tokens":["Find","element","by","id","in","the","player_list",".","Args",":","_id",":","ID","of","this","player",".","Return",":","index",":","The","index","of","this","player","in","the","player","list","."],"function":"def find_by_id(_id):\n \"\"\"\n Find element by id in the player_list.\n Args:\n _id: ID of this player.\n Return:\n index: The index of this player in the player list.\n \"\"\"\n filt = [i for (i, item) in enumerate(player_list) if item['id'] == _id]\n index = None if len(filt) == 0 else filt[0]\n return index","function_tokens":["def","find_by_id","(","_id",")",":","filt","=","[","i","for","(","i",",","item",")","in","enumerate","(","player_list",")","if","item","[","'id'","]","==","_id","]","index","=","None","if","len","(","filt",")","==","0","else","filt","[","0","]","return","index"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L39-L49"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"find_random_waiting","parameters":"(_id)","argument_list":"","return_statement":"return index","docstring":"Randomly choose a 'waiting' player from the list.\n But it can not choose itself(_id).\n Args:\n _id: ID of this player.\n Return:\n index: The index of the chosen player in the player list.","docstring_summary":"Randomly choose a 'waiting' player from the list.\n But it can not choose itself(_id).\n Args:\n _id: ID of this player.\n Return:\n index: The index of the chosen player in the player list.","docstring_tokens":["Randomly","choose","a","waiting","player","from","the","list",".","But","it","can","not","choose","itself","(","_id",")",".","Args",":","_id",":","ID","of","this","player",".","Return",":","index",":","The","index","of","the","chosen","player","in","the","player","list","."],"function":"def find_random_waiting(_id):\n \"\"\"\n Randomly choose a 'waiting' player from the list.\n But it can not choose itself(_id).\n Args:\n _id: ID of this player.\n Return:\n index: The index of the chosen player in the player list.\n \"\"\"\n filt = [i for (i, item) in enumerate(player_list) if (\n item['status'] == 'waiting' and item['id'] != _id)]\n if len(filt):\n index = random.choice(filt)\n else:\n index = None\n return index","function_tokens":["def","find_random_waiting","(","_id",")",":","filt","=","[","i","for","(","i",",","item",")","in","enumerate","(","player_list",")","if","(","item","[","'status'","]","==","'waiting'","and","item","[","'id'","]","!=","_id",")","]","if","len","(","filt",")",":","index","=","random",".","choice","(","filt",")","else",":","index","=","None","return","index"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L52-L67"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"on_connect","parameters":"()","argument_list":"","return_statement":"","docstring":"Called when the client just connected.\n Create a player and push it to the list.\n Player:\n id: Indentifier of the player.\n status: 'idle', 'waiting', 'playing'.\n ear: Eye aspect ratio.\n rival: The rival's id.\n startTime: The start time of the game.\n end: Whether the game has ended. '1' means end, '0' means playing.","docstring_summary":"Called when the client just connected.\n Create a player and push it to the list.\n Player:\n id: Indentifier of the player.\n status: 'idle', 'waiting', 'playing'.\n ear: Eye aspect ratio.\n rival: The rival's id.\n startTime: The start time of the game.\n end: Whether the game has ended. '1' means end, '0' means playing.","docstring_tokens":["Called","when","the","client","just","connected",".","Create","a","player","and","push","it","to","the","list",".","Player",":","id",":","Indentifier","of","the","player",".","status",":","idle","waiting","playing",".","ear",":","Eye","aspect","ratio",".","rival",":","The","rival","s","id",".","startTime",":","The","start","time","of","the","game",".","end",":","Whether","the","game","has","ended",".","1","means","end","0","means","playing","."],"function":"def on_connect():\n \"\"\"\n Called when the client just connected.\n Create a player and push it to the list.\n Player:\n id: Indentifier of the player.\n status: 'idle', 'waiting', 'playing'.\n ear: Eye aspect ratio.\n rival: The rival's id.\n startTime: The start time of the game.\n end: Whether the game has ended. '1' means end, '0' means playing.\n \"\"\"\n client_id = request.args['id']\n index = find_by_id(client_id)\n if not index:\n player_list.append({'id': client_id, 'status': 'idle',\n 'ear': 0, 'rival': None, 'startTime': 0, 'end': 0})\n print(client_id, 'connected !')","function_tokens":["def","on_connect","(",")",":","client_id","=","request",".","args","[","'id'","]","index","=","find_by_id","(","client_id",")","if","not","index",":","player_list",".","append","(","{","'id'",":","client_id",",","'status'",":","'idle'",",","'ear'",":","0",",","'rival'",":","None",",","'startTime'",":","0",",","'end'",":","0","}",")","print","(","client_id",",","'connected !'",")"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L75-L92"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"on_disconnect","parameters":"(message)","argument_list":"","return_statement":"","docstring":"Called when the client just disconnected.\n Delete the player from the list.","docstring_summary":"Called when the client just disconnected.\n Delete the player from the list.","docstring_tokens":["Called","when","the","client","just","disconnected",".","Delete","the","player","from","the","list","."],"function":"def on_disconnect(message):\n \"\"\"\n Called when the client just disconnected.\n Delete the player from the list.\n \"\"\"\n client_id = message['id']\n index = find_by_id(client_id)\n del player_list[index]\n print(client_id, 'disconnected !')","function_tokens":["def","on_disconnect","(","message",")",":","client_id","=","message","[","'id'","]","index","=","find_by_id","(","client_id",")","del","player_list","[","index","]","print","(","client_id",",","'disconnected !'",")"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L96-L104"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"set_player_wait","parameters":"(message)","argument_list":"","return_statement":"","docstring":"Set the player's status to 'waiting'.\n And then Randomly match a player whose status is also 'waiting'.\n Args:\n id: ID of the player.\n Emit:\n get_rival: Tell the player its rival's id.","docstring_summary":"Set the player's status to 'waiting'.\n And then Randomly match a player whose status is also 'waiting'.\n Args:\n id: ID of the player.\n Emit:\n get_rival: Tell the player its rival's id.","docstring_tokens":["Set","the","player","s","status","to","waiting",".","And","then","Randomly","match","a","player","whose","status","is","also","waiting",".","Args",":","id",":","ID","of","the","player",".","Emit",":","get_rival",":","Tell","the","player","its","rival","s","id","."],"function":"def set_player_wait(message):\n \"\"\"\n Set the player's status to 'waiting'.\n And then Randomly match a player whose status is also 'waiting'.\n Args:\n id: ID of the player.\n Emit:\n get_rival: Tell the player its rival's id.\n \"\"\"\n client_id = message['id']\n curr_index = find_by_id(client_id)\n player_list[curr_index]['status'] = 'waiting'\n print(client_id, 'set to waiting...')\n # look for rival\n rival_index = find_random_waiting(client_id)\n if rival_index != None:\n rival_id = player_list[rival_index]['id']\n player_list[curr_index]['rival'] = rival_id\n player_list[curr_index]['status'] = 'playing'\n\n player_list[rival_index]['rival'] = client_id\n player_list[rival_index]['status'] = 'playing'\n\n emit('get_rival', {'id': rival_id})\n print(client_id, 'find rival', rival_id)","function_tokens":["def","set_player_wait","(","message",")",":","client_id","=","message","[","'id'","]","curr_index","=","find_by_id","(","client_id",")","player_list","[","curr_index","]","[","'status'","]","=","'waiting'","print","(","client_id",",","'set to waiting...'",")","# look for rival","rival_index","=","find_random_waiting","(","client_id",")","if","rival_index","!=","None",":","rival_id","=","player_list","[","rival_index","]","[","'id'","]","player_list","[","curr_index","]","[","'rival'","]","=","rival_id","player_list","[","curr_index","]","[","'status'","]","=","'playing'","player_list","[","rival_index","]","[","'rival'","]","=","client_id","player_list","[","rival_index","]","[","'status'","]","=","'playing'","emit","(","'get_rival'",",","{","'id'",":","rival_id","}",")","print","(","client_id",",","'find rival'",",","rival_id",")"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L108-L132"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"set_player_startTime","parameters":"(message)","argument_list":"","return_statement":"","docstring":"Set the player's startTime.\n Args:\n id: ID of the player.","docstring_summary":"Set the player's startTime.\n Args:\n id: ID of the player.","docstring_tokens":["Set","the","player","s","startTime",".","Args",":","id",":","ID","of","the","player","."],"function":"def set_player_startTime(message):\n \"\"\"\n Set the player's startTime.\n Args:\n id: ID of the player.\n \"\"\"\n client_id = message['id']\n curr_index = find_by_id(client_id)\n player_list[curr_index]['startTime'] = time.time()\n print('startTime', player_list[curr_index])","function_tokens":["def","set_player_startTime","(","message",")",":","client_id","=","message","[","'id'","]","curr_index","=","find_by_id","(","client_id",")","player_list","[","curr_index","]","[","'startTime'","]","=","time",".","time","(",")","print","(","'startTime'",",","player_list","[","curr_index","]",")"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L136-L145"}
{"nwo":"ByronHsu\/Never-Blink","sha":"fa5753fb27633ef403e3cc0641b9f78da8117733","path":"server.py","language":"python","identifier":"send_image","parameters":"(message)","argument_list":"","return_statement":"","docstring":"1. Convert base64 string from client to np image.\n 2. Run 'Detector engine' to calculate its value.\n 3. Emit new player data to the player.\n Args:\n uri: Base64 string from client.\n id: ID of the player.\n Emit:\n get_arena_data: send arena status to the client including \n EAR1: Eye aspect ratio of this player.\n EAR2: Eye aspect ratio of its rival.\n elapsed: Elapsed time since start.\n end: Whether the game has ended.","docstring_summary":"1. Convert base64 string from client to np image.\n 2. Run 'Detector engine' to calculate its value.\n 3. Emit new player data to the player.\n Args:\n uri: Base64 string from client.\n id: ID of the player.\n Emit:\n get_arena_data: send arena status to the client including \n EAR1: Eye aspect ratio of this player.\n EAR2: Eye aspect ratio of its rival.\n elapsed: Elapsed time since start.\n end: Whether the game has ended.","docstring_tokens":["1",".","Convert","base64","string","from","client","to","np","image",".","2",".","Run","Detector","engine","to","calculate","its","value",".","3",".","Emit","new","player","data","to","the","player",".","Args",":","uri",":","Base64","string","from","client",".","id",":","ID","of","the","player",".","Emit",":","get_arena_data",":","send","arena","status","to","the","client","including","EAR1",":","Eye","aspect","ratio","of","this","player",".","EAR2",":","Eye","aspect","ratio","of","its","rival",".","elapsed",":","Elapsed","time","since","start",".","end",":","Whether","the","game","has","ended","."],"function":"def send_image(message):\n \"\"\"\n 1. Convert base64 string from client to np image.\n 2. Run 'Detector engine' to calculate its value.\n 3. Emit new player data to the player.\n Args:\n uri: Base64 string from client.\n id: ID of the player.\n Emit:\n get_arena_data: send arena status to the client including \n EAR1: Eye aspect ratio of this player.\n EAR2: Eye aspect ratio of its rival.\n elapsed: Elapsed time since start.\n end: Whether the game has ended.\n \"\"\"\n # ===============\n # Process Base64\n # ===============\n\n uri, _id = message['uri'], message['id']\n # split header and body\n img_data = uri.split(',')[1]\n img_data = base64.b64decode(img_data)\n image = Image.open(io.BytesIO(img_data))\n # bgr\n array = np.array(image)\n REJECT = 0\n\n # ===============\n # Set EAR\n # ===============\n\n index = find_by_id(_id)\n EAR1 = player_list[index]['ear']\n\n # If the game has ended, we should not update the player.\n if player_list[index]['end'] == 0:\n EAR1 = detector.calculate_ear(array)\n player_list[index]['ear'] = EAR1\n player_list[index]['uri'] = uri\n\n # Find rival's EAR.\n rival_id = player_list[index]['rival']\n rival_index = find_by_id(rival_id)\n EAR2 = player_list[rival_index]['ear']\n\n # Calculat elapsed time\n elapsed = time.time() - player_list[index]['startTime']\n\n # EAR < threshold is determined as 'blink'.\n threshold = 0.20\n # The first 3 second is not counted.\n prepare_time = 3\n if ((EAR1 < threshold and EAR1 > 0) or (EAR2 < threshold and EAR2 > 0)) and elapsed > prepare_time:\n # The game end. Set player's status back to 'idle'.\n player_list[index]['end'] = 1\n player_list[index]['status'] = 'idle'\n player_list[rival_index]['end'] = 1\n player_list[rival_index]['status'] = 'idle'\n emit('get_arena_data', {'EAR1': EAR1, 'EAR2': EAR2, 'elapsed': elapsed, 'end': 1,\n 'uri1': player_list[index]['uri'], 'uri2': player_list[rival_index]['uri']})\n else:\n emit('get_arena_data', {'EAR1': EAR1,\n 'EAR2': EAR2, 'elapsed': elapsed, 'end': 0})","function_tokens":["def","send_image","(","message",")",":","# ===============","# Process Base64","# ===============","uri",",","_id","=","message","[","'uri'","]",",","message","[","'id'","]","# split header and body","img_data","=","uri",".","split","(","','",")","[","1","]","img_data","=","base64",".","b64decode","(","img_data",")","image","=","Image",".","open","(","io",".","BytesIO","(","img_data",")",")","# bgr","array","=","np",".","array","(","image",")","REJECT","=","0","# ===============","# Set EAR","# ===============","index","=","find_by_id","(","_id",")","EAR1","=","player_list","[","index","]","[","'ear'","]","# If the game has ended, we should not update the player.","if","player_list","[","index","]","[","'end'","]","==","0",":","EAR1","=","detector",".","calculate_ear","(","array",")","player_list","[","index","]","[","'ear'","]","=","EAR1","player_list","[","index","]","[","'uri'","]","=","uri","# Find rival's EAR.","rival_id","=","player_list","[","index","]","[","'rival'","]","rival_index","=","find_by_id","(","rival_id",")","EAR2","=","player_list","[","rival_index","]","[","'ear'","]","# Calculat elapsed time","elapsed","=","time",".","time","(",")","-","player_list","[","index","]","[","'startTime'","]","# EAR < threshold is determined as 'blink'.","threshold","=","0.20","# The first 3 second is not counted.","prepare_time","=","3","if","(","(","EAR1","<","threshold","and","EAR1",">","0",")","or","(","EAR2","<","threshold","and","EAR2",">","0",")",")","and","elapsed",">","prepare_time",":","# The game end. Set player's status back to 'idle'.","player_list","[","index","]","[","'end'","]","=","1","player_list","[","index","]","[","'status'","]","=","'idle'","player_list","[","rival_index","]","[","'end'","]","=","1","player_list","[","rival_index","]","[","'status'","]","=","'idle'","emit","(","'get_arena_data'",",","{","'EAR1'",":","EAR1",",","'EAR2'",":","EAR2",",","'elapsed'",":","elapsed",",","'end'",":","1",",","'uri1'",":","player_list","[","index","]","[","'uri'","]",",","'uri2'",":","player_list","[","rival_index","]","[","'uri'","]","}",")","else",":","emit","(","'get_arena_data'",",","{","'EAR1'",":","EAR1",",","'EAR2'",":","EAR2",",","'elapsed'",":","elapsed",",","'end'",":","0","}",")"],"url":"https:\/\/github.com\/ByronHsu\/Never-Blink\/blob\/fa5753fb27633ef403e3cc0641b9f78da8117733\/server.py#L149-L212"}