public void summarizePlayer(int playerID) { Player p = players.get(playerID); Map<Integer, Playing> playersGames = p.games; Set<Integer> gamesKeys = playersGames.keySet(); Integer[] gameIDs = gamesKeys.toArray(new Integer[gamesKeys.size()]); System.out.println("Player: " + p); System.out.println("Total Gamerscore: " + p.getGamerScore()); System.out.printf( "%4s %-20s %-16s %-16s %-16s \n", " ", "Game", "Achievements", "Gamerscore", "IGN"); System.out.println( "--------------------------------------------------------------------------------"); for (int i = 0; i < gameIDs.length; i++) { Game g = games.get(gameIDs[i]); Playing playerInfo = p.games.get(gameIDs[i]); int nPlayerAchieve = playerInfo.achievements.size(); int nTotalAchieve = g.achievements.size(); System.out.printf( "%4s %-20s %-16s %-16s %-16s \n", (i + 1 + "."), g, (nPlayerAchieve + "/" + nTotalAchieve), (playerInfo.getPoints() + " pts"), playerInfo.ign); } System.out.printf("\n%4s %-16s %-16s \n", " ", "Friend", "Gamerscore"); System.out.println( "--------------------------------------------------------------------------------"); printSortedPlayers(p.friends); }
private void addTovidName_UserMap(String user, String video) { if (vidName_UserMap.containsKey(video)) { vidName_UserMap.get(video).add(user); } else { Set<String> s = new HashSet<String>(); s.add(user); vidName_UserMap.put(video, s); } }
@RequestMapping(value = CLIENT_CONNECT_PATH, method = RequestMethod.POST) public @ResponseBody int connectClient(@RequestBody String allVid) { try { int reply = FAILED; String[] videos = allVid.split(","); String uName = videos[0].trim(); videos = java.util.Arrays.copyOfRange(videos, 1, videos.length); // System.out.println("Client connect"+hostAdder+" "+uName+" "+ Arrays.asList(videos)); int ans = masterService.psConnectClient(hostAdder, uName, videos); // System.out.println("ans =" +ans +" "+FAILED); while (ans == PS_NOT_CONNECTED) { reconnectToMS(); ans = masterService.psConnectClient(hostAdder, uName, videos); } if (ans == FAILED) return FAILED; // System.out.println("Clinet "+ uName + " connected"); if (user_vidNameMap.containsKey(uName)) { reply = CLIENT_ALREADY_CONNECTED; } else { reply = CLIENT_CONNECTED; user_vidNameMap.put(uName, new HashSet<String>()); } // System.out.println("Clinet "+ uName + " connected"); Set<String> vidSet = user_vidNameMap.get(uName); for (int i = 0; i < videos.length; i++) { String temp = videos[i].trim(); // System.out.println("add video"); if (!temp.equals("")) { vidSet.add(temp); addTovidName_UserMap(uName, temp); } } // System.out.println("Clinet "+ uName + " connected"); userAliveMap.put(uName, new Long(System.currentTimeMillis() + TTL)); // System.out.println("Clinet "+ uName + " connected"); activeUsers.add(uName); System.out.println("Clinet " + uName + " connected"); return reply; } catch (Exception e) { System.out.println("Error: " + e.getMessage()); return FAILED; } }
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = null; String[] split = null; int cases = Integer.valueOf(reader.readLine()); int N, j, z, min, max, result; String matching = null, newMatch = null, tmpLine; Set<String> set = new HashSet<String>(); boolean noResult = false; for (int i = 1; i <= cases; i++) { N = Integer.valueOf(reader.readLine()); set = new HashSet<String>(); noResult = false; result = 0; min = Integer.MAX_VALUE; max = Integer.MIN_VALUE; for (z = 0; z < N; z++) { line = reader.readLine(); tmpLine = REGEX_PATTERN.matcher(line).replaceAll("$1"); if (set.isEmpty()) { set.add(tmpLine); } else if (!set.contains(tmpLine)) { noResult = true; break; } min = Math.min(min, line.length()); max = Math.max(max, line.length()); } if (!noResult) { result = max - min; } if (noResult) { System.out.printf("Case #%d: Fegla Won%n", i); } else { System.out.printf("Case #%d: %d%n", i, result); } } }
@RequestMapping(value = PS_DELETE_CLIENT, method = RequestMethod.DELETE) public @ResponseBody int ps_DeleteClient(@RequestParam(USER_PARAMETER) String user) { try { activeUsers.remove(user); return ACK; } catch (Exception e) { System.err.println("" + e.getMessage()); return FAILED; } }
@RequestMapping(value = PS_DELETE_MULTI_CLIENT, method = RequestMethod.POST) public @ResponseBody int ps_DeleteMultipleClient(@RequestBody String[] user) { try { for (int i = 0; i < user.length; i++) { activeUsers.remove(user[i]); } return ACK; } catch (Exception e) { System.err.println(e.getMessage()); return FAILED; } }
/** * ** Traverses the DBFactory dependency tree, creating a DBFactoryTree ** @param level The * current tree level ** @param dbFact The current DBFactory to add ** @param parentNode The * parent node to which a new DBFactoryNode child will be added ** @param addedTables A set of * table names added to the current DBFactoryTree * */ private static void _traverseDBFactoryTree( int level, DBFactory<? extends DBRecord> dbFact, DBFactoryTree parentNode, Set<String> addedTables) { /* no DBFactory? */ if (dbFact == null) { Print.logError("Null DBFactory!"); return; } String utableName = dbFact.getUntranslatedTableName(); /* already added? */ if (addedTables.contains(utableName)) { return; } addedTables.add(utableName); /* add this node */ // Print.logInfo(StringTools.replicateString(" ",level) + dbFact.getUntranslatedTableName()); DBFactoryTree dbFactNode = new DBFactoryTree(level, parentNode, dbFact); parentNode.addChild(dbFactNode); /* find dependent children */ DBFactory<? extends DBRecord> childFact[] = dbFact.getChildFactories(); for (int i = 0; i < childFact.length; i++) { int index = childFact[i].getParentTables().indexOf(utableName); if (level == index) { DBFactoryTree._traverseDBFactoryTree(level + 1, childFact[i], dbFactNode, addedTables); } else if (!addedTables.contains(childFact[i].getUntranslatedTableName())) { Print.logWarn( "Skipping table in heiarchy: " + utableName + " ==> " + childFact[i].getUntranslatedTableName()); } } }
private void removeUser(String user) { int count = 0; if (user_vidNameMap.containsKey(user)) { Set<String> vids = user_vidNameMap.get(user); Iterator<String> it = vids.iterator(); while (it.hasNext()) { removeFromvidName_UserMap(user, it.next()); } vids.clear(); user_vidNameMap.remove(user); } activeUsers.remove(user); int ans = masterService.psDisConnectClient(hostAdder, user); while (ans != ACK) { if (count > 10) throw new RuntimeException("Time out in removing user from MS"); reconnectToMS(); ans = masterService.psDisConnectClient(hostAdder, user); count++; } System.out.println("User " + user + " removed."); }
@Scheduled(fixedDelay = SCHEDULED_CHECK) public void checkActiveClients() { Set<String> users = user_vidNameMap.keySet(); Iterator<String> it = users.iterator(); long time = System.currentTimeMillis(); int count = 0; while (it.hasNext()) { count++; String user = it.next(); if (user == null) continue; if (userAliveMap.containsKey(user)) { if (userAliveMap.get(user).longValue() < time) { removeUser(user); userAliveMap.remove(user); } } else { throw new RuntimeException("user in user-vid map but not in user-alive map"); } } // System.out.println("Debug: Scheduled Check count:"+count+" user // count:"+user_vidNameMap.size()); System.gc(); }
@RequestMapping(value = VIDEO_SEARCH_PATH, method = RequestMethod.GET) public @ResponseBody String[] searchVideo( @RequestParam(value = "username") String uName, @RequestParam(value = "video") String videoHash, HttpServletResponse response) { System.out.println("Search from:" + uName); if (!user_vidNameMap.containsKey(uName)) { response.setStatus(402); // client not connected return null; } Set<String> users = vidName_UserMap.get(videoHash); if (users == null) { System.out.println("Srearching main server\n"); try { users = masterService.psSearch(hostAdder, videoHash); } catch (Exception e) { System.err.println(e.getMessage()); return null; } if (users == null) return null; if (vidName_UserMap.containsKey(videoHash)) { vidName_UserMap.get(videoHash).addAll(users); } else { Set<String> s = new HashSet<String>(); s.addAll(users); vidName_UserMap.put(videoHash, s); } } else { Iterator<String> it = users.iterator(); while (it.hasNext()) { String temp = it.next(); if (!activeUsers.contains(temp)) { it.remove(); } } } System.out.println("Search result : " + Arrays.asList(users.toArray(new String[0]))); // String [] a = new String[] return users.toArray(new String[0]); }